BikeControl.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/9/5
  6. * Time: 9:36 PM
  7. */
  8. namespace App\Handlers;
  9. use GatewayClient\Gateway;
  10. use Illuminate\Support\Facades\Redis;
  11. class BikeControl
  12. {
  13. private static $start = 'AA AA';
  14. //分割符
  15. const SPLIT_TAG = 'bbbb';
  16. // 控制指令
  17. const CMD_REMOTE_CONTROL = '06';
  18. // 远程查询
  19. const CMD_REMOTE_SELECT = '07';
  20. // 播放语言指令
  21. const CMD_REMOTE_VOICE = '0C';
  22. //远程设置参数
  23. const CMD_REMOTE_CONFIG = '08';
  24. /**
  25. * 控制指令(服务器下发)
  26. */
  27. //保留
  28. // const CONTROL_DEFAULT = '00';
  29. //远程设防(业务关锁)
  30. const CONTROL_REMOTE_CLOSE_LOCK = '01';
  31. // 远程撤防
  32. const CONTROL_REMOTE_CHE_FANG = '02';
  33. // 远程重启
  34. const CONTROL_REMOTE_CHONG_QI = '03';
  35. // 远程关机
  36. const CONTROL_REMOTE_GUAN_JI = '04';
  37. // 恢复出差设置
  38. const CONTROL_HUI_FU_CHU_CHANG = '05';
  39. // 保留
  40. // const CONTROL_REMOTE_NO = '06';
  41. // 保留
  42. // const CONTROL_REMOTE_NO = '07';
  43. // 立即定位
  44. const CONTROL_REMOTE_LOCATION = '08';
  45. //远程寻车(带寻车提示音)
  46. const CONTROL_REMOTE_FIND_BIKE = '09';
  47. // 远程重启蓝牙模块
  48. const CONTROL_REMOTE_REBOOT_LAN_YA = '0A';
  49. //远程开锁(业务开锁)
  50. const CONTROL_REMOTE_UNLOCK = '0B';
  51. //远程加锁
  52. const CONTROL_REMOTE_SHUT_UNLOCK = '0C';
  53. // 主动查询终端电池信息
  54. const CONTROL_GET_BATTERY_INFO = '0D';
  55. //主动查询控制器信息
  56. const CONTROL_GET_BOX_INFO = '0E';
  57. //远程打开电池锁
  58. const CONTROL_REMOTE_OPEN_BATTERY_LOCK = '0F';
  59. //远程关闭电池锁
  60. const CONTROL_REMOTE_CLOSE_BATTERY_LOCK = '10';
  61. //关闭远程寻车(待寻车提示音)
  62. const CONTROL_REMOTE_CLOSE_BELL = '11';
  63. //远程重启整个系统
  64. const CONTROL_REMOTE_REBOOT_SYSTEM = '12';
  65. //远程格式化主控制器
  66. const CONTROL_REMOTE_FORMAT_BOX = '13';
  67. //外部USB供电使能
  68. const CONTROL_REMOTE_USB_OK = '14';
  69. //外部USB供电失能
  70. const CONTROL_REMOTE_USB_NO = '15';
  71. //远程打开轮毂锁
  72. const CONTROL_REMOTE_OPEN_HUB_LOCK = '16';
  73. //远程关闭轮毂锁
  74. const CONTROL_REMOTE_CLOSE_HUB_LOCK = '17';
  75. // const CONTROL_REMOTE_CLOSE_LOCK = '18';
  76. //提示终端超出运营区域进行语音播报
  77. const CONTROL_OUT_AREA_PLAY_VOICE = '19';
  78. //提示终端进入运营区域关闭语音播放
  79. const CONTROL_OUT_AREA_CLOSE_VOICE = '1A';
  80. //出电子围栏直接失能ACC(仅限制在借车状态使用)
  81. const CONTROL_OUT_AREA_LOST_ELECTRIC = '1B';
  82. //入电子围栏直接使能ACC(仅限制在借车状态使用)
  83. const CONTROL_OUT_AREA_OPEN_ELECTRIC = '1C';
  84. // 打开后座
  85. const CONTROL_REMOTE_OPEN_BACKSEAT = '20';
  86. //关闭后座锁
  87. const CONTROL_REMOTE_CLOSE_BACKSEAT = '21';
  88. //远程临时关锁
  89. const CONTROL_REMOTE_TEMPORARY_CLOSE_LOCK = '30';
  90. //远程临时开锁
  91. const CONTROL_REMOTE_TEMPORARY_UNLOCK = '31';
  92. //进入运输模式
  93. const CONTROL_TRANSPORTATION_MODE = '32';
  94. //立即上传融合定位包
  95. const CONTROL_NOW_UP_LOCATION = '33';
  96. /**
  97. * 语言指令(警告指令)
  98. */
  99. // 警告命令
  100. const WARNING_SPARE_BATTERY_LOW = '00';
  101. const WARNING_SHOCK = '01';
  102. const WARNING_FORCE_OPEN_ELECTRIC = '02';
  103. const WARNING_OUT_AREA_PLAY_VIDEO = '03';
  104. const WARNING_BMS = '04';
  105. const WARNING_STOP_ELECTRIC = '05';
  106. const WARNING_OVER_SPEED = '06';
  107. const WARNING_WHEEL_MOVEMENT = '07';
  108. const WARNING_BIKE_TROUBLE = '08';
  109. /**
  110. * 语音指令
  111. * @var string
  112. */
  113. // 欢迎使用共享电单车
  114. const VIDEO_WELCOME = '01';
  115. // 没电了,请换一辆车
  116. const VIDEO_NO_ELECTRIC = '02';
  117. // 骑出运营区,请尽快返回
  118. const VIDEO_OUT_AREA = '03';
  119. // 报警音,车辆未解锁,请扫描骑行
  120. const VIDEO_WARNING = '04';
  121. // HI,主人,我在这辆车。
  122. const VIDEO_BIKE_HERE = '05';
  123. // 还车成功,欢迎再次使用共享电单车
  124. const VIDEO_CLOSE_BIKE = '06';
  125. // 寻车提示音
  126. const VIDEO_FIND_BIKE = '07';
  127. // 临时锁车
  128. const VIDEO_T_CLOSE_BIKE = '08';
  129. // 当前不在停车点,请在手机查看停车点
  130. const VIDEO_NO_STOP_SITE = '09';
  131. // 电池锁已打开
  132. const VIDEO_BATTERY_OPEN = '0A';
  133. // 电池锁已关闭
  134. const VIDEO_BATTERY_CLOSE = '0B';
  135. // 距离服务区过远,车辆即将断电。
  136. const VIDEO_OUT_AREA_CLOSE_ELECTRIC = '0C';
  137. // 已超速,请注意减速慢行
  138. const VIDEO_OUT_SPEED = '0D';
  139. // 车铃音
  140. const VIDEO_BIKE_DELL = '0E';
  141. public static $registerAddress = 'node1:11001';
  142. const CACHE_KEY = 'cache:select:';
  143. /**
  144. * 开车
  145. * @param $box_no
  146. * @return bool
  147. * User: Mead
  148. */
  149. public static function openLock($box_no)
  150. {
  151. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_UNLOCK);
  152. $str = self::makeSendMsg(self::CONTROL_REMOTE_UNLOCK, $msg_id);
  153. return self::send($box_no, $str);
  154. }
  155. /**
  156. * 关锁
  157. * @param $box_no
  158. * @return bool
  159. * User: Mead
  160. */
  161. public static function closeLock($box_no)
  162. {
  163. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_CLOSE_LOCK);
  164. $str = self::makeSendMsg(self::CONTROL_REMOTE_CLOSE_LOCK, $msg_id);
  165. return self::send($box_no, $str);
  166. }
  167. /**
  168. * 临时关锁
  169. * @param $box_no
  170. * @return bool
  171. * User: Mead
  172. */
  173. public static function temporaryCloseLock($box_no)
  174. {
  175. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_CLOSE_LOCK);
  176. $str = self::makeSendMsg(self::CONTROL_REMOTE_CLOSE_LOCK, $msg_id);
  177. return self::send($box_no, $str);
  178. }
  179. /**
  180. * 临时开锁
  181. * @param $box_no
  182. * @return bool
  183. * User: Mead
  184. */
  185. public static function temporaryOpnLock($box_no)
  186. {
  187. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_UNLOCK);
  188. $str = self::makeSendMsg(self::CONTROL_REMOTE_UNLOCK, $msg_id);
  189. return self::send($box_no, $str);
  190. }
  191. /**
  192. * 寻车响铃
  193. * @param $box_no
  194. * @return bool
  195. * User: Mead
  196. */
  197. public static function bellBike($box_no)
  198. {
  199. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_FIND_BIKE);
  200. $str = self::makeSendMsg(self::CONTROL_REMOTE_FIND_BIKE, $msg_id);
  201. return self::send($box_no, $str);
  202. }
  203. /**
  204. * 打开电池锁
  205. * @param $box_no
  206. * @return bool
  207. * User: Mead
  208. */
  209. public static function openBatteryLock($box_no)
  210. {
  211. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_OPEN_BATTERY_LOCK);
  212. Redis::connection()->setex("cache:open_battery:{$box_no}", 300, 1);
  213. $str = self::makeSendMsg(self::CONTROL_REMOTE_OPEN_BATTERY_LOCK, $msg_id);
  214. return self::send($box_no, $str);
  215. }
  216. /**
  217. * 关闭电池锁
  218. * @param $box_no
  219. * @return bool
  220. * User: Mead
  221. */
  222. public static function closeBatteryLock($box_no)
  223. {
  224. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_CLOSE_BATTERY_LOCK);
  225. $str = self::makeSendMsg(self::CONTROL_REMOTE_CLOSE_BATTERY_LOCK, $msg_id);
  226. return self::send($box_no, $str);
  227. }
  228. /**
  229. * 远程重启中控
  230. * @param $box_no
  231. * @return bool
  232. * User: Mead
  233. */
  234. public static function rebootBox($box_no)
  235. {
  236. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_REBOOT_SYSTEM);
  237. $str = self::makeSendMsg(self::CONTROL_REMOTE_REBOOT_SYSTEM, $msg_id);
  238. return self::send($box_no, $str);
  239. }
  240. /**
  241. * 立即定位
  242. * @param $box_no
  243. * @return bool
  244. * User: Mead
  245. */
  246. public static function nowBikeLocation($box_no)
  247. {
  248. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_LOCATION);
  249. //删除ridis位置缓存
  250. self::delRedisCache($box_no, 'update_bike_location');
  251. $str = self::makeSendMsg(self::CONTROL_REMOTE_LOCATION, $msg_id);
  252. return self::send($box_no, $str);
  253. }
  254. /**
  255. * 立即上传电池信息
  256. * @param $box_no
  257. * @return bool
  258. * User: Mead
  259. */
  260. public static function nowBikeBatteryMSG($box_no)
  261. {
  262. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_GET_BATTERY_INFO);
  263. //删除ridis位置缓存
  264. self::delRedisCache($box_no, ['heart', 'update_bike_location', 'update_battery_box_no']);
  265. $str = self::makeSendMsg(self::CONTROL_GET_BATTERY_INFO, $msg_id);
  266. return self::send($box_no, $str);
  267. }
  268. /**
  269. * 超出骑行区域播放音乐
  270. * @param $box_no
  271. * @return bool
  272. * User: Mead
  273. */
  274. public static function outAreaPlayVideo($box_no)
  275. {
  276. return self::playVideo($box_no, self::VIDEO_OUT_AREA);
  277. }
  278. /**
  279. * 播放语音
  280. * @param $box_no
  281. * @param $video_cmd
  282. * @return bool
  283. * User: Mead
  284. */
  285. public static function playVideo($box_no, $video_cmd)
  286. {
  287. $msg_id = self::makeMsgId($box_no, 'A', $video_cmd);
  288. $str = self::makeSendMsg($video_cmd, $msg_id, self::CMD_REMOTE_VOICE);
  289. return self::send($box_no, $str);
  290. }
  291. /**
  292. * 超出骑行区域失能
  293. * @param $box_no
  294. * @return bool
  295. * User: Mead
  296. */
  297. public static function outAreaLoseElectric($box_no)
  298. {
  299. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_OUT_AREA_LOST_ELECTRIC);
  300. $str = self::makeSendMsg(self::CONTROL_OUT_AREA_LOST_ELECTRIC, $msg_id, self::CMD_REMOTE_CONTROL);
  301. return self::send($box_no, $str);
  302. }
  303. /**
  304. * 超出区域后返回骑行区域加电
  305. * @param $box_no
  306. * @return bool
  307. * User: Mead
  308. */
  309. public static function outAreaGetElectric($box_no)
  310. {
  311. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_OUT_AREA_OPEN_ELECTRIC);
  312. $str = self::makeSendMsg(self::CONTROL_OUT_AREA_OPEN_ELECTRIC, $msg_id, self::CMD_REMOTE_CONTROL);
  313. return self::send($box_no, $str);
  314. }
  315. /**
  316. * 查询车的配置
  317. * @param $box_no
  318. * @return bool
  319. * User: Mead
  320. */
  321. public static function downBoxSetting($box_no)
  322. {
  323. $msg_id = self::makeMsgId($box_no, 'A', self::CMD_REMOTE_SELECT);
  324. echo $msg_id . PHP_EOL;
  325. $setting = ['PULSE', 'SERVER', 'MAXECUSPEED'];
  326. $str = self::makeSendMsg($setting, $msg_id, self::CMD_REMOTE_SELECT, false);
  327. self::send($box_no, $str);
  328. return self::getBikeResponse($msg_id);
  329. }
  330. /**
  331. * 参数配置
  332. * @param $box_no
  333. * @return bool
  334. * User: Mead
  335. */
  336. public static function setBoxSetting($box_no, $setting = [])
  337. {
  338. $msg_id = self::makeMsgId($box_no, 'A', self::CMD_REMOTE_CONFIG);
  339. $str = self::makeSendMsg($setting, $msg_id, self::CMD_REMOTE_CONFIG, false);
  340. Gateway::$registerAddress = self::$registerAddress;
  341. if (!Gateway::isUidOnline($box_no)) return false;
  342. self::send($box_no, $str);
  343. return self::getBikeResponse($msg_id, false);
  344. }
  345. /**
  346. * 发送数据
  347. * @param $box_no
  348. * @param $msg
  349. * @return bool
  350. * User: Mead
  351. */
  352. private static function send($box_no, $msg)
  353. {
  354. Gateway::$registerAddress = self::$registerAddress;
  355. Gateway::sendToUid($box_no, hex2bin($msg));
  356. return true;
  357. }
  358. /**
  359. * 生成 msg_id
  360. * @param $box_no
  361. * @param $type
  362. * @param $cmd
  363. * @return string
  364. * User: Mead
  365. */
  366. private static function makeMsgId($box_no, $type, $cmd)
  367. {
  368. $msg = [
  369. 'no' => $box_no,
  370. 'time' => time(),
  371. 'type' => $type,
  372. 'cmd' => $cmd,
  373. ];
  374. $msg_id = bin2hex(implode(',', $msg));
  375. return $msg_id;
  376. }
  377. /**
  378. * 组装命令
  379. * @param $cmd
  380. * @param $msgID
  381. * @return string
  382. * User: Mead
  383. */
  384. private static function makeSendMsg($controller_cmd, $msgID, $cmd = self::CMD_REMOTE_CONTROL, $is_hex = true)
  385. {
  386. if (!$is_hex) {
  387. $controller_cmd = bin2hex((implode(';', $controller_cmd) . ';FUJIA'));
  388. }
  389. $body = [
  390. "{$controller_cmd}",
  391. self::SPLIT_TAG . "{$msgID}"
  392. ];
  393. $body = self::arr2arr($body);
  394. return self::encode($body, $cmd);
  395. }
  396. /**
  397. * 效验编码
  398. * @param $data
  399. * @return string
  400. * User: Mead
  401. */
  402. private static function encode($data, $cmd)
  403. {
  404. $num = 12;
  405. $num += count($data);
  406. $data_length = self::byNumGetDataLength($num);
  407. $header = [
  408. $data_length,
  409. self::getSoftwareVersion(),
  410. $cmd,
  411. self::getPipelineNumber(),
  412. '00',
  413. '00',
  414. '00'
  415. ];
  416. $response = self::arr2str($header, $data);
  417. $response .= self::crc16(explode(' ', $response));
  418. return self::format(self::$start . $response);
  419. }
  420. /**
  421. * 获取数据包的长度
  422. * @param $num
  423. * @return array
  424. * User: Mead
  425. */
  426. private static function byNumGetDataLength($num)
  427. {
  428. $length = dechex($num);
  429. return str_pad($length, 4, '0', STR_PAD_LEFT);
  430. }
  431. /**
  432. * 删除缓存
  433. * @param $box_no
  434. * @param $type
  435. * User: Mead
  436. */
  437. public static function delRedisCache($box_no, $types)
  438. {
  439. $cacheNames = [];
  440. if (is_array($types)) {
  441. foreach ($types as $type) {
  442. $cacheNames[] = "cache:min:{$type}:{$box_no}";
  443. }
  444. } else {
  445. $cacheNames[] = "cache:min:{$types}:{$box_no}";
  446. }
  447. if (!count($cacheNames)) return false;
  448. Redis::connection()->del($cacheNames);
  449. }
  450. /**
  451. * 软件版本号
  452. * @return string
  453. * User: Mead
  454. */
  455. private static function getSoftwareVersion()
  456. {
  457. return '04';
  458. }
  459. /**
  460. * 获取流水号
  461. * @return int
  462. * User: Mead
  463. */
  464. private static function getPipelineNumber()
  465. {
  466. return '01';
  467. }
  468. /**
  469. * 格式数组
  470. * @param $arr
  471. * @return array
  472. * User: Mead
  473. */
  474. private static function arr2arr($arr)
  475. {
  476. $arr = implode('', $arr);
  477. return str_split(str_replace(' ', '', $arr), 2);
  478. }
  479. /**
  480. * 数组转字符
  481. * @return string
  482. * User: Mead
  483. */
  484. private static function arr2str()
  485. {
  486. $args = func_get_args();
  487. $arr = '';
  488. foreach ($args as $arg) {
  489. $arr .= implode('', $arg);
  490. }
  491. return implode(' ', str_split($arr, 2));
  492. }
  493. /**
  494. * 格式字符
  495. * @param $str
  496. * @return string
  497. * User: Mead
  498. */
  499. private static function format($str)
  500. {
  501. return strtoupper(implode('', str_split(str_replace(' ', '', $str), 2)));
  502. }
  503. private static $crc_table = [
  504. 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD,
  505. 0x6536, 0x74BF, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3,
  506. 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108,
  507. 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
  508. 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64,
  509. 0xF9FF, 0xE876, 0x2102, 0x308B, 0x0210, 0x1399,
  510. 0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3,
  511. 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
  512. 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E,
  513. 0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50,
  514. 0xFBEF, 0xEA66, 0xD8FD, 0xC974, 0x4204, 0x538D,
  515. 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
  516. 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1,
  517. 0xAB7A, 0xBAF3, 0x5285, 0x430C, 0x7197, 0x601E,
  518. 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44,
  519. 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
  520. 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB,
  521. 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5,
  522. 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, 0x7387, 0x620E,
  523. 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
  524. 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862,
  525. 0x9AF9, 0x8B70, 0x8408, 0x9581, 0xA71A, 0xB693,
  526. 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, 0x0840, 0x19C9,
  527. 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
  528. 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324,
  529. 0xF1BF, 0xE036, 0x18C1, 0x0948, 0x3BD3, 0x2A5A,
  530. 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, 0xA50A, 0xB483,
  531. 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
  532. 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF,
  533. 0x4C74, 0x5DFD, 0xB58B, 0xA402, 0x9699, 0x8710,
  534. 0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A,
  535. 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
  536. 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1,
  537. 0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF,
  538. 0x0C60, 0x1DE9, 0x2F72, 0x3EFB, 0xD68D, 0xC704,
  539. 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
  540. 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68,
  541. 0x3FF3, 0x2E7A, 0xE70E, 0xF687, 0xC41C, 0xD595,
  542. 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF,
  543. 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
  544. 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022,
  545. 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C,
  546. 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78,
  547. ];
  548. /**
  549. * 生成校检码
  550. * @param $data
  551. * @return string
  552. * User: Mead
  553. */
  554. private static function crc16($data)
  555. {
  556. $crc = 0xFFFF;
  557. foreach ($data as $d) {
  558. $d = hexdec($d);
  559. $crc = self::$crc_table[($d ^ $crc) & 0xFF] ^ ($crc >> 8 & 0xFF);
  560. }
  561. $crc = $crc ^ 0xFFFF;
  562. $crc = $crc & 0xFFFF;
  563. return str_pad(dechex($crc), 4, '0', STR_PAD_LEFT);
  564. }
  565. /**
  566. * 获取下发命令的返回结果
  567. * @param $msg_id
  568. * @param bool $decode
  569. * @return bool|mixed|string
  570. */
  571. public static function getBikeResponse($msg_id, $decode = true)
  572. {
  573. $redis = Redis::connection();
  574. $response = false;
  575. for ($i = 1; $i <= 30; $i++) {
  576. sleep(1);
  577. $data = $redis->get(self::CACHE_KEY . $msg_id);
  578. if ($data) {
  579. $response = self::decodeData($data, $decode);
  580. break;
  581. }
  582. }
  583. return $response;
  584. }
  585. /**
  586. * 解析车辆返回数据
  587. * @param $data
  588. * @return bool|mixed
  589. */
  590. public static function decodeData($data, $decode = true)
  591. {
  592. $array = explode('||', $data);
  593. if (count($array) !== 2) return false;
  594. list($type, $value) = $array;
  595. if (!$decode) return $value;
  596. switch ($type) {
  597. case 'string':
  598. return $value;
  599. break;
  600. case 'array':
  601. return json_decode($value);
  602. }
  603. return false;
  604. }
  605. }