123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace catchAdmin\api\service;
- use catchAdmin\tag_history\model\Dormitory;
- class dispose
- {
- private $redis;
- private $stations="stations";
- public function __construct($redis){
- $this->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'];
- //是否参与计算
- $calculate=true;
-
- if($data['rssi1']==$data['rssi2']){
- debug_log("InAndOUT","前后信号相等则进行过滤掉");
- $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 //最新时间
- ];
- }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 //最新时间
- ];
- 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'];
- //状态
- $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","无初始化状态,则进行初始化");
- 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是出
- ];
- $url_data=[
- "mac"=>$data['mac'],
- "data"=>$data_array
- ];
- // debug_log("InAndOUT","得出计算结果:".json_encode($arr));
- debug_log("InAndOUT","发送给远程".json_encode($url_data));
- $url="http://47.114.185.186:8115/api/accessReport";
- $url_res= curl_http_post(json_encode($url_data),$url,false);
- debug_log("InAndOUT","远程返回结果".$url_res);
- debug_log("result","正常计算返回的结果".json_encode($url_data));
- $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
- );
- 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;
- }
- //查询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 temporary_label($redis,$label,$type){
- $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);
- }
- }
- }
|