Bläddra i källkod

Merge branch 'master' of http://gogs.renlianiot.com:4000/zmcoding/station-test-api

nana_sen 1 år sedan
förälder
incheckning
ff0a145b1a

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 136 - 43
catch/api/controller/Api.php


+ 6 - 1
catch/api/route.php

@@ -5,7 +5,7 @@
  * @Author: likang
  * @Date: 2022-06-09 10:11:32
  * @LastEditors: likang 1186820806@qq.com
- * @LastEditTime: 2023-05-10 09:08:04
+ * @LastEditTime: 2023-05-19 15:32:33
  */
 // +----------------------------------------------------------------------
 // | CatchAdmin [Just Like ~ ]
@@ -27,8 +27,13 @@ $router->group(function () use ($router) {
 $router->post('api/report', '\catchAdmin\api\controller\Api@report');
 $router->get('api/deleteHistory', '\catchAdmin\api\controller\Api@deleteHistory');
 $router->get('api/test', '\catchAdmin\api\controller\Api@test');
+//推送数据
 $router->get('api/redis', '\catchAdmin\api\controller\Api@redis_to_mysql');
 $router->get('api/clear_redis', '\catchAdmin\api\controller\Api@clear_redis');
+//远程推送数据
+$router->get('api/remote_push', '\catchAdmin\api\controller\Api@remotePush');
+
+
 //打印日志
 $router->get('api/temporary_label_log', '\catchAdmin\api\controller\Api@temporary_label_log');
 $router->get('api/redis_parea', '\catchAdmin\api\controller\Api@redis_to_mysql_parea');

+ 189 - 32
catch/api/service/dispose.php

@@ -125,6 +125,8 @@ class dispose
         debug_log("InAndOUT",'接收新数据'.json_encode($data));   
         debug_log("InAndOUT","=======================================");    
        $res=0;
+       //进出
+    
 
        $hashkey='res'.$data['mac'];
        $key=$data['label'];
@@ -132,18 +134,18 @@ class dispose
        $json_list= $this->selectHash($hashkey,$key);
        //最新时间
        $time=$data['report_time'];
+       //进出结果
+       $inAndOut=0;
        //是否参与计算
        $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;
+          $calculate=false;
        }
 
        if($calculate==false){
@@ -151,11 +153,11 @@ class dispose
                 $arr=[
                     "a"=>[],
                     'status'=> ['time'=>null,'dirt'=>null],
-                    "time"=>$time //最新时间
+                    "time"=>$time, //最新时间 
+                    "in_and_out"=>0   //进出结果
                 ];   
             }else{
                 $json_list['time']=$time;
-               
             }
             $this->setHash($hashkey,$key,$json_list);
             return;
@@ -174,18 +176,23 @@ class dispose
             $arr=[
                 "a"=>[['time'=>$time,'dirt'=>$res]],
                 'status'=> ['time'=>null,'dirt'=>null],
-                "time"=>$time //最新时间
+                "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){
@@ -209,12 +216,9 @@ class dispose
                 }
                  $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{
 
@@ -225,7 +229,7 @@ class dispose
                $res=$reverse[0]['dirt'];
                foreach($reverse as $aitem){
                    if($aitem['dirt']!=$res){
-                     $IS_OK=false;
+                      $IS_OK=false;
                    } 
                }
             //    if(($reverse[0]['dirt']==$reverse[1]['dirt'])&&($reverse[1]['dirt']==$reverse[2]['dirt'])){
@@ -246,18 +250,15 @@ class dispose
                                 '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
                             ];
-                            // 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);
+                            $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;
 
@@ -272,15 +273,18 @@ class dispose
             $arr=array(
                 "a"=>$list,
                 'status'=>$status,
-                "time"=>$time
+                "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;
@@ -336,6 +340,157 @@ class dispose
         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];
+
+}
+
+
 
 
 
@@ -354,6 +509,9 @@ class dispose
     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){
@@ -372,7 +530,9 @@ class dispose
      * 解析日志
      */
     public function analysisLog(){
-        $path="D:\公司项目\测试基站\station-test-api\runtime\log\2023-05-15\label_log.log";
+        $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=[];
@@ -386,12 +546,14 @@ class dispose
         };  
         debug_log("in_label","解析运行中");
 
-    foreach ($readCvs($file) as $key=>$data) {
-        debug_log("in_label",$data);
+    foreach ($readCvs($file) as $data) {
+        
         if(strstr($data,"xsj")){
-            $arr=  explode("xsj:",$data);
-            $data= json_decode(trim($arr[1]),true);
-            debug_log("in_label",trim($arr[1]));
+           
+            $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']);
             }
@@ -406,11 +568,6 @@ class dispose
         }
         debug_log("in_label","标签总数".count($array));
           
-       
-
-
-
-
 
 
     }

+ 6 - 1
catch/tag_history/controller/TagHistory.php

@@ -50,6 +50,8 @@ class TagHistory extends CatchController
         $cs_store = Db::name('user_has_roles')->where('uid', $create_id)->where('role_id', 8)->find();
         //公司测试猪耳标
         $companyPigEarTest = Db::name('user_has_roles')->where('uid', $create_id)->where('role_id', 9)->find();
+        //公司测试
+        $companyTest2 = Db::name('user_has_roles')->where('uid', $create_id)->where('role_id', 10)->find();
 
 
 
@@ -147,9 +149,12 @@ class TagHistory extends CatchController
             ,00000098BDA9
             ,00000098BDA7
             "];
+        }elseif($companyTest2){
+            $where[] = ['mac', 'in', '30B5f1013B55'];
+            $where[] = ['lable', 'in', "00000098C145"];
         }
 
-        if(false){
+        if(true){
 
         
         $list = $this->tagHistoryModel->group('mac,lable')->where('addTime','>',strtotime("-10 minutes"))->where($where)