Api.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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);
  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. "firs_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 ){
  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. $url="http://localhost:8115/api/areaReport";
  737. if($flagIn && $status == 2){
  738. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  739. # code...
  740. return $info;
  741. }
  742. //推送进
  743. $info['status'] = 1;
  744. $url_data = [
  745. "mac" => $info["mac"],
  746. "data" => [
  747. [
  748. "label" => $info["id"],
  749. "time" => $info["time"],
  750. "dirt" => 1,
  751. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  752. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  753. ]
  754. ]
  755. ];
  756. debug_log("pushPareaRes","进推送依赖:".json_encode($info));
  757. $url_res= curl_http_post(json_encode($url_data),$url,false);
  758. $info['pushTime'] = time();
  759. debug_log("pushPareaRes","远程返回结果".$url_res);
  760. }
  761. // if($info['id'] == "E2B15AD5"){
  762. // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
  763. // }
  764. if($flagOut && $status == 1){
  765. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  766. # code...
  767. return $info;
  768. }
  769. //推送出
  770. $info['status'] = 2;
  771. $url_data = [
  772. "mac" => $info["mac"],
  773. "data" => [
  774. [
  775. "label" => $info["id"],
  776. "time" => $info["time"],
  777. "dirt" => 2,
  778. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  779. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  780. ]
  781. ]
  782. ];
  783. debug_log("pushPareaRes","出推送依赖:".json_encode($info));
  784. $url_res= curl_http_post(json_encode($url_data),$url,false);
  785. $info['pushTime'] = time();
  786. debug_log("pushPareaRes","远程返回结果".$url_res);
  787. }
  788. return $info;
  789. }
  790. public function test(){
  791. try{
  792. $ues_redis=Cache::store('redis')->handler();
  793. $jsonData= $ues_redis->rpop("mqtt_data");
  794. debug_log("InAndOUT","1212");
  795. }catch(Exception $e){
  796. var_dump($e->getMessage());
  797. }
  798. }
  799. /**
  800. * 删除前1天的数据
  801. *
  802. * @return void
  803. */
  804. public function deleteHistory()
  805. {
  806. while(true)
  807. {
  808. $where[] = ['addTime','<',strtotime("-1 day")];
  809. Db::name('tag_history')->where($where)->delete();
  810. sleep(60*60*24);
  811. }
  812. }
  813. /**
  814. * 检测小程序版本版本 function
  815. *
  816. * @return void
  817. */
  818. public function detectionVersion(){
  819. $token = $_GET['token'];
  820. if (!$token || $token != '444333d3') {
  821. json_fail('缺少token或者token不对');
  822. }
  823. $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
  824. $data["version"]="1.1.1";
  825. json_success("查询版本成功",$data);
  826. }
  827. }