Api.php 40 KB

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