123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace catchAdmin\api\service;
- use catchAdmin\tag_history\model\Dormitory;
- class dispose
- {
- private $redis;
- 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);
- //保留10秒
- $tem_list=$list;
-
- foreach($tem_list as $tem_item){
- debug_log("InAndOUT","测试".($tem_item['report_time']-$data['report_time']));
- if(($data['report_time']-$tem_item['report_time'])>5){
- debug_log("InAndOUT","最旧的上报时间超过5s剔除一条");
- array_shift($list);
- }
- }
- // if(count($list)>5){
- // debug_log("InAndOUT","数据长度大于5,剔除一条");
- // debug_log("InAndOUT","剔除的一条数据为:".json_encode($list[0]));
- // 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));
- }
- $len01=0;
- $len02=0;
- $len03=0;
- $len04=0;
- $rssi1=0;
- $rssi2=0;
- $rssi3=0;
- $rssi4=0;
- foreach($list as $item){
- if(!empty($config)){
- if($config['front']&&$config['front']>$item["rssi1"]){
- $rssi1+=$item["rssi1"];
- $len01+=1;
- }
- if($config['behind']&&$config['behind']>$item["rssi2"]){
- $rssi2+=$item["rssi2"];
- $len02+=1;
- }
- if($config['left']&&$config['left']>$item["rssi3"]){
- $rssi3+=$item["rssi3"];
- $len03+=1;
- }
- if($config['right']&&$config['right']>$item["rssi4"]){
- $rssi4+=$item["rssi4"];
- $len04+=1;
- }
- }else{
- $rssi1=$rssi1+$item["rssi1"];
- $rssi2=$rssi2+$item["rssi2"];
- $rssi3=$rssi3+$item["rssi3"];
- $rssi4=$rssi4+$item["rssi4"];
- $len01+=1;
- $len02+=1;
- $len03+=1;
- $len04+=1;
- }
-
- }
-
- $data["rssi1"]=number_format($rssi1/$len01,'2');
- $data["rssi2"]=number_format($rssi2/$len02,'2');
- $data["rssi3"]=number_format($rssi3/$len03,'2');
- $data["rssi4"]=number_format($rssi4/$len04,'2');
- 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 computeResout($data){
- //前后左右$arr=[$data['resi1'],$data['resi2'],$data['resi3'],$data['resi4']];
- //前后
- // $redis=$this->redis;
- // $arr=[$data['resi1'],$data['resi2']];
- // sort($arr);
- $res=0;
- if($data['rssi1']==$data['rssi2']){
- debug_log("InAndOUT","前后信号相等则进行过滤掉");
- return;
- }
- if($data['rssi1']<$data['rssi2']){
- $res=1;
- }
- if($data['rssi1']>$data['rssi2']){
- $res=2;
- }
- debug_log("InAndOUT","信号强度 :".$res);
- $hashkey='res'.$data['mac'];
- $key=$data['label'];
- $json_list= $this->selectHash($hashkey,$key);
-
- if(!$json_list){
-
- $arr=[
- "a"=>[['time'=>$data['report_time'],'dirt'=>$res]],
- 'status'=> ['time'=>null,'dirt'=>null]
- ];
- 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($resArray));
- debug_log("InAndOUT","缓存的数据".json_encode($json_list));
- $s=$json_list;
- //数据列表
- $list=$s['a'];
- //状态
- $status=$s['status'];
- //判断时间间隔超过2分钟,则从新计算
- if(($resArray['time']-$list[count($list)-1]['time'])>120){
- //时间超过两分钟,进行重置
- debug_log("InAndOUT","时间超过两分钟,清空数据,载入当前数据");
- $list=[];
- $status= ['time'=>null,'dirt'=>null];
- }
- array_push($list,$resArray);
- // if(count($list)>3){
- // debug_log("InAndOUT","数据超过3条,剔除一条旧数据");
- //array_shift($list);
-
- while(count($list)>5){
- array_shift($list);
- debug_log("InAndOUT","数据超过3条,剔除一条旧数据");
- }
- //}
-
- 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{
- //进行保存
- if($res!=$status['dirt']){
- $arr=[
- 'mac'=> $data['mac'],
- 'label'=>$data['label'],
- "begin_time"=>$status['time'],
- "end_time"=>$resArray['time'],
- 'dirt'=> $status['dirt']==1?1:2
- ];
- $data_array=[];
- $data_array[]=[
- "label"=>$data['label'],
- 'time'=>$resArray['time'],
- 'dirt'=> $status['dirt']==1?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);
- // $dor=new Dormitory();
- // $dor->save($arr);
-
-
- debug_log("InAndOUT","得出计算结果:".json_encode($arr));
- $status['dirt']= $res;
- $status['time']= $resArray['time'];
- }else{
- debug_log("InAndOUT","无计算结果",json_encode($status));
- }
-
- }
- }
- $arr=array(
- "a"=>$list,
- 'status'=>$status
- );
- debug_log("InAndOUT","存入缓存中:".json_encode($arr));
- $this->setHash($hashkey,$key,$arr);
- debug_log("InAndOUT",'操作结束');
- }
- }
- //查询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));
-
- }
- }
|