dispose.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $hashKey=$data['mac'].'station';
  13. $key=$data['label'];
  14. $list=[];
  15. $length=0;
  16. $res= $this->selectHash($hashKey,$key);
  17. if($res==false){
  18. debug_log("InAndOUT","redis没有数据");
  19. array_push($list,$data);
  20. debug_log("InAndOUT","存入数据".$data);
  21. }else{
  22. debug_log("InAndOUT","redis初始数据",json_encode($res));
  23. $list=$res;
  24. debug_log("InAndOUT","上次上报的数据 :".json_encode($list[count($list)-1],true));
  25. debug_log("InAndOUT","存入数据".$data);
  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. if(count($list)>5){
  34. debug_log("InAndOUT","数据长度大于5,剔除一条");
  35. debug_log("InAndOUT","剔除的一条数据为:".json_encode($list[0]));
  36. array_shift($list);
  37. }
  38. }
  39. }
  40. //数据
  41. debug_log("InAndOUT","新数据长度 :".json_encode($list));
  42. //现在的数据长度
  43. $count=count($list);
  44. debug_log("InAndOUT","新数据长度 :".$count);
  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. $len01=0;
  53. $len02=0;
  54. $len03=0;
  55. $len04=0;
  56. $rssi1=0;
  57. $rssi2=0;
  58. $rssi3=0;
  59. $rssi4=0;
  60. foreach($list as $item){
  61. if(!empty($config)){
  62. if($config['front']&&$config['front']>$item["rssi1"]){
  63. $rssi1+=$item["rssi1"];
  64. $len01+=1;
  65. }
  66. if($config['behind']&&$config['behind']>$item["rssi2"]){
  67. $rssi2+=$item["rssi2"];
  68. $len02+=1;
  69. }
  70. if($config['left']&&$config['left']>$item["rssi3"]){
  71. $rssi3+=$item["rssi3"];
  72. $len03+=1;
  73. }
  74. if($config['right']&&$config['right']>$item["rssi4"]){
  75. $rssi4+=$item["rssi4"];
  76. $len04+=1;
  77. }
  78. }else{
  79. $rssi1=$rssi1+$item["rssi1"];
  80. $rssi2=$rssi2+$item["rssi2"];
  81. $rssi3=$rssi3+$item["rssi3"];
  82. $rssi4=$rssi4+$item["rssi4"];
  83. $len01+=1;
  84. $len02+=1;
  85. $len03+=1;
  86. $len04+=1;
  87. }
  88. }
  89. $data["rssi1"]=number_format($rssi1/$len01,'2');
  90. $data["rssi2"]=number_format($rssi2/$len02,'2');
  91. $data["rssi3"]=number_format($rssi3/$len03,'2');
  92. $data["rssi4"]=number_format($rssi4/$len04,'2');
  93. debug_log("InAndOUT","计算出的平均数据 :".json_encode($data));
  94. //保存到hash中
  95. $this->setHash($hashKey,$key,$list);
  96. return $data;
  97. }
  98. //计算结果设计
  99. public function computeResout($data){
  100. //前后左右$arr=[$data['resi1'],$data['resi2'],$data['resi3'],$data['resi4']];
  101. //前后
  102. // $redis=$this->redis;
  103. // $arr=[$data['resi1'],$data['resi2']];
  104. // sort($arr);
  105. $res=0;
  106. if($data['rssi1']==$data['rssi2']){
  107. debug_log("InAndOUT","前后信号相等则进行过滤掉");
  108. return;
  109. }
  110. if($data['rssi1']<$data['rssi2']){
  111. $res=1;
  112. }
  113. if($data['rssi1']>$data['rssi2']){
  114. $res=2;
  115. }
  116. debug_log("InAndOUT","信号强度 :".$res);
  117. $hashkey='res'.$data['mac'];
  118. $key=$data['label'];
  119. $json_list= $this->selectHash($hashkey,$key);
  120. if(!$json_list){
  121. $arr=[
  122. "a"=>[['time'=>$data['report_time'],'dirt'=>$res]],
  123. 'status'=> ['time'=>null,'dirt'=>null]
  124. ];
  125. debug_log("InAndOUT","数据不存在,直接存储新数据".json_encode($arr));
  126. $this->setHash($hashkey,$key,$arr);
  127. return;
  128. }else{
  129. $resArray=['time'=>$data['report_time'],'dirt'=>$res];
  130. debug_log("InAndOUT","当前的新数据".json_encode($resArray));
  131. debug_log("InAndOUT","缓存的数据".json_encode($json_list));
  132. $s=$json_list;
  133. //数据列表
  134. $list=$s['a'];
  135. //状态
  136. $status=$s['status'];
  137. //判断时间间隔超过2分钟,则从新计算
  138. if(($resArray['time']-$list[count($list)-1]['time'])>120){
  139. //时间超过两分钟,进行重置
  140. debug_log("InAndOUT","时间超过两分钟,清空数据,载入当前数据");
  141. $list=[];
  142. $status= ['time'=>null,'dirt'=>null];
  143. }
  144. array_push($list,$resArray);
  145. if(count($list)>5){
  146. debug_log("InAndOUT","数据超过五条,剔除一条旧数据");
  147. array_shift($list);
  148. }
  149. debug_log("InAndOUT","时间超过两分钟,清空数据,载入当前数据");
  150. debug_log("InAndOUT","当前队列数据",json_encode($list));
  151. $front=0;
  152. $back=0;
  153. if(count($list)>2){
  154. //
  155. debug_log("InAndOUT","当前队列超过两条可以计算前后:".count($list));
  156. //校验权柄
  157. foreach($list as $item){
  158. if($item['dirt']==1){
  159. $front+=1;
  160. }
  161. if($item['dirt']==2){
  162. $back+=1;
  163. }
  164. }
  165. $res=$front>=$back?1:2;
  166. if(empty($status['dirt'])){
  167. $status['dirt']= $res;
  168. $status['time']= $resArray['time'];
  169. debug_log("InAndOUT","无初始化状态,则进行初始化");
  170. debug_log("InAndOUT","初始化数据为:".json_encode($resArray));
  171. debug_log("InAndOUT","初始化数据为:".json_encode($status));
  172. }else{
  173. //进行保存
  174. if($res!=$status['dirt']){
  175. $arr=[
  176. 'mac'=> $data['mac'],
  177. 'label'=>$data['label'],
  178. "begin_time"=>$status['time'],
  179. "end_time"=>$resArray['time'],
  180. 'dir'=> $status['dirt']==1?1:2
  181. ];
  182. // $dor=new Dormitory();
  183. // $dor->save($arr);
  184. debug_log("InAndOUT","得出计算结果",json_encode($arr));
  185. $status['dirt']= $res;
  186. $status['time']= $resArray['time'];
  187. }else{
  188. debug_log("InAndOUT","无计算结果",json_encode($status));
  189. }
  190. }
  191. }
  192. $arr=array(
  193. "a"=>$list,
  194. 'status'=>$status
  195. );
  196. debug_log("InAndOUT","存入缓存中",json_encode($arr));
  197. $this->setHash($hashkey,$key,$arr);
  198. debug_log("InAndOUT",'操作结束');
  199. }
  200. }
  201. //查询key hash
  202. public function selectHash($hashKey,$key){
  203. $res= $this->redis->hGet($hashKey,$key);
  204. if(empty($res)){
  205. return false;
  206. }else{
  207. return json_decode($res,true);
  208. }
  209. }
  210. public function setHash($hashKey,$key,$data){
  211. $this->redis->hSet($hashKey,$key,json_encode($data));
  212. }
  213. }