ManDeviceJobLf.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Jobs;
  3. use App\Model\DeviceDataLf;
  4. use App\Model\LfDevice;
  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 ManDeviceJobLf 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.zslf');
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. try{
  35. if(empty($this->data['data'])) return;
  36. $device = LfDevice::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. if(empty($value['value'])) continue;
  48. //发送给朗峰
  49. $this->sendToDevice(['dev_eui' => $dev_eui, 'value' => $value['value']]);
  50. $insert[] = [
  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. DeviceDataLf::insert($insert);
  60. }
  61. }catch (\Exception $exception){
  62. file_put_contents('send_man_error_lf.txt',date("Y-m-d H:i:s").json_encode($this->data).PHP_EOL.$exception->getMessage().$exception->getLine().$exception->getFile().PHP_EOL,8);
  63. }
  64. }
  65. public function sendToDevice($data){
  66. date_default_timezone_set("PRC");
  67. list($status,$token) = ClearDataService::getTokenLf();
  68. if(! $status) return;
  69. $url = $this->url . "api/module-data/device_machine_record/device_machine_record";
  70. $post = [
  71. 'bizId' => -1,
  72. 'bizTypeEk' => 'LOWCODE',
  73. 'data' => [
  74. 'device_machine_record' => [
  75. 'machine_code' => $data['dev_eui'],
  76. 'param_value' => $data['value'],
  77. 'record_time' => $this->getNowDay()
  78. ]
  79. ],
  80. 'dynamicFormId' => '477743923368955904',
  81. 'showModelId' => '477745421456904192'
  82. ];
  83. $header = ["Authorization: Bearer {$token}","Content-Type:application/json"];
  84. $curl = curl_init();
  85. curl_setopt_array($curl, array(
  86. CURLOPT_URL => $url,
  87. CURLOPT_RETURNTRANSFER => true,
  88. CURLOPT_ENCODING => '',
  89. CURLOPT_MAXREDIRS => 10,
  90. CURLOPT_TIMEOUT => 0,
  91. CURLOPT_FOLLOWLOCATION => true,
  92. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  93. CURLOPT_CUSTOMREQUEST => 'POST',
  94. CURLOPT_POSTFIELDS => json_encode($post),
  95. CURLOPT_SSL_VERIFYPEER => false,
  96. CURLOPT_HTTPHEADER => $header,
  97. ));
  98. $response = curl_exec($curl);
  99. if ($response === false) {
  100. // 获取错误号
  101. $errorNumber = curl_errno($curl);
  102. // 获取错误信息
  103. $errorMessage = curl_error($curl);
  104. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  105. file_put_contents('record_man_lf_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $message .PHP_EOL,8);
  106. }
  107. curl_close($curl);
  108. $res = json_decode($response,true);
  109. if(isset($res['code'])){//报错了
  110. file_put_contents('record_man_lf_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8);
  111. }
  112. }
  113. public static function getIP(){
  114. if (getenv('HTTP_CLIENT_IP')) {
  115. $ip = getenv('HTTP_CLIENT_IP');
  116. }
  117. elseif (getenv('HTTP_X_REAL_IP')) {
  118. $ip = getenv('HTTP_X_REAL_IP');
  119. } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  120. $ip = getenv('HTTP_X_FORWARDED_FOR');
  121. $ips = explode(',', $ip);
  122. $ip = $ips[0];
  123. } elseif (getenv('REMOTE_ADDR')) {
  124. $ip = getenv('REMOTE_ADDR');
  125. } else {
  126. $ip = '0.0.0.0';
  127. }
  128. return $ip;
  129. }
  130. protected function echoMessage(OutputInterface $output)
  131. {
  132. $output->writeln($this->data);
  133. }
  134. function getNowDay(){
  135. // 获取当前时间
  136. $currentDateTime = new \DateTime();
  137. // 设置时区为 PRC
  138. $currentDateTime->setTimezone(new \DateTimeZone('UTC'));
  139. // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
  140. $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
  141. return $formattedDateTime;
  142. }
  143. }