ManDeviceJob.php 6.2 KB

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