Events.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. *
  12. * @see http://www.workerman.net/
  13. *
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. /**
  17. * 用于检测业务代码死循环或者长时间阻塞等问题
  18. * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  19. * 然后观察一段时间workerman.log看是否有process_timeout异常.
  20. */
  21. //declare(ticks=1);
  22. use GatewayWorker\Lib\Gateway;
  23. /**
  24. * 主逻辑
  25. * 主要是处理 onConnect onMessage onClose 三个方法
  26. * onConnect 和 onClose 如果不需要可以不用实现并删除.
  27. */
  28. class Events
  29. {
  30. use \App\Handlers\ExceptionTrait, \App\Models\WarningLogTraitModel, \App\Models\BikeTraitModel, \App\Handlers\Crc16Handler;
  31. public static $db = null;
  32. public static $redis = null;
  33. public static $mongo = null;
  34. private static $db_config = [
  35. 'host' => '192.168.10.10',
  36. 'port' => 3306,
  37. 'user' => 'homestead',
  38. 'password' => 'secret',
  39. 'db_name' => 'weilaibike'
  40. ];
  41. private static $redis_config = [
  42. 'host' => '192.168.10.10',
  43. 'port' => 6379,
  44. 'database' => 0
  45. ];
  46. private static $mongodb_config = [
  47. 'url' => 'mongodb://114.116.38.6',
  48. 'database' => 'weilaibike'
  49. ];
  50. // private static $db_config = [
  51. // 'host' => 'node2',
  52. // 'port' => 3306,
  53. // 'user' => 'weilaibike',
  54. // 'password' => 'LGcaxd5WJjepJccx',
  55. // 'db_name' => 'weilaibike'
  56. // ];
  57. //
  58. // private static $redis_config = [
  59. // 'host' => 'node3',
  60. // 'port' => 6379
  61. // ];
  62. // private static $mongodb_config = 'mongodb://node3/weilaibike';
  63. public static function onWorkerStart($businessWorker)
  64. {
  65. self::$db_config = Config['db'];
  66. self::$redis_config = Config['redis'];
  67. self::$mongodb_config = Config['mongodb'];
  68. self::$db = new \Workerman\MySQL\Connection(self::$db_config['host'], self::$db_config['port'], self::$db_config['user'], self::$db_config['password'], self::$db_config['db_name']);
  69. self::$redis = new Redis();
  70. self::$redis->pconnect(self::$redis_config['host'], self::$redis_config['port']);
  71. self::$redis->select(self::$redis_config['database']);
  72. self::$mongo = (new MongoDB\Client(self::$mongodb_config['url']))->selectDatabase(self::$mongodb_config['database']);
  73. }
  74. /**
  75. * 当客户端连接时触发
  76. * 如果业务不需此回调可以删除onConnect.
  77. *
  78. * @param int $client_id 连接id
  79. */
  80. public static function onConnect($client_id)
  81. {
  82. }
  83. /**
  84. * 当客户端发来消息时触发.
  85. *
  86. * @param int $client_id 连接id
  87. * @param mixed $message 具体消息
  88. */
  89. public static function onMessage($client_id, $message)
  90. {
  91. $message = bin2hex($message);
  92. $data = str_split(str_replace(' ', '', trim($message)), 2);
  93. if (($data[0] . $data[1]) !== 'aaaa') return '';
  94. self::log($message, 'EVENT_message', self::$LOG_DEV);
  95. // 传送头
  96. $header = array_slice($data, 0, 10);
  97. $dataLength = hexdec($header[2] . $header[3]);
  98. // 检验包是否完整
  99. if ($dataLength !== count($data)) return 'error:data length';
  100. // 过滤加密
  101. if ($header[4] == '05') {
  102. self::log($message, '**jiami**', self::$LOG_COMMON);
  103. Gateway::closeCurrentClient();
  104. return '';
  105. };
  106. // 流水码
  107. $num_code = $header[6];
  108. // 装载数据
  109. $body = array_slice($data, 10, -2);
  110. // 校检尾
  111. $checkEnd = implode('', array_slice($data, -2, 2));
  112. if ($checkEnd !== self::crc16(array_slice($data, 2, -2))) {
  113. self::log("CRC_ERROR", "CRC16", self::$LOG_MAJOR);
  114. }
  115. $cmd = strtoupper($data[5]);
  116. $cmd_in_box_no = [
  117. \App\Maps\CmdMap::CMD_LOGIN,
  118. \App\Maps\CmdMap::CMD_LOCATION,
  119. \App\Maps\CmdMap::CMD_BATTERY_STATUS,
  120. \App\Maps\CmdMap::CMD_WARNING,
  121. \App\Maps\CmdMap::CMD_HEARTBEAT,
  122. \App\Maps\CmdMap::CMD_STATUS_SYNC,
  123. \App\Maps\CmdMap::CMD_MSG_UP,
  124. \App\Maps\CmdMap::CMD_ADMIN_MSG_FORWARD,
  125. \App\Maps\CmdMap::CMD_BOX_UPGRADE,
  126. \App\Maps\CmdMap::CMD_BOX_ACTIVE_REPORTING,
  127. \App\Maps\CmdMap::CMD_BOX_STATUS_CHANGE_UP
  128. ];
  129. if (in_array($cmd, $cmd_in_box_no)) {
  130. // 响应包含box_no
  131. $box_no = substr(implode('', array_slice($body, 0, 5)), 0, 9);
  132. $_SESSION['box_no'] = $box_no;
  133. Gateway::bindUid($client_id, $box_no);
  134. if (!isset($_SESSION['bike_no'])) {
  135. $_SESSION['bike_no'] = self::byBoxNoGetBikeNoStatic($box_no);
  136. }
  137. //离线检查
  138. if (self::$redis->exists('bike:offline:' . $box_no)) {
  139. self::log($box_no, 'EVENT_ONLINE', self::$LOG_MAJOR);
  140. $cols['is_link'] = \App\Maps\BikeMap::LINK_ONLINE;
  141. self::$db->update('bikes')->where('box_no = ' . $box_no)->cols($cols)->query();
  142. self::$redis->del('bike:offline:' . $box_no);
  143. }
  144. }
  145. $response_body = '';
  146. $response_cmd = false;
  147. $param = [
  148. 'db' => self::$db,
  149. 'mongo' => self::$mongo,
  150. 'redis' => self::$redis
  151. ];
  152. switch ($cmd) {
  153. case \App\Maps\CmdMap::CMD_LOGIN:
  154. echo 'CMD_LOGIN' . PHP_EOL;
  155. // 登录数据包
  156. $response_body = (new \App\Servers\LoginServer($param))->main($body);
  157. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_LOGIN;
  158. break;
  159. case \App\Maps\CmdMap::CMD_LOCATION:
  160. echo 'location' . PHP_EOL;
  161. // 位置数据包
  162. $response_body = (new \App\Servers\LocationServer($param))->main($body);
  163. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_LOCATION;
  164. break;
  165. case \App\Maps\CmdMap::CMD_HEARTBEAT:
  166. echo 'CMD_HEARTBEAT' . PHP_EOL;
  167. // 心跳数据包
  168. $response_body = (new \App\Servers\HeartBeatServer($param))->main($body);
  169. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_HEARTBEAT;
  170. break;
  171. case \App\Maps\CmdMap::CMD_BATTERY_STATUS:
  172. echo 'CMD_BATTERY_STATUS' . PHP_EOL;
  173. //电池数据包
  174. $response_body = (new \App\Servers\BatteryServer($param))->main($body);
  175. break;
  176. case \App\Maps\CmdMap::CMD_WARNING:
  177. echo 'CMD_WARNING' . PHP_EOL;
  178. //告警数据包
  179. $response_body = (new \App\Servers\WarningServer($param))->main($body);
  180. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_WARNING;
  181. break;
  182. case \App\Maps\CmdMap::CMD_REMOTE_CONTROLLER_RESPONSE:
  183. echo 'CMD_REMOTE_CONTROLLER_RESPONSE' . PHP_EOL;
  184. // 服务器下发控制命令时,应答终端
  185. $response_body = (new \App\Servers\RemoteCmdServer($param))->main($body);
  186. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_REMOTE_CMD;
  187. break;
  188. case \App\Maps\CmdMap::CMD_STATUS_SYNC:
  189. echo 'CMD_STATUS_SYNC' . PHP_EOL;
  190. // 终端状态同步
  191. $response_body = (new \App\Servers\BoxSyncServer($param))->main($body);
  192. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_STATUS_SYNC;
  193. break;
  194. case \App\Maps\CmdMap::CMD_MSG_UP:
  195. echo 'CMD_MSG_UP' . PHP_EOL;
  196. // 短信上报
  197. $response_body = (new \App\Servers\MsgUpServer($param))->main($body);
  198. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_MSG_UP;
  199. break;
  200. case \App\Maps\CmdMap::CMD_ADMIN_MSG_FORWARD:
  201. echo 'CMD_MSG_FORWARD' . PHP_EOL;
  202. // 后台短信转发响应
  203. (new \App\Servers\MsgForwardServer($param))->main($body);
  204. break;
  205. case \App\Maps\CmdMap::CMD_BOX_UPGRADE:
  206. echo 'CMD_BOX_UPGRADE' . PHP_EOL;
  207. //终端请求固件升级
  208. $response_body = (new \App\Servers\BoxUpgradeServer($param))->main($body);
  209. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_BOX_UPGRADE;
  210. break;
  211. case \App\Maps\CmdMap::CMD_BOX_ACTIVE_REPORTING:
  212. echo 'CMD_BOX_ACTIVE_REPORTING' . PHP_EOL;
  213. // 终端主动上报
  214. $response_body = (new \App\Servers\BoxActiveReportingServer($param))->main($body);
  215. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_BOX_ACTIVE_REPORTING;
  216. break;
  217. case \App\Maps\CmdMap::CMD_BOX_REPORTING_NEW_BIKE:
  218. echo 'CMD_BOX_REPORTING_NEW_BIKE' . PHP_EOL;
  219. //终端最新车辆信息上报事件
  220. $response_body = (new \App\Servers\NewBikeReportingServer($param))->main($body);
  221. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_BOX_UP_NEW_BIKE_DATA;
  222. break;
  223. case \App\Maps\CmdMap::CMD_BOX_STATUS_CHANGE_UP:
  224. echo 'CMD_BOX_STATUS_CHANGE_UP' . PHP_EOL;
  225. // 终端状态类型变更上报
  226. $response_body = (new \App\Servers\BoxStatusChangeServer($param))->main($body);
  227. $response_cmd = \App\Maps\CmdMap::CMD_RESPONSE_BOX_STATUS_CHANGE_UP;
  228. break;
  229. case \App\Maps\CmdMap::CMD_REMOTE_SELECT_CMD_BOX_UP_DATA:
  230. echo 'CMD_REMOTE_SELECT_CMD_BOX_UP_DATA' . PHP_EOL;
  231. // 远程查询命令,终端上传
  232. (new \App\Servers\BoxSelectCmdServer($param))->main($body);
  233. break;
  234. case \App\Maps\CmdMap::CMD_REMOTE_SETTING_CMD_BOX_UP_DATA:
  235. echo 'CMD_REMOTE_SETTING_CMD_BOX_UP_DATA' . PHP_EOL;
  236. // 远程设置参数,终端上报
  237. (new \App\Servers\BoxSettingServer($param))->main($body);
  238. break;
  239. case \App\Maps\CmdMap::CMD_REMOTE_VOICE_DATA:
  240. echo 'CMD_REMOTE_VOICE_DATA' . PHP_EOL;
  241. //远程语音数据包,终端上传状态
  242. (new \App\Servers\BoxPlayVoiceServer($param))->main($body);
  243. break;
  244. }
  245. // 发送数据
  246. if (is_array($response_body)) {
  247. $response = (new \App\Servers\ResponseServer())->send($response_body, $response_cmd, $num_code);
  248. self::log($response, 'response', self::$LOG_DEV);
  249. Gateway::sendToCurrentClient(hex2bin(str_replace(' ', '', $response)));
  250. }
  251. }
  252. /**
  253. * 当用户断开连接时触发.
  254. *
  255. * @param int $client_id 连接id
  256. */
  257. public static function onClose($client_id)
  258. {
  259. $box_no = $_SESSION['box_no'];
  260. $bike_no = $_SESSION['bike_no'];
  261. // 离线检查
  262. if ($box_no && $bike_no && !Gateway::isUidOnline($box_no)) {
  263. self::log($box_no, 'EVENT_LINK_OFFLINE_ONCLOSE', self::$LOG_COMMON);
  264. self::$db->update('bikes')->where('box_no = ' . $box_no)->cols([
  265. 'is_link' => \App\Maps\BikeMap::LINK_OFFLINE
  266. ])->query();
  267. // 发出警告
  268. self::warningLogBikeOffLineStatic($bike_no, $box_no, 'onClose');
  269. self::$redis->set('bike:offline:' . $box_no, '1', 3600);
  270. }
  271. }
  272. }