dispose.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace catchAdmin\api\service;
  3. use catchAdmin\tag_history\model\Dormitory;
  4. class dispose
  5. {
  6. private $redis;
  7. public function __construct($redis){
  8. $this->redis=$redis;
  9. }
  10. public function computeData($data){
  11. debug_log("InAndOUT","基站:".$data['mac'] .' label :'.$data['label']);
  12. debug_log("InAndOUT","新数据:".json_encode($data));
  13. $hashKey=$data['mac'].'station';
  14. $key=$data['label'];
  15. $list=[];
  16. $length=0;
  17. $res= $this->selectHash($hashKey,$key);
  18. if($res==false){
  19. debug_log("InAndOUT","redis没有数据,存入新数据");
  20. array_push($list,$data);
  21. debug_log("InAndOUT","当前数据队列",json_encode($list));
  22. }else{
  23. debug_log("InAndOUT","redis初始数据 :".json_encode($res));
  24. $list=$res;
  25. debug_log("InAndOUT","上次上报的数据 :".json_encode($list[count($list)-1],true));
  26. if(($data['report_time']-$list[count($list)-1]['report_time'])>=120){
  27. debug_log("InAndOUT","上报次数间隔超过120s,清空redis缓存,从新存入数据");
  28. $list=[];
  29. array_push($list,$data);
  30. }else{
  31. $list=$res;
  32. array_push($list,$data);
  33. //保留10秒
  34. $tem_list=$list;
  35. foreach($tem_list as $tem_item){
  36. debug_log("InAndOUT","测试".($tem_item['report_time']-$data['report_time']));
  37. if(($data['report_time']-$tem_item['report_time'])>5){
  38. debug_log("InAndOUT","最旧的上报时间超过5s剔除一条");
  39. array_shift($list);
  40. }
  41. }
  42. // if(count($list)>5){
  43. // debug_log("InAndOUT","数据长度大于5,剔除一条");
  44. // debug_log("InAndOUT","剔除的一条数据为:".json_encode($list[0]));
  45. // array_shift($list);
  46. // }
  47. }
  48. }
  49. //数据
  50. $count=count($list);
  51. debug_log("InAndOUT","当前数据队列 :".json_encode($list));
  52. debug_log("InAndOUT","新数据长度 :".$count);
  53. //现在的数据长度
  54. //获取配置
  55. $config= $this->selectHash("anbang_four_wire",$data['mac']);
  56. if(empty($config)){
  57. debug_log("InAndOUT","没有配置");
  58. }else{
  59. debug_log("InAndOUT","配置参数为:".json_encode($config));
  60. }
  61. $len01=0;
  62. $len02=0;
  63. $len03=0;
  64. $len04=0;
  65. $rssi1=0;
  66. $rssi2=0;
  67. $rssi3=0;
  68. $rssi4=0;
  69. foreach($list as $item){
  70. if(!empty($config)){
  71. if($config['front']&&$config['front']>$item["rssi1"]){
  72. $rssi1+=$item["rssi1"];
  73. $len01+=1;
  74. }
  75. if($config['behind']&&$config['behind']>$item["rssi2"]){
  76. $rssi2+=$item["rssi2"];
  77. $len02+=1;
  78. }
  79. if($config['left']&&$config['left']>$item["rssi3"]){
  80. $rssi3+=$item["rssi3"];
  81. $len03+=1;
  82. }
  83. if($config['right']&&$config['right']>$item["rssi4"]){
  84. $rssi4+=$item["rssi4"];
  85. $len04+=1;
  86. }
  87. }else{
  88. $rssi1=$rssi1+$item["rssi1"];
  89. $rssi2=$rssi2+$item["rssi2"];
  90. $rssi3=$rssi3+$item["rssi3"];
  91. $rssi4=$rssi4+$item["rssi4"];
  92. $len01+=1;
  93. $len02+=1;
  94. $len03+=1;
  95. $len04+=1;
  96. }
  97. }
  98. $data["rssi1"]=number_format($rssi1/$len01,'2');
  99. $data["rssi2"]=number_format($rssi2/$len02,'2');
  100. $data["rssi3"]=number_format($rssi3/$len03,'2');
  101. $data["rssi4"]=number_format($rssi4/$len04,'2');
  102. debug_log("InAndOUT","计算出的平均数据 :".json_encode($data));
  103. //保存到hash中
  104. debug_log("InAndOUT","存入redis缓存的数据 :".json_encode($list));
  105. $this->setHash($hashKey,$key,$list);
  106. debug_log("InAndOUT","=======================================");
  107. return $data;
  108. }
  109. //计算结果设计
  110. public function computeResout($data){
  111. //前后左右$arr=[$data['resi1'],$data['resi2'],$data['resi3'],$data['resi4']];
  112. //前后
  113. // $redis=$this->redis;
  114. // $arr=[$data['resi1'],$data['resi2']];
  115. // sort($arr);
  116. $res=0;
  117. if($data['rssi1']==$data['rssi2']){
  118. debug_log("InAndOUT","前后信号相等则进行过滤掉");
  119. return;
  120. }
  121. if($data['rssi1']<$data['rssi2']){
  122. $res=1;
  123. }
  124. if($data['rssi1']>$data['rssi2']){
  125. $res=2;
  126. }
  127. debug_log("InAndOUT","信号强度 :".$res);
  128. $hashkey='res'.$data['mac'];
  129. $key=$data['label'];
  130. $json_list= $this->selectHash($hashkey,$key);
  131. if(!$json_list){
  132. $arr=[
  133. "a"=>[['time'=>$data['report_time'],'dirt'=>$res]],
  134. 'status'=> ['time'=>null,'dirt'=>null]
  135. ];
  136. debug_log("InAndOUT","数据不存在,直接存储新数据".json_encode($arr));
  137. $this->setHash($hashkey,$key,$arr);
  138. return;
  139. }else{
  140. $resArray=['time'=>$data['report_time'],'dirt'=>$res];
  141. debug_log("InAndOUT","当前的新数据".json_encode($resArray));
  142. debug_log("InAndOUT","缓存的数据".json_encode($json_list));
  143. $s=$json_list;
  144. //数据列表
  145. $list=$s['a'];
  146. //状态
  147. $status=$s['status'];
  148. //判断时间间隔超过2分钟,则从新计算
  149. if(($resArray['time']-$list[count($list)-1]['time'])>120){
  150. //时间超过两分钟,进行重置
  151. debug_log("InAndOUT","时间超过两分钟,清空数据,载入当前数据");
  152. $list=[];
  153. $status= ['time'=>null,'dirt'=>null];
  154. }
  155. array_push($list,$resArray);
  156. // if(count($list)>3){
  157. // debug_log("InAndOUT","数据超过3条,剔除一条旧数据");
  158. //array_shift($list);
  159. while(count($list)>5){
  160. array_shift($list);
  161. debug_log("InAndOUT","数据超过3条,剔除一条旧数据");
  162. }
  163. //}
  164. debug_log("InAndOUT","当前队列数据:".json_encode($list));
  165. $front=0;
  166. $back=0;
  167. if(count($list)>2){
  168. //
  169. debug_log("InAndOUT","当前队列超过两条可以计算前后:".count($list));
  170. //校验权柄
  171. foreach($list as $item){
  172. if($item['dirt']==1){
  173. $front+=1;
  174. }
  175. if($item['dirt']==2){
  176. $back+=1;
  177. }
  178. }
  179. $res=$front>$back?1:2;
  180. if(empty($status['dirt'])){
  181. $status['dirt']= $res;
  182. $status['time']= $resArray['time'];
  183. debug_log("InAndOUT","无初始化状态,则进行初始化");
  184. debug_log("InAndOUT","初始化状态为:".json_encode($status));
  185. }else{
  186. //进行保存
  187. if($res!=$status['dirt']){
  188. $arr=[
  189. 'mac'=> $data['mac'],
  190. 'label'=>$data['label'],
  191. "begin_time"=>$status['time'],
  192. "end_time"=>$resArray['time'],
  193. 'dirt'=> $status['dirt']==1?1:2
  194. ];
  195. $data_array=[];
  196. $data_array[]=[
  197. "label"=>$data['label'],
  198. 'time'=>$resArray['time'],
  199. 'dirt'=> $status['dirt']==1?1:2
  200. ];
  201. $url_data=[
  202. "mac"=>$data['mac'],
  203. "data"=>$data_array
  204. ];
  205. debug_log("InAndOUT","得出计算结果:".json_encode($arr));
  206. debug_log("InAndOUT","发送给远程".json_encode($url_data));
  207. $url="http://47.114.185.186:8115/api/accessReport";
  208. $url_res= curl_http_post(json_encode($url_data),$url,false);
  209. debug_log("InAndOUT","远程返回结果".$url_res);
  210. // $dor=new Dormitory();
  211. // $dor->save($arr);
  212. debug_log("InAndOUT","得出计算结果:".json_encode($arr));
  213. $status['dirt']= $res;
  214. $status['time']= $resArray['time'];
  215. }else{
  216. debug_log("InAndOUT","无计算结果",json_encode($status));
  217. }
  218. }
  219. }
  220. $arr=array(
  221. "a"=>$list,
  222. 'status'=>$status
  223. );
  224. debug_log("InAndOUT","存入缓存中:".json_encode($arr));
  225. $this->setHash($hashkey,$key,$arr);
  226. debug_log("InAndOUT",'操作结束');
  227. }
  228. }
  229. //查询key hash
  230. public function selectHash($hashKey,$key){
  231. $res= $this->redis->hGet($hashKey,$key);
  232. if(empty($res)){
  233. return false;
  234. }else{
  235. return json_decode($res,true);
  236. }
  237. }
  238. public function setHash($hashKey,$key,$data){
  239. $this->redis->hSet($hashKey,$key,json_encode($data));
  240. }
  241. }