ManDeviceJob.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. $date = date('Ymd',time());
  44. //工序-----------------------------
  45. $model = new DeviceData(['channel' => $date]);
  46. if($this->data['type'] == "dataPoint"){
  47. $insert = [];
  48. foreach ($this->data['data']['dataPoints'] as $value){
  49. $dev_eui = $deviceId . $value['dataPointId'];
  50. if(! in_array($dev_eui,$device)) continue;
  51. //发送给大王椰
  52. $this->sendToDevice(['dev_eui' => $dev_eui, 'value' => $value['value']]);
  53. $insert[] = [
  54. // 'data' => json_encode($value),
  55. 'happening_data' => $value['value'],
  56. 'dev_eui' => $dev_eui,
  57. 'device_name' => $deviceName,
  58. 'source_ip' => $ip,
  59. 'data_type' => $data_type,
  60. 'crt_time' => time()
  61. ];
  62. }
  63. $model->insert($insert);
  64. }elseif ($this->data['type'] == "position"){
  65. $dev_eui = $deviceId;
  66. if(in_array($dev_eui,$device)) {
  67. $insert[] = [
  68. // 'data' => json_encode($this->data['data']['position']),
  69. 'happening_data' => '',
  70. 'dev_eui' => $deviceId,
  71. 'device_name' => $deviceName,
  72. 'source_ip' => $ip,
  73. 'data_type' => $data_type,
  74. 'crt_time' => time()
  75. ];
  76. $model->insert($insert);
  77. }
  78. }
  79. // if(is_array($this->data) || is_object($this->data)){
  80. // $data = json_encode($this->data);
  81. // }else {
  82. // $data = $this->data;
  83. // }
  84. // file_put_contents('send_man.txt',date('Y-m-d H:i:s').PHP_EOL . $data . PHP_EOL,8);
  85. }catch (\Exception $exception){
  86. 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);
  87. }
  88. }
  89. public function sendToDevice($data){
  90. date_default_timezone_set("PRC");
  91. list($status,$token) = ClearDataService::getToken();
  92. if(! $status) return;
  93. $url = $this->url . "api/module-data/device_machine_record/device_machine_record";
  94. $post = [
  95. 'bizId' => -1,
  96. 'bizTypeEk' => 'LOWCODE',
  97. 'data' => [
  98. 'device_machine_record' => [
  99. 'machine_code' => $data['dev_eui'],
  100. 'param_value' => $data['value'],
  101. 'record_time' => $this->getNowDay()
  102. ]
  103. ],
  104. 'dynamicFormId' => '477743923368955904',
  105. 'showModelId' => '477745421456904192'
  106. ];
  107. $header = ["Authorization: Bearer {$token}","Content-Type:application/json"];
  108. $curl = curl_init();
  109. curl_setopt_array($curl, array(
  110. CURLOPT_URL => $url,
  111. CURLOPT_RETURNTRANSFER => true,
  112. CURLOPT_ENCODING => '',
  113. CURLOPT_MAXREDIRS => 10,
  114. CURLOPT_TIMEOUT => 0,
  115. CURLOPT_FOLLOWLOCATION => true,
  116. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  117. CURLOPT_CUSTOMREQUEST => 'POST',
  118. CURLOPT_POSTFIELDS => json_encode($post),
  119. CURLOPT_HTTPHEADER => $header,
  120. ));
  121. $response = curl_exec($curl);
  122. curl_close($curl);
  123. $res = json_decode($response,true);
  124. if(isset($res['code'])){//报错了
  125. file_put_contents('record_man_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8);
  126. }
  127. }
  128. public static function getIP(){
  129. if (getenv('HTTP_CLIENT_IP')) {
  130. $ip = getenv('HTTP_CLIENT_IP');
  131. }
  132. elseif (getenv('HTTP_X_REAL_IP')) {
  133. $ip = getenv('HTTP_X_REAL_IP');
  134. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  135. $ip = getenv('HTTP_X_FORWARDED_FOR');
  136. $ips = explode(',', $ip);
  137. $ip = $ips[0];
  138. } elseif (getenv('REMOTE_ADDR')) {
  139. $ip = getenv('REMOTE_ADDR');
  140. } else {
  141. $ip = '0.0.0.0';
  142. }
  143. return $ip;
  144. }
  145. protected function echoMessage(OutputInterface $output)
  146. {
  147. $output->writeln($this->data);
  148. }
  149. function getNowDay(){
  150. // 获取当前时间
  151. $currentDateTime = new \DateTime();
  152. // 设置时区为 PRC
  153. $currentDateTime->setTimezone(new \DateTimeZone('UTC'));
  154. // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
  155. $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
  156. return $formattedDateTime;
  157. }
  158. }