dispose.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <?php
  2. namespace catchAdmin\api\service;
  3. use catchAdmin\tag_history\model\Dormitory;
  4. class dispose
  5. {
  6. private $redis;
  7. private $stations="stations";
  8. public function __construct($redis){
  9. $this->redis=$redis;
  10. }
  11. // public function computeData($data){
  12. // debug_log("InAndOUT","基站:".$data['mac'] .' label :'.$data['label']);
  13. // debug_log("InAndOUT","新数据:".json_encode($data));
  14. // $hashKey=$data['mac'].'station';
  15. // $key=$data['label'];
  16. // $list=[];
  17. // $length=0;
  18. // $res= $this->selectHash($hashKey,$key);
  19. // if($res==false){
  20. // debug_log("InAndOUT","redis没有数据,存入新数据");
  21. // array_push($list,$data);
  22. // debug_log("InAndOUT","当前数据队列",json_encode($list));
  23. // }else{
  24. // debug_log("InAndOUT","redis初始数据 :".json_encode($res));
  25. // $list=$res;
  26. // debug_log("InAndOUT","上次上报的数据 :".json_encode($list[count($list)-1],true));
  27. // // if(($data['report_time']-$list[count($list)-1]['report_time'])>=120){
  28. // // debug_log("InAndOUT","上报次数间隔超过120s,清空redis缓存,从新存入数据");
  29. // // $list=[];
  30. // // array_push($list,$data);
  31. // // }else{
  32. // $list=$res;
  33. // array_push($list,$data);
  34. // //按照个数
  35. // while(count($list)>5){
  36. // array_shift($list);
  37. // }
  38. // // }
  39. // }
  40. // //数据
  41. // $count=count($list);
  42. // debug_log("InAndOUT","当前数据队列 :".json_encode($list));
  43. // debug_log("InAndOUT","新数据长度 :".$count);
  44. // //现在的数据长度
  45. // //获取配置
  46. // $config= $this->selectHash("anbang_four_wire",$data['mac']);
  47. // if(empty($config)){
  48. // debug_log("InAndOUT","没有配置");
  49. // }else{
  50. // debug_log("InAndOUT","配置参数为:".json_encode($config));
  51. // }
  52. // $sortlist= $this->sortlist($list);
  53. // $data["rssi1"]=$sortlist[0];
  54. // $data["rssi2"]=$sortlist[1];
  55. // $data["rssi3"]=$sortlist[2];
  56. // $data["rssi4"]=$sortlist[3];
  57. // debug_log("InAndOUT","计算出的平均数据 :".json_encode($data));
  58. // //保存到hash中
  59. // debug_log("InAndOUT","存入redis缓存的数据 :".json_encode($list));
  60. // $this->setHash($hashKey,$key,$list);
  61. // debug_log("InAndOUT","=======================================");
  62. // return $data;
  63. // }
  64. public function check_data($data){
  65. $this->setStations($data['mac']);
  66. $res='';
  67. $hashKey=$data['mac'].'station';
  68. $key=$data['label'];
  69. $old_data=$this->selectHash($hashKey,$key);
  70. if($data["rssi1"]==90&&!empty($old_data)){
  71. if($old_data["rssi1"]<90){
  72. $data["rssi1"]=$old_data["rssi1"]+8;
  73. $data["rssi1"]= $data["rssi1"]>90 ? 90:$data["rssi1"];
  74. $res=$res."rssi1 ";
  75. }
  76. }
  77. if($data["rssi2"]==90&&!empty($old_data)){
  78. if($old_data["rssi2"]<90){
  79. $data["rssi2"]=$old_data["rssi2"]+8;
  80. $data["rssi2"]= $data["rssi2"]>90 ? 90:$data["rssi2"];
  81. $res=$res."rssi2";
  82. }
  83. }
  84. $this->setHash($hashKey,$key,$data);
  85. return $data;
  86. }
  87. //计算结果设计
  88. public function computeResout($data){
  89. debug_log("InAndOUT","=======================================");
  90. debug_log("InAndOUT",'接收新数据'.json_encode($data));
  91. debug_log("InAndOUT","=======================================");
  92. $res=0;
  93. //进出
  94. $hashkey='res'.$data['mac'];
  95. $key=$data['label'];
  96. //存入redis的数据
  97. $json_list= $this->selectHash($hashkey,$key);
  98. //最新时间
  99. $time=$data['report_time'];
  100. //进出结果
  101. $inAndOut=0;
  102. //是否参与计算
  103. $calculate=true;
  104. if($data['rssi1']==$data['rssi2']){
  105. $calculate=false;
  106. }
  107. $t1s= $data['rssi1']-$data['rssi2'];
  108. if(abs($t1s)<3){
  109. debug_log("InAndOUT","差值小于3不做计算");
  110. $calculate=false;
  111. }
  112. if($calculate==false){
  113. if(!$json_list){
  114. $arr=[
  115. "a"=>[],
  116. 'status'=> ['time'=>null,'dirt'=>null],
  117. "time"=>$time, //最新时间
  118. "in_and_out"=>0 //进出结果
  119. ];
  120. }else{
  121. $json_list['time']=$time;
  122. }
  123. $this->setHash($hashkey,$key,$json_list);
  124. return;
  125. }
  126. if($data['rssi1']<$data['rssi2']){
  127. $res=1;
  128. }
  129. if($data['rssi1']>$data['rssi2']){
  130. $res=2;
  131. }
  132. if(!$json_list){
  133. $arr=[
  134. "a"=>[['time'=>$time,'dirt'=>$res]],
  135. 'status'=> ['time'=>null,'dirt'=>null],
  136. "time"=>$time, //最新时间
  137. "in_and_out"=>0
  138. ];
  139. debug_log("InAndOUT","数据不存在,直接存储新数据".json_encode($arr));
  140. $this->setHash($hashkey,$key,$arr);
  141. return;
  142. }else{
  143. $resArray=['time'=>$data['report_time'],'dirt'=>$res];
  144. debug_log("InAndOUT","缓存的数据".json_encode($json_list));
  145. $s=$json_list;
  146. //数据列表
  147. $list=$s['a'];
  148. //进出结果
  149. $inAndOut=$s["in_and_out"];
  150. //状态
  151. $status=$s['status'];
  152. array_push($list,$resArray);
  153. while(count($list)>5){
  154. array_shift($list);
  155. debug_log("InAndOUT","数据超过5条,剔除一条旧数据");
  156. }
  157. debug_log("InAndOUT","当前队列数据:".json_encode($list));
  158. $front=0;
  159. $back=0;
  160. if(count($list)>2){
  161. //
  162. debug_log("InAndOUT","当前队列超过两条可以计算前后:".count($list));
  163. //校验权柄
  164. foreach($list as $item){
  165. if($item['dirt']==1){
  166. $front+=1;
  167. }
  168. if($item['dirt']==2){
  169. $back+=1;
  170. }
  171. }
  172. $res=$front>$back?1:2;
  173. if(empty($status['dirt'])){
  174. $status['dirt']= $res;
  175. $status['time']= $resArray['time'];
  176. debug_log("InAndOUT","初始化状态为:".json_encode($status));
  177. }else{
  178. //判断是否连贯
  179. $IS_OK=true;
  180. $res=0;
  181. $reverse=array_reverse($list);
  182. $res=$reverse[0]['dirt'];
  183. foreach($reverse as $aitem){
  184. if($aitem['dirt']!=$res){
  185. $IS_OK=false;
  186. }
  187. }
  188. // if(($reverse[0]['dirt']==$reverse[1]['dirt'])&&($reverse[1]['dirt']==$reverse[2]['dirt'])){
  189. // $res=$reverse[0]['dirt'];
  190. // $IS_OK=true;
  191. // }
  192. //进行保存
  193. if($res!=$status['dirt']&&$IS_OK){
  194. // if(!empty($status['status']['dirt'])){
  195. // if($resArray['time']-$status['status']['time']>5){
  196. $data_array=[];
  197. $data_array[]=[
  198. "label"=>$data['label'],
  199. 'time'=>$resArray['time'],
  200. 'dirt'=> $status['dirt']==1?1:2 //1是进 2是出
  201. ];
  202. $inAndOut=$status['dirt']==1?1:2;
  203. $url_data=[
  204. "mac"=>$data['mac'],
  205. "data"=>$data_array
  206. ];
  207. //设定最后结果
  208. $this->set_time_results($data['mac'],$data['label'],$resArray['time'],$inAndOut);
  209. //推送远程数据
  210. $this->getRemoteData($url_data);
  211. //清除缓存数据
  212. $this->get_label_history(3,$data['mac'],$data['label']);
  213. $this->temporary_label($this->redis,$data['label'],2);
  214. $status['dirt']=$res;
  215. $status['time']=$time;
  216. }else{
  217. debug_log("InAndOUT","无计算结果",json_encode($status));
  218. }
  219. }
  220. }
  221. $arr=array(
  222. "a"=>$list,
  223. 'status'=>$status,
  224. "time"=>$time,
  225. "in_and_out"=>$inAndOut
  226. );
  227. debug_log("InAndOUT","存入缓存中:".json_encode($arr));
  228. $this->setHash($hashkey,$key,$arr);
  229. debug_log("InAndOUT",'操作结束');
  230. }
  231. }
  232. //获取所有基站
  233. public function getStations(){
  234. $key=$this->stations;
  235. $list= $this->redis->hKeys($key);
  236. return $list;
  237. }
  238. //存入基站号
  239. public function setStations($Stations){
  240. $key=$this->stations;
  241. if(!$this->redis->hExists($key,$Stations)){
  242. $this->redis->hSet($key,$Stations,0);
  243. }
  244. }
  245. //排序
  246. // public function sortlist($list){
  247. // $list01=[];
  248. // $list02=[];
  249. // $list03=[];
  250. // $list04=[];
  251. // debug_log("InAndOUT","排序的数据:".json_encode($list));
  252. // foreach($list as $item){
  253. // array_push($list01,$item['rssi1']);
  254. // array_push($list02,$item['rssi2']);
  255. // array_push($list03,$item['rssi3']);
  256. // array_push($list04,$item['rssi4']);
  257. // }
  258. // if(count($list)>=3){
  259. // sort($list01);
  260. // sort($list02);
  261. // sort($list03);
  262. // sort($list04);
  263. // array_pop($list01);
  264. // array_pop($list02);
  265. // array_pop($list03);
  266. // array_pop($list04);
  267. // array_shift($list01);
  268. // array_shift($list02);
  269. // array_shift($list03);
  270. // array_shift($list04);
  271. // }
  272. // debug_log("InAndOUT","排序后的队列:".json_encode($list01));
  273. // debug_log("InAndOUT","排序后的队列:".json_encode($list02));
  274. // debug_log("InAndOUT","排序后的队列:".json_encode($list03));
  275. // debug_log("InAndOUT","排序后的队列:".json_encode($list04));
  276. // $rssi1= array_sum($list01)/count($list01);
  277. // $rssi2= array_sum($list02)/count($list02);
  278. // $rssi3= array_sum($list03)/count($list03);
  279. // $rssi4= array_sum($list04)/count($list04);
  280. // $res=[$rssi1,$rssi2,$rssi3,$rssi4];
  281. // debug_log("InAndOUT","结算后的结果:".json_encode($res));
  282. // return $res;
  283. // }
  284. /**
  285. * 获取远程推送的数据
  286. *
  287. * @param [type] $url
  288. * @param [type] $data
  289. * @return void
  290. */
  291. public function getRemoteData($data){
  292. $key="push_data";
  293. $redis=$this->redis;
  294. $redis->Rpush($key,json_encode($data));
  295. }
  296. /**
  297. * 历史标签信号
  298. *
  299. * @return void
  300. */
  301. public function set_label_history($data){
  302. $MAC=$data["mac"];
  303. $key=$MAC."_label_history";
  304. $head=$data['label']."_head";
  305. $tail=$data["label"]."_tail";
  306. //保存头部
  307. $head_data= $this->selectHash($key,$head);
  308. $tail_data=$this->selectHash($key,$tail);
  309. debug_log("InAndOUT","最旧信号间隔-".($data["report_time"]-$tail_data[count($tail_data)-1]["report_time"]));
  310. debug_log("InAndOUT","最旧信号间隔--".json_encode($tail_data));
  311. debug_log("InAndOUT","最旧信号间隔---".json_encode($tail_data[count($tail_data)-1]));
  312. debug_log("InAndOUT","最旧信号间隔----".$data["report_time"]);
  313. if(!empty($tail_data)){
  314. if(($data["report_time"]-$tail_data[count($tail_data)-1]["report_time"])>6){
  315. $head_data=[];
  316. $tail_data=[];
  317. }
  318. }
  319. if(empty($head_data)||count($head_data)<3){
  320. $head_data=empty($head_data)?[]: $head_data;
  321. array_push($head_data,$data);
  322. $this->setHash($key,$head,$head_data);
  323. }
  324. //保存尾部标签数据
  325. $tail_data=empty($tail_data)?[]: $tail_data;
  326. array_push($tail_data,$data);
  327. while(count($tail_data)>3){
  328. array_shift($tail_data);
  329. }
  330. $this->setHash($key,$tail,$tail_data);
  331. }
  332. /**
  333. * 获取基站
  334. *
  335. * @param [type] $type 1 头部 2 尾部 3清空
  336. * @param [type] $mac
  337. * @param [type] $label
  338. * @return void
  339. */
  340. public function get_label_history($type,$mac,$label){
  341. $key=$mac."_label_history";
  342. $head=$label."_head";
  343. $tail=$label."_tail";
  344. if($type==1){
  345. $head_data= $this->selectHash($key,$head);
  346. return $head_data;
  347. }
  348. if($type==2){
  349. $tail_data= $this->selectHash($key,$tail);
  350. return $tail_data;
  351. }
  352. if($type==3){
  353. debug_log("InAndOUT","清空头部和尾部历史:".$mac." ".$label);
  354. $tail_data= $this->delHash($key,$head);
  355. $tail_data= $this->delHash($key,$tail);
  356. }
  357. }
  358. /**
  359. * 二次生成进出
  360. *
  361. * @return void
  362. */
  363. public function second_create_direction($mac,$label){
  364. debug_log("second_dirt","==============mac:".$mac."=======label".$label."================================");
  365. $key=$mac."_label_history";
  366. $head=$label."_head";
  367. $tail=$label."_tail";
  368. $head_dir=0;
  369. $tail_dir=0;
  370. $front=0;
  371. $behind=0;
  372. $res=0;
  373. $time=0;
  374. $head_data= $this->get_label_history(1,$mac,$label);
  375. $tail_data= $this->get_label_history(2,$mac,$label);
  376. debug_log("second_dirt","头部标签数据".json_encode($head_data));
  377. debug_log("second_dirt","尾部标签数据".json_encode($tail_data));
  378. if(empty($head_data)){
  379. return false;
  380. }
  381. if(empty($tail_data)){
  382. return false;
  383. }
  384. if(count($head_data)<3){
  385. return false;
  386. }
  387. foreach($head_data as $item){
  388. if($item["rssi1"]<$item["rssi2"]){
  389. $front+=1;
  390. }else{
  391. $behind+=1;
  392. }
  393. }
  394. $head_dir=$front>$behind?1:2;
  395. $front=0;
  396. $behind=0;
  397. foreach($tail_data as $item){
  398. if($item["rssi1"]<$item["rssi2"]){
  399. $front+=1;
  400. }else{
  401. $behind+=1;
  402. }
  403. $time=$item['report_time'];
  404. }
  405. $tail_dir=$front>$behind?1:2;
  406. if($tail_dir== $head_dir){
  407. return false;
  408. }
  409. if($head_dir==1){
  410. $res=1;
  411. }else{
  412. $res=2;
  413. }
  414. //早上对"进寝室不做二次计算
  415. $start_time=strtotime(date("Y-m-d",time()));
  416. $end_time=$start_time+60*60*8;
  417. debug_log("second_dirt","判断的时间".date("Y-m-d H:i:s",$end_time));
  418. debug_log("second_dirt","判断的时间".date("Y-m-d H:i:s",$time));
  419. //当天结束之间 当天早上7点之前进寝室,不做二次计算
  420. if($time<$end_time&&$res==1){
  421. debug_log("second_dirt","当天早上7点之前进寝室,不做二次计算");
  422. return false;
  423. }
  424. $befor_res= $this->get_time_results($mac,$label,2);
  425. if(!empty($befor_res)){
  426. debug_log("second_dirt","间隔时间".json_encode($befor_res));
  427. if($befor_res['dir']==$res){
  428. $interval_time=$time-$befor_res["time"];
  429. debug_log("second_dirt","间隔时间".$interval_time);
  430. debug_log("second_dirt","间隔时间第一个小时,且方向相同 不做二次计算".$interval_time);
  431. if($interval_time<1*60*60){
  432. return false;
  433. }
  434. }
  435. }else{
  436. debug_log("second_dirt","数据为空,进行生成");
  437. }
  438. debug_log("second_dirt","最终结果".json_encode(["dirt"=>$res,"time"=>$time]));
  439. return ["dirt"=>$res,"time"=>$time];
  440. }
  441. /**
  442. * Undocumented function 基站配置
  443. *
  444. * @param [type] $mac
  445. * @return void
  446. */
  447. public function get_station_config($mac){
  448. $Key="station_config";
  449. $mackey=$mac;
  450. $data=[
  451. "filter_signal"=>72,//过滤信号强度
  452. "history_filter_signal"=>82,//历史记录过滤的信号强度
  453. "init_data"=>3, //需要三条数据确定初始方向
  454. "change_data"=>5, //需要5条数据确定变换的方向
  455. "timeout"=>6,
  456. "second_create_res"=>[]//二次根据历史记录生成配置 数组中的参数配置 start_time,end_time,dir 1 前 2后;
  457. ];
  458. $config_data=$this->selectHash($Key,$mackey);
  459. if(!empty($config_data)){
  460. foreach($data as $key=>$value){
  461. $data[$key]=empty($config_data[$key])?$data[$key]:$config_data[$key];
  462. }
  463. }
  464. return $data;
  465. }
  466. /**
  467. * function 设置存储结果的时间
  468. *
  469. * @param [type] $mac
  470. * @param [type] $label
  471. * @param [type] $time
  472. * @param [type] $dir 进出结果
  473. * @return void
  474. */
  475. public function set_time_results($mac,$label,$time,$dir){
  476. $mac_key=$mac."res_time";
  477. $array=["time"=>$time,"dir"=>$dir];
  478. $this->setHash($mac_key,$label,$array);
  479. }
  480. /**
  481. * function 获取存储结果的时间
  482. *
  483. * @param [type] $mac
  484. * @param [type] $label
  485. * @param [type] $type 类型
  486. * @return void
  487. */
  488. public function get_time_results($mac,$label,$type=1){
  489. $mac_key=$mac."res_time";
  490. $data=$this->selectHash($mac_key,$label);
  491. if(empty($data)){
  492. return false;
  493. }
  494. if($type==1){
  495. return $data["time"];
  496. }else{
  497. return $data;
  498. }
  499. }
  500. //查询key hash
  501. public function selectHash($hashKey,$key){
  502. $res= $this->redis->hGet($hashKey,$key);
  503. if(empty($res)){
  504. return false;
  505. }else{
  506. return json_decode($res,true);
  507. }
  508. }
  509. public function setHash($hashKey,$key,$data){
  510. $this->redis->hSet($hashKey,$key,json_encode($data));
  511. }
  512. public function delHash($hashKey,$key){
  513. $this->redis->Hdel($hashKey,$key);
  514. }
  515. public function temporary_label($redis,$label,$type){
  516. return;
  517. $key="temporary_label";
  518. if($type==1){
  519. $res= $redis->hGet($key,$label);
  520. if($res==false){
  521. $redis->hSet($key,$label,1);
  522. }
  523. }else{
  524. $redis->hSet($key,$label,2);
  525. }
  526. }
  527. /**
  528. * 解析日志
  529. */
  530. public function analysisLog(){
  531. $path=app()->getRootPath()."runtime/log/2023-05-28/label_log.log";
  532. var_dump($path);
  533. var_dump(is_file($path));
  534. $file = fopen($path, "rb");
  535. debug_log("in_label","开始解析标签");
  536. $array=[];
  537. $readCvs=function($file){
  538. while (feof($file)===false) {
  539. # code...
  540. yield fgets($file);
  541. }
  542. fclose($file);
  543. };
  544. debug_log("in_label","解析运行中");
  545. foreach ($readCvs($file) as $data) {
  546. if(strstr($data,"xsj")){
  547. $arr= explode("{",$data);
  548. $data= json_decode("{".trim($arr[1]),true);
  549. debug_log("in_label",$data);
  550. if(!in_array($data['label'],$array)&&$data["rssi1"]>$data["rssi2"]){
  551. array_push($array,$data['label']);
  552. }
  553. }
  554. }
  555. foreach($array as $item){
  556. debug_log("in_label",$item);
  557. }
  558. debug_log("in_label","标签总数".count($array));
  559. }
  560. }