likang 2 years ago
parent
commit
a2be23e747
3 changed files with 120 additions and 45 deletions
  1. 46 15
      catch/api/controller/Api.php
  2. 69 28
      catch/api/service/dispose.php
  3. 5 2
      task_script/RL4RSSI_MQTT_CLIENT.php

+ 46 - 15
catch/api/controller/Api.php

@@ -58,7 +58,14 @@ class Api extends CatchController
        
         $ues_redis=Cache::store('redis')->handler();
         $text=null;
-      
+        $testArray=[
+            '884619516',
+            '885907612',
+            '3827256674',
+            '885833308',
+            '884019740',
+            '884765612'
+        ];
         while(1){
             $jsonData= $ues_redis->rpop("mqtt_data");
             debug_log("InAndOUT",'数据redis'.$jsonData);
@@ -80,6 +87,7 @@ class Api extends CatchController
             if(strlen($text)<24){
                 continue;
             }
+           // $arr=['']
             while(strlen($text)>=24){
      
                 $da=substr($text,0,24);
@@ -97,14 +105,21 @@ class Api extends CatchController
                      $DA=[
                         'mac' => $mac,
                         'label' =>$label,
-                        'rssi1' => hexdec($rssi1),
-                        'rssi2' => hexdec($rssi2),
-                        'rssi3' => hexdec($rssi3),
+                        'rssi1' => hexdec($rssi2),
+                        'rssi2' => hexdec($rssi3),
+                        'rssi3' => hexdec($rssi1),
                         'rssi4' => hexdec($rssi4),
                         'report_time'=>$time
                     ];
-
-                    debug_log("InAndOUT","数据:".json_encode($DA,true));
+                    // if($DA['rssi1']==255){
+                    //     $DA['rssi1']=100;
+                    // }
+                    // if($DA['rssi2']==255){
+                    //     $DA['rssi2']=100;
+                    // }
+                   
+
+                   // debug_log("InAndOUT","数据:".json_encode($DA,true));
                     //保存计算中
                     //分为8个桶
                     // $num=Hexdec($label);
@@ -112,16 +127,32 @@ class Api extends CatchController
                     // $key="buckets".$Identification;
                     // $ues_redis->rPush($key,json_encode($DA,true));
                     
-                    try{
-                        $dispose = new dispose($ues_redis);
-                        $computedata=$dispose->computeData($DA);
-                         debug_log("InAndOUT","计算完成的数据:".json_encode($DA,true));
-                         $dispose->computeResout($computedata);
-                        
-                    }catch(Exception $e){
-                        debug_log("InAndOUT","抛出异常".json_encode($DA,true));
+                    $num=Hexdec($label);
+                    $vs=false;
+                    if($DA['rssi1']<255&&$DA['rssi2']<255){
+                        $vs=true;
                     }
-                    
+
+                 
+                     if(in_array($num,$testArray)&&$vs&&(substr($da,8,2)=='01')){
+                        
+                        $testData=$DA;
+                        $testData['num']=$num;
+                        debug_log("label_log",json_encode($testData));
+                        debug_log('label_log',$da);
+                        try{
+                            $dispose = new dispose($ues_redis);
+                            $computedata=$dispose->computeData($DA);
+                             //debug_log("InAndOUT","计算完成的数据:".json_encode($computedata,true));
+                             $dispose->computeResout($computedata);
+                            
+                        }catch(Exception $e){
+                            debug_log("InAndOUT","抛出异常".json_encode($DA,true));
+                        }
+                        
+
+                     }
+                   
                      $text=substr($text,24);
                 }else{
                   $text=substr($text,1);

+ 69 - 28
catch/api/service/dispose.php

@@ -14,7 +14,7 @@ class dispose
     
     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=[];
@@ -22,14 +22,15 @@ class dispose
         $res= $this->selectHash($hashKey,$key);
        
         if($res==false){
-            debug_log("InAndOUT","redis没有数据");
+            debug_log("InAndOUT","redis没有数据,存入新数据");
             array_push($list,$data);
-            debug_log("InAndOUT","存入数据".$data);
+            debug_log("InAndOUT","当前数据队列",json_encode($list));
+            
         }else{
-            debug_log("InAndOUT","redis初始数据",json_encode($res));
+            debug_log("InAndOUT","redis初始数据 :".json_encode($res));
             $list=$res;
             debug_log("InAndOUT","上次上报的数据 :".json_encode($list[count($list)-1],true));
-            debug_log("InAndOUT","存入数据".$data);
+           
             if(($data['report_time']-$list[count($list)-1]['report_time'])>=120){
                 debug_log("InAndOUT","上报次数间隔超过120s,清空redis缓存,从新存入数据");
                 $list=[];
@@ -37,19 +38,33 @@ class dispose
             }else{
                 $list=$res;
                 array_push($list,$data);
-                if(count($list)>5){
-                    debug_log("InAndOUT","数据长度大于5,剔除一条");
-                    debug_log("InAndOUT","剔除的一条数据为:".json_encode($list[0]));
-                    array_shift($list);
+                //保留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);
+                // }
             }  
 
         }
         //数据
-        debug_log("InAndOUT","新数据长度 :".json_encode($list));
-        //现在的数据长度
         $count=count($list);
+        debug_log("InAndOUT","当前数据队列 :".json_encode($list));
         debug_log("InAndOUT","新数据长度 :".$count);
+        //现在的数据长度
+      
         //获取配置
         $config= $this->selectHash("anbang_four_wire",$data['mac']);
         
@@ -69,7 +84,7 @@ class dispose
         $rssi3=0;
         $rssi4=0;
         foreach($list as $item){
-            if(!empty($config)){
+            if(!empty($config)){    
                 if($config['front']&&$config['front']>$item["rssi1"]){
                     $rssi1+=$item["rssi1"];
                     $len01+=1;
@@ -99,13 +114,16 @@ class dispose
             }
             
         }
+        
         $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;
     }
     //计算结果设计
@@ -134,8 +152,6 @@ class dispose
        $key=$data['label'];
        $json_list= $this->selectHash($hashkey,$key);
       
-    
-
 
        if(!$json_list){
        
@@ -145,8 +161,7 @@ class dispose
             ];
             debug_log("InAndOUT","数据不存在,直接存储新数据".json_encode($arr));
            $this->setHash($hashkey,$key,$arr);
-            
-         
+
             return;
        }else{
 
@@ -166,12 +181,17 @@ class dispose
             $status= ['time'=>null,'dirt'=>null];
          }
          array_push($list,$resArray);
-         if(count($list)>5){
-            debug_log("InAndOUT","数据超过五条,剔除一条旧数据");
-            array_shift($list);
-         }
-         debug_log("InAndOUT","时间超过两分钟,清空数据,载入当前数据");
-         debug_log("InAndOUT","当前队列数据",json_encode($list));
+        //  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){
@@ -186,13 +206,14 @@ class dispose
                         $back+=1;
                     }
                 }
-                 $res=$front>=$back?1:2;
+             
+                 $res=$front>$back?1:2;
+
             if(empty($status['dirt'])){
                 $status['dirt']= $res;
                 $status['time']= $resArray['time'];
                 debug_log("InAndOUT","无初始化状态,则进行初始化");
-                debug_log("InAndOUT","初始化数据为:".json_encode($resArray));
-                debug_log("InAndOUT","初始化数据为:".json_encode($status));
+                debug_log("InAndOUT","初始化状态为:".json_encode($status));
             }else{
                 //进行保存
                 if($res!=$status['dirt']){
@@ -201,13 +222,33 @@ class dispose
                         'label'=>$data['label'],
                         "begin_time"=>$status['time'],
                         "end_time"=>$resArray['time'],
-                        'dir'=> $status['dirt']==1?1:2
+                        '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));
+                   
+                    
+                   debug_log("InAndOUT","得出计算结果:".json_encode($arr));
                     $status['dirt']= $res;
                     $status['time']= $resArray['time'];
+
                 }else{
                     debug_log("InAndOUT","无计算结果",json_encode($status));
                 }
@@ -219,7 +260,7 @@ class dispose
                 "a"=>$list,
                 'status'=>$status
             );
-            debug_log("InAndOUT","存入缓存中",json_encode($arr));
+            debug_log("InAndOUT","存入缓存中:".json_encode($arr));
 
             $this->setHash($hashkey,$key,$arr);
             debug_log("InAndOUT",'操作结束');

+ 5 - 2
task_script/RL4RSSI_MQTT_CLIENT.php

@@ -302,7 +302,7 @@ function loop()
 {
     $server   = 'develop.rltest.cn';
     $port     = 1883;
-    $clientId = 'mqttx_test12312';
+    $clientId = 'mqttx_test17837032683';
     $username = 'rl517';
     $password = "rlian2022";
     $clean_session = true;
@@ -346,7 +346,10 @@ function loop()
     //终端上报系统信息数据
     $mqtt->subscribe('RL4RSSI/rfidinfos', function ($topic, $message) {
         rlog("INFO", 'recv', $topic, $message);
-        if(!empty($message)){
+        $data=json_decode($message,true);
+
+        if((!empty($message))&&(!empty($data['cnt']))){
+
             mqttToRedis($message);
         }