123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/9/3
- * Time: 8:09 PM
- */
- namespace App\Servers;
- use App\Handlers\ExceptionTrait;
- use App\Handlers\MapHandler;
- use App\Handlers\ToolsHandler;
- use App\Models\BikeTraitModel;
- class BaseServer
- {
- use ExceptionTrait, ToolsHandler, BikeTraitModel;
- protected $db;
- protected $redis;
- protected $mongo;
- protected $client_id;
- public function __construct($param)
- {
- $this->db = $param['db'];
- $this->redis = $param['redis'];
- $this->mongo = $param['mongo'];
- }
- /**
- * 解码车辆编号
- * @param $no16
- * @return bool|string
- * User: Mead
- */
- protected function decodeBoxNo($no16)
- {
- return substr($no16, 0, 9);
- }
- /**
- * 解析配置项
- * @param $no16
- * @return array
- * User: Mead
- */
- protected function decodeSetting($no16)
- {
- $arr = explode(';', hex2bin($no16), -1);
- $setting = [];
- foreach ($arr as $key => $a) {
- if (!strstr($a, '=')) {
- if ($a === '') break;
- $setting[$a] = '';
- } else {
- list($k, $v) = explode('=', $a);
- $setting[$k] = $v;
- }
- }
- // unset($setting['FUJIA']);
- return $setting;
- }
- /**
- * 解析msg_id
- * @param $no16
- * @return array
- * User: Mead
- */
- protected function decodeMsgId($no16)
- {
- $msg = explode(',', hex2bin($no16));
- return [
- 'no' => $msg[0],
- 'time' => $msg[1],
- 'type' => $msg[2],
- 'cmd' => $msg[3],
- ];
- }
- /**
- * 16转2进制
- * @param $no16
- * @return string
- * User: Mead
- */
- protected function decodeHex2Bin($no16)
- {
- return base_convert($no16, 16, 2);
- }
- protected function decodeStatus8($no16)
- {
- $code2 = str_pad(base_convert($no16, 16, 2), 8, '0', STR_PAD_LEFT);
- list($status7, $status6, $status5, $status4, $status3, $status2, $status1, $status0) = str_split($code2, 1);
- return [
- 'battery' => $status0,
- 'bluetooth' => $status1,
- 'ecu' => $status2,
- 'yixianzhi' => $status3,
- 'wheel_lock' => $status4,
- 'bms' => $status5,
- 'voice' => $status6,
- 'default' => $status7,
- ];
- }
- /**
- * 解码车的状态
- * @param $code16
- * @return array
- * User: Mead
- */
- protected function decodeBikeStatus($code16)
- {
- $code2 = str_pad(base_convert($code16, 16, 2), 16, '0', STR_PAD_LEFT);
- list($status15, $status14, $status13, $status12, $status11, $status10, $status9, $status8, $status7, $status6, $status5, $status4, $status3, $status2, $status1, $status0) = str_split($code2, 1);
- return [
- 'dianchi_chongdian_zhaungtai' => $status15,
- 'yaoshisuo' => $status14,
- 'lanyaganying' => $status13,
- 'is_yunyingquyu' => $status12,
- 'is_houlunsuo' => $status11,
- 'is_zhuanba' => $status10,
- 'is_weijiedianyuan' => $status9,
- 'is_ride' => $status8,
- 'is_dianchisuo' => $status7,
- 'is_lanyalianjie' => $status6,
- 'is_xiumian' => bindec($status5 . $status4),
- 'is_gongdian' => $status3,
- 'is_jie_dianji_lock' => $status2,
- 'is_yundong' => $status1,
- 'is_huanche' => $status0,
- ];
- }
- /**
- * 解码车的维度
- * @param $lat16
- * User: Mead
- */
- protected function decodeLatAndLng($lat16, $lng16)
- {
- $lat10 = hexdec($lat16);
- $lng10 = hexdec($lng16);
- $lat_ok = bcdiv($lat10, 1800000, 10);
- $lng_ok = bcdiv($lng10, 1800000, 10);
- return (new MapHandler())->wgs84togcj02($lng_ok, $lat_ok);
- }
- /**
- * 解析电车电量
- * @param $battery_voltage16
- * @return float|int
- * User: Mead
- */
- protected function decodeBatteryVoltage($battery_voltage16)
- {
- return round(hexdec($battery_voltage16) / 1000);
- }
- /**
- * 解析时间
- * @param $time
- * @return false|string
- * User: Mead
- */
- protected function decodeTime($time)
- {
- return date('Y-m-d H:i:s', hexdec($time));
- }
- /**
- * 过滤包
- * @param $bike_id
- * @param $num
- * @return bool
- * User: Mead
- */
- protected function is_throw($bike_id, $type, $num, $throw_ep_min = 1)
- {
- $redis = $this->redis;
- $key = "throw_tag_{$type}_{$bike_id}";
- //判断是否存在
- if (!$redis->exists($key)) {
- $redis->set($key, 1, $throw_ep_min * 60);
- return false;
- }
- $val = $redis->get($key);
- if ($val >= $num) {
- $redis->set($key, 1, $throw_ep_min * 60);
- return false;
- }
- $redis->incr($key);
- return true;
- }
- /**
- * 多长时间内达到几次触发
- * @param $bike_id
- * @param $num
- * @return bool
- * User: Mead
- */
- protected function is_throw_num_time($bike_id, $type, $num, $throw_ep_min = 1)
- {
- $redis = $this->redis;
- $key = "cache:num:{$type}:{$bike_id}";
- //判断是否存在
- if (!$redis->exists($key)) {
- $redis->set($key, 1, $throw_ep_min * 60);
- return false;
- }
- $val = $redis->get($key);
- if ($val >= $num) {
- $redis->set($key, 1, $throw_ep_min * 60);
- return true;
- }
- $redis->incr($key);
- return false;
- }
- /**
- * 按照过期时间过滤
- * @param $bike_id
- * @param $type
- * @param int $throw_ep_min
- * @return bool
- * User: Mead
- */
- protected function is_ep_min($box_no, $type, $throw_ep_min = 1)
- {
- $redis = $this->redis;
- $key = "cache:min:{$type}:{$box_no}";
- //判断是否存在
- if (!$redis->exists($key)) {
- $redis->set($key, 1, $throw_ep_min * 60);
- return false;
- }
- return true;
- }
- /**
- * 按照过期时间(秒)过滤
- * @param $bike_id
- * @param $type
- * @param int $throw_ep_sec
- * @return bool
- * User: Mead
- */
- protected function is_ep_sec($box_no, $type, $throw_ep_sec = 1)
- {
- $redis = $this->redis;
- $key = "cache:sec:{$type}:{$box_no}";
- //判断是否存在
- if (!$redis->exists($key)) {
- $redis->set($key, 1, $throw_ep_sec);
- return false;
- }
- return true;
- }
- /**
- * 响应
- * @return array
- * User: Mead
- */
- protected function response()
- {
- $body = [];
- return $body;
- }
- }
|