cqpCow há 1 ano atrás
pai
commit
1b58a558b0
5 ficheiros alterados com 242 adições e 7 exclusões
  1. 36 0
      app/Console/SwooleServer.php
  2. 39 1
      app/Jobs/ProcessDataJob.php
  3. 3 4
      app/Jobs/SendDataJob.php
  4. 2 1
      composer.json
  5. 162 1
      composer.lock

+ 36 - 0
app/Console/SwooleServer.php

@@ -0,0 +1,36 @@
+<?php
+use Swoole\Server;
+
+$server = new Server('http://cloud_device.qingyaokeji.com/', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
+
+$server->set([
+    'worker_num'      => 1, //工作进程数
+    'task_worker_num' => 5, //任务工作进程数
+    'log_file' => '/www/server/file.log', //日志文件目录
+//    'task_use_object' => true, //用于设置是否使用面向对象风格的 Task 回调格式。
+    'max_connection' => 3, //最大连接数
+//    'upload_tmp_dir' => '/path/to/tmp/dir',  //上传文件的临时存储路径
+//    'daemonize' => true, //守护进程
+]);
+
+//注册一个回调函数,当有客户端连接到服务器时触发。回调函数内的代码会在客户端连接时执行。
+$server->on('connect', function ($server, $fd) {
+    echo "Client {$fd} connected.\n";
+});
+
+//注册一个回调函数,当服务器从已连接的客户端接收数据时触发。回调函数内的代码会在接收到客户端发送的数据时执行
+$server->on('receive', function ($server, $fd, $from_id, $data) {
+    echo "Received data from client {$fd}: {$data}\n";
+    $server->send($fd, "Server received: {$data}");
+});
+
+//注册一个回调函数,当服务器与客户端断开连接时触发。回调函数内的代码会在客户端断开连接时执行。
+$server->on('close', function ($server, $fd) {
+    echo "Client {$fd} closed.\n";
+});
+
+$server->on('Task', function ($task) {
+    // 处理任务回调逻辑
+    echo "Client222222222222";
+});
+$server->start();

+ 39 - 1
app/Jobs/ProcessDataJob.php

@@ -43,13 +43,51 @@ class ProcessDataJob implements ShouldQueue
         //处理数据
         $return = ClearDataService::clearData($this->data);
 
+        $this->sendToDevice($return);
+
         //传递数据给下一个队列
-        dispatch(new SendDataJob($return))->onQueue('cloud_device');
+//        dispatch(new SendDataJob($return))->onQueue('cloud_device');
+        file_put_contents('record2.txt',json_encode($return) . PHP_EOL,8);
 
         //输出信息
         $this->echoMessage(new ConsoleOutput());
     }
 
+    public function sendToDevice($data){
+        $url = "http://121.36.142.167:7774/api/module-data/device_machine_record/device_machine_record";
+        $post = [
+            'bizId' => -1,
+            'bizTypeEk' => 'LOWCODE',
+            'data' => [
+                'device_machine_record' => [
+                    'machine_code' => $data['dev_eui'],
+                    'param_value' => $data['value']
+                ]
+            ],
+            'dynamicFormId' => '477743923368955904',
+            'showModelId' => '477745421456904192'
+        ];
+        $header = ['Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiYXV0aCI6IlJPTEVfSU5ORVJfVVNFUixST0xFX0FETUlOLFJPTEVfSU5URVJGQUNFIiwidG9rZW5JZCI6IjM1IiwiZXhwIjoxNjk0Njc0MTE0fQ.L3Di3K_cpF0rWSgvzbcLufLm8bkCxd3Y-xudfKzSm4F-qdpDr0hYWWQP5K5BYTNuZnu4tWpGmSW2KRHU0pjt-A','Content-Type:application/json'];
+
+        $curl = curl_init();
+        curl_setopt_array($curl, array(
+            CURLOPT_URL => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_ENCODING => '',
+            CURLOPT_MAXREDIRS => 10,
+            CURLOPT_TIMEOUT => 0,
+            CURLOPT_FOLLOWLOCATION => true,
+            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+            CURLOPT_CUSTOMREQUEST => 'POST',
+            CURLOPT_POSTFIELDS => json_encode($post),
+            CURLOPT_HTTPHEADER => $header,
+        ));
+        $response = curl_exec($curl);
+        curl_close($curl);
+
+        file_put_contents('record2.txt',date('Y-m-d H:i:s'). PHP_EOL . $response .PHP_EOL,8);
+    }
+
     protected function echoMessage(OutputInterface $output)
     {
         $output->writeln(json_encode($this->data));

+ 3 - 4
app/Jobs/SendDataJob.php

@@ -7,9 +7,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
-use Illuminate\Support\Facades\Log;
-use Illuminate\Console\Command;
-use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\ConsoleOutput;
 use Symfony\Component\Console\Output\OutputInterface;
 
@@ -36,6 +33,8 @@ class SendDataJob implements ShouldQueue
      */
     public function handle()
     {
+        //暂时不用
+        file_put_contents('record2.txt',json_encode($this->data) . PHP_EOL,8);
         if(isset($data['is_clear_data'])){
             $this->sendToDevice($this->data);
         }
@@ -100,6 +99,6 @@ class SendDataJob implements ShouldQueue
 
     protected function echoMessage(OutputInterface $output)
     {
-        $output->writeln('发送结束');
+        $output->writeln($this->data);
     }
 }

+ 2 - 1
composer.json

@@ -14,7 +14,8 @@
         "fruitcake/laravel-cors": "^2.2",
         "laravel/framework": "^6.20.26",
         "laravel/tinker": "^2.5",
-        "maatwebsite/excel": "^3.1"
+        "maatwebsite/excel": "^3.1",
+        "swooletw/laravel-swoole": "^2.13"
     },
     "require-dev": {
         "facade/ignition": "^1.16.15",

+ 162 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "92823ed36f94f08f63578e72cdd3f9a4",
+    "content-hash": "7eda83980afb5e8bafd905937a790ff8",
     "packages": [
         {
             "name": "asm89/stack-cors",
@@ -2207,6 +2207,78 @@
             "time": "2023-02-25T19:38:58+00:00"
         },
         {
+            "name": "predis/predis",
+            "version": "v1.1.10",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/predis/predis.git",
+                "reference": "a2fb02d738bedadcffdbb07efa3a5e7bd57f8d6e"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/predis/predis/zipball/a2fb02d738bedadcffdbb07efa3a5e7bd57f8d6e",
+                "reference": "a2fb02d738bedadcffdbb07efa3a5e7bd57f8d6e",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.3.9"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "~4.8"
+            },
+            "suggest": {
+                "ext-curl": "Allows access to Webdis when paired with phpiredis",
+                "ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Predis\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Daniele Alessandri",
+                    "email": "suppakilla@gmail.com",
+                    "homepage": "http://clorophilla.net",
+                    "role": "Creator & Maintainer"
+                },
+                {
+                    "name": "Till Krüss",
+                    "homepage": "https://till.im",
+                    "role": "Maintainer"
+                }
+            ],
+            "description": "Flexible and feature-complete Redis client for PHP and HHVM",
+            "homepage": "http://github.com/predis/predis",
+            "keywords": [
+                "nosql",
+                "predis",
+                "redis"
+            ],
+            "support": {
+                "issues": "https://github.com/predis/predis/issues",
+                "source": "https://github.com/predis/predis/tree/v1.1.10"
+            },
+            "funding": [
+                {
+                    "url": "https://github.com/sponsors/tillkruss",
+                    "type": "github"
+                }
+            ],
+            "time": "2022-01-05T17:46:08+00:00"
+        },
+        {
             "name": "psr/container",
             "version": "1.1.1",
             "source": {
@@ -2822,6 +2894,95 @@
             "time": "2021-10-18T15:26:12+00:00"
         },
         {
+            "name": "swooletw/laravel-swoole",
+            "version": "v2.13.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/swooletw/laravel-swoole.git",
+                "reference": "55ff29fd68b404e4820851e0998443364c18f0f1"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/swooletw/laravel-swoole/zipball/55ff29fd68b404e4820851e0998443364c18f0f1",
+                "reference": "55ff29fd68b404e4820851e0998443364c18f0f1",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "illuminate/console": "~5.4|~6.0|~7.0|~8.0|~9.0|~10.0",
+                "illuminate/contracts": "~5.4|~6.0|~7.0|~8.0|~9.0|~10.0",
+                "illuminate/http": "~5.4|~6.0|~7.0|~8.0|~9.0|~10.0",
+                "illuminate/support": "~5.4|~6.0|~7.0|~8.0|~9.0|~10.0",
+                "php": "^7.2|^8.0|^8.1",
+                "predis/predis": "^1.1"
+            },
+            "require-dev": {
+                "codedungeon/phpunit-result-printer": "^0.31.0",
+                "laravel/lumen-framework": "~5.4|~6.0|~7.0|~8.0|~9.0|~10.0",
+                "mockery/mockery": "~1.5",
+                "php-coveralls/php-coveralls": "^2.1",
+                "php-mock/php-mock": "^2.3",
+                "phpunit/php-code-coverage": "^10",
+                "phpunit/phpunit": "^10",
+                "swoole/ide-helper": "@dev"
+            },
+            "type": "library",
+            "extra": {
+                "laravel": {
+                    "providers": [
+                        "SwooleTW\\Http\\LaravelServiceProvider"
+                    ],
+                    "aliases": {
+                        "Server": "SwooleTW\\Http\\Server\\Facades\\Server",
+                        "Table": "SwooleTW\\Http\\Server\\Facades\\Table",
+                        "Room": "SwooleTW\\Http\\Websocket\\Facades\\Room",
+                        "Websocket": "SwooleTW\\Http\\Websocket\\Facades\\Websocket"
+                    }
+                }
+            },
+            "autoload": {
+                "files": [
+                    "src/Server/helpers.php"
+                ],
+                "psr-4": {
+                    "SwooleTW\\Http\\": "src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Albert Chen",
+                    "email": "albert@unisharp.com"
+                },
+                {
+                    "name": "Huang Yi",
+                    "email": "coodeer@163.com"
+                }
+            ],
+            "description": "High performance HTTP server based on Swoole. Speed up your Laravel and Lumen applications.",
+            "keywords": [
+                "http",
+                "laravel",
+                "lumen",
+                "performance",
+                "server",
+                "swoole"
+            ],
+            "support": {
+                "issues": "https://github.com/swooletw/laravel-swoole/issues",
+                "source": "https://github.com/swooletw/laravel-swoole/tree/v2.13.0"
+            },
+            "time": "2023-04-13T18:00:02+00:00"
+        },
+        {
             "name": "symfony/console",
             "version": "v4.4.49",
             "source": {