DeviceServer.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. namespace App\Server;
  3. use AlibabaCloud\Iot\V20180120\RRpc;
  4. use App\Model\DeliverInfo;
  5. use App\Model\DeviceInfo;
  6. use App\Model\SystemConfig;
  7. use App\Model\WaringMessage;
  8. use Illuminate\Support\Facades\Log;
  9. use Illuminate\Support\Facades\Cache;
  10. class DeviceServer
  11. {
  12. private $conf;
  13. private $property;
  14. private $blueProperty;
  15. const CLOSE = 0;
  16. const OPEN = 1;
  17. const AGENTOPEN = 2;
  18. const AGENTCLOSE = 3;
  19. public function __construct()
  20. {
  21. $this->property = [
  22. 'box1up' => 'box1',
  23. 'box2up' => 'box2',
  24. 'box3up' => 'box3',
  25. 'box4up' => 'box4',
  26. 'box5up' => 'box5',
  27. 'box6up' => 'box6',
  28. 'Software_Version' => 'version',
  29. 'IMEI' => 'imei',
  30. // 'GeoLocation' => 'location',
  31. 'MagDoorSwitch' => 'lock_switch',
  32. 'RodSwitch' => 'deliver_lock_switch',
  33. 'RunningState' => 'runningStatus',
  34. 'Weight' => 'device_weight',
  35. 'RestCapacity' => 'rest_capacity',
  36. ];
  37. $this->blueProperty = [
  38. 'CurrentTemperature' => 'device_tem',
  39. 'IMEI' => 'imei',
  40. //'GeoLocation' => 'location',
  41. 'MagDoorSwitch' => 'lock_switch',
  42. 'RodSwitch' => 'deliver_lock_switch',
  43. 'RunningState' => 'runningStatus',
  44. 'Weight' => 'device_weight',
  45. 'RestCapacity' => 'rest_capacity',
  46. ];
  47. $this->conf = SystemConfig::get('ali_config');
  48. }
  49. /**
  50. * 刷新所有设备列表
  51. * @return string|bool
  52. */
  53. public function refreshList()
  54. {
  55. $conf = $this->conf;
  56. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  57. return '请先配置阿里云app秘钥';
  58. }
  59. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  60. return '请先配置阿里云appKey';
  61. }
  62. if (!isset($conf['productKey']) || empty($conf['productKey'])) {
  63. return '请先配置阿里云产品识别码';
  64. }
  65. $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  66. $list = $ali->getDeviceList($conf['productKey'], 100);
  67. $deviceList = $list['Data']['DeviceInfo'];
  68. if ($list['Page'] < $list['PageCount']) {
  69. for ($i = ($list['Page'] + 1); $i <= $list['PageCount']; $i++) {
  70. $list = $ali->getDeviceList($conf['productKey'], 100, $i);
  71. $deviceList = array_merge($deviceList, $list['Data']['DeviceInfo']);
  72. }
  73. }
  74. foreach ($deviceList as $key => $val) {
  75. $check = DeviceInfo::where([['device_name', $val['DeviceName']], ['product_key', $val['ProductKey']]])
  76. ->orWhere('iot_id', $val['IotId'])->first(['id', 'status']);
  77. $status = 0;
  78. switch ($val['DeviceStatus']) {
  79. case 'ONLINE':
  80. $status = DeviceInfo::ONLINE;
  81. break;
  82. case 'OFFLINE':
  83. $status = DeviceInfo::OFFLINE;
  84. break;
  85. case 'UNACTIVE':
  86. $status = DeviceInfo::UNACTIVE;
  87. break;
  88. case 'DISABLE':
  89. $status = DeviceInfo::DISABLE;
  90. break;
  91. }
  92. if ($check) {
  93. $check->status = $status;
  94. $check->save();
  95. } else {
  96. $create = [
  97. 'status' => $status,
  98. 'iot_id' => $val['IotId'],
  99. 'product_key' => $val['ProductKey'],
  100. 'device_secret' => $val['DeviceSecret'],
  101. 'device_name' => $val['DeviceName'],
  102. ];
  103. if ($status != 3) {
  104. DeviceInfo::insert($create);
  105. }
  106. }
  107. $this->refreshDevice($val['IotId']);
  108. }
  109. return true;
  110. }
  111. /**
  112. * 刷新设备属性
  113. * @param String $iot_id
  114. * @return bool|string|array
  115. */
  116. public function refreshDevice(string $iot_id)
  117. {
  118. $conf = $this->conf;
  119. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  120. return '请先配置阿里云app秘钥';
  121. }
  122. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  123. return '请先配置阿里云appKey';
  124. }
  125. $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  126. $deviceProperty = $ali->getDeviceProperty($iot_id);
  127. Log::info("设备数据刷新[1]:" . json_encode($deviceProperty));
  128. if (isset($deviceProperty['Success']) && $deviceProperty['Success']) {
  129. $data = [];
  130. foreach ($deviceProperty['Data']['List']['PropertyStatusInfo'] as $index => $item) {
  131. if (isset($this->property[$item['Identifier']])) {
  132. if (isset($item['Value'])) {
  133. $data[$this->property[$item['Identifier']]] = $item['Value'];
  134. } else {
  135. return ['data' => ['ErrorMessage' => "请检查设备属性[{$item['Identifier']}]是否初始化"], 'code' => 4];
  136. }
  137. }
  138. }
  139. if (empty($data)) {
  140. return ['data' => ['ErrorMessage' => '未获取到设备属性'], 'code' => 3];
  141. }
  142. //修复
  143. // if ($data['deliver_lock_switch'] == 1) {
  144. // return ['data' => ['ErrorMessage' => '当前设备已开启,请等待'], 'code' => '2'];
  145. // }
  146. /* if (isset($data['location'])) {
  147. $location = json_decode($data['location'], true);
  148. $data['device_latitude'] = $location['latitude'];
  149. $data['device_longitude'] = $location['longitude'];
  150. unset($data['location']);
  151. }*/
  152. /*if ($data['rest_capacity'] == 0) {
  153. $data['status'] = DeviceInfo::MAX;
  154. } else {
  155. $data['status'] = DeviceInfo::WELL;
  156. }*/
  157. $insert['device_item'] = $data['device_tem'];
  158. $insert['imei'] = $data['imei'];
  159. if ($data['device_tem'] >= 80) {
  160. $id = DeviceInfo::where('iot_id', $iot_id)->first(['id'])->id;
  161. // Log::info("设备温度【{$id}】:{$data['device_tem']}" );
  162. // WaringMessage::temWaring($id);
  163. }
  164. $res = DeviceInfo::where('iot_id', $iot_id)->update($insert);
  165. if (!$res) {
  166. Log::info('设备数据刷新[2]:' . json_encode($data));
  167. }
  168. return ['data' => $data, 'code' => '0'];
  169. } else {
  170. return ['data' => $deviceProperty, 'code' => 1];
  171. }
  172. }
  173. /**
  174. * 获取设备属性
  175. * @param String $iot_id
  176. * @return bool|string|array
  177. */
  178. public function getProperty(string $iot_id)
  179. {
  180. $conf = $this->conf;
  181. //Todo 全局 两个if合并
  182. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  183. return '请先配置阿里云app秘钥';
  184. }
  185. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  186. return '请先配置阿里云appKey';
  187. }
  188. $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  189. $deviceProperty = $ali->getDeviceProperty($iot_id);
  190. // dd($deviceProperty);
  191. if (isset($deviceProperty['Success']) && $deviceProperty['Success']) {
  192. $data = [];
  193. foreach ($deviceProperty['Data']['List']['PropertyStatusInfo'] as $index => $item) {
  194. if (isset($this->property[$item['Identifier']])) {
  195. if (isset($item['Value'])) {
  196. $data[$this->property[$item['Identifier']]] = $item['Value'];
  197. } else {
  198. $data[$this->property[$item['Identifier']]] = '';
  199. // return ['data' => ['ErrorMessage' => "请检查设备属性[{$item['Identifier']}]是否初始化"], 'code' => 4];
  200. }
  201. }
  202. }
  203. if (empty($data)) {
  204. return ['data' => ['ErrorMessage' => '未获取到设备属性'], 'code' => 3];
  205. }
  206. /*if (isset($data['location'])) {
  207. $location = json_decode($data['location'], true);
  208. $data['device_latitude'] = $location['latitude'];
  209. $data['device_longitude'] = $location['longitude'];
  210. unset($data['location']);
  211. }*/
  212. /*if ($data['rest_capacity'] == 0) {
  213. $data['status'] = DeviceInfo::MAX;
  214. } else {
  215. $data['status'] = DeviceInfo::WELL;
  216. }*/
  217. return ['data' => $data, 'code' => '0'];
  218. } else {
  219. return ['data' => $deviceProperty, 'code' => 1];
  220. }
  221. }
  222. //蓝牙链接 传输加密数据
  223. public function getDecodeProperty(string $base64_data, string $iot_id)
  224. {
  225. Log::info($base64_data);
  226. $conf = $this->conf;
  227. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  228. return '请先配置阿里云app秘钥';
  229. }
  230. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  231. return '请先配置阿里云appKey';
  232. }
  233. $dataDecode = ToolServer::decode($base64_data, '1234567890123456');
  234. //$dataDecode = json_decode($base64_data, true);
  235. Log::info("蓝牙设备数据刷新:" . json_encode($dataDecode));
  236. $data = [];
  237. if (!$dataDecode["P"]) {
  238. return ['message' => '未获取到设备属性', 'code' => 3];
  239. }
  240. $updateIotData = [];
  241. foreach ($dataDecode["P"] as $index => $item) {
  242. if (isset($this->property[$item['I']])) {
  243. if (isset($item['V'])) {
  244. $data[$this->property[$item['I']]] = $item['V'];
  245. $updateIotData[$item['I']] = $item['V'];
  246. } else {
  247. return ['data' => ['ErrorMessage' => "请检查设备属性[{$item['Identifier']}]是否初始化"], 'code' => 4];
  248. }
  249. }
  250. }
  251. if (empty($data)) {
  252. return ['message' => '未获取到设备属性', 'code' => 3];
  253. }
  254. $iotService = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  255. $resUpload = $iotService->setDeviceProperty($iot_id, $updateIotData);
  256. /*if (!$resUpload['Success']) {
  257. return ['message' => '设备数据上报物联网失败', 'code' => 4];
  258. }*/
  259. if ($data['device_tem'] >= 80) {
  260. $id = DeviceInfo::where('iot_id', $iot_id)->first(['id'])->id;
  261. WaringMessage::temWaring($id);
  262. }
  263. $res = DeviceInfo::where('iot_id', $iot_id)->update($data);
  264. if (!$res) {
  265. Log::info('设备数据刷新[3]:' . json_encode($data));
  266. }
  267. return ['data' => $data, 'code' => '0'];
  268. }
  269. public function getBase64Property(string $base64_data)
  270. {
  271. $dataDecode = json_decode(base64_decode($base64_data), true);
  272. // Log::debug($dataDecode);
  273. if ( empty($dataDecode)|| !is_array($dataDecode) || !isset($dataDecode['identifier']) ) {
  274. return true;
  275. }
  276. if ($dataDecode['identifier'] == 'errorEvent') {
  277. $id = DeviceInfo::where('iot_id', $dataDecode['iotId'])->first(['id'])->id;
  278. WaringMessage::normalWarning($id, $dataDecode['value']['errorEvent'], WaringMessage::ALARM);
  279. return true;
  280. }
  281. if ($dataDecode['identifier'] == 'warnEvent') {
  282. $device = DeviceInfo::where('iot_id', $dataDecode['iotId'])->first(['id', 'device_comment']);
  283. $last = WaringMessage::where([['no', $device->id], ['message', $dataDecode['value']['warnEvent']], ['type', WaringMessage::WARN]])->first(['created_at']);
  284. $mobile = SystemConfig::get('system_config', 'admin_mobile', 18981831779);
  285. if ($mobile && (!$last || ($last && strtotime($last->created_at) + 1800 <= time()))) {
  286. $type = SystemConfig::get('sms_config', 'sms_type', 'AliSmsServer');
  287. $template = SystemConfig::get('ali_config', 'sms', '');
  288. $template = json_decode($template, true);
  289. if ($template['DeviceTmpId'] && $template['sign']) {
  290. $type = '\App\Server\Sms\\' . $type;
  291. $sms = new SmsServer(new $type($mobile, $template['DeviceTmpId'], $template['sign'], ['deviceId' => $device->id, 'deviceName' => $device->device_comment, 'warnInfo' => $dataDecode['value']['warnEvent']]));
  292. $sms->send();
  293. }
  294. }
  295. WaringMessage::normalWarning($device->id, $dataDecode['value']['warnEvent'], WaringMessage::WARN);
  296. return true;
  297. }
  298. if ($dataDecode['identifier'] == 'commoneEvent') {
  299. //toDO 开/关门
  300. $device = DeviceInfo::where('iot_id', $dataDecode['iotId'])->first(['id']);
  301. if(!$device){
  302. return true;
  303. }
  304. $order = DeliverInfo::where('device_id', $device->id)->orderBy('id', 'Desc')->first();
  305. //优化关门统一走回调
  306. if(!$order || $order->status != DeliverInfo::UNFINISHED){
  307. return true;
  308. }
  309. Log::info('Cache:' . 'device-msg-'.$dataDecode['iotId'].'-'.$dataDecode['requestId']);
  310. //处理设备发送重复消息
  311. if (Cache::has('device-msg-'.$dataDecode['iotId'].'-'.$dataDecode['requestId'])) {
  312. Log::info('关门回调通知重复消息:' .$dataDecode['requestId']);
  313. return true;
  314. }
  315. if($dataDecode['value']['action']=='open') {
  316. //开门回调通知
  317. Log::info('开门回调通知:' . json_encode($dataDecode));
  318. $order->open_weight = $dataDecode['value']['weight'];
  319. $order->save();
  320. Cache::put('device-msg-'.$dataDecode['iotId'].'-'.$dataDecode['requestId'], $dataDecode['requestId'], 3600);
  321. return true;
  322. }
  323. if($dataDecode['value']['action']=='close'){
  324. //关门回调通知
  325. Log::info('关门回调通知:' . json_encode($dataDecode));
  326. $close_weight = $dataDecode['value']['weight'];
  327. $weight = $close_weight - $order->open_weight;
  328. $order->close_weight = $close_weight;
  329. $order->weight = $weight;
  330. $order->finished_at = date('Y-m-d H:i:s');
  331. Log::info('关门回调变化重量:'.$weight);
  332. Log::info('关门回调关门重量:'.$close_weight);
  333. Log::info('关门回调获得金额:'.$order->money);
  334. Log::info('关门回调变化重量:'.($weight <= 0));
  335. Cache::forget('deviceUse_' . $device->id);
  336. $fetch_price= SystemConfig::get('system_config', 'fetch_per_g', '0.1');
  337. $price = SystemConfig::get('system_config', 'put_per_g', '0.06');
  338. //如果有小区加个就取小区价格
  339. $deviceInfo = DeviceInfo::with('communities')->where(['id'=>$order['device_id']])->first();
  340. if(!empty($deviceInfo->communities)) {
  341. if(!empty($deviceInfo->communities->put_per_g)) $price = $deviceInfo->communities->put_per_g;
  342. if(!empty($deviceInfo->communities->fetch_per_g)) $fetch_price = $deviceInfo->communities->fetch_per_g;
  343. }
  344. $order->deliver_price = $price;
  345. $order->transport_price = $fetch_price;
  346. if ($weight <= 0) {
  347. $order->status=DeliverInfo::LOSS;
  348. $order->income = 0;
  349. if($weight < -5000){
  350. $order->status=DeliverInfo::UNNORMAL;
  351. WaringMessage::deliverWarning($order->no);
  352. }
  353. $order->save();
  354. Log::info('关门回调失效订单:' . json_encode($order));
  355. Cache::put('device-msg-'.$dataDecode['iotId'].'-'.$dataDecode['requestId'], $dataDecode['requestId'], 3600);
  356. return true;
  357. }
  358. $start = date('Y') . '-' . date('m') . '-' . date('d') . ' 00:00:00';
  359. $checkCount = DeliverInfo::where([['user_id', $order->user_id], ['status', DeliverInfo::SUCCESS]])
  360. ->where('created_at', '>', $start)->count();
  361. $deliverLimit = SystemConfig::get('system_config', 'deliver_limit', 10);
  362. $deliverWeight = SystemConfig::get('system_config', 'deliver_weight', 0);
  363. //To Do 地区价格计算公式
  364. Log::info('筛选时间:' . $start);
  365. Log::info('投递次数:' . $checkCount);
  366. Log::info('投递限制:' . $deliverLimit);
  367. Log::info('地区价格:' . $price);
  368. if (($deliverWeight != 0 && $weight > $deliverWeight) || $checkCount > $deliverLimit) {
  369. $order->status = DeliverInfo::WAIT_CHECK;
  370. } else {
  371. $order->status = DeliverInfo::SUCCESS;
  372. }
  373. $order->money = $price*$weight;
  374. $order->income = ($fetch_price - $price)*$weight;
  375. if ($order->save()) {
  376. Cache::put('device-msg-'.$dataDecode['iotId'].'-'.$dataDecode['requestId'], $dataDecode['requestId'], 3600);
  377. return true;
  378. } else {
  379. return false;
  380. }
  381. }
  382. }
  383. if (isset($dataDecode['items'])) {
  384. $property = $dataDecode['items'];
  385. $data = [];
  386. if (!$property) {
  387. return ['message' => '未获取到设备属性', 'code' => 3];
  388. }
  389. foreach ($property as $index => $item) {
  390. if (isset($this->property[$index])) {
  391. if (isset($item['value'])) {
  392. $data[$this->property[$index]] = $item['value'];
  393. } else {
  394. return ['data' => ['ErrorMessage' => "请检查设备属性[{$item['Identifier']}]是否初始化"], 'code' => 4];
  395. }
  396. }
  397. }
  398. if (empty($data)) {
  399. return ['message' => '未获取到设备属性', 'code' => 3];
  400. }
  401. if ($data['device_tem'] >= 80) {
  402. $id = DeviceInfo::where('iot_id', $dataDecode['iotId'])->first(['id'])->id;
  403. WaringMessage::temWaring($id);
  404. }
  405. $res = DeviceInfo::where('iot_id', $dataDecode['iotId'])->update($data);
  406. $this->getDeviceStatus($dataDecode['iotId']);
  407. if (!$res) {
  408. Log::info('设备数据刷新[4]:' . json_encode($data));
  409. }
  410. Log::info('success');
  411. return ['data' => $data, 'code' => '0'];
  412. }
  413. return true;
  414. }
  415. public function getDeviceStatus($iotId)
  416. {
  417. $conf = $this->conf;
  418. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  419. return '请先配置阿里云app秘钥';
  420. }
  421. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  422. return '请先配置阿里云appKey';
  423. }
  424. if (!isset($conf['productKey']) || empty($conf['productKey'])) {
  425. return '请先配置阿里云产品识别码';
  426. }
  427. $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  428. $res = $ali->GetDeviceStatus($iotId);
  429. if ($res['Success'] == true) {
  430. $check = DeviceInfo::Where('iot_id', $iotId)->first(['status']);
  431. $status = 0;
  432. switch ($res['Data']['Status']) {
  433. case 'ONLINE':
  434. $status = DeviceInfo::ONLINE;
  435. break;
  436. case 'OFFLINE':
  437. $status = DeviceInfo::OFFLINE;
  438. break;
  439. case 'UNACTIVE':
  440. $status = DeviceInfo::UNACTIVE;
  441. break;
  442. case 'DISABLE':
  443. $status = DeviceInfo::DISABLE;
  444. break;
  445. }
  446. ////// dd($val);
  447. $check->status = $status;
  448. $check->save();
  449. return $status;
  450. } else {
  451. return 'fail';
  452. }
  453. }
  454. /**
  455. * 启用和禁用
  456. * @param String $iot_id
  457. * @param int $state AliYunIotServer::ENABLE
  458. * @return array|string
  459. */
  460. public function switchDevice(string $iot_id, int $state = AliYunIotServer::ENABLE)
  461. {
  462. $conf = $this->conf;
  463. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  464. return '请先配置阿里云app秘钥';
  465. }
  466. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  467. return '请先配置阿里云appKey';
  468. }
  469. $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  470. $res = $ali->switchDevice($state, $iot_id);
  471. if (isset($res['Success']) && $res['Success']) {
  472. if ($state == AliYunIotServer::ENABLE) {
  473. $state = 0;
  474. } else {
  475. $state = 2;
  476. }
  477. DeviceInfo::where('iot_id', $iot_id)->update(['status' => $state]);
  478. return 'success';
  479. } else {
  480. return $res['ErrorMessage'];
  481. }
  482. }
  483. /** 投递门/收运门 开关
  484. * @param string $iot_id
  485. * @param int $mode
  486. * @return string
  487. */
  488. public function doorOperation(string $iot_id, int $mode = self::OPEN)
  489. {
  490. $conf = $this->conf;
  491. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  492. return '请先配置阿里云app秘钥';
  493. }
  494. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  495. return '请先配置阿里云appKey';
  496. }
  497. $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  498. if ($mode == self::OPEN) {
  499. $identifier = 'open1';
  500. } else if ($mode == self::CLOSE) {
  501. $identifier = 'close1';
  502. } else if ($mode == self::AGENTOPEN) {
  503. $identifier = 'RemoteAgentOpen';
  504. } else if ($mode == self::AGENTCLOSE) {
  505. $identifier = 'RemoteAgentClose';
  506. }
  507. $args = (object)[];
  508. $res = $ali->invokeThingService($iot_id, $identifier, $args);
  509. Log::info('doorOperation 参数:'. $iot_id .' '. $mode .'返回数据:'.json_encode($res));
  510. if (isset($res['Success']) && $res['Success']) {
  511. DeviceInfo::where('iot_id', $iot_id)->update(['deliver_lock_switch' => $mode]);
  512. return 'success';
  513. } else {
  514. return $res['ErrorMessage'];
  515. }
  516. }
  517. /** 下发锁协议
  518. * @param string $device_name 设备名称
  519. * @param string $rules 下发规则
  520. **/
  521. public function sendMsg(string $device_name, string $rules)
  522. {
  523. $conf = $this->conf;
  524. if (!isset($conf['appSecret']) || empty($conf['appSecret'])) {
  525. return '请先配置阿里云app秘钥';
  526. }
  527. if (!isset($conf['appKey']) || empty($conf['appKey'])) {
  528. return '请先配置阿里云appKey';
  529. }
  530. $ali = new AliYunIotServer($conf['appKey'], $conf['appSecret']);
  531. $args['Action'] = "Pub";
  532. $args['DeviceName'] = $device_name;
  533. $args['ProductKey'] = 'a15yVIP0Onl';
  534. $args['MessageContent'] = base64_encode($rules);
  535. $args['TopicFullName'] = "/a15yVIP0Onl/{$device_name}/user/get";
  536. // $args['RequestBase64Byte'] = base64_encode($rules);
  537. // $args['Timeout'] = 7000;
  538. // $args['Topic'] = "/a15yVIP0Onl/{$device_name}/user/get";
  539. $data = $ali->RRpc($args);
  540. return $data;
  541. }
  542. }