You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

  1. <?php
  2. namespace App\TaskWorker;
  3. use App\Commons\Log;
  4. use Hyperf\Utils\ApplicationContext;
  5. use Hyperf\Task\Annotation\Task;
  6. use AlibabaCloud\Client\AlibabaCloud;
  7. use AlibabaCloud\Client\Exception\ClientException;
  8. use AlibabaCloud\Client\Exception\ServerException;
  9. use Hyperf\Di\Annotation\Inject;
  10. class AliIotTask
  11. {
  12. /**
  13. * @Inject
  14. * @var Log
  15. */
  16. protected $log;
  17. /**
  18. * @var DefaultAcsClient
  19. */
  20. public $client = null;
  21. /**
  22. * @Task
  23. */
  24. public function exec($device_name, $msg)
  25. {
  26. AlibabaCloud::accessKeyClient('LTAI4GJEWrN6dVh7HmPKHMyF', 'wMae4ckfVGwMQPVw5ZlVDDpihVeUap')
  27. ->regionId('cn-shanghai')
  28. ->asDefaultClient();
  29. try {
  30. AlibabaCloud::rpc()
  31. ->product('Iot')
  32. ->version('2018-01-20')
  33. ->action('Pub')
  34. ->method('POST')
  35. ->host('iot.cn-shanghai.aliyuncs.com')
  36. ->options([
  37. 'query' => [
  38. 'RegionId' => "cn-shanghai",
  39. 'TopicFullName' => "/a1ZSurIJmO0/".$device_name."/user/get",
  40. 'MessageContent' => base64_encode($msg),
  41. 'ProductKey' => "a1ZSurIJmO0",
  42. ],
  43. ])
  44. ->request();
  45. } catch (ClientException $e) {
  46. echo $e->getErrorMessage() . PHP_EOL;
  47. } catch (ServerException $e) {
  48. echo $e->getErrorMessage() . PHP_EOL;
  49. }
  50. return true;
  51. }
  52. }