BikeControl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/9/5
  6. * Time: 9:36 PM
  7. */
  8. namespace App\Servers;
  9. use App\Handlers\ExceptionTrait;
  10. use App\Maps\CmdMap;
  11. use App\Maps\WarningMap;
  12. use GatewayWorker\Lib\Gateway;
  13. class BikeControl
  14. {
  15. use ExceptionTrait;
  16. private static $start = 'AA AA';
  17. //分割符
  18. const SPLIT_TAG = 'bbbb';
  19. //保留
  20. // const CONTROL_DEFAULT = '00';
  21. //远程设防(业务关锁)
  22. const CONTROL_REMOTE_CLOSE_LOCK = '01';
  23. // 远程撤防
  24. const CONTROL_REMOTE_CHE_FANG = '02';
  25. // 远程重启
  26. const CONTROL_REMOTE_CHONG_QI = '03';
  27. // 远程关机
  28. const CONTROL_REMOTE_GUAN_JI = '04';
  29. // 恢复出差设置
  30. const CONTROL_HUI_FU_CHU_CHANG = '05';
  31. // 保留
  32. // const CONTROL_REMOTE_NO = '06';
  33. // 保留
  34. // const CONTROL_REMOTE_NO = '07';
  35. // 立即定位
  36. const CONTROL_REMOTE_LOCATION = '08';
  37. //远程寻车(带寻车提示音)
  38. const CONTROL_REMOTE_FIND_BIKE = '09';
  39. // 远程重启蓝牙模块
  40. const CONTROL_REMOTE_REBOOT_LAN_YA = '0A';
  41. //远程开锁(业务开锁)
  42. const CONTROL_REMOTE_UNLOCK = '0B';
  43. //远程加锁
  44. const CONTROL_REMOTE_SHUT_UNLOCK = '0C';
  45. // 主动查询终端电池信息
  46. const CONTROL_GET_BATTERY_INFO = '0D';
  47. //主动查询控制器信息
  48. const CONTROL_GET_BOX_INFO = '0E';
  49. //远程打开电池锁
  50. const CONTROL_REMOTE_OPEN_BATTERY_LOCK = '0F';
  51. //远程关闭电池锁
  52. const CONTROL_REMOTE_CLOSE_BATTERY_LOCK = '10';
  53. //关闭远程寻车(待寻车提示音)
  54. const CONTROL_REMOTE_CLOSE_BELL = '11';
  55. //远程重启整个系统
  56. const CONTROL_REMOTE_REBOOT_SYSTEM = '12';
  57. //远程格式化主控制器
  58. const CONTROL_REMOTE_FORMAT_BOX = '13';
  59. //外部USB供电使能
  60. const CONTROL_REMOTE_USB_OK = '14';
  61. //外部USB供电失能
  62. const CONTROL_REMOTE_USB_NO = '15';
  63. //远程打开轮毂锁
  64. const CONTROL_REMOTE_OPEN_HUB_LOCK = '16';
  65. //远程关闭轮毂锁
  66. const CONTROL_REMOTE_CLOSE_HUB_LOCK = '17';
  67. // const CONTROL_REMOTE_CLOSE_LOCK = '18';
  68. //提示终端超出运营区域进行语音播报
  69. const CONTROL_OUT_AREA_PLAY_VOICE = '19';
  70. //提示终端进入运营区域关闭语音播放
  71. const CONTROL_OUT_AREA_CLOSE_VOICE = '1A';
  72. //出电子围栏直接失能ACC(仅限制在借车状态使用)
  73. const CONTROL_OUT_AREA_LOST_ELECTRIC = '1B';
  74. //入电子围栏直接使能ACC(仅限制在借车状态使用)
  75. const CONTROL_OUT_AREA_OPEN_ELECTRIC = '1C';
  76. // 打开后座
  77. const CONTROL_REMOTE_OPEN_BACKSEAT = '20';
  78. //关闭后座锁
  79. const CONTROL_REMOTE_CLOSE_BACKSEAT = '21';
  80. //远程临时关锁
  81. const CONTROL_REMOTE_TEMPORARY_CLOSE_LOCK = '30';
  82. //远程临时开锁
  83. const CONTROL_REMOTE_TEMPORARY_UNLOCK = '31';
  84. //进入运输模式
  85. const CONTROL_TRANSPORTATION_MODE = '32';
  86. //立即上传融合定位包
  87. const CONTROL_NOW_UP_LOCATION = '33';
  88. // 警告命令
  89. const WARNING_SPARE_BATTERY_LOW = '00';
  90. const WARNING_SHOCK = '01';
  91. const WARNING_FORCE_OPEN_ELECTRIC = '02';
  92. const WARNING_OUT_AREA_PLAY_VIDEO = '03';
  93. const WARNING_BMS = '04';
  94. const WARNING_STOP_ELECTRIC = '05';
  95. const WARNING_OVER_SPEED = '06';
  96. const WARNING_WHEEL_MOVEMENT = '07';
  97. const WARNING_BIKE_TROUBLE = '08';
  98. private static $registerAddress = 'node1:11001';
  99. /**
  100. * 开车
  101. * @param $box_no
  102. * @return bool
  103. * User: Mead
  104. */
  105. public static function openLock($box_no)
  106. {
  107. $msg_id = self::makeMsgId($box_no, 'S', BikeControl::CONTROL_REMOTE_CLOSE_LOCK);
  108. $str = self::makeSendMsg(BikeControl::CONTROL_REMOTE_UNLOCK, $msg_id);
  109. return self::send($box_no, $str);
  110. }
  111. /**
  112. * 关锁
  113. * @param $box_no
  114. * @return bool
  115. * User: Mead
  116. */
  117. public static function closeLock($box_no)
  118. {
  119. $msg_id = self::makeMsgId($box_no, 'S', BikeControl::CONTROL_REMOTE_CLOSE_LOCK);
  120. $str = self::makeSendMsg(BikeControl::CONTROL_REMOTE_CLOSE_LOCK, $msg_id);
  121. return self::send($box_no, $str);
  122. }
  123. /**
  124. * 临时关锁
  125. * @param $box_no
  126. * @return bool
  127. * User: Mead
  128. */
  129. public static function temporaryCloseLock($box_no)
  130. {
  131. $msg_id = self::makeMsgId($box_no, 'S', BikeControl::CONTROL_REMOTE_CLOSE_LOCK);
  132. $str = self::makeSendMsg(BikeControl::CONTROL_REMOTE_CLOSE_LOCK, $msg_id);
  133. return self::send($box_no, $str);
  134. }
  135. /**
  136. * 临时开锁
  137. * @param $box_no
  138. * @return bool
  139. * User: Mead
  140. */
  141. public static function temporaryOpnLock($box_no)
  142. {
  143. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_OUT_AREA_LOST_ELECTRIC);
  144. $str = self::makeSendMsg(BikeControl::CONTROL_REMOTE_UNLOCK, $msg_id);
  145. return self::send($box_no, $str);
  146. }
  147. /**
  148. * 寻车响铃
  149. * @param $box_no
  150. * @return bool
  151. * User: Mead
  152. */
  153. public static function bellBike($box_no)
  154. {
  155. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_OUT_AREA_LOST_ELECTRIC);
  156. $str = self::makeSendMsg(BikeControl::CONTROL_REMOTE_FIND_BIKE, $msg_id);
  157. return self::send($box_no, $str);
  158. }
  159. /**
  160. * 超出骑行区域播放音乐
  161. * @param $box_no
  162. * @return bool
  163. * User: Mead
  164. */
  165. public static function outAreaPlayVideo($box_no)
  166. {
  167. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_OUT_AREA_LOST_ELECTRIC);
  168. $str = self::makeSendMsg(WarningMap::WARNING_OUT_AREA_PLAY_VIDEO, $msg_id, CmdMap::CMD_REMOTE_VOICE);
  169. return self::send($box_no, $str);
  170. }
  171. /**
  172. * 超出骑行区域失能
  173. * @param $box_no
  174. * @return bool
  175. * User: Mead
  176. */
  177. public static function outAreaLoseElectric($box_no)
  178. {
  179. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_OUT_AREA_LOST_ELECTRIC);
  180. $str = self::makeSendMsg(self::CONTROL_OUT_AREA_LOST_ELECTRIC, $msg_id, CmdMap::CMD_REMOTE_CONTROL);
  181. return self::send($box_no, $str);
  182. }
  183. /**
  184. * 超出区域后返回骑行区域加电
  185. * @param $box_no
  186. * @return bool
  187. * User: Mead
  188. */
  189. public static function outAreaGetElectric($box_no)
  190. {
  191. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_OUT_AREA_OPEN_ELECTRIC);
  192. $str = self::makeSendMsg(self::CONTROL_OUT_AREA_OPEN_ELECTRIC, $msg_id, CmdMap::CMD_REMOTE_CONTROL);
  193. return self::send($box_no, $str);
  194. }
  195. /**
  196. * 远程重启中控
  197. * @param $box_no
  198. * @return bool
  199. * User: Mead
  200. */
  201. public static function rebootBox($box_no)
  202. {
  203. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_REMOTE_REBOOT_SYSTEM);
  204. $str = self::makeSendMsg(self::CONTROL_REMOTE_REBOOT_SYSTEM, $msg_id);
  205. return self::send($box_no, $str);
  206. }
  207. /**
  208. * 立即定位
  209. * @param $box_no
  210. * @return bool
  211. * User: Mead
  212. */
  213. public static function nowBikeLocation($box_no)
  214. {
  215. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_REMOTE_LOCATION);
  216. $str = self::makeSendMsg(self::CONTROL_REMOTE_LOCATION, $msg_id);
  217. return self::send($box_no, $str);
  218. }
  219. /**
  220. * 立即上传电池信息
  221. * @param $box_no
  222. * @return bool
  223. * User: Mead
  224. */
  225. public static function nowBikeBatteryMSG($box_no)
  226. {
  227. $msg_id = self::makeMsgId($box_no, 'S', self::CONTROL_GET_BATTERY_INFO);
  228. $str = self::makeSendMsg(self::CONTROL_GET_BATTERY_INFO, $msg_id);
  229. return self::send($box_no, $str);
  230. }
  231. /**
  232. * 生成 msg_id
  233. * @param $box_no
  234. * @param $type
  235. * @param $cmd
  236. * @return string
  237. * User: Mead
  238. */
  239. private static function makeMsgId($box_no, $type, $cmd)
  240. {
  241. $msg = [
  242. 'no' => "{$box_no}",
  243. 'time' => time(),
  244. 'type' => $type,
  245. 'cmd' => $cmd,
  246. ];
  247. $msg_id = bin2hex(implode(',', $msg));
  248. return $msg_id;
  249. }
  250. /**
  251. * 发送数据
  252. * @param $box_no
  253. * @param $msg
  254. * @return bool
  255. * User: Mead
  256. */
  257. private static function send($box_no, $msg)
  258. {
  259. self::log($msg, 'sendMsg');
  260. Gateway::sendToUid($box_no, hex2bin($msg));
  261. return true;
  262. }
  263. /**
  264. * 组装命令
  265. * @param $cmd
  266. * @param $msgID
  267. * @return string
  268. * User: Mead
  269. */
  270. public static function makeSendMsg($controller_cmd, $msgID = false, $cmd = CmdMap::CMD_REMOTE_CONTROL, $is_hex = true)
  271. {
  272. if (!$is_hex) {
  273. $controller_cmd = bin2hex((implode(';', $controller_cmd) . ';FUJIA'));
  274. }
  275. $body = [
  276. "{$controller_cmd}",
  277. self::SPLIT_TAG . "{$msgID}"
  278. ];
  279. $body = self::arr2arr($body);
  280. return self::encode($body, $cmd);
  281. }
  282. /**
  283. * 效验编码
  284. * @param $data
  285. * @return string
  286. * User: Mead
  287. */
  288. private static function encode($data, $cmd)
  289. {
  290. $num = 12;
  291. $num += count($data);
  292. $data_length = self::byNumGetDataLength($num);
  293. $header = [
  294. $data_length,
  295. self::getSoftwareVersion(),
  296. $cmd,
  297. self::getPipelineNumber(),
  298. '00',
  299. '00',
  300. '00'
  301. ];
  302. $response = self::arr2str($header, $data);
  303. $response .= self::crc16(explode(' ', $response));
  304. return self::format(self::$start . $response);
  305. }
  306. /**
  307. * 获取数据包的长度
  308. * @param $num
  309. * @return array
  310. * User: Mead
  311. */
  312. private static function byNumGetDataLength($num)
  313. {
  314. $length = dechex($num);
  315. return str_pad($length, 4, '0', STR_PAD_LEFT);
  316. }
  317. /**
  318. * 软件版本号
  319. * @return string
  320. * User: Mead
  321. */
  322. private static function getSoftwareVersion()
  323. {
  324. return '04';
  325. }
  326. /**
  327. * 获取流水号
  328. * @return int
  329. * User: Mead
  330. */
  331. private static function getPipelineNumber()
  332. {
  333. return '01';
  334. }
  335. /**
  336. * 格式数组
  337. * @param $arr
  338. * @return array
  339. * User: Mead
  340. */
  341. private static function arr2arr($arr)
  342. {
  343. $arr = implode('', $arr);
  344. return str_split(str_replace(' ', '', $arr), 2);
  345. }
  346. /**
  347. * 数组转字符
  348. * @return string
  349. * User: Mead
  350. */
  351. private static function arr2str()
  352. {
  353. $args = func_get_args();
  354. $arr = '';
  355. foreach ($args as $arg) {
  356. $arr .= implode('', $arg);
  357. }
  358. return implode(' ', str_split($arr, 2));
  359. }
  360. /**
  361. * 格式字符
  362. * @param $str
  363. * @return string
  364. * User: Mead
  365. */
  366. private static function format($str)
  367. {
  368. return strtoupper(implode('', str_split(str_replace(' ', '', $str), 2)));
  369. }
  370. private static $crc_table = [
  371. 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD,
  372. 0x6536, 0x74BF, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3,
  373. 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108,
  374. 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
  375. 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64,
  376. 0xF9FF, 0xE876, 0x2102, 0x308B, 0x0210, 0x1399,
  377. 0x6726, 0x76AF, 0x4434, 0x55BD, 0xAD4A, 0xBCC3,
  378. 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
  379. 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E,
  380. 0x54B5, 0x453C, 0xBDCB, 0xAC42, 0x9ED9, 0x8F50,
  381. 0xFBEF, 0xEA66, 0xD8FD, 0xC974, 0x4204, 0x538D,
  382. 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
  383. 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1,
  384. 0xAB7A, 0xBAF3, 0x5285, 0x430C, 0x7197, 0x601E,
  385. 0x14A1, 0x0528, 0x37B3, 0x263A, 0xDECD, 0xCF44,
  386. 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
  387. 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB,
  388. 0x0630, 0x17B9, 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5,
  389. 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, 0x7387, 0x620E,
  390. 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
  391. 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862,
  392. 0x9AF9, 0x8B70, 0x8408, 0x9581, 0xA71A, 0xB693,
  393. 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, 0x0840, 0x19C9,
  394. 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
  395. 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324,
  396. 0xF1BF, 0xE036, 0x18C1, 0x0948, 0x3BD3, 0x2A5A,
  397. 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, 0xA50A, 0xB483,
  398. 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
  399. 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF,
  400. 0x4C74, 0x5DFD, 0xB58B, 0xA402, 0x9699, 0x8710,
  401. 0xF3AF, 0xE226, 0xD0BD, 0xC134, 0x39C3, 0x284A,
  402. 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
  403. 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1,
  404. 0xA33A, 0xB2B3, 0x4A44, 0x5BCD, 0x6956, 0x78DF,
  405. 0x0C60, 0x1DE9, 0x2F72, 0x3EFB, 0xD68D, 0xC704,
  406. 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
  407. 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68,
  408. 0x3FF3, 0x2E7A, 0xE70E, 0xF687, 0xC41C, 0xD595,
  409. 0xA12A, 0xB0A3, 0x8238, 0x93B1, 0x6B46, 0x7ACF,
  410. 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
  411. 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022,
  412. 0x92B9, 0x8330, 0x7BC7, 0x6A4E, 0x58D5, 0x495C,
  413. 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78,
  414. ];
  415. /**
  416. * 生成校检码
  417. * @param $data
  418. * @return string
  419. * User: Mead
  420. */
  421. private static function crc16($data)
  422. {
  423. $crc = 0xFFFF;
  424. foreach ($data as $d) {
  425. $d = hexdec($d);
  426. $crc = self::$crc_table[($d ^ $crc) & 0xFF] ^ ($crc >> 8 & 0xFF);
  427. }
  428. $crc = $crc ^ 0xFFFF;
  429. $crc = $crc & 0xFFFF;
  430. return str_pad(dechex($crc), 4, '0', STR_PAD_LEFT);
  431. }
  432. }