db = $param['db']; $this->redis = $param['redis']; $this->mongo = $param['mongo']; if (array_key_exists('msg_id', $param)) { $this->msg_id = $param['msg_id']; } } /** * 解码车辆编号 * @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转10进制 * @param $no16 * @return string * User: Mead */ protected function decodeHex2Dec($no16) { return base_convert($no16, 16, 10); } /** * 16转10进制 * @param $no16 * @return string * User: Mead */ protected function decodeHex2($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, 2); } /** * 解析时间 * @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; } public function decodeWeiKeMuTop34($body) { $i = 0; $box_time = self::stitching($body, $i, 4); $i += 4; $i += 2; $i += 1; $i += 1; // 状态信息 $status = self::stitching($body, $i, 4); // i= 8 $i += 4; $bike_battery_voltage = self::stitching($body, $i, 2); // i = 12 $i += 2; // $i += 1; $kilometers = self::stitching($body, $i, 2); // i = 14 $i += 2; // $i += 1; // 终端电量信息 $d = self::stitching($body, $i, 1); // i=16 $i += 1; $i += 1; // 设备IMEI号 $imei = substr(self::stitching($body, $i, 8), 0, -1); // i=18 $i += 8; //SIM卡IMSI号 $imsi = substr(self::stitching($body, $i, 8), 0, -1); // i=26 return [ 'box_time' => $this->decodeTime($box_time), 'imsi' => $imsi, 'imei' => $imei, 'box_no' => (string)$imei, 'battery_voltage' => $this->byVoltageGetElectric(hexdec($bike_battery_voltage) * 0.1), 'status' => self::handleStatus($status), 'bike_battery_voltage' => $bike_battery_voltage, 'kilometers' => hexdec($kilometers) * 100,//米 ]; } }