ProcessDataJob.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Jobs;
  3. use App\Service\ClearDataService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Symfony\Component\Console\Output\ConsoleOutput;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. class ProcessDataJob implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  14. protected $data;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct($data)
  21. {
  22. $this->data = $data;
  23. }
  24. /**
  25. * Execute the job.
  26. *
  27. * @return void
  28. */
  29. public function handle()
  30. {
  31. //标记
  32. file_put_contents('record.txt',json_encode($this->data) . PHP_EOL,8);
  33. //保存数据到自己服务器
  34. ClearDataService::saveData($this->data);
  35. //处理数据
  36. $return = ClearDataService::clearData($this->data);
  37. $this->sendToDevice($return);
  38. //传递数据给下一个队列
  39. file_put_contents('record2.txt',json_encode($return) . PHP_EOL,8);
  40. //输出信息
  41. $this->echoMessage(new ConsoleOutput());
  42. }
  43. public function sendToDevice($data){
  44. date_default_timezone_set("PRC");
  45. list($status,$token) = ClearDataService::getToken();
  46. if(! $status) return;
  47. $url = "http://121.36.142.167:7774/api/module-data/device_machine_record/device_machine_record";
  48. $post = [
  49. 'bizId' => -1,
  50. 'bizTypeEk' => 'LOWCODE',
  51. 'data' => [
  52. 'device_machine_record' => [
  53. 'machine_code' => $data['dev_eui'],
  54. 'param_value' => $data['value'],
  55. 'record_time' => $this->getNowDay()
  56. ]
  57. ],
  58. 'dynamicFormId' => '477743923368955904',
  59. 'showModelId' => '477745421456904192'
  60. ];
  61. $header = ["Authorization: Bearer {$token}","Content-Type:application/json",'Site:91451322MA5P9JNKXA'];
  62. $curl = curl_init();
  63. curl_setopt_array($curl, array(
  64. CURLOPT_URL => $url,
  65. CURLOPT_RETURNTRANSFER => true,
  66. CURLOPT_ENCODING => '',
  67. CURLOPT_MAXREDIRS => 10,
  68. CURLOPT_TIMEOUT => 0,
  69. CURLOPT_FOLLOWLOCATION => true,
  70. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  71. CURLOPT_CUSTOMREQUEST => 'POST',
  72. CURLOPT_POSTFIELDS => json_encode($post),
  73. CURLOPT_HTTPHEADER => $header,
  74. ));
  75. $response = curl_exec($curl);
  76. curl_close($curl);
  77. $res = json_decode($response,true);
  78. if(isset($res['code'])){//报错了
  79. file_put_contents('record_chuangan_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8);
  80. }
  81. }
  82. protected function echoMessage(OutputInterface $output)
  83. {
  84. $output->writeln(json_encode($this->data));
  85. }
  86. function getNowDay(){
  87. // 获取当前时间
  88. $currentDateTime = new \DateTime();
  89. // 设置时区为 PRC
  90. $currentDateTime->setTimezone(new \DateTimeZone('UTC'));
  91. // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
  92. $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
  93. return $formattedDateTime;
  94. }
  95. }