BikeControl.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. const CACHE_KEY = 'cache:select:';
  142. //查询参数
  143. const SETTING_VIN = "VIN";
  144. const SETTING_ACCFILT = "ACCFILT";
  145. const SETTING_FDZT = "FDZT";
  146. const SETTING_OILSTATE = "OILSTATE";
  147. const SETTING_PHASESTATUS = "PHASESTATUS";
  148. public static $registerAddress = 'node1:1238';
  149. /**
  150. * 开车
  151. * @param $box_no
  152. * @return bool
  153. * User: Mead
  154. */
  155. public static function openLock($box_no)
  156. {
  157. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_UNLOCK);
  158. $str = self::makeSendMsg(self::CONTROL_REMOTE_UNLOCK, $msg_id);
  159. return self::send($box_no, $str);
  160. }
  161. /**
  162. * 关锁
  163. * @param $box_no
  164. * @return bool
  165. * User: Mead
  166. */
  167. public static function closeLock($box_no)
  168. {
  169. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_CLOSE_LOCK);
  170. $str = self::makeSendMsg(self::CONTROL_REMOTE_CLOSE_LOCK, $msg_id);
  171. return self::send($box_no, $str);
  172. }
  173. /**
  174. * 临时关锁
  175. * @param $box_no
  176. * @return bool
  177. * User: Mead
  178. */
  179. public static function temporaryCloseLock($box_no)
  180. {
  181. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_CLOSE_LOCK);
  182. $str = self::makeSendMsg(self::CONTROL_REMOTE_CLOSE_LOCK, $msg_id);
  183. return self::send($box_no, $str);
  184. }
  185. /**
  186. * 临时开锁
  187. * @param $box_no
  188. * @return bool
  189. * User: Mead
  190. */
  191. public static function temporaryOpnLock($box_no)
  192. {
  193. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_UNLOCK);
  194. $str = self::makeSendMsg(self::CONTROL_REMOTE_UNLOCK, $msg_id);
  195. return self::send($box_no, $str);
  196. }
  197. /**
  198. * 寻车响铃
  199. * @param $box_no
  200. * @return bool
  201. * User: Mead
  202. */
  203. public static function bellBike($box_no)
  204. {
  205. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_FIND_BIKE);
  206. $str = self::makeSendMsg(self::CONTROL_REMOTE_FIND_BIKE, $msg_id);
  207. return self::send($box_no, $str);
  208. }
  209. /**
  210. * 打开电池锁
  211. * @param $box_no
  212. * @return bool
  213. * User: Mead
  214. */
  215. public static function openBatteryLock($box_no)
  216. {
  217. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_OPEN_BATTERY_LOCK);
  218. Redis::connection()->setex("cache:open_battery:{$box_no}", 300, 1);
  219. $str = self::makeSendMsg(self::CONTROL_REMOTE_OPEN_BATTERY_LOCK, $msg_id);
  220. return self::send($box_no, $str);
  221. }
  222. /**
  223. * 关闭电池锁
  224. * @param $box_no
  225. * @return bool
  226. * User: Mead
  227. */
  228. public static function closeBatteryLock($box_no)
  229. {
  230. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_CLOSE_BATTERY_LOCK);
  231. $str = self::makeSendMsg(self::CONTROL_REMOTE_CLOSE_BATTERY_LOCK, $msg_id);
  232. return self::send($box_no, $str);
  233. }
  234. /**
  235. * 远程重启中控
  236. * @param $box_no
  237. * @return bool
  238. * User: Mead
  239. */
  240. public static function rebootBox($box_no)
  241. {
  242. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_REBOOT_SYSTEM);
  243. $str = self::makeSendMsg(self::CONTROL_REMOTE_REBOOT_SYSTEM, $msg_id);
  244. return self::send($box_no, $str);
  245. }
  246. /**
  247. * 立即定位
  248. * @param $box_no
  249. * @return bool
  250. * User: Mead
  251. */
  252. public static function nowBikeLocation($box_no)
  253. {
  254. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_REMOTE_LOCATION);
  255. //删除ridis位置缓存
  256. self::delRedisCache($box_no, 'update_bike_location');
  257. $str = self::makeSendMsg(self::CONTROL_REMOTE_LOCATION, $msg_id);
  258. return self::send($box_no, $str);
  259. }
  260. /**
  261. * 立即上传电池信息
  262. * @param $box_no
  263. * @return bool
  264. * User: Mead
  265. */
  266. public static function nowBikeBatteryMSG($box_no)
  267. {
  268. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_GET_BATTERY_INFO);
  269. //删除ridis位置缓存
  270. self::delRedisCache($box_no, ['heart', 'update_bike_location', 'update_battery_box_no']);
  271. $str = self::makeSendMsg(self::CONTROL_GET_BATTERY_INFO, $msg_id);
  272. return self::send($box_no, $str);
  273. }
  274. /**
  275. * 超出骑行区域播放音乐
  276. * @param $box_no
  277. * @return bool
  278. * User: Mead
  279. */
  280. public static function outAreaPlayVideo($box_no)
  281. {
  282. return self::playVideo($box_no, self::VIDEO_OUT_AREA);
  283. }
  284. /**
  285. * 播放语音
  286. * @param $box_no
  287. * @param $video_cmd
  288. * @return bool
  289. * User: Mead
  290. */
  291. public static function playVideo($box_no, $video_cmd)
  292. {
  293. $msg_id = self::makeMsgId($box_no, 'A', $video_cmd);
  294. $str = self::makeSendMsg($video_cmd, $msg_id, self::CMD_REMOTE_VOICE);
  295. return self::send($box_no, $str);
  296. }
  297. /**
  298. * 超出骑行区域失能
  299. * @param $box_no
  300. * @return bool
  301. * User: Mead
  302. */
  303. public static function outAreaLoseElectric($box_no)
  304. {
  305. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_OUT_AREA_LOST_ELECTRIC);
  306. $str = self::makeSendMsg(self::CONTROL_OUT_AREA_LOST_ELECTRIC, $msg_id, self::CMD_REMOTE_CONTROL);
  307. return self::send($box_no, $str);
  308. }
  309. /**
  310. * 超出区域后返回骑行区域加电
  311. * @param $box_no
  312. * @return bool
  313. * User: Mead
  314. */
  315. public static function outAreaGetElectric($box_no)
  316. {
  317. $msg_id = self::makeMsgId($box_no, 'A', self::CONTROL_OUT_AREA_OPEN_ELECTRIC);
  318. $str = self::makeSendMsg(self::CONTROL_OUT_AREA_OPEN_ELECTRIC, $msg_id, self::CMD_REMOTE_CONTROL);
  319. return self::send($box_no, $str);
  320. }
  321. /**
  322. * 查询车的配置
  323. * @param $box_no
  324. * @return bool
  325. * User: Mead
  326. */
  327. public static function downBoxSetting($box_no)
  328. {
  329. $msg_id = self::makeMsgId($box_no, 'A', self::CMD_REMOTE_SELECT);
  330. $setting = ['PULSE', 'SERVER', 'MAXECUSPEED', 'FREQ', 'SOFTVERSION', 'MAXSPEEDPERCENT'];
  331. $str = self::makeSendMsg($setting, $msg_id, self::CMD_REMOTE_SELECT, false);
  332. self::send($box_no, $str);
  333. return self::getBikeResponse($msg_id);
  334. }
  335. /**
  336. * 参数配置
  337. * @param $box_no
  338. * @return bool
  339. * User: Mead
  340. */
  341. public static function setBoxSetting($box_no, $setting = [], $is_result = false)
  342. {
  343. $msg_id = self::makeMsgId($box_no, 'A', self::CMD_REMOTE_CONFIG);
  344. $str = self::makeSendMsg($setting, $msg_id, self::CMD_REMOTE_CONFIG, false);
  345. Gateway::$registerAddress = self::$registerAddress;
  346. if (!Gateway::isUidOnline($box_no)) return false;
  347. if (!$is_result) {
  348. return self::send($box_no, $str);
  349. }
  350. self::send($box_no, $str);
  351. return self::getBikeResponse($msg_id, false);
  352. }
  353. /**
  354. * 查询车的状态
  355. * @param $box_no
  356. * @return bool
  357. * User: Mead
  358. */
  359. public static function getBoxStatus($box_no, $setting = [])
  360. {
  361. $msg_id = self::makeMsgId($box_no, 'A', self::CMD_REMOTE_SELECT);
  362. if (count($setting) === 0) return false;
  363. $str = self::makeSendMsg($setting, $msg_id, self::CMD_REMOTE_SELECT, false);
  364. self::send($box_no, $str);
  365. return self::getBikeResponse($msg_id);
  366. }
  367. /**
  368. * 发送数据
  369. * @param $box_no
  370. * @param $msg
  371. * @return bool
  372. * User: Mead
  373. */
  374. private static function send($box_no, $msg)
  375. {
  376. Gateway::$registerAddress = self::$registerAddress;
  377. Gateway::sendToUid($box_no, hex2bin($msg));
  378. return true;
  379. }
  380. /**
  381. * 生成 msg_id
  382. * @param $box_no
  383. * @param $type
  384. * @param $cmd
  385. * @return string
  386. * User: Mead
  387. */
  388. private static function makeMsgId($box_no, $type, $cmd)
  389. {
  390. $msg = [
  391. 'no' => $box_no,
  392. 'time' => time(),
  393. 'type' => $type,
  394. 'cmd' => $cmd,
  395. ];
  396. $msg_id = bin2hex(implode(',', $msg));
  397. return $msg_id;
  398. }
  399. /**
  400. * 组装命令
  401. * @param $cmd
  402. * @param $msgID
  403. * @return string
  404. * User: Mead
  405. */
  406. private static function makeSendMsg($controller_cmd, $msgID, $cmd = self::CMD_REMOTE_CONTROL, $is_hex = true)
  407. {
  408. if (!$is_hex) {
  409. $controller_cmd = bin2hex((implode(';', $controller_cmd) . ';FUJIA'));
  410. }
  411. $body = [
  412. "{$controller_cmd}",
  413. self::SPLIT_TAG . "{$msgID}"
  414. ];
  415. $body = self::arr2arr($body);
  416. return self::encode($body, $cmd);
  417. }
  418. /**
  419. * 效验编码
  420. * @param $data
  421. * @return string
  422. * User: Mead
  423. */
  424. private static function encode($data, $cmd)
  425. {
  426. $num = 12;
  427. $num += count($data);
  428. $data_length = self::byNumGetDataLength($num);
  429. $header = [
  430. $data_length,
  431. self::getSoftwareVersion(),
  432. $cmd,
  433. self::getPipelineNumber(),
  434. '00',
  435. '00',
  436. '00'
  437. ];
  438. $response = self::arr2str($header, $data);
  439. $response .= self::crc16(explode(' ', $response));
  440. return self::format(self::$start . $response);
  441. }
  442. /**
  443. * 获取数据包的长度
  444. * @param $num
  445. * @return array
  446. * User: Mead
  447. */
  448. private static function byNumGetDataLength($num)
  449. {
  450. $length = dechex($num);
  451. return str_pad($length, 4, '0', STR_PAD_LEFT);
  452. }
  453. /**
  454. * 删除缓存
  455. * @param $box_no
  456. * @param $type
  457. * User: Mead
  458. */
  459. public static function delRedisCache($box_no, $types)
  460. {
  461. $cacheNames = [];
  462. if (is_array($types)) {
  463. foreach ($types as $type) {
  464. $cacheNames[] = "cache:min:{$type}:{$box_no}";
  465. }
  466. } else {
  467. $cacheNames[] = "cache:min:{$types}:{$box_no}";
  468. }
  469. if (!count($cacheNames)) return false;
  470. Redis::connection()->del($cacheNames);
  471. }
  472. /**
  473. * 软件版本号
  474. * @return string
  475. * User: Mead
  476. */
  477. private static function getSoftwareVersion()
  478. {
  479. return '04';
  480. }
  481. /**
  482. * 获取流水号
  483. * @return int
  484. * User: Mead
  485. */
  486. private static function getPipelineNumber()
  487. {
  488. return '01';
  489. }
  490. /**
  491. * 格式数组
  492. * @param $arr
  493. * @return array
  494. * User: Mead
  495. */
  496. private static function arr2arr($arr)
  497. {
  498. $arr = implode('', $arr);
  499. return str_split(str_replace(' ', '', $arr), 2);
  500. }
  501. /**
  502. * 数组转字符
  503. * @return string
  504. * User: Mead
  505. */
  506. private static function arr2str()
  507. {
  508. $args = func_get_args();
  509. $arr = '';
  510. foreach ($args as $arg) {
  511. $arr .= implode('', $arg);
  512. }
  513. return implode(' ', str_split($arr, 2));
  514. }
  515. /**
  516. * 格式字符
  517. * @param $str
  518. * @return string
  519. * User: Mead
  520. */
  521. private static function format($str)
  522. {
  523. return strtoupper(implode('', str_split(str_replace(' ', '', $str), 2)));
  524. }
  525. private static $crc_table = [
  526. 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD,
  527. 0x6536, 0x74BF, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3,
  528. 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108,
  529. 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
  530. 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64,
  531. 0xF9FF, 0xE876, 0x2102, 0x308B, 0x0210, 0x1399,
  532. 0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3,
  533. 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
  534. 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E,
  535. 0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50,
  536. 0xFBEF, 0xEA66, 0xD8FD, 0xC974, 0x4204, 0x538D,
  537. 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
  538. 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1,
  539. 0xAB7A, 0xBAF3, 0x5285, 0x430C, 0x7197, 0x601E,
  540. 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44,
  541. 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
  542. 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB,
  543. 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5,
  544. 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, 0x7387, 0x620E,
  545. 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
  546. 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862,
  547. 0x9AF9, 0x8B70, 0x8408, 0x9581, 0xA71A, 0xB693,
  548. 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, 0x0840, 0x19C9,
  549. 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
  550. 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324,
  551. 0xF1BF, 0xE036, 0x18C1, 0x0948, 0x3BD3, 0x2A5A,
  552. 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, 0xA50A, 0xB483,
  553. 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
  554. 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF,
  555. 0x4C74, 0x5DFD, 0xB58B, 0xA402, 0x9699, 0x8710,
  556. 0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A,
  557. 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
  558. 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1,
  559. 0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF,
  560. 0x0C60, 0x1DE9, 0x2F72, 0x3EFB, 0xD68D, 0xC704,
  561. 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
  562. 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68,
  563. 0x3FF3, 0x2E7A, 0xE70E, 0xF687, 0xC41C, 0xD595,
  564. 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF,
  565. 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
  566. 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022,
  567. 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C,
  568. 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78,
  569. ];
  570. /**
  571. * 生成校检码
  572. * @param $data
  573. * @return string
  574. * User: Mead
  575. */
  576. private static function crc16($data)
  577. {
  578. $crc = 0xFFFF;
  579. foreach ($data as $d) {
  580. $d = hexdec($d);
  581. $crc = self::$crc_table[($d ^ $crc) & 0xFF] ^ ($crc >> 8 & 0xFF);
  582. }
  583. $crc = $crc ^ 0xFFFF;
  584. $crc = $crc & 0xFFFF;
  585. return str_pad(dechex($crc), 4, '0', STR_PAD_LEFT);
  586. }
  587. /**
  588. * 获取下发命令的返回结果
  589. * @param $msg_id
  590. * @param bool $decode
  591. * @return bool|mixed|string
  592. */
  593. public static function getBikeResponse($msg_id, $decode = true)
  594. {
  595. $redis = Redis::connection();
  596. $response = false;
  597. for ($i = 1; $i <= 30; $i++) {
  598. sleep(1);
  599. $data = $redis->get(self::CACHE_KEY . $msg_id);
  600. if ($data) {
  601. $response = self::decodeData($data, $decode);
  602. break;
  603. }
  604. }
  605. return $response;
  606. }
  607. /**
  608. * 解析车辆返回数据
  609. * @param $data
  610. * @return bool|mixed
  611. */
  612. public static function decodeData($data, $decode = true)
  613. {
  614. $array = explode('||', $data);
  615. if (count($array) !== 2) return false;
  616. list($type, $value) = $array;
  617. if (!$decode) return $value;
  618. switch ($type) {
  619. case 'string':
  620. return $value;
  621. break;
  622. case 'array':
  623. return json_decode($value);
  624. }
  625. return false;
  626. }
  627. /**
  628. * 速度转速度百分比
  629. * @param $maxecuspeed
  630. * @return int
  631. * Author: Mead
  632. */
  633. public static function maxecuspeedToMaxspeedpercent($maxecuspeed = 7)
  634. {
  635. $MAXSPEEDPERCENT = 100;
  636. switch ($maxecuspeed) {
  637. case 1:
  638. $MAXSPEEDPERCENT = 70;
  639. break;
  640. case 2:
  641. $MAXSPEEDPERCENT = 75;
  642. break;
  643. case 3:
  644. $MAXSPEEDPERCENT = 80;
  645. break;
  646. case 4:
  647. $MAXSPEEDPERCENT = 85;
  648. break;
  649. case 5:
  650. $MAXSPEEDPERCENT = 90;
  651. break;
  652. case 6:
  653. $MAXSPEEDPERCENT = 95;
  654. break;
  655. case 7:
  656. $MAXSPEEDPERCENT = 100;
  657. break;
  658. }
  659. return $MAXSPEEDPERCENT;
  660. }
  661. }