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.

54 lines
1.8 KiB

5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use App\Commons\Log;
  7. use App\Constants\v3\LogLabel;
  8. use App\Service\v3\Interfaces\IOTServiceInterface;
  9. use Hyperf\Di\Annotation\Inject;
  10. class IOTAliService implements IOTServiceInterface
  11. {
  12. /**
  13. * @Inject
  14. * @var Log
  15. */
  16. protected $log;
  17. public function pub($device_name, $msg)
  18. {
  19. AlibabaCloud::accessKeyClient(config('aliiot.key'), config('aliiot.secret'))
  20. ->regionId(config('aliiot.region'))
  21. ->asDefaultClient();
  22. try {
  23. AlibabaCloud::rpc()
  24. ->product('Iot')
  25. ->version('2018-01-20')
  26. ->action('Pub')
  27. ->method('POST')
  28. ->host(config('aliiot.host'))
  29. ->options([
  30. 'query' => [
  31. 'RegionId' => "cn-shanghai",
  32. 'TopicFullName' => "/".config('aliiot.prod_key')."/".$device_name."/user/get",
  33. 'MessageContent' => base64_encode($msg),
  34. 'ProductKey' => config('aliiot.prod_key'),
  35. 'IotInstanceId'=> config('aliiot.inst_id'),
  36. ],
  37. ])
  38. ->request();
  39. } catch (ClientException $e) {
  40. $this->log->event(LogLabel::DEVICE_SEND_LOG, ['msg' => 'ClientException发布失败:'.$e->getErrorMessage()]);
  41. return false;
  42. } catch (ServerException $e) {
  43. $this->log->event(LogLabel::DEVICE_SEND_LOG, ['msg' => 'ServerException发布失败:'.$e->getErrorMessage()]);
  44. return false;
  45. }
  46. return true;
  47. }
  48. }