redis=$redis; } // public function computeData($data){ // debug_log("InAndOUT","基站:".$data['mac'] .' label :'.$data['label']); // debug_log("InAndOUT","新数据:".json_encode($data)); // $hashKey=$data['mac'].'station'; // $key=$data['label']; // $list=[]; // $length=0; // $res= $this->selectHash($hashKey,$key); // if($res==false){ // debug_log("InAndOUT","redis没有数据,存入新数据"); // array_push($list,$data); // debug_log("InAndOUT","当前数据队列",json_encode($list)); // }else{ // debug_log("InAndOUT","redis初始数据 :".json_encode($res)); // $list=$res; // debug_log("InAndOUT","上次上报的数据 :".json_encode($list[count($list)-1],true)); // // if(($data['report_time']-$list[count($list)-1]['report_time'])>=120){ // // debug_log("InAndOUT","上报次数间隔超过120s,清空redis缓存,从新存入数据"); // // $list=[]; // // array_push($list,$data); // // }else{ // $list=$res; // array_push($list,$data); // //按照个数 // while(count($list)>5){ // array_shift($list); // } // // } // } // //数据 // $count=count($list); // debug_log("InAndOUT","当前数据队列 :".json_encode($list)); // debug_log("InAndOUT","新数据长度 :".$count); // //现在的数据长度 // //获取配置 // $config= $this->selectHash("anbang_four_wire",$data['mac']); // if(empty($config)){ // debug_log("InAndOUT","没有配置"); // }else{ // debug_log("InAndOUT","配置参数为:".json_encode($config)); // } // $sortlist= $this->sortlist($list); // $data["rssi1"]=$sortlist[0]; // $data["rssi2"]=$sortlist[1]; // $data["rssi3"]=$sortlist[2]; // $data["rssi4"]=$sortlist[3]; // debug_log("InAndOUT","计算出的平均数据 :".json_encode($data)); // //保存到hash中 // debug_log("InAndOUT","存入redis缓存的数据 :".json_encode($list)); // $this->setHash($hashKey,$key,$list); // debug_log("InAndOUT","======================================="); // return $data; // } public function check_data($data){ $this->setStations($data['mac']); $res=''; $hashKey=$data['mac'].'station'; $key=$data['label']; $old_data=$this->selectHash($hashKey,$key); if($data["rssi1"]==90&&!empty($old_data)){ if($old_data["rssi1"]<90){ $data["rssi1"]=$old_data["rssi1"]+8; $data["rssi1"]= $data["rssi1"]>90 ? 90:$data["rssi1"]; $res=$res."rssi1 "; } } if($data["rssi2"]==90&&!empty($old_data)){ if($old_data["rssi2"]<90){ $data["rssi2"]=$old_data["rssi2"]+8; $data["rssi2"]= $data["rssi2"]>90 ? 90:$data["rssi2"]; $res=$res."rssi2"; } } $this->setHash($hashKey,$key,$data); return $data; } //计算结果设计 public function computeResout($data){ debug_log("InAndOUT","======================================="); debug_log("InAndOUT",'接收新数据'.json_encode($data)); debug_log("InAndOUT","======================================="); $res=0; //进出 $hashkey='res'.$data['mac']; $key=$data['label']; //存入redis的数据 $json_list= $this->selectHash($hashkey,$key); //最新时间 $time=$data['report_time']; //进出结果 $inAndOut=0; //是否参与计算 $calculate=true; if($data['rssi1']==$data['rssi2']){ $calculate=false; } $t1s= $data['rssi1']-$data['rssi2']; if(abs($t1s)<3){ debug_log("InAndOUT","差值小于3不做计算"); $calculate=false; } if($calculate==false){ if(!$json_list){ $arr=[ "a"=>[], 'status'=> ['time'=>null,'dirt'=>null], "time"=>$time, //最新时间 "in_and_out"=>0 //进出结果 ]; }else{ $json_list['time']=$time; } $this->setHash($hashkey,$key,$json_list); return; } if($data['rssi1']<$data['rssi2']){ $res=1; } if($data['rssi1']>$data['rssi2']){ $res=2; } if(!$json_list){ $arr=[ "a"=>[['time'=>$time,'dirt'=>$res]], 'status'=> ['time'=>null,'dirt'=>null], "time"=>$time, //最新时间 "in_and_out"=>0 ]; debug_log("InAndOUT","数据不存在,直接存储新数据".json_encode($arr)); $this->setHash($hashkey,$key,$arr); return; }else{ $resArray=['time'=>$data['report_time'],'dirt'=>$res]; debug_log("InAndOUT","缓存的数据".json_encode($json_list)); $s=$json_list; //数据列表 $list=$s['a']; //进出结果 $inAndOut=$s["in_and_out"]; //状态 $status=$s['status']; array_push($list,$resArray); while(count($list)>5){ array_shift($list); debug_log("InAndOUT","数据超过5条,剔除一条旧数据"); } debug_log("InAndOUT","当前队列数据:".json_encode($list)); $front=0; $back=0; if(count($list)>2){ // debug_log("InAndOUT","当前队列超过两条可以计算前后:".count($list)); //校验权柄 foreach($list as $item){ if($item['dirt']==1){ $front+=1; } if($item['dirt']==2){ $back+=1; } } $res=$front>$back?1:2; if(empty($status['dirt'])){ $status['dirt']= $res; $status['time']= $resArray['time']; debug_log("InAndOUT","初始化状态为:".json_encode($status)); }else{ //判断是否连贯 $IS_OK=true; $res=0; $reverse=array_reverse($list); $res=$reverse[0]['dirt']; foreach($reverse as $aitem){ if($aitem['dirt']!=$res){ $IS_OK=false; } } // if(($reverse[0]['dirt']==$reverse[1]['dirt'])&&($reverse[1]['dirt']==$reverse[2]['dirt'])){ // $res=$reverse[0]['dirt']; // $IS_OK=true; // } //进行保存 if($res!=$status['dirt']&&$IS_OK){ // if(!empty($status['status']['dirt'])){ // if($resArray['time']-$status['status']['time']>5){ $data_array=[]; $data_array[]=[ "label"=>$data['label'], 'time'=>$resArray['time'], 'dirt'=> $status['dirt']==1?1:2 //1是进 2是出 ]; $inAndOut=$status['dirt']==1?1:2; $url_data=[ "mac"=>$data['mac'], "data"=>$data_array ]; $this->getRemoteData($url_data); //清除缓存数据 $this->get_label_history(3,$data['mac'],$data['label']); $this->temporary_label($this->redis,$data['label'],2); $status['dirt']=$res; $status['time']=$time; }else{ debug_log("InAndOUT","无计算结果",json_encode($status)); } } } $arr=array( "a"=>$list, 'status'=>$status, "time"=>$time, "in_and_out"=>$inAndOut ); debug_log("InAndOUT","存入缓存中:".json_encode($arr)); $this->setHash($hashkey,$key,$arr); debug_log("InAndOUT",'操作结束'); } } //获取所有基站 public function getStations(){ $key=$this->stations; $list= $this->redis->hKeys($key); return $list; } //存入基站号 public function setStations($Stations){ $key=$this->stations; if(!$this->redis->hExists($key,$Stations)){ $this->redis->hSet($key,$Stations,0); } } //排序 public function sortlist($list){ $list01=[]; $list02=[]; $list03=[]; $list04=[]; debug_log("InAndOUT","排序的数据:".json_encode($list)); foreach($list as $item){ array_push($list01,$item['rssi1']); array_push($list02,$item['rssi2']); array_push($list03,$item['rssi3']); array_push($list04,$item['rssi4']); } if(count($list)>=3){ sort($list01); sort($list02); sort($list03); sort($list04); array_pop($list01); array_pop($list02); array_pop($list03); array_pop($list04); array_shift($list01); array_shift($list02); array_shift($list03); array_shift($list04); } debug_log("InAndOUT","排序后的队列:".json_encode($list01)); debug_log("InAndOUT","排序后的队列:".json_encode($list02)); debug_log("InAndOUT","排序后的队列:".json_encode($list03)); debug_log("InAndOUT","排序后的队列:".json_encode($list04)); $rssi1= array_sum($list01)/count($list01); $rssi2= array_sum($list02)/count($list02); $rssi3= array_sum($list03)/count($list03); $rssi4= array_sum($list04)/count($list04); $res=[$rssi1,$rssi2,$rssi3,$rssi4]; debug_log("InAndOUT","结算后的结果:".json_encode($res)); return $res; } /** * 获取远程推送的数据 * * @param [type] $url * @param [type] $data * @return void */ public function getRemoteData($data){ $key="push_data"; $redis=$this->redis; $redis->Rpush($key,json_encode($data)); } /** * 历史标签信号 * * @return void */ public function set_label_history($data){ $MAC=$data["mac"]; $key=$MAC."_label_history"; $head=$data['label']."_head"; $tail=$data["label"]."_tail"; //保存头部 $head_data= $this->selectHash($key,$head); $tail_data=$this->selectHash($key,$tail); debug_log("InAndOUT","最旧信号间隔-".($data["report_time"]-$tail_data[count($tail_data)-1]["report_time"])); debug_log("InAndOUT","最旧信号间隔--".json_encode($tail_data)); debug_log("InAndOUT","最旧信号间隔---".json_encode($tail_data[count($tail_data)-1])); debug_log("InAndOUT","最旧信号间隔----".$data["report_time"]); if(!empty($tail_data)){ if(($data["report_time"]-$tail_data[count($tail_data)-1]["report_time"])>6){ $head_data=[]; $tail_data=[]; } } if(empty($head_data)||count($head_data)<3){ $head_data=empty($head_data)?[]: $head_data; array_push($head_data,$data); $this->setHash($key,$head,$head_data); } //保存尾部标签数据 $tail_data=empty($tail_data)?[]: $tail_data; array_push($tail_data,$data); while(count($tail_data)>3){ array_shift($tail_data); } $this->setHash($key,$tail,$tail_data); } /** * 获取基站 * * @param [type] $type 1 头部 2 尾部 3清空 * @param [type] $mac * @param [type] $label * @return void */ public function get_label_history($type,$mac,$label){ $key=$mac."_label_history"; $head=$label."_head"; $tail=$label."_tail"; if($type==1){ $head_data= $this->selectHash($key,$head); return $head_data; } if($type==2){ $tail_data= $this->selectHash($key,$tail); return $tail_data; } if($type==3){ debug_log("InAndOUT","清空头部和尾部历史:".$mac." ".$label); $tail_data= $this->delHash($key,$head); $tail_data= $this->delHash($key,$tail); } } /** * 二次生成进出 * * @return void */ public function second_create_direction($mac,$label){ debug_log("second_dirt","==============mac:".$mac."=======label".$label."================================"); $key=$mac."_label_history"; $head=$label."_head"; $tail=$label."_tail"; $head_dir=0; $tail_dir=0; $front=0; $behind=0; $res=0; $time=0; $head_data= $this->get_label_history(1,$mac,$label); $tail_data= $this->get_label_history(2,$mac,$label); debug_log("second_dirt","头部标签数据".json_encode($head_data)); debug_log("second_dirt","尾部标签数据".json_encode($tail_data)); if(empty($head_data)){ return false; } if(empty($tail_data)){ return false; } if(count($head_data)<3){ return false; } foreach($head_data as $item){ if($item["rssi1"]<$item["rssi2"]){ $front+=1; }else{ $behind+=1; } } $head_dir=$front>$behind?1:2; $front=0; $behind=0; foreach($tail_data as $item){ if($item["rssi1"]<$item["rssi2"]){ $front+=1; }else{ $behind+=1; } $time=$item['report_time']; } $tail_dir=$front>$behind?1:2; if($tail_dir== $head_dir){ return false; } if($head_dir==1){ $res=1; }else{ $res=2; } debug_log("second_dirt","最终结果".json_encode(["dirt"=>$res,"time"=>$time])); return ["dirt"=>$res,"time"=>$time]; } //查询key hash public function selectHash($hashKey,$key){ $res= $this->redis->hGet($hashKey,$key); if(empty($res)){ return false; }else{ return json_decode($res,true); } } public function setHash($hashKey,$key,$data){ $this->redis->hSet($hashKey,$key,json_encode($data)); } public function delHash($hashKey,$key){ $this->redis->Hdel($hashKey,$key); } public function temporary_label($redis,$label,$type){ return; $key="temporary_label"; if($type==1){ $res= $redis->hGet($key,$label); if($res==false){ $redis->hSet($key,$label,1); } }else{ $redis->hSet($key,$label,2); } } /** * 解析日志 */ public function analysisLog(){ $path=app()->getRootPath()."runtime/log/2023-05-15/label_log.log"; var_dump($path); var_dump(is_file($path)); $file = fopen($path, "rb"); debug_log("in_label","开始解析标签"); $array=[]; $readCvs=function($file){ while (feof($file)===false) { # code... yield fgets($file); } fclose($file); }; debug_log("in_label","解析运行中"); foreach ($readCvs($file) as $data) { if(strstr($data,"xsj")){ $arr= explode("{",$data); $data= json_decode("{".trim($arr[1]),true); debug_log("in_label",$data); if(!in_array($data['label'],$array)&&$data["rssi1"]>$data["rssi2"]){ array_push($array,$data['label']); } } } foreach($array as $item){ debug_log("in_label",$item); } debug_log("in_label","标签总数".count($array)); } }