dispose.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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[count($tail_data)-1]));
  308. debug_log("InAndOUT","最旧信号间隔".$data["report_time"]);
  309. if(!empty($tail_data)){
  310. if(($data["report_time"]-$tail_data[count($tail_data)-1]["report_time"])>6){
  311. $head_data=[];
  312. $tail_data=[];
  313. }
  314. }
  315. if(empty($head_data)||count($head_data)<3){
  316. $head_data=empty($head_data)?[]: $head_data;
  317. array_push($head_data,$data);
  318. $this->setHash($key,$head,$head_data);
  319. }
  320. //保存尾部标签数据
  321. $tail_data=empty($tail_data)?[]: $tail_data;
  322. array_push($tail_data,$data);
  323. while(count($tail_data)>3){
  324. array_shift($tail_data);
  325. }
  326. $this->setHash($key,$tail,$tail_data);
  327. }
  328. /**
  329. * 获取基站
  330. *
  331. * @param [type] $type 1 头部 2 尾部 3清空
  332. * @param [type] $mac
  333. * @param [type] $label
  334. * @return void
  335. */
  336. public function get_label_history($type,$mac,$label){
  337. $key=$mac."_label_history";
  338. $head=$label."_head";
  339. $tail=$label."_tail";
  340. if($type==1){
  341. $head_data= $this->selectHash($key,$head);
  342. return $head_data;
  343. }
  344. if($type==2){
  345. $tail_data= $this->selectHash($key,$tail);
  346. return $tail_data;
  347. }
  348. if($type==3){
  349. debug_log("InAndOUT","清空头部和尾部历史:".$mac." ".$label);
  350. $tail_data= $this->delHash($key,$head);
  351. $tail_data= $this->delHash($key,$tail);
  352. }
  353. }
  354. /**
  355. * 二次生成进出
  356. *
  357. * @return void
  358. */
  359. public function second_create_direction($mac,$label){
  360. debug_log("second_dirt","==============mac:".$mac."=======label".$label."================================");
  361. $key=$mac."_label_history";
  362. $head=$label."_head";
  363. $tail=$label."_tail";
  364. $head_dir=0;
  365. $tail_dir=0;
  366. $front=0;
  367. $behind=0;
  368. $res=0;
  369. $time=0;
  370. $head_data= $this->get_label_history(1,$mac,$label);
  371. $tail_data= $this->get_label_history(2,$mac,$label);
  372. debug_log("second_dirt","头部标签数据".json_encode($head_data));
  373. debug_log("second_dirt","尾部标签数据".json_encode($tail_data));
  374. if(empty($head_data)){
  375. return false;
  376. }
  377. if(empty($tail_data)){
  378. return false;
  379. }
  380. if(count($head_data)<3){
  381. return false;
  382. }
  383. foreach($head_data as $item){
  384. if($item["rssi1"]<$item["rssi2"]){
  385. $front+=1;
  386. }else{
  387. $behind+=1;
  388. }
  389. }
  390. $head_dir=$front>$behind?1:2;
  391. $front=0;
  392. $behind=0;
  393. foreach($tail_data as $item){
  394. if($item["rssi1"]<$item["rssi2"]){
  395. $front+=1;
  396. }else{
  397. $behind+=1;
  398. }
  399. $time=$item['report_time'];
  400. }
  401. $tail_dir=$front>$behind?1:2;
  402. if($tail_dir== $head_dir){
  403. return false;
  404. }
  405. if($head_dir==1){
  406. $res=1;
  407. }else{
  408. $res=2;
  409. }
  410. debug_log("second_dirt","最终结果".json_encode(["dirt"=>$res,"time"=>$time]));
  411. return ["dirt"=>$res,"time"=>$time];
  412. }
  413. //查询key hash
  414. public function selectHash($hashKey,$key){
  415. $res= $this->redis->hGet($hashKey,$key);
  416. if(empty($res)){
  417. return false;
  418. }else{
  419. return json_decode($res,true);
  420. }
  421. }
  422. public function setHash($hashKey,$key,$data){
  423. $this->redis->hSet($hashKey,$key,json_encode($data));
  424. }
  425. public function delHash($hashKey,$key){
  426. $this->redis->Hdel($hashKey,$key);
  427. }
  428. public function temporary_label($redis,$label,$type){
  429. $key="temporary_label";
  430. if($type==1){
  431. $res= $redis->hGet($key,$label);
  432. if($res==false){
  433. $redis->hSet($key,$label,1);
  434. }
  435. }else{
  436. $redis->hSet($key,$label,2);
  437. }
  438. }
  439. /**
  440. * 解析日志
  441. */
  442. public function analysisLog(){
  443. $path=app()->getRootPath()."runtime/log/2023-05-15/label_log.log";
  444. var_dump($path);
  445. var_dump(is_file($path));
  446. $file = fopen($path, "rb");
  447. debug_log("in_label","开始解析标签");
  448. $array=[];
  449. $readCvs=function($file){
  450. while (feof($file)===false) {
  451. # code...
  452. yield fgets($file);
  453. }
  454. fclose($file);
  455. };
  456. debug_log("in_label","解析运行中");
  457. foreach ($readCvs($file) as $data) {
  458. if(strstr($data,"xsj")){
  459. $arr= explode("{",$data);
  460. $data= json_decode("{".trim($arr[1]),true);
  461. debug_log("in_label",$data);
  462. if(!in_array($data['label'],$array)&&$data["rssi1"]>$data["rssi2"]){
  463. array_push($array,$data['label']);
  464. }
  465. }
  466. }
  467. foreach($array as $item){
  468. debug_log("in_label",$item);
  469. }
  470. debug_log("in_label","标签总数".count($array));
  471. }
  472. }