SendDataJob.php 3.3 KB

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