likang 2 years ago
parent
commit
ebcda64c9a

+ 256 - 0
catch/api/service/dispose.php

@@ -0,0 +1,256 @@
+<?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']);
+       
+        $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","存入数据".$data);
+        }else{
+            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=[];
+                array_push($list,$data);
+            }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);
+                }
+            }  
+
+        }
+        //数据
+        debug_log("InAndOUT","新数据长度 :".json_encode($list));
+        //现在的数据长度
+        $count=count($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中
+        $this->setHash($hashKey,$key,$list);
+        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)>5){
+            debug_log("InAndOUT","数据超过五条,剔除一条旧数据");
+            array_shift($list);
+         }
+         debug_log("InAndOUT","时间超过两分钟,清空数据,载入当前数据");
+         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($resArray));
+                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'],
+                        'dir'=> $status['dirt']==1?1:2
+                    ];
+                   // $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));
+      
+    }
+
+
+
+
+
+
+
+
+
+
+
+
+
+}

+ 104 - 0
catch/tag_history/controller/Dormitory.php

@@ -0,0 +1,104 @@
+<?php
+
+namespace catchAdmin\tag_history\controller;
+
+use catcher\base\CatchRequest as Request;
+use catcher\CatchResponse;
+use catcher\base\CatchController;
+use catchAdmin\tag_history\model\Dormitory as dormitoryModel;
+
+class Dormitory extends CatchController
+{
+    protected $dormitoryModel;
+    private $label=['44010201','44010202'];
+    private $mac=[];
+    
+    public function __construct(DormitoryModel $dormitoryModel)
+    {
+        $this->dormitoryModel = $dormitoryModel;
+    }
+    
+    /**
+     * 列表
+     * @time 2023年04月23日 14:24
+     * @param Request $request 
+     */
+    public function index(Request $request) : \think\Response
+    {
+        $data = $request->get();
+        $time =  isset($data['time']) ? $data['time'] : '';
+        $mac = isset($data['mac']) ? $data['mac'] : '';
+        $tage = isset($data['tage']) ? $data['tage'] : '';
+        $endtime = isset($data['endtime']) ? $data['endtime'] : "";
+        $meter = isset($data['meter']) ? $data['meter'] : "";
+        $where = [];
+         $onewhere=[];
+         if (!empty($time)) {
+            $onewhere[] = ['addTime', '>=', $time];
+        } else {
+            $onewhere[] = ['addTime', '>=', time()];
+        }
+
+        if (!empty($tage)) {
+            $where[] = ['label', 'in',  implode(",", $tage)];   
+        }
+        $list=[];
+        
+        $labelArray=empty($tage) ? $this->label:$tage;
+        foreach($labelArray as $item){
+            $otherWhere=[];
+           // $otherWhere[]=["mac","=",$othermac];
+            $otherWhere[]=["label","=",trim($item)];
+            
+            $t1 = $this->dormitoryModel->where($otherWhere)->order("id","desc")->find();
+            if(empty($t1)){
+              
+                continue;
+            }
+            $t1['report_time'] = date('Y-m-d H:i:s', $t1['report_time']);
+            array_push($list,$t1);
+        }
+        return CatchResponse::success($list);
+    }
+    
+    /**
+     * 保存信息
+     * @time 2023年04月23日 14:24
+     * @param Request $request 
+     */
+    public function save(Request $request) : \think\Response
+    {
+        return CatchResponse::success($this->dormitoryModel->storeBy($request->post()));
+    }
+    
+    /**
+     * 读取
+     * @time 2023年04月23日 14:24
+     * @param $id 
+     */
+    public function read($id) : \think\Response
+    {
+        return CatchResponse::success($this->dormitoryModel->findBy($id));
+    }
+    
+    /**
+     * 更新
+     * @time 2023年04月23日 14:24
+     * @param Request $request 
+     * @param $id
+     */
+    public function update(Request $request, $id) : \think\Response
+    {
+        return CatchResponse::success($this->dormitoryModel->updateBy($id, $request->post()));
+    }
+    
+    /**
+     * 删除
+     * @time 2023年04月23日 14:24
+     * @param $id
+     */
+    public function delete($id) : \think\Response
+    {
+        return CatchResponse::success($this->dormitoryModel->deleteBy($id));
+    }
+}

+ 44 - 0
catch/tag_history/database/migrations/20230423142448_dormitory.php

@@ -0,0 +1,44 @@
+<?php
+
+use think\migration\Migrator;
+use think\migration\db\Column;
+use Phinx\Db\Adapter\MysqlAdapter;
+
+class Dormitory extends Migrator
+{
+    /**
+     * Change Method.
+     *
+     * Write your reversible migrations using this method.
+     *
+     * More information on writing migrations is available here:
+     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
+     *
+     * The following commands can be used in this method and Phinx will
+     * automatically reverse them when rolling back:
+     *
+     *    createTable
+     *    renameTable
+     *    addColumn
+     *    renameColumn
+     *    addIndex
+     *    addForeignKey
+     *
+     * Remember to call "create()" or "update()" and NOT "save()" when working
+     * with the Table class.
+     */
+    public function change()
+    {
+        $table = $this->table('dormitory', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '学生寝室监听' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]);
+        $table->addColumn('mac', 'string', ['limit' => 20,'null' => true,'signed' => true,'comment' => '基站',])
+			->addColumn('label', 'string', ['limit' => 20,'null' => true,'signed' => true,'comment' => '标签',])
+			->addColumn('begin_time', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => true,'signed' => true,'comment' => '开始时间',])
+			->addColumn('end_time', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => true,'signed' => true,'comment' => '结束时间',])
+			->addColumn('dire', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => true,'signed' => true,'comment' => '1 进    2出',])
+			->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => false,'comment' => '创建人ID',])
+			->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => false,'comment' => '创建时间',])
+			->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => false,'comment' => '更新时间',])
+			->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => false,'comment' => '软删除',])
+            ->create();
+    }
+}

+ 33 - 0
catch/tag_history/model/Dormitory.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace catchAdmin\tag_history\model;
+
+use catcher\base\CatchModel as Model;
+
+class Dormitory extends Model
+{
+    // 表名
+    public $name = 'dormitory';
+    // 数据库字段映射
+    public $field = array(
+        'id',
+        // 基站
+        'mac',
+        // 标签
+        'label',
+        // 开始时间
+        'begin_time',
+        // 结束时间
+        'end_time',
+        // 1 进    2出
+        'dire',
+        // 创建人ID
+        'creator_id',
+        // 创建时间
+        'created_at',
+        // 更新时间
+        'updated_at',
+        // 软删除
+        'deleted_at',
+    );
+}