Api.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. <?php
  2. namespace catchAdmin\api\controller;
  3. use catchAdmin\api\service\dispose;
  4. use catchAdmin\tag_history\model\Access;
  5. use catchAdmin\tag_history\model\Dormitory;
  6. use catchAdmin\tag_history\model\TagHistory;
  7. use catchAdmin\worklocation\model\WorkRecord as ModelWorkRecord;
  8. use catcher\base\CatchRequest as Request;
  9. use catcher\CatchResponse;
  10. use catcher\base\CatchController;
  11. use Exception;
  12. use think\facade\Cache;
  13. use \think\facade\Db;
  14. error_reporting(0);
  15. class Api extends CatchController
  16. {
  17. public function report()
  18. {
  19. $token = $_GET['token'];
  20. if (!$token || $token != '22723927C') {
  21. json_fail('缺少token或者token不对');
  22. }
  23. $param = json_decode(optimize_json(file_get_contents("php://input")), true);
  24. debug_log("上报数据",json_encode($param));
  25. if (json_last_error() != 0) {
  26. json_fail('解析异常', json_last_error_msg());
  27. }
  28. $mac = $param['mac'];
  29. if (empty($mac)) {
  30. json_fail('mac地址不存在');
  31. }
  32. $data = [];
  33. foreach ($param['list'] as $item) {
  34. // $onetime= TagHistory::where("mac",$mac)->where('lable',$item['label'])->order("id","desc")->value("time");
  35. $data[] = [
  36. 'mac' => $mac,
  37. 'lable' => $item['label'],
  38. 'rssi' => $item['rssi'],
  39. 'time' => $item['time'],
  40. 'addTime' => time(),
  41. 'move' => $item['move'],
  42. 'step' => empty($item['step']) ? 0 : $item['step'],
  43. 'temperature' => empty($item['temperature']) ? 0 : $item['temperature'],
  44. //告警
  45. 'temperWarn' => empty($item['temperWarn']) ? 00 : $item['temperWarn'],
  46. // "interval_time"=>empty($onetime) ?0:($item['time']-$onetime)
  47. ];
  48. }
  49. $tage = new TagHistory();
  50. $tage->saveAll($data);
  51. json_success('上传成功');
  52. }
  53. //储存数据到redis
  54. public function redis_to_mysql(){
  55. $ues_redis=Cache::store('redis')->handler();
  56. $dispose = new dispose($ues_redis);
  57. $text=null;
  58. while(1){
  59. $jsonData= $ues_redis->rpop("mqtt_data");
  60. if(empty($jsonData)){
  61. sleep(1);
  62. continue;
  63. }
  64. debug_log("InAndOUT",'数据redis'.$jsonData);
  65. $data=json_decode($jsonData,true);
  66. $time=$data['time'];
  67. $mac=$data['devId'];
  68. //存入基站的最新时间
  69. $dispose->setStations($mac,$time);
  70. $list=[];
  71. $text=$text.$data['cnt'];
  72. if(strlen($text)<24){
  73. continue;
  74. }
  75. while(strlen($text)>=24){
  76. $da=substr($text,0,24);
  77. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  78. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  79. if($t1&&$t2){
  80. $label= substr($da,0,8);
  81. $rssi1= substr($da,16,2);
  82. $rssi2= substr($da,18,2);
  83. $rssi3= substr($da,20,2);
  84. $rssi4= substr($da,22,2);
  85. $DA=[
  86. 'mac' => $mac,
  87. 'label' =>$label,
  88. 'rssi1' => hexdec($rssi1),
  89. 'rssi2' => hexdec($rssi2),
  90. 'rssi3' => hexdec($rssi3),
  91. 'rssi4' => hexdec($rssi4),
  92. 'report_time'=>$time
  93. ];
  94. $vs=false;
  95. $history_status=false;
  96. $DA['other_time']=date("Y-m-d H:i:s",$time);
  97. //打印原始的数据日志
  98. debug_log("label_log","cssh: ".json_encode($DA));
  99. if($DA['rssi1']<=85||$DA['rssi2']<=85||$DA['rssi3']<=85||$DA['rssi4']<=85){
  100. //原始数据只要其中有一条信号小于85 都存入redis 历史数据
  101. $history_status=true;
  102. }
  103. if($DA['rssi1']<=72||$DA['rssi2']<=72||$DA['rssi3']<=72||$DA['rssi4']<=72){
  104. //原始数据只要其中有一条信号小于72 都参与计算
  105. $vs=true;
  106. }
  107. $DA['rssi1']=$DA['rssi1']==255?100:$DA['rssi1'];
  108. $DA['rssi2']=$DA['rssi2']==255?100:$DA['rssi2'];
  109. $DA['rssi3']=$DA['rssi3']==255?100:$DA['rssi3'];
  110. $DA['rssi4']=$DA['rssi4']==255?100:$DA['rssi4'];
  111. $DA["rssi1"]=$DA["rssi1"]<=$DA["rssi3"]?$DA["rssi1"]:$DA["rssi3"];
  112. $DA["rssi2"]=$DA["rssi2"]<=$DA["rssi4"]?$DA["rssi2"]:$DA["rssi4"];
  113. /**
  114. * 缓存到redis中历史数据
  115. */
  116. if(substr($da,8,2)=='01'&&$history_status&&$DA["rssi1"]!=$DA["rssi2"]){
  117. debug_log("label_log","cache: ".json_encode($DA));
  118. $diff= $DA["rssi1"]-$DA["rssi2"];
  119. if(abs($diff)>2){
  120. //历史数据朝前和朝后的信号的差值大于2参与存储
  121. $dispose->set_label_history($DA);
  122. }
  123. debug_log("label_log","cache_diff: ".$diff);
  124. }
  125. /**
  126. * 参与计算的数据
  127. */
  128. if($vs&&(substr($da,8,2)=='01')){
  129. try{
  130. $DA= $dispose->check_data($DA);
  131. //处理的数据参与计算
  132. debug_log("label_log","xsj:".json_encode($DA));
  133. $dispose->computeResout($DA);
  134. }catch(Exception $e){
  135. debug_log("InAndOUT","抛出异常:".$e->getMessage());
  136. }
  137. }
  138. $text=substr($text,24);
  139. }else{
  140. $text=substr($text,1);
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * 清除数据
  147. *
  148. * @return void
  149. */
  150. public function clear_redis(){
  151. //清理超时的标签
  152. while(1){
  153. sleep(1);
  154. $redis=Cache::store('redis')->handler();
  155. $dispose = new dispose($redis);
  156. $keylist= $dispose->getStations();
  157. //获取存储的的基站
  158. debug_log("clear_label","查询数据".json_encode($keylist));
  159. if(empty($keylist)){
  160. continue;
  161. }
  162. foreach($keylist as $station){
  163. //结果key值
  164. $key="res".$station;
  165. //存取旧的数据的reids数据
  166. $catchkey=$station."station";
  167. //获取该基站的所有key
  168. $keys= $redis->hKeys($key);
  169. if(empty($keys)){
  170. continue;
  171. }
  172. foreach($keys as $item){
  173. if( $json_data=$redis->hget($key,$item)){
  174. debug_log("clear_label",'基站'.$station.'获取的数据'.'标签'.$item.$json_data);
  175. $json_data=json_decode($json_data,true);
  176. //列表
  177. $a=$json_data['a'];
  178. //状态
  179. $status=$json_data['status'];
  180. //是否形成进出考勤
  181. $in_and_out=$json_data["in_and_out"];
  182. //总数
  183. $count= count($a);
  184. $time= $dispose->getNowStationTime($station);
  185. debug_log("clear_label","标签最新最新时间". date('Y-m-d H:i:s', $time));
  186. //如果标签间隔超过7s,则超时并判断能否生成进出
  187. if(($time-$json_data["time"])<=7){
  188. debug_log("clear_label", $item.':'. "标签上报间隔没有超过7秒进行跳过");
  189. continue;
  190. }
  191. if($count<5){
  192. //如果信号条数小于等于2条不参与计算,信号条数大于2小于5条,则直接进行判断是否形成进出
  193. if($count>2){
  194. debug_log("clear_label", $item.':'. "标签信号个数等于超过2个,参与计算");
  195. $frist=$a[0]['dirt'];
  196. $end=$a[$count-1]['dirt'];
  197. if($frist!=$end){
  198. debug_log("clear_label", $item.':'. "标签信号个数等于超过2个,参与计算,得出结果:".$frist);
  199. $dispose->network_push($station,$item,$a[$count-1]['time'],$frist==1?1:2);
  200. }else{
  201. debug_log("clear_label", $item.':'. "标签信号个数没有超过2个,全部去清除");
  202. }
  203. }
  204. }else{
  205. //如果信号条数大于5条,则判断最后三条数据的信号强的方向是否和初始方向相反
  206. $front=0;
  207. $back=0;
  208. $res=0;
  209. if($a[$count-3]['dirt']==1){
  210. $front+=1;
  211. }else{
  212. $back+=1;
  213. }
  214. if($a[$count-2]['dirt']==1){
  215. $front+=1;
  216. }else{
  217. $back+=1;
  218. }
  219. if($a[$count-1]['dirt']==1){
  220. $front+=1;
  221. }else{
  222. $back+=1;
  223. }
  224. if($front>$back){
  225. $res=1;
  226. }else{
  227. $res=2;
  228. }
  229. if($status['dirt']!=$res){
  230. //最后三条数据的信号强的方向和初始方向相反,则形成进出考勤
  231. debug_log("clear_label", $item.':'. "最后一个信号,方向相反,得出结果,清除缓存数据");
  232. $dispose->network_push($station,$item,$a[$count-1]['time'],$status['dirt']==1?1:2);
  233. }else{
  234. //最后三条数据的信号强的方向和初始方向不相反,并且在连续的时间内没有生成考勤,则开始第二次判断
  235. if(empty($in_and_out)){
  236. //判断第二次能否生成考勤结果
  237. $res=$dispose->second_create_direction($station,$item);
  238. if(!empty($res)){
  239. //获取该标签的之前最新的考勤,和当前形成考勤时间间隔是否大于10,
  240. //如果大于10则生成考勤结果
  241. $res_time= $dispose->get_time_results($station,$item);
  242. $res_time= empty($res_time)?0:$res_time;
  243. $diff_time=$res['time']-$res_time;
  244. debug_log("clear_label", $item.':'. "间隔时间".$diff_time);
  245. //判断如果结果超过时间
  246. if($diff_time>10){
  247. debug_log("clear_label", $item.':'. "二次生成进出");
  248. $dispose->network_push($station,$item,$res['time'],$res['dirt']);
  249. }
  250. }else{
  251. //二次判断如果也没生成考勤,如果朝前的信号最大强度低于65,
  252. //朝后的信号最大强度也低于65,则记录下来最后的朝向,
  253. //存入没有生成考勤时的初始数据
  254. //获取朝前的最大值
  255. //获取朝后的最大值
  256. $frontMax= $dispose->get_label_history(4,$station,$item);
  257. $backMax= $dispose->get_label_history(5,$station,$item);
  258. $records=false;
  259. if($status['dirt']==1){
  260. $records=$frontMax<65;
  261. }
  262. if($status['dirt']==2){
  263. $records=$backMax<65;
  264. }
  265. if($records){
  266. $dispose->no_check_data($station,$item,$status['dirt'],$a[$count-1]['time'],1);
  267. }
  268. }
  269. }
  270. //二次进出
  271. debug_log("clear_label", $item.':'. "最后一个信号,方向没有变化,清除缓存数据");
  272. }
  273. }
  274. //清除对应的键值对
  275. //清空头部尾部历史
  276. $dispose->get_label_history(3,$station,$item);
  277. $redis->hdel($key,$item);
  278. $redis->hdel($catchkey,$item);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. /**
  285. * 远程推送数据
  286. *
  287. * @return void
  288. */
  289. public function remotePush(){
  290. while(true){
  291. $redis=Cache::store('redis')->handler();
  292. $key="push_data";
  293. $url="http://localhost:8115/api/accessReport";
  294. sleep(2);
  295. while($data=$redis->lPop($key)){
  296. if(!empty($data)){
  297. debug_log($key,"远程推送的数据".$data);
  298. $url_res= curl_http_post($data,$url,false);
  299. $num=1;
  300. while($url_res==false){
  301. $num++;
  302. $url_res= curl_http_post($data,$url,false);
  303. if($num>=3){
  304. debug_log($key,"远程推送的地址".$url);
  305. debug_log($key,"连续推送三次失败,远程推送的数据".$data);
  306. break;
  307. }
  308. }
  309. debug_log($key,"远程返回结果".$url_res);
  310. }
  311. }
  312. }
  313. }
  314. public function redis_to_mysql_parea(){
  315. $ues_redis=Cache::store('redis')->handler();
  316. $text="";
  317. $dispose = new dispose($ues_redis);
  318. while(1){
  319. $jsonData= $ues_redis->rpop("mqtt_data_parea4rssi");
  320. debug_log("pare_InAndOUT",'数据redis'.$jsonData);
  321. if(empty($jsonData)){
  322. debug_log("pare_InAndOUT",'没有数据');
  323. sleep(1);
  324. continue;
  325. }
  326. // debug_log("InAndOUT",$jsonData);
  327. $data=json_decode($jsonData,true);
  328. // debug_log("InAndOUT",$data['cnt']);
  329. $time=$data['time'];
  330. $mac=$data['devId'];
  331. $text=$text.$data['cnt'];
  332. if(strlen($text)<24){
  333. continue;
  334. }
  335. $text = $this->handleMessage($text, $mac, $time, $ues_redis);
  336. }
  337. }
  338. public function redis_to_mysql_indoor(){
  339. $ues_redis=Cache::store('redis')->handler();
  340. $text="";
  341. $dispose = new dispose($ues_redis);
  342. while(1){
  343. $jsonData= $ues_redis->lpop("mqtt_data_indoor4rssi");
  344. debug_log("indoor_InAndOUT",'mqtt数据redis'.$jsonData);
  345. if(!$jsonData){
  346. debug_log("indoor_InAndOUT",'mqtt没有数据');
  347. sleep(1);
  348. continue;
  349. }
  350. // debug_log("InAndOUT",$jsonData);
  351. $data=json_decode($jsonData,true);
  352. // debug_log("InAndOUT",$data['cnt']);
  353. $time=$data['time'];
  354. $mac=$data['devId'];
  355. $text=$text.$data['cnt'];
  356. if(strlen($text)<24){
  357. continue;
  358. }
  359. // var_dump("before",microtime());
  360. $text = $this->handleMessageIndoor($text, $mac, $time, $ues_redis);
  361. // var_dump("after",microtime());
  362. }
  363. }
  364. //打印日志
  365. public function temporary_label_log(){
  366. $redis=Cache::store('redis')->handler();
  367. // $key="temporary_label";
  368. // $list= $redis->HgetAll($key);
  369. // debug_log($key,json_encode($list));
  370. // debug_log($key,"所有的标签");
  371. // foreach($list as $k=>$value){
  372. // debug_log($key,json_encode([$k=>$value]));
  373. // }
  374. // debug_log($key,"所有总数".count($list));
  375. // debug_log($key,"上报的标签");
  376. // $i=0;
  377. // foreach($list as $k=>$value){
  378. // if($value==2){
  379. // debug_log($key,json_encode([$k=>$value]));
  380. // $i++;
  381. // }
  382. // }
  383. // debug_log($key,"正常上报的标签总数".$i);
  384. // debug_log($key,"补出的标签");
  385. // $t=0;
  386. // foreach($list as $k=>$value){
  387. // if($value==3){
  388. // $t++;
  389. // debug_log($key,json_encode([$k=>$value]));
  390. // }
  391. // }
  392. // debug_log($key,"补出的标签总数".$t);
  393. // debug_log($key,"没有上报的的标签");
  394. // $f=0;
  395. // foreach($list as $k=>$value){
  396. // if($value==1){
  397. // debug_log($key,json_encode([$k=>$value]));
  398. // $f++;
  399. // }
  400. // }
  401. // debug_log($key,"没有上报的的标签总数".$f);
  402. // // $redis->del($key);
  403. $dis= new dispose($redis);
  404. $dis->analysisLog();
  405. }
  406. // public function temporary_label($redis,$label,$type){
  407. // $key="temporary_label";
  408. // if($type==1){
  409. // $res= $redis->hGet($key,$label);
  410. // if($res==false){
  411. // $redis->hSet($key,$label,1);
  412. // }
  413. // }else{
  414. // $redis->hSet($key,$label,3);
  415. // }
  416. // }
  417. //消息处理--重点区域
  418. public function handleMessage($text, $mac, $time, $ues_redis){
  419. while(strlen($text)>=24){
  420. $da=substr($text,0,24);
  421. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  422. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  423. if($t1&&$t2){
  424. // var_dump($text);
  425. $rfid= substr($da,0,8);
  426. $rssi1= hexdec(substr($da,16,2));
  427. $rssi2= hexdec(substr($da,18,2));
  428. $rssi3= hexdec(substr($da,20,2));
  429. $rssi4= hexdec(substr($da,22,2));
  430. // $rc1 = ($rssi1 == 255 ? 0 : 1);
  431. // $rc2 = ($rssi2 == 255 ? 0 : 1);
  432. // $rc3 = ($rssi3 == 255 ? 0 : 1);
  433. // $rc4 = ($rssi4 == 255 ? 0 : 1);
  434. $DA=[
  435. 'mac' => $mac,
  436. 'label' =>$rfid,
  437. 'rssi1' => $rssi1,
  438. 'rssi2' => $rssi2,
  439. 'rssi3' => $rssi3,
  440. 'rssi4' => $rssi4,
  441. 'report_time'=>$time
  442. ];
  443. $field = $mac."_".$rfid;
  444. $info = $ues_redis->hget("parea_rfidinfos",$field);
  445. debug_log("pare_InAndOUT","数据:".json_encode($DA,true));
  446. if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
  447. debug_log("pare_InAndOUT","无效数据:".json_encode($DA,true));
  448. }else{
  449. if(!$info){
  450. $info = [
  451. "mac" => $mac,
  452. "id" => $rfid,
  453. "firs_time" => $time,
  454. "time" => $time,
  455. "status" => 2,//2出状态,1进状态
  456. "rssi" => [
  457. [
  458. "front" => $rssi1,
  459. "behind" => $rssi2,
  460. "left" => $rssi3,
  461. "right" => $rssi4,
  462. "time" => $time
  463. ]
  464. ]
  465. ];
  466. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  467. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  468. }else{
  469. $info = json_decode($info,true);
  470. $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  471. if (!$config) {
  472. # code...
  473. $config = [
  474. "front_in" => 80,
  475. "behind_in" => 80,
  476. "left_in" => 80,
  477. "right_in" => 80,
  478. "front_out" => 255,
  479. "behind_out" => 255,
  480. "left_out" => 255,
  481. "right_out" => 255,
  482. "expInt" => 12,
  483. "effSigNum" => 6
  484. ];
  485. }else{
  486. $config = json_decode($config,true);
  487. }
  488. // var_dump("+++++++++++++++++",$config);
  489. $info["time"] = $time;
  490. $newRssi = [
  491. "front" => $rssi1,
  492. "behind" => $rssi2,
  493. "left" => $rssi3,
  494. "right" => $rssi4,
  495. "time" => $time
  496. ];
  497. $rssisArr = $info["rssi"];
  498. // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
  499. foreach ($rssisArr as $k => $v) {
  500. # code...
  501. if( ($time - $v["time"] ) > $config['expInt'] ){
  502. unset($rssisArr[$k]);
  503. }
  504. }
  505. // var_dump("+++++++++++++++++",$rssisArr);
  506. if(count($rssisArr) == $config['effSigNum']){
  507. array_shift($rssisArr);
  508. array_push($rssisArr, $newRssi);
  509. }else{
  510. array_push($rssisArr, $newRssi);
  511. }
  512. $rssisArr =array_values($rssisArr);
  513. // var_dump("+++++++++++++++++",$rssisArr);
  514. $info["rssi"] = $rssisArr;
  515. // var_dump("------------",$info["rssi"]);
  516. $cacCount = count($rssisArr);
  517. // if($cacCount > $EFF_SIG_NUM){
  518. $sum1 = 0;
  519. $sum2 = 0;
  520. $sum3 = 0;
  521. $sum4 = 0;
  522. foreach ($rssisArr as $k => $v) {
  523. # code...
  524. $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
  525. $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
  526. $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
  527. $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
  528. }
  529. $info["avg"] = [
  530. "front" => number_format( $sum1/$cacCount, 2 ),
  531. "behind" => number_format( $sum2/$cacCount, 2 ),
  532. "left" => number_format( $sum3/$cacCount, 2 ),
  533. "right" => number_format( $sum4/$cacCount, 2 ),
  534. ];
  535. // var_dump($cacCount, $EFF_SIG_NUM, "dddddddd");
  536. if ($cacCount == $config['effSigNum']) {
  537. # code...
  538. $info = $this->compAndPush($info ,$config, $ues_redis);
  539. }
  540. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  541. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  542. }
  543. }
  544. $text=substr($text,24);
  545. }else{
  546. $text=substr($text,1);
  547. }
  548. }
  549. return $text ?$text : "";
  550. }
  551. //消息处理--室内
  552. public function handleMessageIndoor($text, $mac, $time, $ues_redis){
  553. while(strlen($text)>=24){
  554. $da=substr($text,0,24);
  555. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  556. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  557. if($t1&&$t2){
  558. // var_dump($text);
  559. $rfid= substr($da,0,8);
  560. $rssi1= hexdec(substr($da,16,2));
  561. $rssi2= hexdec(substr($da,18,2));
  562. $rssi3= hexdec(substr($da,20,2));
  563. $rssi4= hexdec(substr($da,22,2));
  564. $DA=[
  565. 'mac' => $mac,
  566. 'label' =>$rfid,
  567. 'rssi1' => $rssi1,
  568. 'rssi2' => $rssi2,
  569. 'rssi3' => $rssi3,
  570. 'rssi4' => $rssi4,
  571. 'report_time'=>$time
  572. ];
  573. $field = $mac."_".$rfid;
  574. $info = $ues_redis->hget("indoor_rfidinfos",$field);
  575. debug_log("indoor_InAndOUT","数据:".json_encode($DA,true));
  576. if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
  577. debug_log("indoor_InAndOUT","无效数据:".json_encode($DA,true));
  578. }else{
  579. if(!$info){
  580. $info = [
  581. "mac" => $mac,
  582. "id" => $rfid,
  583. "first_time" => $time,
  584. "time" => $time,
  585. "status" => 2,//2出状态,1进状态
  586. "rssi" => [
  587. [
  588. "front" => $rssi1,
  589. "behind" => $rssi2,
  590. "left" => $rssi3,
  591. "right" => $rssi4,
  592. "time" => $time
  593. ]
  594. ]
  595. ];
  596. $ues_redis->hset("indoor_rfidinfos",$field, json_encode($info,true));
  597. debug_log("indoor_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  598. }else{
  599. $info = json_decode($info,true);
  600. $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  601. if (!$config) {
  602. # code...
  603. $config = [
  604. "front_in" => 80,
  605. "behind_in" => 80,
  606. "left_in" => 80,
  607. "right_in" => 80,
  608. "front_out" => 255,
  609. "behind_out" => 255,
  610. "left_out" => 255,
  611. "right_out" => 255,
  612. "expInt" => 12,
  613. "effSigNum" => 6
  614. ];
  615. }else{
  616. $config = json_decode($config,true);
  617. }
  618. // var_dump("+++++++++++++++++",$config);
  619. $info["time"] = $time;
  620. $newRssi = [
  621. "front" => $rssi1,
  622. "behind" => $rssi2,
  623. "left" => $rssi3,
  624. "right" => $rssi4,
  625. "time" => $time
  626. ];
  627. $rssisArr = $info["rssi"];
  628. // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
  629. foreach ($rssisArr as $k => $v) {
  630. # code...
  631. if( ($time - $v["time"] ) > $config['expInt'] ){
  632. unset($rssisArr[$k]);
  633. }
  634. }
  635. // var_dump("+++++++++++++++++",$rssisArr);
  636. if(count($rssisArr) == $config['effSigNum']){
  637. array_shift($rssisArr);
  638. array_push($rssisArr, $newRssi);
  639. }else{
  640. array_push($rssisArr, $newRssi);
  641. }
  642. $rssisArr =array_values($rssisArr);
  643. // var_dump("+++++++++++++++++",$rssisArr);
  644. $info["rssi"] = $rssisArr;
  645. // var_dump("------------",$info["rssi"]);
  646. $cacCount = count($rssisArr);
  647. // if($cacCount > $EFF_SIG_NUM){
  648. $sum1 = 0;
  649. $sum2 = 0;
  650. $sum3 = 0;
  651. $sum4 = 0;
  652. foreach ($rssisArr as $k => $v) {
  653. # code...
  654. $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
  655. $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
  656. $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
  657. $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
  658. }
  659. $info["avg"] = [
  660. "front" => number_format( $sum1/$cacCount, 2 ),
  661. "behind" => number_format( $sum2/$cacCount, 2 ),
  662. "left" => number_format( $sum3/$cacCount, 2 ),
  663. "right" => number_format( $sum4/$cacCount, 2 ),
  664. ];
  665. // var_dump($cacCount, $EFF_SIG_NUM, "dddddddd");
  666. if ($cacCount == $config['effSigNum']) {
  667. # code...
  668. $info = $this->compAndPushIndoor($info ,$config, $ues_redis);
  669. }
  670. $ues_redis->hset("indoor_rfidinfos",$field, json_encode($info,true));
  671. debug_log("indoor_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  672. }
  673. }
  674. $text=substr($text,24);
  675. }else{
  676. $text=substr($text,1);
  677. }
  678. }
  679. return $text ?$text : "";
  680. }
  681. //判断进出和推送--室内考勤
  682. public function compAndPushIndoor($info, $config, $ues_redis){
  683. //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
  684. // $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  685. //{"front_in":44,"front_out":44,"behind_in":50,"behind_out":50,"left_in":55,"left_out":55,"right_out":44,"right_out":44}
  686. $avg = $info['avg'];
  687. $status = $info['status'];
  688. $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
  689. $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
  690. $url="http://localhost:8115/api/api/accessClassReport";
  691. // $url="http://61.175.203.188:10001/api/accessClassReport";
  692. // if($flagIn && $status == 2){
  693. if($flagIn){
  694. if ($info["pushTime"] && (time()-$info['pushTime']) < 120 ) {
  695. # code...
  696. return $info;
  697. }
  698. //推送进
  699. $info['status'] = 1;
  700. $url_data = [
  701. "mac" => $info["mac"],
  702. "data" => [
  703. [
  704. "label" => $info["id"],
  705. "time" => $info["time"],
  706. "dirt" => 1,
  707. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  708. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  709. ]
  710. ]
  711. ];
  712. $ues_redis->lpush("indoor_inoutres_push", json_encode($url_data));
  713. $info['pushTime'] = time();
  714. // debug_log("push_indoor_res","进推送依赖:".json_encode($info));
  715. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  716. // debug_log("push_indoor_res","远程返回结果".$url_res);
  717. }
  718. // if($info['id'] == "E2B15AD5"){
  719. // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
  720. // }
  721. if($flagOut && $status == 1){
  722. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  723. # code...
  724. return $info;
  725. }
  726. //推送出
  727. $info['status'] = 2;
  728. $url_data = [
  729. "mac" => $info["mac"],
  730. "data" => [
  731. [
  732. "label" => $info["id"],
  733. "time" => $info["time"],
  734. "dirt" => 2,
  735. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  736. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  737. ]
  738. ]
  739. ];
  740. $ues_redis->lpush("indoor_inoutres_push", json_encode($url_data));
  741. // debug_log("push_indoor_res","出推送依赖:".json_encode($info));
  742. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  743. $info['pushTime'] = time();
  744. // debug_log("push_indoor_res","远程返回结果".$url_res);
  745. }
  746. return $info;
  747. }
  748. //判断进出和推送
  749. public function compAndPush($info, $config,$ues_redis){
  750. //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
  751. $avg = $info['avg'];
  752. $status = $info['status'];
  753. $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
  754. $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
  755. if($flagIn && $status == 2){
  756. // if($flagIn){
  757. $inoutIntFilter = $ues_redis->hget("anbang_four_wire", "anbang_4rssi_intFilter");
  758. $inoutIntFilter = $inoutIntFilter ? $inoutIntFilter : 20;
  759. if ($info["pushTime"] && (time()-$info['pushTime']) < $inoutIntFilter ) {
  760. # code...
  761. return $info;
  762. }
  763. //推送进
  764. $info['status'] = 1;
  765. $url_data = [
  766. "mac" => $info["mac"],
  767. "data" => [
  768. [
  769. "label" => $info["id"],
  770. "time" => $info["time"],
  771. "first_time" => $info["first_time"],
  772. "dirt" => 1,
  773. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  774. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  775. ]
  776. ]
  777. ];
  778. // debug_log("pushPareaRes","进推送依赖:".json_encode($info));
  779. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  780. $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
  781. $info['pushTime'] = time();
  782. // debug_log("pushPareaRes","远程返回结果".$url_res);
  783. }
  784. // if($info['id'] == "E2B15AD5"){
  785. // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
  786. // }
  787. if($flagOut && $status == 1){
  788. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  789. # code...
  790. return $info;
  791. }
  792. //推送出
  793. $info['status'] = 2;
  794. $url_data = [
  795. "mac" => $info["mac"],
  796. "data" => [
  797. [
  798. "label" => $info["id"],
  799. "time" => $info["time"],
  800. "dirt" => 2,
  801. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  802. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  803. ]
  804. ]
  805. ];
  806. // debug_log("pushPareaRes","出推送依赖:".json_encode($info));
  807. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  808. $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
  809. $info['pushTime'] = time();
  810. // debug_log("pushPareaRes","远程返回结果".$url_res);
  811. }
  812. if($flagIn && $info['status'] == 1){
  813. $inoutIntFilter = $ues_redis->hget("anbang_four_wire", "anbang_4rssi_intFilter");
  814. $inoutIntFilter = $inoutIntFilter ? $inoutIntFilter : 20;
  815. if ($info["camPushTime"] && (time()-$info['camPushTime']) < $inoutIntFilter ) {
  816. # code...
  817. return $info;
  818. }
  819. //推送进
  820. $url_data = [
  821. "mac" => $info["mac"],
  822. "first_time" => $info["first_time"],
  823. "data" => [
  824. [
  825. "label" => $info["id"],
  826. "time" => $info["time"],
  827. "dirt" => 1,
  828. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  829. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  830. ]
  831. ]
  832. ];
  833. $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
  834. $info["camPushTime"] = time();
  835. }
  836. return $info;
  837. }
  838. public function test(){
  839. try{
  840. $ues_redis=Cache::store('redis')->handler();
  841. $jsonData= $ues_redis->rpop("mqtt_data");
  842. debug_log("InAndOUT","1212");
  843. }catch(Exception $e){
  844. var_dump($e->getMessage());
  845. }
  846. }
  847. /**
  848. * 删除前1天的数据
  849. *
  850. * @return void
  851. */
  852. public function deleteHistory()
  853. {
  854. while(true)
  855. {
  856. $where[] = ['addTime','<',strtotime("-1 day")];
  857. Db::name('tag_history')->where($where)->delete();
  858. sleep(60*60*24);
  859. }
  860. }
  861. /**
  862. * 检测小程序版本版本 function
  863. *
  864. * @return void
  865. */
  866. public function detectionVersion(){
  867. $token = $_GET['token'];
  868. if (!$token || $token != '444333d3') {
  869. json_fail('缺少token或者token不对');
  870. }
  871. $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
  872. $data["version"]="1.1.1";
  873. json_success("查询版本成功",$data);
  874. }
  875. }