123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- namespace catchAdmin\api\controller;
- use catchAdmin\api\service\dispose;
- use catchAdmin\tag_history\model\Access;
- use catchAdmin\tag_history\model\Dormitory;
- use catchAdmin\tag_history\model\TagHistory;
- use catchAdmin\worklocation\model\WorkRecord as ModelWorkRecord;
- use catcher\base\CatchRequest as Request;
- use catcher\CatchResponse;
- use catcher\base\CatchController;
- use Exception;
- use think\facade\Cache;
- use \think\facade\Db;
- error_reporting(0);
- class Api extends CatchController
- {
- public function report()
- {
- $token = $_GET['token'];
- if (!$token || $token != '22723927C') {
- json_fail('缺少token或者token不对');
- }
- $param = json_decode(optimize_json(file_get_contents("php://input")), true);
- debug_log("上报数据",json_encode($param));
- if (json_last_error() != 0) {
- json_fail('解析异常', json_last_error_msg());
- }
- $mac = $param['mac'];
- if (empty($mac)) {
- json_fail('mac地址不存在');
- }
- $data = [];
- foreach ($param['list'] as $item) {
- // $onetime= TagHistory::where("mac",$mac)->where('lable',$item['label'])->order("id","desc")->value("time");
- $data[] = [
- 'mac' => $mac,
- 'lable' => $item['label'],
- 'rssi' => $item['rssi'],
- 'time' => $item['time'],
- 'addTime' => time(),
- 'move' => $item['move'],
- 'step' => empty($item['step']) ? 0 : $item['step'],
- 'temperature' => empty($item['temperature']) ? 0 : $item['temperature'],
- //告警
- 'temperWarn' => empty($item['temperWarn']) ? 00 : $item['temperWarn'],
- // "interval_time"=>empty($onetime) ?0:($item['time']-$onetime)
- ];
- }
- $tage = new TagHistory();
- $tage->saveAll($data);
- json_success('上传成功');
- }
- //储存数据到redis
- public function redis_to_mysql(){
- $ues_redis=Cache::store('redis')->handler();
- $text=null;
- $testArray=[
- '52A51239','52A50A09','52A4B3F9','52A53899','52A4FA19','52A56489','52A4B839','52A535C9','2E2C17B2','52A4F279','52A4B879','2E37D1A2','52A57409','2E2C2802','52A52649','52A55059','52A519F9','52A4B109','52A54539','52A4CB09','52A55839','52A56C59','52A4E059','52A51979','52A55FB9','52A4E849','2E2C1FE2','52A4EFC9','2E245942','2E40F632','2E25C912','52A52969','52A4B8F9','52A53D59','52A4E7C9','52A52E09','52A54D49','52A4D059','52A4AC59','52A519B9','52A50719','52A54D09','2E2EE362','52A4C389','52A4D7D9','2E3AE732','2E2137B2','52A56C19','52A58439','52A511C9'
- ];
-
- while(1){
- $jsonData= $ues_redis->rpop("mqtt_data");
- debug_log("InAndOUT",'数据redis'.$jsonData);
-
- if(empty($jsonData)){
- debug_log("InAndOUT",'没有数据');
- sleep(1);
- continue;
-
- }
- $data=json_decode($jsonData,true);
- $time=$data['time'];
- $mac=$data['devId'];
- $list=[];
- $text=$text.$data['cnt'];
-
- if(strlen($text)<24){
- continue;
- }
-
- while(strlen($text)>=24){
-
- $da=substr($text,0,24);
- $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
- $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
-
- if($t1&&$t2){
-
- $label= substr($da,0,8);
- $rssi1= substr($da,16,2);
- $rssi2= substr($da,18,2);
- $rssi3= substr($da,20,2);
- $rssi4= substr($da,22,2);
- $DA=[
- 'mac' => $mac,
- 'label' =>$label,
- 'rssi1' => hexdec($rssi1),
- 'rssi2' => hexdec($rssi2),
- 'rssi3' => hexdec($rssi3),
- 'rssi4' => hexdec($rssi4),
- 'report_time'=>$time
- ];
-
-
-
- $num=$label;
- $vs=false;
- $DA['num']=$num;
- $DA['other_time']=date("Y-m-d H:i:s",$time);
- if($DA['rssi1']<=75||$DA['rssi2']<=75||$DA['rssi3']<=75||$DA['rssi4']<=75){
- $vs=true;
- }else{
- debug_log("label_log","delete:".json_encode($DA));
-
- }
- $this->temporary_label($ues_redis,$label,1);
- //in_array($num,$testArray)&&
-
- if($vs&&(substr($da,8,2)=='01')){
- if($DA['rssi1']==255){
- $DA['rssi1']=90;
- }
- if($DA['rssi2']==255){
- $DA['rssi2']=90;
- }
- if($DA['rssi3']==255){
- $DA['rssi3']=90;
- }
- if($DA['rssi4']==255){
- $DA['rssi4']=90;
- }
- debug_log("label_log","cssh: ".json_encode($DA));
- $DA["rssi1"]=$DA["rssi1"]<=$DA["rssi3"]?$DA["rssi1"]:$DA["rssi3"];
- $DA["rssi2"]=$DA["rssi2"]<=$DA["rssi4"]?$DA["rssi2"]:$DA["rssi4"];
-
- // debug_log("label_log","标签总数量为".count($arr));
-
- try{
- $dispose = new dispose($ues_redis);
- // $computedata=$dispose->computeData($DA);
- //$dispose->check_data($DA);
- $DA= $dispose->check_data($DA);
- debug_log("label_log","xsj:".json_encode($DA));
- $dispose->computeResout($DA);
-
-
- }catch(Exception $e){
- debug_log("InAndOUT","抛出异常".json_encode($DA,true));
- }
-
- }
-
- $text=substr($text,24);
- }else{
- $text=substr($text,1);
- }
-
- }
-
-
- }
-
- }
- /**
- * 清除数据
- *
- * @return void
- */
- public function clear_redis(){
-
- while(1){
- sleep(1);
- $redis=Cache::store('redis')->handler();
- $dispose = new dispose($redis);
- $keylist= $dispose->getStations();
- if(empty($keylist)){
- continue;
- }
- foreach($keylist as $station){
- //结果key值
- $key="res".$station;
- //存取旧的数据的reids数据
- $catchkey=$station."station";
- //获取该基站的所有key
- $keys= $redis->hKeys($key);
- if(empty($keys)){
- continue;
- }
- foreach($keys as $item){
- if( $json_data=$redis->hget($key,$item)){
-
- debug_log("clear_label",'获取的数据'.$json_data);
- $json_data=json_decode($json_data,true);
- $a=$json_data['a'];
- $status=$json_data['status'];
- $count= count($a);
- if((time()-$json_data["time"])<20){
- debug_log("clear_label", $item.':'. "标签上报间隔没有超过20秒进行跳过");
- continue;
- }
- if($count<5){
- // $redis->hdel($key,$item);
- debug_log("clear_label", $item.':'. "标签信号个数没有超过5个,全部去清除");
- //continue;
-
- }else{
- $front=0;
- $back=0;
- $res=0;
- if($a[$count-3]['dirt']==1){
- $front+=1;
- }else{
- $back+=1;
- }
- if($a[$count-2]['dirt']==1){
- $front+=1;
- }else{
- $back+=1;
- }
- if($a[$count-1]['dirt']==1){
- $front+=1;
- }else{
- $back+=1;
- }
- if($front>$back){
- $res=1;
- }else{
- $res=2;
- }
-
- if($status['dirt']!=$res){
- debug_log("clear_label", $item.':'. "最后一个信号,方向相反,出来结果,清除缓存数据");
- $redis->hdel($key,$item);
- $data_array=[];
- $data_array[]=[
- "label"=>$item,
- 'time'=>$a[$count-1]['time'],
- 'dirt'=> $status['dirt']==1?1:2 //1是进 2是出
- ];
- $url_data=[
- "mac"=>"863488051843244",
- "data"=>$data_array
- ];
- // debug_log("InAndOUT","得出计算结果:".json_encode($arr));
- debug_log("clear_label","发送给远程".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("clear_label","远程返回结果".$url_res);
- debug_log("result","补充计算出的结果计算返回的结果".json_encode($url_data));
- $this->temporary_label($redis,$item,2);
-
- }else{
- debug_log("clear_label", $item.':'. "最后一个信号,方向没有变化,清除缓存数据");
-
- }
-
-
- }
- //清除对应的键值对
- $redis->hdel($key,$item);
- $redis->hdel($catchkey,$item);
-
- }
-
- }
- }
-
-
-
- }
- }
- //打印日志
- public function temporary_label_log(){
- $redis=Cache::store('redis')->handler();
- $key="temporary_label";
- $list= $redis->HgetAll($key);
- debug_log($key,json_encode($list));
- debug_log($key,"所有的标签");
- foreach($list as $k=>$value){
- debug_log($key,json_encode([$k=>$value]));
- }
- debug_log($key,"所有总数".count($list));
- debug_log($key,"上报的标签");
- $i=0;
- foreach($list as $k=>$value){
- if($value==2){
- debug_log($key,json_encode([$k=>$value]));
- $i++;
- }
- }
- debug_log($key,"正常上报的标签总数".$i);
- debug_log($key,"补出的标签");
- $t=0;
- foreach($list as $k=>$value){
- if($value==3){
- $t++;
- debug_log($key,json_encode([$k=>$value]));
- }
- }
- debug_log($key,"补出的标签总数".$t);
- debug_log($key,"没有上报的的标签");
- $f=0;
- foreach($list as $k=>$value){
- if($value==1){
- debug_log($key,json_encode([$k=>$value]));
- $f++;
- }
-
- }
- debug_log($key,"没有上报的的标签总数".$f);
- $redis->del($key);
- }
- public function temporary_label($redis,$label,$type){
- $key="temporary_label";
- if($type==1){
- $res= $redis->hGet($key,$label);
- if($res==false){
- $redis->hSet($key,$label,1);
- }
- }else{
- $redis->hSet($key,$label,3);
- }
- }
- public function test(){
- try{
- $ues_redis=Cache::store('redis')->handler();
- $jsonData= $ues_redis->rpop("mqtt_data");
- debug_log("InAndOUT","1212");
- }catch(Exception $e){
- var_dump($e->getMessage());
- }
-
- }
- /**
- * 删除前1天的数据
- *
- * @return void
- */
- public function deleteHistory()
- {
- while(true)
- {
-
- $where[] = ['addTime','<',strtotime("-1 day")];
- Db::name('tag_history')->where($where)->delete();
- sleep(60*60*24);
- }
-
- }
-
- /**
- * 检测小程序版本版本 function
- *
- * @return void
- */
- public function detectionVersion(){
- $token = $_GET['token'];
- if (!$token || $token != '444333d3') {
- json_fail('缺少token或者token不对');
- }
- $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
- $data["version"]="1.1.1";
- json_success("查询版本成功",$data);
- }
-
- }
|