SendDataJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Symfony\Component\Console\Output\ConsoleOutput;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. class SendDataJob implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. protected $data;
  14. protected $url;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct($data)
  21. {
  22. $this->data = $data;
  23. $this->url = config('ip.zs');
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. //暂时不用
  33. file_put_contents('record2.txt',json_encode($this->data) . PHP_EOL,8);
  34. if(isset($data['is_clear_data'])){
  35. $this->sendToDevice($this->data);
  36. }
  37. //输出信息 测试
  38. $this->echoMessage(new ConsoleOutput());
  39. }
  40. private function sendRequest($url,$data){
  41. $data = json_encode($data);
  42. $header[] = "Content-Type:application/json";
  43. $ch=curl_init($url);
  44. curl_setopt($ch,CURLOPT_POST,1);
  45. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  46. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  47. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  49. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  50. $response=curl_exec($ch);
  51. $response=json_decode($response,true);
  52. if(curl_errno($ch) ){
  53. sc(curl_error($ch));
  54. }
  55. return $response;
  56. }
  57. public function sendToDevice($data){
  58. $url = $this->url . "api/module-data/device_machine_record/device_machine_record";
  59. $post = [
  60. 'bizId' => -1,
  61. 'bizTypeEk' => 'LOWCODE',
  62. 'data' => [
  63. 'device_machine_record' => [
  64. 'machine_code' => $data['dev_eui'],
  65. 'param_value' => $data['value']
  66. ]
  67. ],
  68. 'dynamicFormId' => '477743923368955904',
  69. 'showModelId' => '477745421456904192'
  70. ];
  71. $header = ['Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfSU5ORVJfVVNFUixST0xFX0FETUlOLFJPTEVfSU5URVJGQUNFIiwidG9rZW5JZCI6IjM1IiwiZXhwIjoxNjk0Njc0MTE0fQ.L3Di3K_cpF0rWSgvzbcLufLm8bkCxd3Y-xudfKzSm4F-qdpDr0hYWWQP5K5BYTNuZnu4tWpGmSW2KRHU0pjt-A','Content-Type:application/json'];
  72. $curl = curl_init();
  73. curl_setopt_array($curl, array(
  74. CURLOPT_URL => $url,
  75. CURLOPT_RETURNTRANSFER => true,
  76. CURLOPT_ENCODING => '',
  77. CURLOPT_MAXREDIRS => 10,
  78. CURLOPT_TIMEOUT => 0,
  79. CURLOPT_FOLLOWLOCATION => true,
  80. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  81. CURLOPT_CUSTOMREQUEST => 'POST',
  82. CURLOPT_POSTFIELDS => json_encode($post),
  83. CURLOPT_HTTPHEADER => $header,
  84. ));
  85. $response = curl_exec($curl);
  86. curl_close($curl);
  87. file_put_contents('record2.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
  88. }
  89. protected function echoMessage(OutputInterface $output)
  90. {
  91. $output->writeln($this->data);
  92. }
  93. }