BaseServer.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/9/3
  6. * Time: 8:09 PM
  7. */
  8. namespace App\Servers;
  9. use App\Handlers\ExceptionTrait;
  10. use App\Handlers\MapHandler;
  11. use App\Handlers\ToolsHandler;
  12. use App\Models\BatteryTraitModel;
  13. use App\Models\BikeTraitModel;
  14. class BaseServer
  15. {
  16. use ExceptionTrait, ToolsHandler, BikeTraitModel, BatteryTraitModel;
  17. protected $db;
  18. protected $redis;
  19. protected $mongo;
  20. protected $client_id;
  21. protected $msg_id;
  22. public function __construct($param)
  23. {
  24. $this->db = $param['db'];
  25. $this->redis = $param['redis'];
  26. $this->mongo = $param['mongo'];
  27. if (array_key_exists('msg_id', $param)) {
  28. $this->msg_id = $param['msg_id'];
  29. }
  30. }
  31. /**
  32. * 解码车辆编号
  33. * @param $no16
  34. * @return bool|string
  35. * User: Mead
  36. */
  37. protected function decodeBoxNo($no16)
  38. {
  39. return substr($no16, 0, 9);
  40. }
  41. /**
  42. * 解析配置项
  43. * @param $no16
  44. * @return array
  45. * User: Mead
  46. */
  47. protected function decodeSetting($no16)
  48. {
  49. $arr = explode(';', hex2bin($no16), -1);
  50. $setting = [];
  51. foreach ($arr as $key => $a) {
  52. if (!strstr($a, '=')) {
  53. if ($a === '') break;
  54. $setting[$a] = '';
  55. } else {
  56. list($k, $v) = explode('=', $a);
  57. $setting[$k] = $v;
  58. }
  59. }
  60. // unset($setting['FUJIA']);
  61. return $setting;
  62. }
  63. /**
  64. * 解析msg_id
  65. * @param $no16
  66. * @return array
  67. * User: Mead
  68. */
  69. protected function decodeMsgId($no16)
  70. {
  71. $msg = explode(',', hex2bin($no16));
  72. return [
  73. 'no' => $msg[0],
  74. 'time' => $msg[1],
  75. 'type' => $msg[2],
  76. 'cmd' => $msg[3],
  77. ];
  78. }
  79. /**
  80. * 16转10进制
  81. * @param $no16
  82. * @return string
  83. * User: Mead
  84. */
  85. protected function decodeHex2Dec($no16)
  86. {
  87. return base_convert($no16, 16, 10);
  88. }
  89. /**
  90. * 16转10进制
  91. * @param $no16
  92. * @return string
  93. * User: Mead
  94. */
  95. protected function decodeHex2($no16)
  96. {
  97. return base_convert($no16, 16, 2);
  98. }
  99. protected function decodeStatus8($no16)
  100. {
  101. $code2 = str_pad(base_convert($no16, 16, 2), 8, '0', STR_PAD_LEFT);
  102. list($status7, $status6, $status5, $status4, $status3, $status2, $status1, $status0) = str_split($code2, 1);
  103. return [
  104. 'battery' => $status0,
  105. 'bluetooth' => $status1,
  106. 'ecu' => $status2,
  107. 'yixianzhi' => $status3,
  108. 'wheel_lock' => $status4,
  109. 'bms' => $status5,
  110. 'voice' => $status6,
  111. 'default' => $status7,
  112. ];
  113. }
  114. /**
  115. * 解码车的状态
  116. * @param $code16
  117. * @return array
  118. * User: Mead
  119. */
  120. protected function decodeBikeStatus($code16)
  121. {
  122. $code2 = str_pad(base_convert($code16, 16, 2), 16, '0', STR_PAD_LEFT);
  123. list($status15, $status14, $status13, $status12, $status11, $status10, $status9, $status8, $status7, $status6, $status5, $status4, $status3, $status2, $status1, $status0) = str_split($code2, 1);
  124. return [
  125. 'dianchi_chongdian_zhaungtai' => $status15,
  126. 'yaoshisuo' => $status14,
  127. 'lanyaganying' => $status13,
  128. 'is_yunyingquyu' => $status12,
  129. 'is_houlunsuo' => $status11,
  130. 'is_zhuanba' => $status10,
  131. 'is_weijiedianyuan' => $status9,
  132. 'is_ride' => $status8,
  133. 'is_dianchisuo' => $status7,
  134. 'is_lanyalianjie' => $status6,
  135. 'is_xiumian' => bindec($status5 . $status4),
  136. 'is_gongdian' => $status3,
  137. 'is_jie_dianji_lock' => $status2,
  138. 'is_yundong' => $status1,
  139. 'is_huanche' => $status0,
  140. ];
  141. }
  142. /**
  143. * 解码车的维度
  144. * @param $lat16
  145. * User: Mead
  146. */
  147. protected function decodeLatAndLng($lat16, $lng16)
  148. {
  149. $lat10 = hexdec($lat16);
  150. $lng10 = hexdec($lng16);
  151. $lat_ok = bcdiv($lat10, 1800000, 10);
  152. $lng_ok = bcdiv($lng10, 1800000, 10);
  153. return (new MapHandler())->wgs84togcj02($lng_ok, $lat_ok);
  154. }
  155. /**
  156. * 解析电车电量
  157. * @param $battery_voltage16
  158. * @return float|int
  159. * User: Mead
  160. */
  161. protected function decodeBatteryVoltage($battery_voltage16)
  162. {
  163. return round(hexdec($battery_voltage16) / 1000, 2);
  164. }
  165. /**
  166. * 解析时间
  167. * @param $time
  168. * @return false|string
  169. * User: Mead
  170. */
  171. protected function decodeTime($time)
  172. {
  173. return date('Y-m-d H:i:s', hexdec($time));
  174. }
  175. /**
  176. * 过滤包
  177. * @param $bike_id
  178. * @param $num
  179. * @return bool
  180. * User: Mead
  181. */
  182. protected function is_throw($bike_id, $type, $num, $throw_ep_min = 1)
  183. {
  184. $redis = $this->redis;
  185. $key = "throw_tag_{$type}_{$bike_id}";
  186. //判断是否存在
  187. if (!$redis->exists($key)) {
  188. $redis->set($key, 1, $throw_ep_min * 60);
  189. return false;
  190. }
  191. $val = $redis->get($key);
  192. if ($val >= $num) {
  193. $redis->set($key, 1, $throw_ep_min * 60);
  194. return false;
  195. }
  196. $redis->incr($key);
  197. return true;
  198. }
  199. /**
  200. * 多长时间内达到几次触发
  201. * @param $bike_id
  202. * @param $num
  203. * @return bool
  204. * User: Mead
  205. */
  206. protected function is_throw_num_time($bike_id, $type, $num, $throw_ep_min = 1)
  207. {
  208. $redis = $this->redis;
  209. $key = "cache:num:{$type}:{$bike_id}";
  210. //判断是否存在
  211. if (!$redis->exists($key)) {
  212. $redis->set($key, 1, $throw_ep_min * 60);
  213. return false;
  214. }
  215. $val = $redis->get($key);
  216. if ($val >= $num) {
  217. $redis->set($key, 1, $throw_ep_min * 60);
  218. return true;
  219. }
  220. $redis->incr($key);
  221. return false;
  222. }
  223. /**
  224. * 按照过期时间过滤
  225. * @param $bike_id
  226. * @param $type
  227. * @param int $throw_ep_min
  228. * @return bool
  229. * User: Mead
  230. */
  231. protected function is_ep_min($box_no, $type, $throw_ep_min = 1)
  232. {
  233. $redis = $this->redis;
  234. $key = "cache:min:{$type}:{$box_no}";
  235. //判断是否存在
  236. if (!$redis->exists($key)) {
  237. $redis->set($key, 1, $throw_ep_min * 60);
  238. return false;
  239. }
  240. return true;
  241. }
  242. /**
  243. * 按照过期时间(秒)过滤
  244. * @param $bike_id
  245. * @param $type
  246. * @param int $throw_ep_sec
  247. * @return bool
  248. * User: Mead
  249. */
  250. protected function is_ep_sec($box_no, $type, $throw_ep_sec = 1)
  251. {
  252. $redis = $this->redis;
  253. $key = "cache:sec:{$type}:{$box_no}";
  254. //判断是否存在
  255. if (!$redis->exists($key)) {
  256. $redis->set($key, 1, $throw_ep_sec);
  257. return false;
  258. }
  259. return true;
  260. }
  261. /**
  262. * 响应
  263. * @return array
  264. * User: Mead
  265. */
  266. protected function response()
  267. {
  268. $body = [];
  269. return $body;
  270. }
  271. public function decodeWeiKeMuTop34($body)
  272. {
  273. $i = 0;
  274. $box_time = self::stitching($body, $i, 4);
  275. $i += 4;
  276. $i += 2;
  277. $i += 1;
  278. $i += 1;
  279. // 状态信息
  280. $status = self::stitching($body, $i, 4); // i= 8
  281. $i += 4;
  282. $bike_battery_voltage = self::stitching($body, $i, 2); // i = 12
  283. $i += 2;
  284. // $i += 1;
  285. $kilometers = self::stitching($body, $i, 2); // i = 14
  286. $i += 2;
  287. // $i += 1;
  288. // 终端电量信息
  289. $d = self::stitching($body, $i, 1); // i=16
  290. $i += 1;
  291. $i += 1;
  292. // 设备IMEI号
  293. $imei = substr(self::stitching($body, $i, 8), 0, -1); // i=18
  294. $i += 8;
  295. //SIM卡IMSI号
  296. $imsi = substr(self::stitching($body, $i, 8), 0, -1); // i=26
  297. return [
  298. 'box_time' => $this->decodeTime($box_time),
  299. 'imsi' => $imsi,
  300. 'imei' => $imei,
  301. 'box_no' => (string)$imei,
  302. 'battery_voltage' => $this->byVoltageGetElectric(hexdec($bike_battery_voltage) * 0.1),
  303. 'status' => self::handleStatus($status),
  304. 'bike_battery_voltage' => $bike_battery_voltage,
  305. 'kilometers' => hexdec($kilometers) * 100,//米
  306. ];
  307. }
  308. }