Parcourir la source

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

nana_sen il y a 1 an
Parent
commit
5af26d4aff
2 fichiers modifiés avec 109 ajouts et 1 suppressions
  1. 108 1
      catch/api/controller/Api.php
  2. 1 0
      catch/api/route.php

+ 108 - 1
catch/api/controller/Api.php

@@ -168,7 +168,114 @@ class Api extends CatchController
   
 
     }
+    //储存数据到redis
+    public function redis_to_mysql2(){
+
+        $redis=Cache::store('redis')->handler();
+        // $dispose = new dispose($redis); 
+        $text=null;
+       
+      
+        while(1){
+            $jsonData= $redis->rpop("mqtt_data");
+            if(empty($jsonData)){
+                sleep(1);
+                continue;
+            }
+            debug_log("920_redis_data",'数据redis'.$jsonData);
+            //{"devId":"637405","time":1691596816,"imei":"863488051843244","cnt":"FFFFFFFFFF00002CFF0000032000005DD9E37D01FFFFFF4EFFFFFF5DDADB3D01FFFFFFFF4EFFFF5DDDFF1D01FFFFFF4FFFFFFFA7"}
+            $data=json_decode($jsonData,true);
+            $time=$data['time'];
+            $mac=$data['devId'];
+
+            $list=[];
+            $text=$text.$data['cnt'];//FFFFFFFFFF00002CFF0000032000005DD9E37D01FFFFFF4EFFFFFF5DDADB3D01FFFFFFFF4EFFFF5DDDFF1D01FFFFFF4FFFFFFFA7
+            
+            if(strlen($text)<24){
+                continue;
+            }
+            $redis=Cache::store('redis')->handler();
+            $received_state=$redis->hget('api_receiver_asset_state', 'accessClassReport');
+            while(strlen($text)>=24){
+     
+                $str=substr($text,0,24);//FFFFFFFFFF00002CFF000003
 
+                $t1 = substr($str,8,2)=='00'||substr($str,8,2)=='10'||substr($str,8,2)=='01' ? true:false;
+                $t2 =substr($str,10,6)=="FFFFFF"||substr($str,10,6)=="000000" ?true:false;
+               
+                if($t1&&$t2){
+                     
+                     $label= substr($str,0,8);
+                     $rssi1= substr($str,16,2);
+                     $rssi2= substr($str,18,2);
+                     $rssi3= substr($str,20,2);
+                     $rssi4= substr($str,22,2);
+                     $report_data=[
+                        'mac' => $mac,
+                        'label' =>$label,
+                        'rssi1' => hexdec($rssi1),
+                        'rssi2' => hexdec($rssi2),
+                        'rssi3' => hexdec($rssi3),
+                        'rssi4' => hexdec($rssi4),
+                        'report_time'=>$time
+                    ];
+                    if($received_state=='1'){
+                       //如果开启盘点 则有数据就推送
+                       $url="http://localhost:8115/api/assetReceiveReport";
+                       $url_res= curl_http_post($report_data,$url,false);
+                    }
+                    if($report_data['rssi1']==255 && $report_data['rssi2']!=255){
+                        $report_data['loc']=2;
+                    }elseif($report_data['rssi1']!=255 && $report_data['rssi2']==255){
+                        $report_data['loc']=1;
+                    }
+                    $hashKey='station_access_data';
+                    $key=$mac.'_'.$label;
+                    $old_data=json_decode($redis->hGet($hashKey,$key),true);
+                    if($$old_data){
+                        //取出历史数据 对比两次数据变化
+                        if($old_data['loc']==$report_data['loc']){
+                            //相同 则更新时间
+                            $redis->hSet($hashKey,$key,json_encode($report_data));
+                        }else{
+                            //比较数据时间
+                            //默认新数据更晚上报
+                            $send_data=[
+                                "mac"=>$mac,
+                                "label"=>$label,
+                                'time'=>$time,
+                            ];
+
+                            if($report_data['loc']>$old_data['loc']){
+                                //1->2 内到外 出
+                                $send_data['dirt']=2;
+                            }else{
+                                //2>1  外到内 进
+                                $send_data['dirt']=1;
+                            }
+                        
+                            //推送数据
+                            $url="http://localhost:8115/api/assetReport";
+                            $url_res= curl_http_post($report_data,$url,false);
+                            
+                        }
+                        
+                    }else{
+                        $redis->hSet($hashKey,$key,json_encode($report_data));
+                    }
+
+                    $text=substr($text,24);
+                }else{
+                     $text=substr($text,1);
+                }
+         
+             }
+         
+           
+        }
+  
+
+    }
     /**
      * 清除数据
      *
@@ -377,7 +484,7 @@ class Api extends CatchController
 
     }
 
-
+    
     public function redis_to_mysql_parea(){
 
         $ues_redis=Cache::store('redis')->handler();

+ 1 - 0
catch/api/route.php

@@ -29,6 +29,7 @@ $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/redis2', '\catchAdmin\api\controller\Api@redis_to_mysql2');
 $router->get('api/clear_redis', '\catchAdmin\api\controller\Api@clear_redis');
 //远程推送数据
 $router->get('api/remote_push', '\catchAdmin\api\controller\Api@remotePush');