AliYunIotServer.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Server;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use Illuminate\Support\Facades\Log;
  7. class AliYunIotServer
  8. {
  9. protected $errMsg;
  10. private $appKey;
  11. private $appSecret;
  12. private $regionId;
  13. public const DISABLE = 0;
  14. public const ENABLE = 1;
  15. /**
  16. * AliYunIotServer constructor.
  17. */
  18. public function __construct(string $appKey, string $appSecret, string $regionId = 'cn-hangzhou')
  19. {
  20. $this->appKey = $appKey;
  21. $this->appSecret = $appSecret;
  22. $this->regionId = $regionId;
  23. }
  24. /**
  25. * 获取服务列表.
  26. *
  27. * @return array|string
  28. */
  29. public function getProductList(int $pageSize = 5, int $currentPage = 1)
  30. {
  31. $params = [
  32. 'RegionId' => $this->regionId,
  33. 'PageSize' => "$pageSize",
  34. 'CurrentPage' => "$currentPage",
  35. ];
  36. return $this->AliCurl('post', 'QueryProductList', $params);
  37. }
  38. /**
  39. * 获取设备列表.
  40. *
  41. * @return array|string
  42. */
  43. public function getDeviceList(string $productKey, int $pageSize = 5, int $currentPage = 1)
  44. {
  45. $params = [
  46. 'RegionId' => $this->regionId,
  47. 'PageSize' => "$pageSize",
  48. 'ProductKey' => "$productKey",
  49. 'CurrentPage' => "$currentPage",
  50. ];
  51. return $this->AliCurl('post', 'QueryDevice', $params);
  52. }
  53. /**
  54. * 获取设备属性.
  55. *
  56. * @return array|string
  57. */
  58. public function getDeviceProperty(string $iotId, string $iotInstanceId = '')
  59. {
  60. $params['IotId'] = $iotId;
  61. $params['RegionId'] = $this->regionId;
  62. /*foreach ($identifier as $key => $val) {
  63. $params['Identifier.' . ($key + 1)] = $val;
  64. }*/
  65. if ($iotInstanceId) {
  66. $params['IotInstanceId'] = $iotInstanceId;
  67. }
  68. return $this->AliCurl('post', 'QueryDevicePropertyStatus', $params);
  69. }
  70. /**
  71. * 获取设备状态
  72. *
  73. * @return array|string
  74. */
  75. public function getDeviceStatus(string $iotId, string $iotInstanceId = '')
  76. {
  77. $params['IotId'] = $iotId;
  78. $params['RegionId'] = $this->regionId;
  79. /*foreach ($identifier as $key => $val) {
  80. $params['Identifier.' . ($key + 1)] = $val;
  81. }*/
  82. if ($iotInstanceId) {
  83. $params['IotInstanceId'] = $iotInstanceId;
  84. }
  85. return $this->AliCurl('post', 'GetDeviceStatus', $params);
  86. }
  87. /**
  88. * 设置设备属性.
  89. *
  90. * @return array|string
  91. */
  92. public function setDeviceProperty(string $iotId, array $property, string $iotInstanceId = '')
  93. {
  94. /*if ($this->checkProperty($property)) {
  95. return $this->errMsg;
  96. }*/
  97. $params['Items'] = json_encode($property);
  98. // dd($params);
  99. Log::info('手动上报设备属性' . $params['Items']);
  100. $params['IotId'] = $iotId;
  101. if ($iotInstanceId) {
  102. $params['IotInstanceId'] = $iotInstanceId;
  103. }
  104. // dd($params);
  105. return $this->AliCurl('post', 'SetDeviceProperty', $params);
  106. }
  107. /**
  108. * 调用设备服务
  109. *
  110. * @return array|string
  111. */
  112. public function invokeThingService(string $iotId, string $identifier, object $args, string $iotInstanceId = '')
  113. {
  114. $params['IotId'] = $iotId;
  115. $params['Identifier'] = $identifier;
  116. $params['Args'] = json_encode($args);
  117. if ($iotInstanceId) {
  118. $params['IotInstanceId'] = $iotInstanceId;
  119. }
  120. return $this->AliCurl('post', 'InvokeThingService', $params);
  121. }
  122. /**
  123. * 禁用&解禁设备.
  124. *
  125. * @return array|string
  126. */
  127. public function switchDevice(int $status, string $iotId, string $iotInstanceId = '')
  128. {
  129. $params['IotId'] = $iotId;
  130. if ($iotInstanceId) {
  131. $params['IotInstanceId'] = $iotInstanceId;
  132. }
  133. if (self::DISABLE == $status) {
  134. return $this->AliCurl('post', 'DisableThing', $params);
  135. } elseif (self::ENABLE == $status) {
  136. return $this->AliCurl('post', 'EnableThing', $params);
  137. }
  138. }
  139. /**
  140. * @return array|string
  141. */
  142. private function AliCurl(string $method, string $action, array $params = [])
  143. {
  144. try {
  145. AlibabaCloud::accessKeyClient($this->appKey, $this->appSecret)
  146. ->regionId($this->regionId)
  147. ->asDefaultClient();
  148. $result = AlibabaCloud::rpc()
  149. ->product('Iot')
  150. // ->scheme('https') // https | http
  151. ->version('2018-01-20')
  152. ->action($action)
  153. ->method(strtoupper($method))
  154. ->host('iot.cn-shanghai.aliyuncs.com')
  155. ->options([
  156. 'query' => $params,
  157. ])
  158. ->request();
  159. return $result->toarray();
  160. } catch (ClientException $e) {
  161. return $e->getErrorMessage() . PHP_EOL;
  162. } catch (ServerException $e) {
  163. return $e->getErrorMessage() . PHP_EOL;
  164. }
  165. }
  166. /**
  167. * @return bool
  168. */
  169. protected function checkProperty(array $params)
  170. {
  171. $initialProperty = ['LockSwitch' => 'is_bool'];
  172. foreach ($params as $key => $val) {
  173. if (!isset($initialProperty[$key])) {
  174. $this->errMsg = '该属性未定义';
  175. return false;
  176. }
  177. if ($initialProperty[$key]($val)) {
  178. $this->errMsg = '该属性数据类型异常';
  179. return false;
  180. }
  181. if ('' == $val || [] == $val || null == $val) {
  182. $this->errMsg = '属性不能为空';
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188. }