BaseServer.php 6.7 KB

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