ManDeviceJob.php 6.2 KB

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