ProcessDataJob.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // dispatch(new SendDataJob($return))->onQueue('cloud_device');
  40. file_put_contents('record2.txt',json_encode($return) . PHP_EOL,8);
  41. //输出信息
  42. $this->echoMessage(new ConsoleOutput());
  43. }
  44. public function sendToDevice($data){
  45. $url = "http://121.36.142.167:7774/api/module-data/device_machine_record/device_machine_record";
  46. $post = [
  47. 'bizId' => -1,
  48. 'bizTypeEk' => 'LOWCODE',
  49. 'data' => [
  50. 'device_machine_record' => [
  51. 'machine_code' => $data['dev_eui'],
  52. 'param_value' => $data['value']
  53. ]
  54. ],
  55. 'dynamicFormId' => '477743923368955904',
  56. 'showModelId' => '477745421456904192'
  57. ];
  58. $header = ['Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfSU5ORVJfVVNFUixST0xFX0FETUlOLFJPTEVfSU5URVJGQUNFIiwidG9rZW5JZCI6IjM1IiwiZXhwIjoxNjk0Njc0MTE0fQ.L3Di3K_cpF0rWSgvzbcLufLm8bkCxd3Y-xudfKzSm4F-qdpDr0hYWWQP5K5BYTNuZnu4tWpGmSW2KRHU0pjt-A','Content-Type:application/json'];
  59. $curl = curl_init();
  60. curl_setopt_array($curl, array(
  61. CURLOPT_URL => $url,
  62. CURLOPT_RETURNTRANSFER => true,
  63. CURLOPT_ENCODING => '',
  64. CURLOPT_MAXREDIRS => 10,
  65. CURLOPT_TIMEOUT => 0,
  66. CURLOPT_FOLLOWLOCATION => true,
  67. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  68. CURLOPT_CUSTOMREQUEST => 'POST',
  69. CURLOPT_POSTFIELDS => json_encode($post),
  70. CURLOPT_HTTPHEADER => $header,
  71. ));
  72. $response = curl_exec($curl);
  73. curl_close($curl);
  74. file_put_contents('record2.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
  75. }
  76. protected function echoMessage(OutputInterface $output)
  77. {
  78. $output->writeln(json_encode($this->data));
  79. }
  80. }