ManDeviceJob.php 6.2 KB

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