ProcessDataJob.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. protected $url;
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct($data)
  22. {
  23. $this->data = $data;
  24. $this->url = config('ip.zs');
  25. }
  26. /**
  27. * Execute the job.
  28. *
  29. * @return void
  30. */
  31. public function handle()
  32. {
  33. //标记
  34. file_put_contents('record.txt',json_encode($this->data) . PHP_EOL,8);
  35. //保存数据到自己服务器
  36. ClearDataService::saveData($this->data);
  37. //处理数据
  38. $return = ClearDataService::clearData($this->data);
  39. $this->sendToDevice($return);
  40. //传递数据给下一个队列
  41. file_put_contents('record2.txt',json_encode($return) . PHP_EOL,8);
  42. //输出信息
  43. $this->echoMessage(new ConsoleOutput());
  44. }
  45. public function sendToDevice($data){
  46. date_default_timezone_set("PRC");
  47. list($status,$token) = ClearDataService::getToken();
  48. if(! $status) return;
  49. $url = $this->url . "api/module-data/device_machine_record/device_machine_record";
  50. $header = ["Authorization: Bearer {$token}","Content-Type:application/json"];
  51. if(! empty($data['multiple'])){
  52. foreach ($data['multiple'] as $key => $value){
  53. $post = [
  54. 'bizId' => -1,
  55. 'bizTypeEk' => 'LOWCODE',
  56. 'data' => [
  57. 'device_machine_record' => [
  58. 'machine_code' => $key,
  59. 'param_value' => $value,
  60. 'record_time' => $this->getNowDay()
  61. ]
  62. ],
  63. 'dynamicFormId' => '477743923368955904',
  64. 'showModelId' => '477745421456904192'
  65. ];
  66. $this->sendData($url,$post,$header);
  67. }
  68. } else{
  69. $post = [
  70. 'bizId' => -1,
  71. 'bizTypeEk' => 'LOWCODE',
  72. 'data' => [
  73. 'device_machine_record' => [
  74. 'machine_code' => $data['dev_eui'],
  75. 'param_value' => $data['value'],
  76. 'record_time' => $this->getNowDay()
  77. ]
  78. ],
  79. 'dynamicFormId' => '477743923368955904',
  80. 'showModelId' => '477745421456904192'
  81. ];
  82. $this->sendData($url,$post,$header);
  83. }
  84. }
  85. public function sendData($url, $post, $header){
  86. $curl = curl_init();
  87. curl_setopt_array($curl, array(
  88. CURLOPT_URL => $url,
  89. CURLOPT_RETURNTRANSFER => true,
  90. CURLOPT_ENCODING => '',
  91. CURLOPT_MAXREDIRS => 10,
  92. CURLOPT_TIMEOUT => 0,
  93. CURLOPT_FOLLOWLOCATION => true,
  94. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  95. CURLOPT_CUSTOMREQUEST => 'POST',
  96. CURLOPT_POSTFIELDS => json_encode($post),
  97. CURLOPT_HTTPHEADER => $header,
  98. ));
  99. $response = curl_exec($curl);
  100. curl_close($curl);
  101. $res = json_decode($response,true);
  102. if(isset($res['code'])){//报错了
  103. file_put_contents('record_chuangan_error.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL.json_encode($post),8);
  104. }
  105. }
  106. protected function echoMessage(OutputInterface $output)
  107. {
  108. $output->writeln(json_encode($this->data));
  109. }
  110. function getNowDay(){
  111. // 获取当前时间
  112. $currentDateTime = new \DateTime();
  113. // 设置时区为 PRC
  114. $currentDateTime->setTimezone(new \DateTimeZone('UTC'));
  115. // 格式化时间为 "2023-09-21T16:00:00.000Z" 的格式
  116. $formattedDateTime = $currentDateTime->format('Y-m-d\TH:i:s.000\Z');
  117. return $formattedDateTime;
  118. }
  119. }