From cb16f46f1a18fef2e4409a5fbee728a59fa43b36 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 1 Aug 2020 19:47:38 +0800 Subject: [PATCH] add ali iot vendor --- app/Controller/TestController.php | 10 +++- app/TaskWorker/AliIotTask.php | 91 +++++++++++++++++++++++++++++++ app/TaskWorker/PubRequest.php | 56 +++++++++++++++++++ composer.json | 4 +- 4 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 app/TaskWorker/AliIotTask.php create mode 100644 app/TaskWorker/PubRequest.php diff --git a/app/Controller/TestController.php b/app/Controller/TestController.php index bc034d0..78c2327 100644 --- a/app/Controller/TestController.php +++ b/app/Controller/TestController.php @@ -11,6 +11,7 @@ use Hyperf\Utils\ApplicationContext; use Hyperf\Task\TaskExecutor; use Hyperf\Task\Task; use App\TaskWorker\SSDBTask; +use App\TaskWorker\AliIotTask; /** * @AutoController() @@ -28,9 +29,12 @@ class TestController extends AbstractController // $exec = $container->get(TaskExecutor::class); // $result = $exec->execute(new Task([MethodTask::class, 'handle'], [Coroutine::id()])); - $client = ApplicationContext::getContainer()->get(SSDBTask::class); - $result = $client->exec("set","bar","1234"); - $result = $client->exec("get","bar"); + // $client = ApplicationContext::getContainer()->get(SSDBTask::class); + // $result = $client->exec("set","bar","1234"); + // $result = $client->exec("get","bar"); + $client = ApplicationContext::getContainer()->get(AliIotTask::class); + + $client->exec("set","bar","1234"); // $client = ApplicationContext::getContainer()->get(MethodTask::class); // $result = $client->handle("set"); diff --git a/app/TaskWorker/AliIotTask.php b/app/TaskWorker/AliIotTask.php new file mode 100644 index 0000000..e629137 --- /dev/null +++ b/app/TaskWorker/AliIotTask.php @@ -0,0 +1,91 @@ +regionId('cn-shanghai') + ->asDefaultClient(); + $result2 = ''; + + $msg = "{\"msg\":\"收款123元\"}"; + try{ + AlibabaCloud::rpc() + ->product('Iot') + // ->scheme('https') // https | http + ->version('2018-01-20') + ->action('Pub') + ->method('POST') + ->host('iot.cn-shanghai.aliyuncs.com') + ->options([ + 'query' => [ + 'RegionId' => "cn-shanghai", + 'TopicFullName' => "/a1ZSurIJmO0/BOX0000000000003/user/get", + 'MessageContent' => base64_encode($msg), + 'ProductKey' => "a1ZSurIJmO0", + ], + ]) + ->request(); + //var_dump($result2->toArray()); + } catch (ClientException $e) { + echo $e->getErrorMessage() . PHP_EOL; + } catch (ServerException $e) { + echo $e->getErrorMessage() . PHP_EOL; + } + + //print_r($response); + return ""; + } + +// protected function getClient(){ +// AlibabaCloud::accessKeyClient('LTAI4GJEWrN6dVh7HmPKHMyF', 'wMae4ckfVGwMQPVw5ZlVDDpihVeUap') +// ->regionId('cn-shanghai') +// ->asDefaultClient(); +// $result = ''; + +// $msg = "{\"msg\":\"收款123元\"}"; +// try{ +// $result = AlibabaCloud::rpc() +// ->product('Iot') +// // ->scheme('https') // https | http +// ->version('2018-01-20') +// ->action('Pub') +// ->method('POST') +// ->host('iot.cn-shanghai.aliyuncs.com') +// ->options([ +// 'query' => [ +// 'RegionId' => "cn-shanghai", +// 'TopicFullName' => "/a1ZSurIJmO0/BOX0000000000003/user/get", +// 'MessageContent' => base64_encode($msg), +// 'ProductKey' => "a1ZSurIJmO0", +// ], +// ]) +// ->request(); +// var_dump($result->toArray()); +// } catch (ClientException $e) { +// echo $e->getErrorMessage() . PHP_EOL; +// } catch (ServerException $e) { +// echo $e->getErrorMessage() . PHP_EOL; +// } +// return $result; + +// } +} + diff --git a/app/TaskWorker/PubRequest.php b/app/TaskWorker/PubRequest.php new file mode 100644 index 0000000..e784ee1 --- /dev/null +++ b/app/TaskWorker/PubRequest.php @@ -0,0 +1,56 @@ +setMethod("POST"); + } + + private $topicFullName; + + private $qos; + + private $messageContent; + + private $productKey; + + public function getTopicFullName() { + return $this->topicFullName; + } + + public function setTopicFullName($topicFullName) { + $this->topicFullName = $topicFullName; + $this->queryParameters["TopicFullName"]=$topicFullName; + } + + public function getQos() { + return $this->qos; + } + + public function setQos($qos) { + $this->qos = $qos; + $this->queryParameters["Qos"]=$qos; + } + + public function getMessageContent() { + return $this->messageContent; + } + + public function setMessageContent($messageContent) { + $this->messageContent = $messageContent; + $this->queryParameters["MessageContent"]=$messageContent; + } + + public function getProductKey() { + return $this->productKey; + } + + public function setProductKey($productKey) { + $this->productKey = $productKey; + $this->queryParameters["ProductKey"]=$productKey; + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json index c9a89d0..0910063 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,8 @@ "hyperf/paginator": "^2.0", "hyperf/filesystem": "^2.0", "xxtime/flysystem-aliyun-oss": "^1.5", - "hyperf/validation": "^2.0", - "hyperf/task": "^2.0" + "hyperf/task": "^2.0", + "alibabacloud/iot": "^1.8" }, "require-dev": { "swoole/ide-helper": "^4.5",