dispose.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. $this->getRemoteData($url_data);
  208. //清除缓存数据
  209. $this->get_label_history(3,$data['mac'],$data['label']);
  210. $this->temporary_label($this->redis,$data['label'],2);
  211. $status['dirt']=$res;
  212. $status['time']=$time;
  213. }else{
  214. debug_log("InAndOUT","无计算结果",json_encode($status));
  215. }
  216. }
  217. }
  218. $arr=array(
  219. "a"=>$list,
  220. 'status'=>$status,
  221. "time"=>$time,
  222. "in_and_out"=>$inAndOut
  223. );
  224. debug_log("InAndOUT","存入缓存中:".json_encode($arr));
  225. $this->setHash($hashkey,$key,$arr);
  226. debug_log("InAndOUT",'操作结束');
  227. }
  228. }
  229. //获取所有基站
  230. public function getStations(){
  231. $key=$this->stations;
  232. $list= $this->redis->hKeys($key);
  233. return $list;
  234. }
  235. //存入基站号
  236. public function setStations($Stations){
  237. $key=$this->stations;
  238. if(!$this->redis->hExists($key,$Stations)){
  239. $this->redis->hSet($key,$Stations,0);
  240. }
  241. }
  242. //排序
  243. public function sortlist($list){
  244. $list01=[];
  245. $list02=[];
  246. $list03=[];
  247. $list04=[];
  248. debug_log("InAndOUT","排序的数据:".json_encode($list));
  249. foreach($list as $item){
  250. array_push($list01,$item['rssi1']);
  251. array_push($list02,$item['rssi2']);
  252. array_push($list03,$item['rssi3']);
  253. array_push($list04,$item['rssi4']);
  254. }
  255. if(count($list)>=3){
  256. sort($list01);
  257. sort($list02);
  258. sort($list03);
  259. sort($list04);
  260. array_pop($list01);
  261. array_pop($list02);
  262. array_pop($list03);
  263. array_pop($list04);
  264. array_shift($list01);
  265. array_shift($list02);
  266. array_shift($list03);
  267. array_shift($list04);
  268. }
  269. debug_log("InAndOUT","排序后的队列:".json_encode($list01));
  270. debug_log("InAndOUT","排序后的队列:".json_encode($list02));
  271. debug_log("InAndOUT","排序后的队列:".json_encode($list03));
  272. debug_log("InAndOUT","排序后的队列:".json_encode($list04));
  273. $rssi1= array_sum($list01)/count($list01);
  274. $rssi2= array_sum($list02)/count($list02);
  275. $rssi3= array_sum($list03)/count($list03);
  276. $rssi4= array_sum($list04)/count($list04);
  277. $res=[$rssi1,$rssi2,$rssi3,$rssi4];
  278. debug_log("InAndOUT","结算后的结果:".json_encode($res));
  279. return $res;
  280. }
  281. /**
  282. * 获取远程推送的数据
  283. *
  284. * @param [type] $url
  285. * @param [type] $data
  286. * @return void
  287. */
  288. public function getRemoteData($data){
  289. $key="push_data";
  290. $redis=$this->redis;
  291. $redis->Rpush($key,json_encode($data));
  292. }
  293. /**
  294. * 历史标签信号
  295. *
  296. * @return void
  297. */
  298. public function set_label_history($data){
  299. $MAC=$data["mac"];
  300. $key=$MAC."_label_history";
  301. $head=$data['label']."_head";
  302. $tail=$data["label"]."_tail";
  303. //保存头部
  304. $head_data= $this->selectHash($key,$head);
  305. $tail_data=$this->selectHash($key,$tail);
  306. debug_log("InAndOUT","最旧信号间隔-".($data["report_time"]-$tail_data[count($tail_data)-1]["report_time"]));
  307. debug_log("InAndOUT","最旧信号间隔--".json_encode($tail_data));
  308. debug_log("InAndOUT","最旧信号间隔---".json_encode($tail_data[count($tail_data)-1]));
  309. debug_log("InAndOUT","最旧信号间隔----".$data["report_time"]);
  310. if(!empty($tail_data)){
  311. if(($data["report_time"]-$tail_data[count($tail_data)-1]["report_time"])>6){
  312. $head_data=[];
  313. $tail_data=[];
  314. }
  315. }
  316. if(empty($head_data)||count($head_data)<3){
  317. $head_data=empty($head_data)?[]: $head_data;
  318. array_push($head_data,$data);
  319. $this->setHash($key,$head,$head_data);
  320. }
  321. //保存尾部标签数据
  322. $tail_data=empty($tail_data)?[]: $tail_data;
  323. array_push($tail_data,$data);
  324. while(count($tail_data)>3){
  325. array_shift($tail_data);
  326. }
  327. $this->setHash($key,$tail,$tail_data);
  328. }
  329. /**
  330. * 获取基站
  331. *
  332. * @param [type] $type 1 头部 2 尾部 3清空
  333. * @param [type] $mac
  334. * @param [type] $label
  335. * @return void
  336. */
  337. public function get_label_history($type,$mac,$label){
  338. $key=$mac."_label_history";
  339. $head=$label."_head";
  340. $tail=$label."_tail";
  341. if($type==1){
  342. $head_data= $this->selectHash($key,$head);
  343. return $head_data;
  344. }
  345. if($type==2){
  346. $tail_data= $this->selectHash($key,$tail);
  347. return $tail_data;
  348. }
  349. if($type==3){
  350. debug_log("InAndOUT","清空头部和尾部历史:".$mac." ".$label);
  351. $tail_data= $this->delHash($key,$head);
  352. $tail_data= $this->delHash($key,$tail);
  353. }
  354. }
  355. /**
  356. * 二次生成进出
  357. *
  358. * @return void
  359. */
  360. public function second_create_direction($mac,$label){
  361. debug_log("second_dirt","==============mac:".$mac."=======label".$label."================================");
  362. $key=$mac."_label_history";
  363. $head=$label."_head";
  364. $tail=$label."_tail";
  365. $head_dir=0;
  366. $tail_dir=0;
  367. $front=0;
  368. $behind=0;
  369. $res=0;
  370. $time=0;
  371. $head_data= $this->get_label_history(1,$mac,$label);
  372. $tail_data= $this->get_label_history(2,$mac,$label);
  373. debug_log("second_dirt","头部标签数据".json_encode($head_data));
  374. debug_log("second_dirt","尾部标签数据".json_encode($tail_data));
  375. if(empty($head_data)){
  376. return false;
  377. }
  378. if(empty($tail_data)){
  379. return false;
  380. }
  381. if(count($head_data)<3){
  382. return false;
  383. }
  384. foreach($head_data as $item){
  385. if($item["rssi1"]<$item["rssi2"]){
  386. $front+=1;
  387. }else{
  388. $behind+=1;
  389. }
  390. }
  391. $head_dir=$front>$behind?1:2;
  392. $front=0;
  393. $behind=0;
  394. foreach($tail_data as $item){
  395. if($item["rssi1"]<$item["rssi2"]){
  396. $front+=1;
  397. }else{
  398. $behind+=1;
  399. }
  400. $time=$item['report_time'];
  401. }
  402. $tail_dir=$front>$behind?1:2;
  403. if($tail_dir== $head_dir){
  404. return false;
  405. }
  406. if($head_dir==1){
  407. $res=1;
  408. }else{
  409. $res=2;
  410. }
  411. debug_log("second_dirt","最终结果".json_encode(["dirt"=>$res,"time"=>$time]));
  412. return ["dirt"=>$res,"time"=>$time];
  413. }
  414. //查询key hash
  415. public function selectHash($hashKey,$key){
  416. $res= $this->redis->hGet($hashKey,$key);
  417. if(empty($res)){
  418. return false;
  419. }else{
  420. return json_decode($res,true);
  421. }
  422. }
  423. public function setHash($hashKey,$key,$data){
  424. $this->redis->hSet($hashKey,$key,json_encode($data));
  425. }
  426. public function delHash($hashKey,$key){
  427. $this->redis->Hdel($hashKey,$key);
  428. }
  429. public function temporary_label($redis,$label,$type){
  430. return;
  431. $key="temporary_label";
  432. if($type==1){
  433. $res= $redis->hGet($key,$label);
  434. if($res==false){
  435. $redis->hSet($key,$label,1);
  436. }
  437. }else{
  438. $redis->hSet($key,$label,2);
  439. }
  440. }
  441. /**
  442. * 解析日志
  443. */
  444. public function analysisLog(){
  445. $path=app()->getRootPath()."runtime/log/2023-05-15/label_log.log";
  446. var_dump($path);
  447. var_dump(is_file($path));
  448. $file = fopen($path, "rb");
  449. debug_log("in_label","开始解析标签");
  450. $array=[];
  451. $readCvs=function($file){
  452. while (feof($file)===false) {
  453. # code...
  454. yield fgets($file);
  455. }
  456. fclose($file);
  457. };
  458. debug_log("in_label","解析运行中");
  459. foreach ($readCvs($file) as $data) {
  460. if(strstr($data,"xsj")){
  461. $arr= explode("{",$data);
  462. $data= json_decode("{".trim($arr[1]),true);
  463. debug_log("in_label",$data);
  464. if(!in_array($data['label'],$array)&&$data["rssi1"]>$data["rssi2"]){
  465. array_push($array,$data['label']);
  466. }
  467. }
  468. }
  469. foreach($array as $item){
  470. debug_log("in_label",$item);
  471. }
  472. debug_log("in_label","标签总数".count($array));
  473. }
  474. }