DeviceServer.php 25 KB

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