AliYunIotServer.php 6.5 KB

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