ManDeviceJob.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\BigKingDevice;
  4. use App\Model\DeviceData;
  5. use App\Service\ClearDataService;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. class ManDeviceJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. protected $data;
  16. protected $url;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct($data)
  23. {
  24. $this->data = $data;
  25. $this->url = config('ip.zs');
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. // {"data":{"position":{"lngGcj":118.56216535926838,"lngBd":118.56858403462685,"lngWgs":118.557395,"latGcj":28.684134864745076,"locationType":"CELL","time":1695365429,"latWgs":28.687429,"deviceId":"01401422100800000103","cusdeviceNos":["0000130876000007"],"latBd":28.690489277743108},"deviceId":"01401422100800000103","deviceName":"1\u53f7\u70ed\u538b\u673a"},"type":"position"}
  35. try{
  36. if(empty($this->data['data'])) return;
  37. $device = BigKingDevice::select('data')->get()->toArray();
  38. $device = array_column($device,'data');
  39. $deviceId = $this->data['data']['deviceId'];
  40. $deviceName = $this->data['data']['deviceName'];
  41. $ip = self::getIP();
  42. $data_type = 3;
  43. if($this->data['type'] == "dataPoint"){
  44. $insert = [];
  45. foreach ($this->data['data']['dataPoints'] as $value){
  46. $dev_eui = $deviceId . $value['dataPointId'];
  47. if(! in_array($dev_eui,$device)) continue;
  48. //发送给大王椰
  49. $this->sendToDevice(['dev_eui' => $dev_eui, 'value' => $value['value']]);
  50. $insert[] = [
  51. // 'data' => json_encode($value),
  52. 'happening_data' => $value['value'],
  53. 'dev_eui' => $dev_eui,
  54. 'device_name' => $deviceName,
  55. 'source_ip' => $ip,
  56. 'data_type' => $data_type,
  57. 'crt_time' => time()
  58. ];
  59. }
  60. DeviceData::insert($insert);
  61. }
  62. // elseif ($this->data['type'] == "position"){
  63. // $dev_eui = $deviceId;
  64. // if(in_array($dev_eui,$device)) {
  65. // $insert[] = [
  66. //// 'data' => json_encode($this->data['data']['position']),
  67. // 'happening_data' => '',
  68. // 'dev_eui' => $deviceId,
  69. // 'device_name' => $deviceName,
  70. // 'source_ip' => $ip,
  71. // 'data_type' => $data_type,
  72. // 'crt_time' => time()
  73. // ];
  74. // DeviceData::insert($insert);
  75. // }
  76. // }
  77. // if(is_array($this->data) || is_object($this->data)){
  78. // $data = json_encode($this->data);
  79. // }else {
  80. // $data = $this->data;
  81. // }
  82. // file_put_contents('send_man.txt',date('Y-m-d H:i:s').PHP_EOL . $data . PHP_EOL,8);
  83. }catch (\Exception $exception){
  84. file_put_contents('send_man_error.txt',date("Y-m-d H:i:s").json_encode($this->data).PHP_EOL.$exception->getMessage().$exception->getLine().$exception->getFile().PHP_EOL,8);
  85. }
  86. }
  87. public function sendToDevice($data){
  88. date_default_timezone_set("PRC");
  89. list($status,$token) = ClearDataService::getToken();
  90. if(! $status) return;
  91. $url = $this->url . "api/module-data/device_machine_record/device_machine_record";
  92. $post = [
  93. 'bizId' => -1,
  94. 'bizTypeEk' => 'LOWCODE',
  95. 'data' => [
  96. 'device_machine_record' => [
  97. 'machine_code' => $data['dev_eui'],
  98. 'param_value' => $data['value'],
  99. 'record_time' => $this->getNowDay()
  100. ]
  101. ],
  102. 'dynamicFormId' => '477743923368955904',
  103. 'showModelId' => '477745421456904192'
  104. ];
  105. $header = ["Authorization: Bearer {$token}","Content-Type:application/json"];
  106. $curl = curl_init();
  107. curl_setopt_array($curl, array(
  108. CURLOPT_URL => $url,
  109. CURLOPT_RETURNTRANSFER => true,
  110. CURLOPT_ENCODING => '',
  111. CURLOPT_MAXREDIRS => 10,
  112. CURLOPT_TIMEOUT => 0,
  113. CURLOPT_FOLLOWLOCATION => true,
  114. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  115. CURLOPT_CUSTOMREQUEST => 'POST',
  116. CURLOPT_POSTFIELDS => json_encode($post),
  117. CURLOPT_HTTPHEADER => $header,
  118. ));
  119. $response = curl_exec($curl);
  120. curl_close($curl);
  121. $res = json_decode($response,true);
  122. if(isset($res['code'])){//报错了
  123. file_put_contents('record_man_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8);
  124. }
  125. }
  126. public static function getIP(){
  127. if (getenv('HTTP_CLIENT_IP')) {
  128. $ip = getenv('HTTP_CLIENT_IP');
  129. }
  130. elseif (getenv('HTTP_X_REAL_IP')) {
  131. $ip = getenv('HTTP_X_REAL_IP');
  132. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  133. $ip = getenv('HTTP_X_FORWARDED_FOR');
  134. $ips = explode(',', $ip);
  135. $ip = $ips[0];
  136. } elseif (getenv('REMOTE_ADDR')) {
  137. $ip = getenv('REMOTE_ADDR');
  138. } else {
  139. $ip = '0.0.0.0';
  140. }
  141. return $ip;
  142. }
  143. protected function echoMessage(OutputInterface $output)
  144. {
  145. $output->writeln($this->data);
  146. }
  147. function getNowDay(){
  148. // 获取当前时间
  149. $currentDateTime = new \DateTime();
  150. // 设置时区为 PRC
  151. $currentDateTime->setTimezone(new \DateTimeZone('UTC'));
  152. // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
  153. $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
  154. return $formattedDateTime;
  155. }
  156. }