From c28404e3cd0d0c80d28299c10acae28e2cc58d18 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 24 Mar 2022 20:37:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0iot=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Commons/Log.php | 160 +++++++++--------- .../v3/Implementations/IOTAliService.php | 2 +- composer.json | 3 +- composer.lock | 70 +++++++- config/autoload/aliiot.php | 1 + 5 files changed, 153 insertions(+), 83 deletions(-) diff --git a/app/Commons/Log.php b/app/Commons/Log.php index d99cb34..fa26243 100644 --- a/app/Commons/Log.php +++ b/app/Commons/Log.php @@ -29,90 +29,90 @@ class Log public function event($labels=null,$datas){ - co(function () use ($labels,$datas){ - - $client = $this->getClient(); - $kv = []; - foreach ($datas as $key => $value) { - $kv[] = $key."=".$value; - } - $pushLabels = []; - - $event_name = 'event_'.env('APP_ENV'); - if(!empty($labels)) $pushLabels[$event_name] = $labels; - - /* - * data format: - curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \ - '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}' - */ - $ts = $this->getMsecTime() . '000000'; - $datas = implode("&",$kv); - $values = [[$ts,$datas]]; - $app_name = env('APP_NAME').'_'.env('APP_ENV'); - - $pushLabels['app']= $app_name; - $pushDatas = [ - 'streams'=>[ - [ - 'stream'=>$pushLabels, - 'values'=>$values, - ] - ] - ]; - $client->post( - env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push', - [ - 'headers'=>[ - 'Content-Type'=>'application/json' - ], - 'body' => json_encode($pushDatas) - ] - ); - //var_dump(json_encode($pushDatas) ); - }); + // co(function () use ($labels,$datas){ + + // $client = $this->getClient(); + // $kv = []; + // foreach ($datas as $key => $value) { + // $kv[] = $key."=".$value; + // } + // $pushLabels = []; + + // $event_name = 'event_'.env('APP_ENV'); + // if(!empty($labels)) $pushLabels[$event_name] = $labels; + + // /* + // * data format: + // curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \ + // '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}' + // */ + // $ts = $this->getMsecTime() . '000000'; + // $datas = implode("&",$kv); + // $values = [[$ts,$datas]]; + // $app_name = env('APP_NAME').'_'.env('APP_ENV'); + + // $pushLabels['app']= $app_name; + // $pushDatas = [ + // 'streams'=>[ + // [ + // 'stream'=>$pushLabels, + // 'values'=>$values, + // ] + // ] + // ]; + // $client->post( + // env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push', + // [ + // 'headers'=>[ + // 'Content-Type'=>'application/json' + // ], + // 'body' => json_encode($pushDatas) + // ] + // ); + // //var_dump(json_encode($pushDatas) ); + // }); } public function eventInTask($labels=null,$datas){ - $client = $this->getClient(); - $kv = []; - foreach ($datas as $key => $value) { - $kv[] = $key."=".$value; - } - $pushLabels = []; - - $event_name = 'event_'.env('APP_ENV'); - if(!empty($labels)) $pushLabels[$event_name] = $labels; - - /* - * data format: - curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \ - '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}' - */ - $ts = $this->getMsecTime() . '000000'; - $datas = implode("&",$kv); - $values = [[$ts,$datas]]; - $app_name = env('APP_NAME').'_'.env('APP_ENV'); - - $pushLabels['app']= $app_name; - $pushDatas = [ - 'streams'=>[ - [ - 'stream'=>$pushLabels, - 'values'=>$values, - ] - ] - ]; - $client->post( - env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push', - [ - 'headers'=>[ - 'Content-Type'=>'application/json' - ], - 'body' => json_encode($pushDatas) - ] - ); + // $client = $this->getClient(); + // $kv = []; + // foreach ($datas as $key => $value) { + // $kv[] = $key."=".$value; + // } + // $pushLabels = []; + + // $event_name = 'event_'.env('APP_ENV'); + // if(!empty($labels)) $pushLabels[$event_name] = $labels; + + // /* + // * data format: + // curl -v -H "Content-Type: application/json" -XPOST -s "http://39.96.12.39:3100/loki/api/v1/push" --data-raw \ + // '{"streams": [{ "stream": { "foo": "bar2" }, "values": [ [ "1596274538882028800", "fizzbuzz" ] ] }]}' + // */ + // $ts = $this->getMsecTime() . '000000'; + // $datas = implode("&",$kv); + // $values = [[$ts,$datas]]; + // $app_name = env('APP_NAME').'_'.env('APP_ENV'); + + // $pushLabels['app']= $app_name; + // $pushDatas = [ + // 'streams'=>[ + // [ + // 'stream'=>$pushLabels, + // 'values'=>$values, + // ] + // ] + // ]; + // $client->post( + // env('LOG_HOST','http://39.96.12.39:3100').'/loki/api/v1/push', + // [ + // 'headers'=>[ + // 'Content-Type'=>'application/json' + // ], + // 'body' => json_encode($pushDatas) + // ] + // ); //var_dump(json_encode($pushDatas) ); } diff --git a/app/Service/v3/Implementations/IOTAliService.php b/app/Service/v3/Implementations/IOTAliService.php index 0c10a48..ed8439c 100644 --- a/app/Service/v3/Implementations/IOTAliService.php +++ b/app/Service/v3/Implementations/IOTAliService.php @@ -24,7 +24,6 @@ class IOTAliService implements IOTServiceInterface AlibabaCloud::accessKeyClient(config('aliiot.key'), config('aliiot.secret')) ->regionId(config('aliiot.region')) ->asDefaultClient(); - try { AlibabaCloud::rpc() ->product('Iot') @@ -38,6 +37,7 @@ class IOTAliService implements IOTServiceInterface 'TopicFullName' => "/".config('aliiot.prod_key')."/".$device_name."/user/get", 'MessageContent' => base64_encode($msg), 'ProductKey' => config('aliiot.prod_key'), + 'IotInstanceId'=> config('aliiot.inst_id'), ], ]) ->request(); diff --git a/composer.json b/composer.json index 647241f..41b54a8 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,8 @@ "hyperf/consul": "^2.0", "alibabacloud/client": "^1.5", "hashids/hashids": "^4.0", - "hyperf/crontab": "^2.0" + "hyperf/crontab": "^2.0", + "hyperf/swoole-tracker": "2.0" }, "require-dev": { "swoole/ide-helper": "^4.5", diff --git a/composer.lock b/composer.lock index c73f3b9..5343a4f 100644 --- a/composer.lock +++ b/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": "7ab1eb814ce8a0d34cfec6c2c0725022", + "content-hash": "8b0c7e443a061d0a1b78cdb85db6d105", "packages": [ { "name": "adbario/php-dot-notation", @@ -3646,6 +3646,74 @@ }, "time": "2020-12-16T01:47:21+00:00" }, + { + "name": "hyperf/swoole-tracker", + "version": "v2.0.0", + "source": { + "type": "git", + "url": "https://github.com/hyperf/swoole-tracker.git", + "reference": "85af3c06333d01f1dd27957379f38d48d1871f08" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/hyperf/swoole-tracker/zipball/85af3c06333d01f1dd27957379f38d48d1871f08", + "reference": "85af3c06333d01f1dd27957379f38d48d1871f08", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "hyperf/contract": "~2.0.0", + "hyperf/utils": "~2.0.0", + "php": ">=7.2", + "psr/container": "^1.0", + "psr/http-server-middleware": "^1.0", + "psr/log": "^1.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.9", + "malukenho/docheader": "^0.1.6", + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + }, + "hyperf": { + "config": "Hyperf\\SwooleTracker\\ConfigProvider" + } + }, + "autoload": { + "psr-4": { + "Hyperf\\SwooleTracker\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A swoole tracker library for Hyperf.", + "homepage": "https://hyperf.io", + "keywords": [ + "hyperf", + "php", + "swoole", + "swoole-tracker" + ], + "support": { + "docs": "https://hyperf.wiki", + "issues": "https://github.com/hyperf/hyperf/issues", + "pull-request": "https://github.com/hyperf/hyperf/pulls", + "source": "https://github.com/hyperf/hyperf" + }, + "time": "2020-06-19T13:11:07+00:00" + }, { "name": "hyperf/task", "version": "v2.0.18", diff --git a/config/autoload/aliiot.php b/config/autoload/aliiot.php index 57c27d2..67c3cc2 100644 --- a/config/autoload/aliiot.php +++ b/config/autoload/aliiot.php @@ -8,4 +8,5 @@ return [ 'secret' => env('ALI_IOT_SECRET', ''), 'region' => env('ALI_IOT_REGION', ''), 'prod_key' => env('ALI_IOT_PRODUCT_KEY', ''), + 'inst_id' => env('ALI_IOT_INST_ID', ''), ]; \ No newline at end of file