123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- <?php
- class RcgjfwDecodeAction extends Action {
- /*
- 参数说明:
- packet:
- packet_text:带空格16进制文本
- */
- private function import($packet, $packet_text){
- //解析消息体
- $result = $this->rfid_proto->tcp_unpack_msg($packet);
- if(! $result['Success'] ){
-
- $this->rfid_proto->decode_error_log('unpack_fail','unpack packet failed ,result : '.json_encode($result));
- return;
- }
- //记录从redis获取到的基站数据包
- $this->rfid_proto->decode_packet_log($result['StationCode'],$result['StationCode'].' => '.$packet_text);
-
- //获取一条车辆标签(电子标签编码)
- $FirstLabelNumber = $result['VehicleList'][0]['VehicleNumber'];
- //tcp_log('tcp packet received. station code= ' . $result['StationCode'] . ', label_number= ' . $FirstLabelNumber);
- //未入库基站,缓存命中,直接返回
- $not_exit_station_key = 'not_exit_station_'.$result['StationCode'];
- $not_exit_station = \Jiaruan\StaticCache::get($not_exit_station_key);
- if( \Jiaruan\StaticCache::ishit($not_exit_station) ){
- decode_error_log('not_existed_station', 'station not existed,cath is hit, station_code = ' . $result['StationCode']);
- return;
- }
- //已开局基站,缓存一小时基站信息,避免频繁查询
- $station_key = 'station_info_'.$result['StationCode'];
- $station_info = \Jiaruan\StaticCache::get($station_key);
- if( !\Jiaruan\StaticCache::ishit($station_info) ){
- try{
- //获取基站信息
- $cond = array('DeviceCode' => $result['StationCode']);
- //排除指定字段,防止旧系统查询不存在的字段时报错
- $station_info = M('jms_station')->where($cond)->field('IsDuplicate,ChargerId,PhotoUrl,SimCardNo,DeviceType,AddTime,InstallationTime,OpenSucessTime,OnlineTime,Comment,IsRecycled,PoliceId,MoveStationLeader,MoveStationPhone,ManufactType',true)->find();
-
- //$station_info = M('jms_station')->where($cond)->field('ID,StationNumericId,FirstLabelNumber,InstallationStatus,CityId,Longitude,Latitude,Address,StationType,DeviceNumber,DeviceName,')->find();
- }catch (Exception $e){
- $msg = 'get_station_info ,cach exception!! error_code = '.$e->getCode().', error_msg = '.$e->getMessage().', station_code = '.$result['StationCode'];
- decode_error_log('get_station_conn_mysql_exception',$msg);
- }
-
- //不存在的基站,缓存十分钟,并返回
- if(!$station_info){
- decode_error_log('not_existed_station', 'station not existed, station_code = ' . $result['StationCode']);
- \Jiaruan\StaticCache::set($not_exit_station_key,true,600);
- return;
- }
-
- if ( $station_info['InstallationStatus'] == C('开局状态_已成功') ){
- \Jiaruan\StaticCache::set($station_key,$station_info,3600);
- }
- }
- //对数据包进行日志记录
- /*
- $data = array(
- 'StationId' => $station_info['ID'] ? : 0,
- 'StationCode' => $result['StationCode'],
- 'PacketData' => $packet_text,
- 'AddTime' => date('Y-m-d H:i:s')
- );
- */
-
- $data = array(
- 'StationId' => $station_info['StationNumericId'] ? : ($station_info['ID']?:0),//兼容旧版本
- 'StationCode' => $result['StationCode'],
- 'PacketData' => $packet_text,
- 'AddTime' => date('Y-m-d H:i:s')
- );
-
- //原始包存到表格存储
- $rawData = $data;//tablestore
- $rawData['StationCode'] = $station_info['DeviceNumber'];//tablestore
- $this->routeStore->addRawPacket($station_info['CityId'], $rawData);
-
-
- //移动基站,过大的数据包,跳过处理
- if($station_info['StationType']==C('基站类型_移动基站')&&$result['body_length']>1000){
- decode_error_log('too_big_packet_move_station','move_station packet too big, skip decode; station_code => '.$result['StationCode']);
- return;
- }
- //普通基站,过大的数据包,记录一下,但继续处理
- if($station_info['StationType']==C('基站类型_普通基站')&&$result['body_length']>1000){
- decode_error_log('too_big_packet_normal_station','normal_station packet too big, station_code => '.$result['StationCode']);
- }
- /*
- $start = microtime(true);
- if( ! M('jms_rpacket')->createAdd($data) ){
- decode_error_log('add_rpacket_fail','add rpacket to jms_rpacket fail, error_message:'.M('jms_rpacket')->getLastSql().', data: '.json_encode($data));
- return;
- }
- echo '************time0: '.use_microtime($start).PHP_EOL;
- */
-
-
- //更新基站信息(已开局基站,间隔一分钟更新一次,避免频繁更新)
- $station_key_update_time = 'station_key_update_time_'. $result['StationCode'];
- $station_update_time = \Jiaruan\StaticCache::get($station_key_update_time);
- if( !\Jiaruan\StaticCache::ishit($station_update_time) && $FirstLabelNumber && !is_mock_route($FirstLabelNumber) ){
- $data = array();
- if(!$station_info['FirstLabelNumber'] && $FirstLabelNumber)
- $data['FirstLabelNumber'] = $FirstLabelNumber;
- if( $station_info['InstallationStatus'] == C('开局状态_开局中') ){
- $data['InstallationStatus'] = C('开局状态_已成功');
- $data['OpenSucessTime'] = date('Y-m-d H:i:s');
- }
- //异常基站跳过更新基站状态
- $cond = array('DeviceCode' => $result['StationCode']);
- $station_status = M('jms_station')->where($cond)->getField('DeviceStatus');
- if($station_status != C('基站状态_异常')){
- $data['DeviceStatus'] = C('基站状态_在线');
- }
- $data['OnlineTime'] = date('Y-m-d H:i:s');
- $start = microtime(true);
- try{
- $cond = array('ID'=>$station_info['ID']);
- if(! M('jms_station')->where($cond)->save($data) ){
- decode_error_log('update_station_info',"update station info failed. sql = " . M('jms_station')->getLastSql().',db_error = '. M('jms_station')->getDbError());
- }
- }catch (Exception $e) {
- $msg = 'update_station_info ,cach exception!! error_code = '.$e->getCode().', error_msg = '.$e->getMessage().', station_code = '.$result['StationCode'];
- decode_error_log('update_station_conn_mysql_exception',$msg);
- }
-
- echo '************time1:'.use_microtime($start).PHP_EOL;
- //已开局基站,缓存更新时间
- if( $station_info['InstallationStatus'] == C('开局状态_已成功') ){
- \Jiaruan\StaticCache::set($station_key_update_time,time(),60);
- }
- }
-
- //还没有指定城市,跳过
- if(! $station_info['CityId'] ){
- decode_error_log('cityid_empty','station cityid not existed, station_code: '.$result['StationCode']);
- return;
- }
-
- //还没有开局成功,跳过
- if($station_info['InstallationStatus'] != C('开局状态_已成功') ){
- decode_error_log('not_kaiju','station not kaiju, station_code: '.$result['StationCode']);
- return;
- }
- /*
- //检测基站近一分钟内是否更新过,更新过的跳过
- if($this->check_station_is_cathed($station_info['DeviceNumber'])){
- echo 'skip update station vehicle route;station_code: '.$station_info['DeviceCode'].PHP_EOL;
- return;
- }*/
- //保存到车辆表
- foreach($result['VehicleList'] as $key=>$row){
- $result['VehicleList'][$key]['StationCode'] = $result['StationCode'];
- $result['VehicleList'][$key]['CityId'] = $station_info['CityId'];
- $start = microtime(true);
-
- $not_exist_key = 'not_exist_key_'.$row['VehicleNumber'];
- $not_exist = \Jiaruan\StaticCache::get($not_exist_key);
- // 未入库的标签缓存命中就跳过
- if( \Jiaruan\StaticCache::ishit($not_exist) ){
- continue;
- }
- try{
- $cond = array('DeviceNumber'=> $row['VehicleNumber'] );
- $vehicle_info = M('jms_vehicle')->where($cond)->field('ID,VehicleNumericId,UserId,StationCode,DeviceStatus,LicensePlate')->find();
- }catch (Exception $e){
- $msg = 'get_vehicle_info ,cach exception!! error_code = '.$e->getCode().', error_msg = '.$e->getMessage().', vehicle_no = '.$row['VehicleNumber'];
- decode_error_log('get_vehicle_conn_mysql_exception',$msg);
- continue;
- }
- //获取未入库标签,缓存十分钟,避免频繁查询
- if(!$vehicle_info){
- $result['VehicleList'][$key]['VehicleId'] = 0;
- $result['VehicleList'][$key]['VehicleStatus'] = -1; //-1表示不存在的车辆标签
- //tcp_log( 'skip save vehicle (not exists) '. $row['VehicleNumber'] );
- decode_error_log('not_exist_vehicles','skip save vehicle (not exists) '. $row['VehicleNumber'].',station_code:'.$result['StationCode']);
- \Jiaruan\StaticCache::get($not_exist_key,true,600);
- continue;
- }
-
-
- $row['StationCode'] = $result['StationCode'];
- $row['Longitude'] = $station_info['Longitude'];
- $row['Latitude'] = $station_info['Latitude'];
- $row['Address'] = $station_info['Address'];
-
- $start = microtime(true);
- $result['VehicleList'][$key]['VehicleId'] = $vehicle_info['VehicleNumericId'] ? : ($vehicle_info['ID']?:0);//兼容旧版本
- $result['VehicleList'][$key]['LastStationCode'] = $vehicle_info['StationCode'];
- $result['VehicleList'][$key]['VehicleStatus'] = $vehicle_info['DeviceStatus'];
- $result['VehicleList'][$key]['LicensePlate'] = $vehicle_info['LicensePlate'];
-
- $notkaihu_key = 'cach_notkaihu_'.$row['VehicleNumber'];
- $not_kaihu = \Jiaruan\StaticCache::get($notkaihu_key);
- //获取未开户标签缓存命中,跳过
- if( \Jiaruan\StaticCache::ishit($not_kaihu) ){
- echo 'no kaihu , cach is hit, skip,vehicle_no = '.$row['VehicleNumber'].PHP_EOL;
- continue;
- }
- //模拟轨迹数据,跳过更新车辆信息
- if(is_mock_route($row['VehicleNumber'])){
- echo 'mock_route,skip update jms_vehicle'.PHP_EOL;
- continue;
- }
- //未开户车辆,缓存十分钟,避免频繁查询
- if(!$vehicle_info['UserId']){
- echo 'no kaihu skip '.PHP_EOL;
- echo '************time: '.use_microtime($start).PHP_EOL;
- \Jiaruan\StaticCache::set($notkaihu_key,true,600);
- continue;
- }
- //移动基站跳过更新
- if($station_info['StationType']==C('基站类型_移动基站')){
- echo 'skip move station route '.PHP_EOL;
- continue;
- }
-
- try{
- if( M('jms_vehicle')->createSave($cond,$row) ){
- decode_log( 'save vehicle ok '. $row['VehicleNumber'] );
- echo '************time2:'.use_microtime($start).PHP_EOL;
- }else{
- decode_error_log( 'update_vehicle_info ', 'update_vehicle_info failed,vehicle_no = '. $row['VehicleNumber'] . ', sql = ' .M('jms_vehicle')->getLastSql().',db_error = '. M('jms_vehicle')->getDbError() );
- }
- }catch (Exception $e) {
- $msg = 'update_vehicle_info ,cach exception!! error_code = '.$e->getCode().', error_msg = '.$e->getMessage().', vehicle_no = '.$row['VehicleNumber'];
- decode_error_log('update_vehicle_conn_mysql_exception',$msg);
- }
-
-
- }
- /*
- //如果是移动基站,轨迹保存到临时监控轨迹表中
- if($station_info['StationType']==C('基站类型_移动基站')){
- $this->add_monitor_sroute($result,$station_info);
- }else{
- $this->add_normal_sroute($result,$station_info);
- }
- */
- //添加解包日志
- vehicle_route_log('decode',$result);
- station_route_log('decode',$result);
- //推送车辆轨迹信号到redis
- $this->push_redis_vsignal($result['VehicleList']);
-
- // tablestore
- if (C("是否支持表格存储")) {
- //if (!in_array($station_info['CityId'], [1, 28])) {//28义乌 1鹿邑
- // return;
- //}
- //$this->TsAddRawPacket($station_info['CityId'], $rawData);
- if ($station_info['StationType'] == C('基站类型_移动基站')) {
- $this->add_monitor_tablestore_sroute($result, $station_info);
- } else {
- $this->add_normal_tablsestore_sroute($result, $station_info);
- }
- }
- }
- /*
- */
- public function index(){
- $redis_raw_packet = Redis("fdqu_raw_packet","queue");
- //实例化信号处理类
- $this->rfid_proto = new Rlgs\Proto\RfidStation2();
- $start_time = time();
- do{
- //query one record
- $packet_text = $redis_raw_packet->pop();
- if(!$packet_text){
- echo 'no raw packet.'.PHP_EOL;
- sleep(1);
- continue;
- }
- //parse packet
- $packet = $this->rfid_proto->tcp_str2bin($packet_text);
- if(!$packet){
-
- $this->rfid_proto->decode_error_log('parse_fail','parse packet failed ,packet_text : '.$packet_text);
- continue;
- }
- $this->import($packet,$packet_text);
- }while(time()-$start_time<60);
-
-
- }
- /*
- 参数说明:
- signal_list:
- */
- private function push_redis_vsignal($signal_list){
- foreach($signal_list as $key=>$row){
- if($row['IsOnline']){
- $data = array(
- 'CityId' => $row['CityId'],
- 'VehicleNumber'=>$row['VehicleNumber'],
- 'StationCode'=> $row['StationCode'],
- 'OnlineTime' => $row['OnlineTime'],
- 'Timestamp' => $row['Timestamp'],
- 'SignalCount' => $row['SignalCount'],
- 'AddTime' => date('Y-m-d H:i:s'),
- );
- $r = Redis("fdqu_vehicle_signal","queue");
- $r->push($data);
- echo '成功添加一条轨迹信号到redis'. PHP_EOL;
- }
- }
- }
- /*
- 参数说明:
- result:
- station_info:
- */
- private function add_monitor_sroute($result, $station_info){
- /*
- //先删除该移动基站2天以前的轨迹
- $cond = array(
- 'StationId'=>$station_info['ID'],
- 'OnlineTime' => array( 'lt',date('Y-m-d 00:00:00',strtotime("-2 day")) )
- );
- if(M('jms_monitor_sroute')->where($cond)->find()){
- $result = M('jms_monitor_sroute')->where($cond)->delete();
- tcp_log( 'success delete 2 days ago data count : '.$result.PHP_EOL );
- }*/
- //添加车辆轨迹到临时表
- $addTime = date('Y-m-d H:i:s');
- foreach($result['VehicleList'] as $row){
- $row['StationCode'] = $result['StationCode'];
- if(!$row['VehicleId']){
- decode_log( '[not found]skip add route '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- continue;
- }
- $start = microtime(true);
- //不接收所有标签数据的基站,要检查车辆是否已开户
- if(!$station_info['IsReceiveAll']){
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- if(!M('jms_vehicle')->where($cond)->getField('UserId')){
- $end = microtime(true);
- decode_log( 'not kaihu , skip add monitor route '. $row['VehicleNumber']);
- continue;
- }
- }
- echo '************time1:'.use_microtime($start).PHP_EOL;
- if($row['IsOnline']){
- //查看车辆最近一个基站是否这个基站
- $row['CityId'] = $station_info['CityId'];
- $row['StationId'] = $station_info['StationNumericId']?:$station_info['ID'];
- $row['StationName'] = $station_info['DeviceName'];
- $row['Longitude'] = $station_info['Longitude'];
- $row['Latitude'] = $station_info['Latitude'];
- $row['Address'] = $station_info['Address'];
- $row['AddTime'] = $addTime;
- //如果是被盗状态车辆,轨迹同时保存到固定基站轨迹表
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- $vehicle_status = M('jms_vehicle')->where($cond)->getField('DeviceStatus');
- $start = microtime(true);
- if($vehicle_status == C('车辆状态_被盗')){
- if( M('jms_sroute')->createAdd($row) ){
- decode_log( 'stolen monitor_route add to sroute ok '. $row['StationName'] . ' '. $row['VehicleNumber']);
- }
- else{
- decode_log( 'stolen monitor_route add to sroute fail '. $row['StationName'] . ' '. $row['VehicleNumber'] . ' sql-' .M('jms_sroute')->getLastSql());
- }
- echo '************time2:'.use_microtime($start).PHP_EOL;
- }
- $start = microtime(true);
- if( M('jms_monitor_sroute')->createAdd($row) )
- decode_log( 'add monitor_sroute ok '. $row['StationName'] . ' '. $row['VehicleNumber'] .' OnlineTime: '.$row['OnlineTime'].' ,AddTime: '.$addTime.' ,timestamp: '.$row['time']);
- else
- decode_log( 'add monitor_sroute fail '. $row['StationName'] . ' '. $row['VehicleNumber'] . ' sql-' .M('jms_sroute_tmp')->getLastSql());
- echo '************time3:'.use_microtime($start).PHP_EOL;
- }
- else{
- decode_log( '[offline]skip monitor_sroute add route '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- }
- }
- }
- /*
- 参数说明:
- result:
- station_info:
- */
- private function add_normal_sroute($result, $station_info){
- //保存到轨迹表
- $addTime = date('Y-m-d H:i:s');
- foreach($result['VehicleList'] as $row){
- if(!$row['Timestamp']){
- decode_error_log('time_stamp_empty','skip update(time_stamp is empty),vehicle: '. $row['VehicleNumber'].',station_code: '.$result['StationCode']);
- continue;
- }
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- if(!M('jms_vehicle')->where($cond)->getField('UserId')){
- decode_log( 'not kaihu , skip add route ' .$row['VehicleNumber'] );
- continue;
- }
- $row['StationCode'] = $result['StationCode'];
- if(! $row['VehicleId']){
- decode_log( '[not found]skip add route '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- continue;
- }
- if($row['IsOnline']){
- //查看车辆最近一个基站是否这个基站
- if($result['StationCode'] != $result['LastStationCode']){ //不是的话,是个新的轨迹
- $row['CityId'] = $station_info['CityId'];
- //分表轨迹数据要,stationid要用整数类型,不能用guid类型
- $row['StationId'] = $station_info['StationNumericId']?:($station_info['ID']?:0);//兼容旧版本
- $row['StationName'] = $station_info['DeviceName'];
- $row['Longitude'] = $station_info['Longitude'];
- $row['Latitude'] = $station_info['Latitude'];
- $row['Address'] = $station_info['Address'];
- $row['AddTime'] = $addTime;
- $start = microtime(true);
- if( M('jms_vroute')->createAdd($row) )
- decode_log( 'add vroute ok '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- else
- decode_log( 'add vroute fail '. $row['StationName'] . ' '. $row['VehicleNumber'] . ' sql-' .M('jms_route')->getLastSql());
- echo '************time2:'.use_microtime($start).PHP_EOL;
- $start = microtime(true);
- if( M('jms_sroute')->createAdd($row) )
- decode_log( 'add sroute ok '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- else
- decode_log( 'add sroute fail '. $row['StationName'] . ' '. $row['VehicleNumber'] . ' sql-' .M('jms_route')->getLastSql());
- echo '************time3:'.use_microtime($start).PHP_EOL;
-
- //如果是被盗状态车辆,轨迹同时保存到监控轨迹表
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- $vehicle_status = M('jms_vehicle')->where($cond)->getField('DeviceStatus');
- if($vehicle_status == C('车辆状态_被盗')){
- $row['StationType'] = C('基站类型_普通基站');
- $start = microtime(true);
- if( M('jms_monitor_sroute')->createAdd($row) ){
- decode_log( 'add monitor_sroute ok '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- }
- else{
- decode_log( 'add monitor_sroute fail '. $row['StationName'] . ' '. $row['VehicleNumber'] . ' sql-' .M('jms_monitor_sroute')->getLastSql());
- }
- echo '************time1:'.use_microtime($start).PHP_EOL;
- }
- }
- else{
- decode_log( '[same-station]skip add route '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- }
- }
- else{
- decode_log( '[offline]skip add route '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- }
- }
- }
- /*
- */
- public function delete_monitor_route(){
-
- //先删除该移动基站2天以前的轨迹
- $cond = array(
- 'OnlineTime' => array( 'lt',date('Y-m-d 00:00:00',strtotime("-2 day")) )
- );
- if(M('jms_monitor_sroute')->where($cond)->find()){
- echo 'start to delete '.PHP_EOL;
- $result = M('jms_monitor_sroute')->where($cond)->delete();
- echo 'finished to delete '.PHP_EOL;
- decode_log( 'success delete 2 days ago data result : '.$result.PHP_EOL );
- }else{
- echo 'no data to delete '.PHP_EOL;
- }
- exit;
- }
- /*
- 参数说明:
- station_no:基站标签
- */
- private function check_station_is_cathed($station_no){
- $cache=S('is_cached_station_'.$station_no);
- if(!$cache){
- S('is_cached_station_'.$station_no,1,60);
- return false;
- }else{
- return true;
- }
- }
- /*
- 参数说明:
- result:
- station_info:
- */
- public function add_normal_tablsestore_sroute($result, $station_info){
- //保存到轨迹表
- $addTime = date('Y-m-d H:i:s');
- $monitorRows = []; // tablestore
- $normalRows = []; // tablestore
- foreach($result['VehicleList'] as $row){
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- if(!M('jms_vehicle')->where($cond)->getField('UserId')){
- continue;
- }
- $row['StationCode'] = $result['StationCode'];
- if(! $row['VehicleId']){
- continue;
- }
- if($row['IsOnline']){
- //查看车辆最近一个基站是否这个基站
- if($result['StationCode'] != $result['LastStationCode']){ //不是的话,是个新的轨迹
- $row['StationType'] = C('基站类型_普通基站');//tablestore
- $row['CityId'] = $station_info['CityId'];
- //分表轨迹数据要,stationid要用整数类型,不能用guid类型
- $row['StationId'] = $station_info['StationNumericId']?:($station_info['ID']?:0);//兼容旧版本
- $row['StationName'] = $station_info['DeviceName'];
- $row['Longitude'] = $station_info['Longitude'];
- $row['Latitude'] = $station_info['Latitude'];
- $row['Address'] = $station_info['Address'];
- $row['AddTime'] = $addTime;
-
- //如果是被盗状态车辆,轨迹同时保存到监控轨迹表
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- $vehicle_status = M('jms_vehicle')->where($cond)->getField('DeviceStatus');
- if($vehicle_status == C('车辆状态_被盗')){
- $row['StationType'] = C('基站类型_普通基站');
- $row['StationCode'] = $station_info['DeviceNumber'];// tablestore
- $monitorRows[] = $row; // tablestore
-
- } else {
- $row['StationCode'] = $station_info['DeviceNumber'];// tablestore
- }
- $normalRows[] = $row; // tablestore
- }
- }
- }
- $this->routeStore->addMonitorRoute($monitorRows); // tablestore
- $this->routeStore->addNormalRoute($normalRows); // tablestore
- }
- /*
- 参数说明:
- result:
- station_info:
- */
- public function add_monitor_tablestore_sroute($result, $station_info){
- /*
- //先删除该移动基站2天以前的轨迹
- $cond = array(
- 'StationId'=>$station_info['ID'],
- 'OnlineTime' => array( 'lt',date('Y-m-d 00:00:00',strtotime("-2 day")) )
- );
- if(M('jms_monitor_sroute')->where($cond)->find()){
- $result = M('jms_monitor_sroute')->where($cond)->delete();
- tcp_log( 'success delete 2 days ago data count : '.$result.PHP_EOL );
- }*/
- //添加车辆轨迹到临时表
- $monitorRows = []; // tablestore
- $mobileRows = []; // tablestore
- $addTime = date('Y-m-d H:i:s');
- foreach($result['VehicleList'] as $row){
- $row['StationCode'] = $result['StationCode'];
- if(!$row['VehicleId']){
- // tcp_log( '[not found]skip add route '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- continue;
- }
- $start = microtime(true);
- //不接收所有标签数据的基站,要检查车辆是否已开户
- if(!$station_info['IsReceiveAll']){
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- if(!M('jms_vehicle')->where($cond)->getField('UserId')){
- $end = microtime(true);
- // tcp_log( 'not kaihu , skip add monitor route '. $row['VehicleNumber']);
- continue;
- }
- }
- echo '************time1:'.use_microtime($start).PHP_EOL;
- if($row['IsOnline']){
- //查看车辆最近一个基站是否这个基站
- $row['StationType'] = C('基站类型_移动基站');// tablestore
- $row['CityId'] = $station_info['CityId'];
- $row['StationId'] = $station_info['StationNumericId']?:$station_info['ID'];
- $row['StationName'] = $station_info['DeviceName'];
- $row['Longitude'] = $station_info['Longitude'];
- $row['Latitude'] = $station_info['Latitude'];
- $row['Address'] = $station_info['Address'];
- $row['AddTime'] = $addTime;
- //如果是被盗状态车辆,轨迹同时保存到固定基站轨迹表
- $cond = array('DeviceNumber'=>$row['VehicleNumber']);
- $vehicle_status = M('jms_vehicle')->where($cond)->getField('DeviceStatus');
- $start = microtime(true);
- if($vehicle_status == C('车辆状态_被盗')){
- // if( M('jms_sroute')->createAdd($row) ){
- // tcp_log( 'stolen monitor_route add to sroute ok '. $row['StationName'] . ' '. $row['VehicleNumber']);
- // }
- // else{
- // tcp_log( 'stolen monitor_route add to sroute fail '. $row['StationName'] . ' '. $row['VehicleNumber'] . ' sql-' .M('jms_sroute')->getLastSql());
- // }
- // echo '************time2:'.use_microtime($start).PHP_EOL;
- $row['StationCode'] = $station_info['DeviceNumber'];// tablestore
- $monitorRows[] = $row; // tablestore
- } else {
- $row['StationCode'] = $station_info['DeviceNumber'];// tablestore
-
- }
- $mobileRows[] = $row; // tablestore
- // $start = microtime(true);
- // if( M('jms_monitor_sroute')->createAdd($row) )
- // tcp_log( 'add monitor_sroute ok '. $row['StationName'] . ' '. $row['VehicleNumber'] .' OnlineTime: '.$row['OnlineTime'].' ,AddTime: '.$addTime.' ,timestamp: '.$row['time']);
- // else
- // tcp_log( 'add monitor_sroute fail '. $row['StationName'] . ' '. $row['VehicleNumber'] . ' sql-' .M('jms_sroute_tmp')->getLastSql());
- // echo '************time3:'.use_microtime($start).PHP_EOL;
- }
- else{
- // tcp_log( '[offline]skip monitor_sroute add route '. $row['StationName'] . ' '. $row['VehicleNumber'] );
- }
- }
- //移动基站的数据全部添加到移动表里面
- //$this->routeStore->addMonitorRoute($monitorRows); // tablestore
- $this->routeStore->addMobileRoute($mobileRows); // tablestore
- }
- /*
- */
- public function index_test(){
- /*
- 在cli模式下,用手工生成的数据,测试解码程序使用
- 例子:php index.php decode/index_test '9c bc 01 31 00 ca 00 20 88 88 '
- */
-
- //$redis_raw_packet = Redis("fdqu_raw_packet","queue");
- $start_time = time();
- if (C("是否支持表格存储")) {
- //C("是否支持表格存储", false);
- $this->routeStore = new \Jiaruan\RouteStore();// tablestore
- $this->routeStore->reAddTimeoutInfo();
- }
- do{
- //query one record
- //$packet_text = $redis_raw_packet->pop();
-
- global $argv;
- if (count($argv) != 3 || strpos($argv[2], "9c bc") !==0) {
- echo "参数错误";
- exit();
- }
- $packet_text = $argv[2];//'9c bc 01 31 00 cd 00 10 88 88 cd 88 88 ca 01 01 00 5a 54 68 cb 58 00 01';
-
- if(!$packet_text){
- echo 'no raw packet.'.PHP_EOL;
- sleep(1);
- break;
- continue;
- }
- //parse packet
- $packet = tcp_str2bin($packet_text);
- if(!$packet){
- echo 'parse packet failed.'.PHP_EOL;
- decode_error_log('parse_fail','parse packet failed ,packet_text : '.$packet_text);
- break;
- continue;
- }
- $this->import($packet,$packet_text);
- break;
- }while(time()-$start_time<60);
-
-
- }
- /*
- 参数说明:
- cityId:
- data:
- */
- private function TsAddRawPacket($cityId, $data){
- $tableName = 'yiwushi_28_rpacket';//\Jiaruan\ClTableStoreGridTp::getCountyTableName($cityId, "rpacket");
- $model = new OtsModel($tableName);
- $addTime = strtotime($data['AddTime']);
- $stationMac = strtoupper(substr(md5($data['StationCode']), 0, 2)) . '-' . $data['StationCode'] . '-' . date('ymd', $addTime);
- $micro = str_pad(explode(' ', microtime())[0] * 1000000, 6, 0, STR_PAD_LEFT);
- $onlineTime = $addTime . "." . $micro;
- $ret = $model->add(['StationMac' => $stationMac, 'OnlineTime' => $onlineTime, 'PacketData' => $data['PacketData']]);
- if ($ret == false || $ret != 1) {
- echo $model->getError();
- $error = $model->getDbError();
- if (strpos($error, "cURL error 28:") !== false) {//received超时 记录到日志里面 需要重试
-
- } else {
-
- }
- echo $error;
- }
- }
- }
|