cqpCow 1 rok temu
rodzic
commit
399006efcf

+ 2 - 2
app/Http/Controllers/Api/JobController.php

@@ -12,10 +12,10 @@ class JobController extends BaseController
 {
     public function processDataJobAdd(Request $request)
     {
-       dispatch(new ProcessDataJob($request->all()));
+       dispatch(new ProcessDataJob($request->all()))->onQueue('cloud_device');;
     }
 
     public function processDataJobAdd2(Request $request){
-        dispatch(new SendDataJob($request->all()));
+        dispatch(new SendDataJob($request->all()))->onQueue('cloud_device');;
     }
 }

+ 6 - 11
app/Jobs/ProcessDataJob.php

@@ -2,6 +2,7 @@
 
 namespace App\Jobs;
 
+use App\Service\ClearDataService;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
@@ -23,14 +24,6 @@ class ProcessDataJob implements ShouldQueue
      */
     public function __construct($data)
     {
-        $data = [
-            'id' => 22,
-            'url' => 'www.baidu.com',
-            'data' => [
-                '222',
-                '444',
-            ]
-        ];
         $this->data = $data;
     }
 
@@ -42,11 +35,13 @@ class ProcessDataJob implements ShouldQueue
     public function handle()
     {
         //标记
-        file_put_contents('charge.txt',json_encode($this->data),8);
+        file_put_contents('record.txt',json_encode($this->data) . PHP_EOL,8);
+
         //处理数据
+        $return = ClearDataService::clearData($this->data);
 
-        //传递数据给一个队列
-        dispatch(new SendDataJob($this->data));
+        //传递数据给一个队列
+        dispatch(new SendDataJob($return))->onQueue('cloud_device');
 
         //输出信息
         $this->echoMessage(new ConsoleOutput());

+ 11 - 15
app/Jobs/SendDataJob.php

@@ -2,6 +2,7 @@
 
 namespace App\Jobs;
 
+use App\Service\ClearDataService;
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
@@ -13,7 +14,7 @@ use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\ConsoleOutput;
 use Symfony\Component\Console\Output\OutputInterface;
 
-class SendDataJob extends Command implements ShouldQueue
+class SendDataJob implements ShouldQueue
 {
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
@@ -27,8 +28,6 @@ class SendDataJob extends Command implements ShouldQueue
     public function __construct($data)
     {
         $this->data = $data;
-
-        parent::__construct(); // 调用父类的构造函数
     }
 
     /**
@@ -38,13 +37,19 @@ class SendDataJob extends Command implements ShouldQueue
      */
     public function handle()
     {
-        $url = '';
+        file_put_contents('record2.txt',json_encode($this->data) .PHP_EOL,8);
+
+        //数据要发送的地址
+        $url = ClearDataService::getUrl($this->data);
         $return = $this->sendRequest($url,$this->data);
-        file_put_contents('charge.txt',json_encode($this->data),8);
+
+//        file_put_contents('send.txt',json_encode($return) .PHP_EOL,8);
+
+        //输出信息 测试
         $this->echoMessage(new ConsoleOutput());
     }
 
-    private function sendRequest($url,$data){return true;
+    private function sendRequest($url,$data){
         $data = json_encode($data);
         $header[] = "Content-Type:application/json";
         $ch=curl_init($url);
@@ -55,7 +60,6 @@ class SendDataJob extends Command implements ShouldQueue
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
         $response=curl_exec($ch);
-        file_put_contents('job_send.txt',$response.PHP_EOL,8);
         $response=json_decode($response,true);
 
         if(curl_errno($ch) ){
@@ -68,12 +72,4 @@ class SendDataJob extends Command implements ShouldQueue
     {
         $output->writeln('2222222');
     }
-
-    protected function execute(InputInterface $input, OutputInterface $output)
-    {
-        $output->write('22222222');
-        $output->writeln('2222222');
-    }
-
-
 }

+ 25 - 0
app/Service/ClearDataService.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Service;
+
+
+use App\Model\SysMenu;
+
+class ClearDataService extends Service
+{
+    public static function clearData($data){
+        if(empty($data)) return $data;
+
+        //具体处理数据的业务 TODO
+
+        return $data;
+    }
+
+    public static function getUrl($data){
+        //根据业务将数据发送到对应的地址
+        // 温度传感器数据包含temperature 字段  压力传感器包含pressure 字段
+
+        $url = 'http://fyy_api.qingyaokeji.com/api/testData';
+        return $url;
+    }
+}

+ 1 - 1
config/queue.php

@@ -14,7 +14,7 @@ return [
     */
 
     'default' => env('QUEUE_CONNECTION', 'redis'),
-
+    'cloud_device' => env('QUEUE_CONNECTION', 'redis'),
     /*
     |--------------------------------------------------------------------------
     | Queue Connections