dispose.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. $hashkey='res'.$data['mac'];
  94. $key=$data['label'];
  95. //存入redis的数据
  96. $json_list= $this->selectHash($hashkey,$key);
  97. //最新时间
  98. $time=$data['report_time'];
  99. //是否参与计算
  100. $calculate=true;
  101. if($data['rssi1']==$data['rssi2']){
  102. debug_log("InAndOUT","前后信号相等则进行过滤掉");
  103. $calculate=false;
  104. }
  105. $t1s= $data['rssi1']-$data['rssi2'];
  106. if(abs($t1s)<3){
  107. debug_log("InAndOUT","差值小于3不做计算");
  108. $calculate=false;
  109. }
  110. if($calculate==false){
  111. if(!$json_list){
  112. $arr=[
  113. "a"=>[],
  114. 'status'=> ['time'=>null,'dirt'=>null],
  115. "time"=>$time //最新时间
  116. ];
  117. }else{
  118. $json_list['time']=$time;
  119. }
  120. $this->setHash($hashkey,$key,$json_list);
  121. return;
  122. }
  123. if($data['rssi1']<$data['rssi2']){
  124. $res=1;
  125. }
  126. if($data['rssi1']>$data['rssi2']){
  127. $res=2;
  128. }
  129. if(!$json_list){
  130. $arr=[
  131. "a"=>[['time'=>$time,'dirt'=>$res]],
  132. 'status'=> ['time'=>null,'dirt'=>null],
  133. "time"=>$time //最新时间
  134. ];
  135. debug_log("InAndOUT","数据不存在,直接存储新数据".json_encode($arr));
  136. $this->setHash($hashkey,$key,$arr);
  137. return;
  138. }else{
  139. $resArray=['time'=>$data['report_time'],'dirt'=>$res];
  140. debug_log("InAndOUT","缓存的数据".json_encode($json_list));
  141. $s=$json_list;
  142. //数据列表
  143. $list=$s['a'];
  144. //状态
  145. $status=$s['status'];
  146. array_push($list,$resArray);
  147. while(count($list)>5){
  148. array_shift($list);
  149. debug_log("InAndOUT","数据超过5条,剔除一条旧数据");
  150. }
  151. debug_log("InAndOUT","当前队列数据:".json_encode($list));
  152. $front=0;
  153. $back=0;
  154. if(count($list)>2){
  155. //
  156. debug_log("InAndOUT","当前队列超过两条可以计算前后:".count($list));
  157. //校验权柄
  158. foreach($list as $item){
  159. if($item['dirt']==1){
  160. $front+=1;
  161. }
  162. if($item['dirt']==2){
  163. $back+=1;
  164. }
  165. }
  166. $res=$front>$back?1:2;
  167. if(empty($status['dirt'])){
  168. $status['dirt']= $res;
  169. $status['time']= $resArray['time'];
  170. debug_log("InAndOUT","无初始化状态,则进行初始化");
  171. debug_log("InAndOUT","初始化状态为:".json_encode($status));
  172. }else{
  173. //判断是否连贯
  174. $IS_OK=true;
  175. $res=0;
  176. $reverse=array_reverse($list);
  177. $res=$reverse[0]['dirt'];
  178. foreach($reverse as $aitem){
  179. if($aitem['dirt']!=$res){
  180. $IS_OK=false;
  181. }
  182. }
  183. // if(($reverse[0]['dirt']==$reverse[1]['dirt'])&&($reverse[1]['dirt']==$reverse[2]['dirt'])){
  184. // $res=$reverse[0]['dirt'];
  185. // $IS_OK=true;
  186. // }
  187. //进行保存
  188. if($res!=$status['dirt']&&$IS_OK){
  189. // if(!empty($status['status']['dirt'])){
  190. // if($resArray['time']-$status['status']['time']>5){
  191. $data_array=[];
  192. $data_array[]=[
  193. "label"=>$data['label'],
  194. 'time'=>$resArray['time'],
  195. 'dirt'=> $status['dirt']==1?1:2 //1是进 2是出
  196. ];
  197. $url_data=[
  198. "mac"=>$data['mac'],
  199. "data"=>$data_array
  200. ];
  201. // debug_log("InAndOUT","得出计算结果:".json_encode($arr));
  202. debug_log("InAndOUT","发送给远程".json_encode($url_data));
  203. $url="http://47.114.185.186:8115/api/accessReport";
  204. $url_res= curl_http_post(json_encode($url_data),$url,false);
  205. debug_log("InAndOUT","远程返回结果".$url_res);
  206. debug_log("result","正常计算返回的结果".json_encode($url_data));
  207. $this->temporary_label($this->redis,$data['label'],2);
  208. $status['dirt']=$res;
  209. $status['time']=$time;
  210. }else{
  211. debug_log("InAndOUT","无计算结果",json_encode($status));
  212. }
  213. }
  214. }
  215. $arr=array(
  216. "a"=>$list,
  217. 'status'=>$status,
  218. "time"=>$time
  219. );
  220. debug_log("InAndOUT","存入缓存中:".json_encode($arr));
  221. $this->setHash($hashkey,$key,$arr);
  222. debug_log("InAndOUT",'操作结束');
  223. }
  224. }
  225. //获取所有基站
  226. public function getStations(){
  227. $key=$this->stations;
  228. $list= $this->redis->hKeys($key);
  229. return $list;
  230. }
  231. //存入基站号
  232. public function setStations($Stations){
  233. $key=$this->stations;
  234. if(!$this->redis->hExists($key,$Stations)){
  235. $this->redis->hSet($key,$Stations,0);
  236. }
  237. }
  238. //排序
  239. public function sortlist($list){
  240. $list01=[];
  241. $list02=[];
  242. $list03=[];
  243. $list04=[];
  244. debug_log("InAndOUT","排序的数据:".json_encode($list));
  245. foreach($list as $item){
  246. array_push($list01,$item['rssi1']);
  247. array_push($list02,$item['rssi2']);
  248. array_push($list03,$item['rssi3']);
  249. array_push($list04,$item['rssi4']);
  250. }
  251. if(count($list)>=3){
  252. sort($list01);
  253. sort($list02);
  254. sort($list03);
  255. sort($list04);
  256. array_pop($list01);
  257. array_pop($list02);
  258. array_pop($list03);
  259. array_pop($list04);
  260. array_shift($list01);
  261. array_shift($list02);
  262. array_shift($list03);
  263. array_shift($list04);
  264. }
  265. debug_log("InAndOUT","排序后的队列:".json_encode($list01));
  266. debug_log("InAndOUT","排序后的队列:".json_encode($list02));
  267. debug_log("InAndOUT","排序后的队列:".json_encode($list03));
  268. debug_log("InAndOUT","排序后的队列:".json_encode($list04));
  269. $rssi1= array_sum($list01)/count($list01);
  270. $rssi2= array_sum($list02)/count($list02);
  271. $rssi3= array_sum($list03)/count($list03);
  272. $rssi4= array_sum($list04)/count($list04);
  273. $res=[$rssi1,$rssi2,$rssi3,$rssi4];
  274. debug_log("InAndOUT","结算后的结果:".json_encode($res));
  275. return $res;
  276. }
  277. //查询key hash
  278. public function selectHash($hashKey,$key){
  279. $res= $this->redis->hGet($hashKey,$key);
  280. if(empty($res)){
  281. return false;
  282. }else{
  283. return json_decode($res,true);
  284. }
  285. }
  286. public function setHash($hashKey,$key,$data){
  287. $this->redis->hSet($hashKey,$key,json_encode($data));
  288. }
  289. public function temporary_label($redis,$label,$type){
  290. $key="temporary_label";
  291. if($type==1){
  292. $res= $redis->hGet($key,$label);
  293. if($res==false){
  294. $redis->hSet($key,$label,1);
  295. }
  296. }else{
  297. $redis->hSet($key,$label,2);
  298. }
  299. }
  300. }