Api.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. $text=null;
  57. while(1){
  58. $jsonData= $ues_redis->rpop("mqtt_data");
  59. debug_log("InAndOUT",'数据redis'.$jsonData);
  60. if(empty($jsonData)){
  61. debug_log("InAndOUT",'没有数据');
  62. sleep(1);
  63. continue;
  64. }
  65. $data=json_decode($jsonData,true);
  66. $time=$data['time'];
  67. $mac=$data['devId'];
  68. $list=[];
  69. $text=$text.$data['cnt'];
  70. if(strlen($text)<24){
  71. continue;
  72. }
  73. while(strlen($text)>=24){
  74. $da=substr($text,0,24);
  75. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  76. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  77. if($t1&&$t2){
  78. $label= substr($da,0,8);
  79. $rssi1= substr($da,16,2);
  80. $rssi2= substr($da,18,2);
  81. $rssi3= substr($da,20,2);
  82. $rssi4= substr($da,22,2);
  83. $DA=[
  84. 'mac' => $mac,
  85. 'label' =>$label,
  86. 'rssi1' => hexdec($rssi1),
  87. 'rssi2' => hexdec($rssi2),
  88. 'rssi3' => hexdec($rssi3),
  89. 'rssi4' => hexdec($rssi4),
  90. 'report_time'=>$time
  91. ];
  92. $num=dechex($label);
  93. $vs=false;
  94. $history_status=false;
  95. $DA['num']=$num;
  96. $DA['other_time']=date("Y-m-d H:i:s",$time);
  97. if($DA['rssi1']<=85||$DA['rssi2']<=85||$DA['rssi3']<=85||$DA['rssi4']<=85){
  98. $history_status=true;
  99. }else{
  100. debug_log("label_log","cache_del:".json_encode($DA));
  101. }
  102. if($DA['rssi1']<=72||$DA['rssi2']<=72||$DA['rssi3']<=72||$DA['rssi4']<=72){
  103. $vs=true;
  104. }else{
  105. debug_log("label_log","delete:".json_encode($DA));
  106. }
  107. $DA['rssi1']=$DA['rssi1']==255?90:$DA['rssi1'];
  108. $DA['rssi2']=$DA['rssi2']==255?90:$DA['rssi2'];
  109. $DA['rssi3']=$DA['rssi3']==255?90:$DA['rssi3'];
  110. $DA['rssi4']=$DA['rssi4']==255?90:$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. $dispose = new dispose($ues_redis);
  118. $dispose->set_label_history($DA);
  119. debug_log("label_log","cache: ".json_encode($DA));
  120. }
  121. $this->temporary_label($ues_redis,$label,1);
  122. if($vs&&(substr($da,8,2)=='01')){
  123. debug_log("label_log","cssh: ".json_encode($DA));
  124. try{
  125. $dispose = new dispose($ues_redis);
  126. $DA= $dispose->check_data($DA);
  127. debug_log("label_log","xsj:".json_encode($DA));
  128. $dispose->computeResout($DA);
  129. }catch(Exception $e){
  130. debug_log("InAndOUT","抛出异常".json_encode($DA,true));
  131. }
  132. }
  133. $text=substr($text,24);
  134. }else{
  135. $text=substr($text,1);
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * 清除数据
  142. *
  143. * @return void
  144. */
  145. public function clear_redis(){
  146. while(1){
  147. sleep(1);
  148. $redis=Cache::store('redis')->handler();
  149. $dispose = new dispose($redis);
  150. $keylist= $dispose->getStations();
  151. debug_log("clear_label","查询数据".json_encode($keylist));
  152. if(empty($keylist)){
  153. continue;
  154. }
  155. foreach($keylist as $station){
  156. //结果key值
  157. $key="res".$station;
  158. //存取旧的数据的reids数据
  159. $catchkey=$station."station";
  160. //获取该基站的所有key
  161. $keys= $redis->hKeys($key);
  162. if(empty($keys)){
  163. continue;
  164. }
  165. foreach($keys as $item){
  166. if( $json_data=$redis->hget($key,$item)){
  167. debug_log("clear_label",'基站'.$station);
  168. debug_log("clear_label",'标签'.$item);
  169. debug_log("clear_label",'获取的数据'.$json_data);
  170. $json_data=json_decode($json_data,true);
  171. //列表
  172. $a=$json_data['a'];
  173. //状态
  174. $status=$json_data['status'];
  175. //进出
  176. $in_and_out=$json_data["in_and_out"];
  177. //总数
  178. $count= count($a);
  179. if((time()-$json_data["time"])<8){
  180. debug_log("clear_label", $item.':'. "标签上报间隔没有超过8秒进行跳过");
  181. continue;
  182. }
  183. if($count<5){
  184. if($count>2){
  185. debug_log("clear_label", $item.':'. "标签信号个数等于超过4个,参与计算");
  186. $frist=$a[0]['dirt'];
  187. $end=$a[$count-1]['dirt'];
  188. if($frist!=$end){
  189. $data_array=[];
  190. $data_array[]=[
  191. "label"=>$item,
  192. 'time'=>$a[$count-1]['time'],
  193. 'dirt'=> $frist==1?1:2 //1是进 2是出
  194. ];
  195. $url_data=[
  196. "mac"=>$station,
  197. "data"=>$data_array
  198. ];
  199. //设置key
  200. $dispose->set_time_results($station,$item,$a[$count-1]['time'],$frist==1?1:2);
  201. $dispose->getRemoteData($url_data);
  202. // debug_log("InAndOUT","得出计算结果:".json_encode($arr));
  203. debug_log("clear_label","发送给远程".json_encode($url_data));
  204. }else{
  205. debug_log("clear_label", $item.':'. "标签信号个数没有超过4个,全部去清除");
  206. }
  207. }
  208. }else{
  209. $front=0;
  210. $back=0;
  211. $res=0;
  212. if($a[$count-3]['dirt']==1){
  213. $front+=1;
  214. }else{
  215. $back+=1;
  216. }
  217. if($a[$count-2]['dirt']==1){
  218. $front+=1;
  219. }else{
  220. $back+=1;
  221. }
  222. if($a[$count-1]['dirt']==1){
  223. $front+=1;
  224. }else{
  225. $back+=1;
  226. }
  227. if($front>$back){
  228. $res=1;
  229. }else{
  230. $res=2;
  231. }
  232. if($status['dirt']!=$res){
  233. debug_log("clear_label", $item.':'. "最后一个信号,方向相反,得出结果,清除缓存数据");
  234. $redis->hdel($key,$item);
  235. $data_array=[];
  236. $data_array[]=[
  237. "label"=>$item,
  238. 'time'=>$a[$count-1]['time'],
  239. 'dirt'=> $status['dirt']==1?1:2 //1是进 2是出
  240. ];
  241. $url_data=[
  242. "mac"=>$station,
  243. "data"=>$data_array
  244. ];
  245. //设置key
  246. $dispose->set_time_results($station,$item,$a[$count-1]['time'],$status['dirt']==1?1:2);
  247. // debug_log("InAndOUT","得出计算结果:".json_encode($arr));
  248. debug_log("clear_label","发送给远程".json_encode($url_data));
  249. //远程推送
  250. $dispose->getRemoteData($url_data);
  251. //清除缓存数据
  252. $this->temporary_label($redis,$item,2);
  253. }else{
  254. //上午不做二次计算
  255. if(empty($in_and_out)){
  256. //二次生成结果
  257. $res=$dispose->second_create_direction($station,$item);
  258. if(!empty($res)){
  259. $res_time= $dispose->get_time_results($station,$item);
  260. $res_time= empty($res_time)?0:$res_time;
  261. $diff_time=$res['time']-$res_time;
  262. debug_log("clear_label", $item.':'. "间隔时间".$diff_time);
  263. //判断如果结果超过时间
  264. if($diff_time>10){
  265. debug_log("clear_label", $item.':'. "二次生成进出");
  266. $data_array=[];
  267. $data_array[]=[
  268. "label"=>$item,
  269. 'time'=>$res['time'],
  270. 'dirt'=>$res['dirt'],
  271. ];
  272. $url_data=[
  273. "mac"=>$station,
  274. "data"=>$data_array
  275. ];
  276. debug_log("clear_label","二次生成发送给远程".json_encode($url_data));
  277. //远程推送时间
  278. $dispose->set_time_results($station,$item,$res['time'],$res['dirt']);
  279. //远程推送
  280. $dispose->getRemoteData($url_data);
  281. }
  282. }
  283. }
  284. //二次进出
  285. debug_log("clear_label", $item.':'. "最后一个信号,方向没有变化,清除缓存数据");
  286. }
  287. }
  288. //清除对应的键值对
  289. //清空头部尾部历史
  290. $dispose->get_label_history(3,$station,$item);
  291. $redis->hdel($key,$item);
  292. $redis->hdel($catchkey,$item);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * 远程推送数据
  300. *
  301. * @return void
  302. */
  303. public function remotePush(){
  304. while(true){
  305. $redis=Cache::store('redis')->handler();
  306. $key="push_data";
  307. $url="http://localhost:8115/api/accessReport";
  308. // $url="http://127.0.0.1:8000/api/accessReport";
  309. sleep(2);
  310. while($data=$redis->lPop($key)){
  311. if(!empty($data)){
  312. debug_log($key,"远程推送的数据".$data);
  313. $url_res= curl_http_post($data,$url,false);
  314. $num=1;
  315. while($url_res==false){
  316. $num++;
  317. $url_res= curl_http_post($data,$url,false);
  318. if($num>=3){
  319. debug_log($key,"远程推送的地址".$url);
  320. debug_log($key,"连续推送三次失败,远程推送的数据".$data);
  321. break;
  322. }
  323. }
  324. debug_log($key,"远程返回结果".$url_res);
  325. }
  326. }
  327. }
  328. }
  329. public function redis_to_mysql_parea(){
  330. $ues_redis=Cache::store('redis')->handler();
  331. $text="";
  332. $dispose = new dispose($ues_redis);
  333. while(1){
  334. $jsonData= $ues_redis->rpop("mqtt_data_parea4rssi");
  335. debug_log("pare_InAndOUT",'数据redis'.$jsonData);
  336. if(empty($jsonData)){
  337. debug_log("pare_InAndOUT",'没有数据');
  338. sleep(1);
  339. continue;
  340. }
  341. // debug_log("InAndOUT",$jsonData);
  342. $data=json_decode($jsonData,true);
  343. // debug_log("InAndOUT",$data['cnt']);
  344. $time=$data['time'];
  345. $mac=$data['devId'];
  346. $text=$text.$data['cnt'];
  347. if(strlen($text)<24){
  348. continue;
  349. }
  350. $text = $this->handleMessage($text, $mac, $time, $ues_redis);
  351. // foreach ($jsonData as $k => $v) {
  352. // $data = json_decode($v, true);
  353. // $config = $this->selectHash("anbang_four_wire",$data['mac']);
  354. // if(!$config){
  355. // $config = [
  356. // "front" => 70,
  357. // "behind" => 70,
  358. // "left" => 70,
  359. // "right" => 70
  360. // ];
  361. // }else{
  362. // $config = json_decode($config);
  363. // }
  364. // $field = "";
  365. // $infos = $ues_redis->hget("parea_rfidinfos",$field);
  366. // if(!$infos){
  367. // }
  368. // //判断进出段
  369. // //1。当前时间 - 上次时间 > 设定时间
  370. // if( (time() - $v["time"]) > $setInt ){
  371. // //推送离开
  372. // }
  373. // }
  374. // sleep(2);
  375. }
  376. }
  377. // //http://47.114.185.186:8115/api/areaReport
  378. // //{“mac”:”013560”,”data”:[{“label”:”258962f”,”time”:1682317268,”dirt”:2},{“label”:”258962f”,”time”:1682317268,”dirt”:1}]
  379. // }
  380. //打印日志
  381. public function temporary_label_log(){
  382. $redis=Cache::store('redis')->handler();
  383. // $key="temporary_label";
  384. // $list= $redis->HgetAll($key);
  385. // debug_log($key,json_encode($list));
  386. // debug_log($key,"所有的标签");
  387. // foreach($list as $k=>$value){
  388. // debug_log($key,json_encode([$k=>$value]));
  389. // }
  390. // debug_log($key,"所有总数".count($list));
  391. // debug_log($key,"上报的标签");
  392. // $i=0;
  393. // foreach($list as $k=>$value){
  394. // if($value==2){
  395. // debug_log($key,json_encode([$k=>$value]));
  396. // $i++;
  397. // }
  398. // }
  399. // debug_log($key,"正常上报的标签总数".$i);
  400. // debug_log($key,"补出的标签");
  401. // $t=0;
  402. // foreach($list as $k=>$value){
  403. // if($value==3){
  404. // $t++;
  405. // debug_log($key,json_encode([$k=>$value]));
  406. // }
  407. // }
  408. // debug_log($key,"补出的标签总数".$t);
  409. // debug_log($key,"没有上报的的标签");
  410. // $f=0;
  411. // foreach($list as $k=>$value){
  412. // if($value==1){
  413. // debug_log($key,json_encode([$k=>$value]));
  414. // $f++;
  415. // }
  416. // }
  417. // debug_log($key,"没有上报的的标签总数".$f);
  418. // // $redis->del($key);
  419. $dis= new dispose($redis);
  420. $dis->analysisLog();
  421. }
  422. public function temporary_label($redis,$label,$type){
  423. $key="temporary_label";
  424. if($type==1){
  425. $res= $redis->hGet($key,$label);
  426. if($res==false){
  427. $redis->hSet($key,$label,1);
  428. }
  429. }else{
  430. $redis->hSet($key,$label,3);
  431. }
  432. }
  433. //消息处理
  434. public function handleMessage($text, $mac, $time, $ues_redis){
  435. while(strlen($text)>=24){
  436. $da=substr($text,0,24);
  437. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  438. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  439. if($t1&&$t2){
  440. // var_dump($text);
  441. $rfid= substr($da,0,8);
  442. $rssi1= hexdec(substr($da,16,2));
  443. $rssi2= hexdec(substr($da,18,2));
  444. $rssi3= hexdec(substr($da,20,2));
  445. $rssi4= hexdec(substr($da,22,2));
  446. // $rc1 = ($rssi1 == 255 ? 0 : 1);
  447. // $rc2 = ($rssi2 == 255 ? 0 : 1);
  448. // $rc3 = ($rssi3 == 255 ? 0 : 1);
  449. // $rc4 = ($rssi4 == 255 ? 0 : 1);
  450. $DA=[
  451. 'mac' => $mac,
  452. 'label' =>$rfid,
  453. 'rssi1' => $rssi1,
  454. 'rssi2' => $rssi2,
  455. 'rssi3' => $rssi3,
  456. 'rssi4' => $rssi4,
  457. 'report_time'=>$time
  458. ];
  459. $field = $mac."_".$rfid;
  460. $info = $ues_redis->hget("parea_rfidinfos",$field);
  461. debug_log("pare_InAndOUT","数据:".json_encode($DA,true));
  462. if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
  463. debug_log("pare_InAndOUT","无效数据:".json_encode($DA,true));
  464. }else{
  465. if(!$info){
  466. $info = [
  467. "mac" => $mac,
  468. "id" => $rfid,
  469. "firs_time" => $time,
  470. "time" => $time,
  471. "status" => 2,//2出状态,1进状态
  472. "rssi" => [
  473. [
  474. "front" => $rssi1,
  475. "behind" => $rssi2,
  476. "left" => $rssi3,
  477. "right" => $rssi4,
  478. "time" => $time
  479. ]
  480. ]
  481. ];
  482. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  483. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  484. }else{
  485. $info = json_decode($info,true);
  486. $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  487. if (!$config) {
  488. # code...
  489. $config = [
  490. "front_in" => 80,
  491. "behind_in" => 80,
  492. "left_in" => 80,
  493. "right_in" => 80,
  494. "front_out" => 255,
  495. "behind_out" => 255,
  496. "left_out" => 255,
  497. "right_out" => 255,
  498. "expInt" => 12,
  499. "effSigNum" => 6
  500. ];
  501. }else{
  502. $config = json_decode($config,true);
  503. }
  504. // var_dump("+++++++++++++++++",$config);
  505. $info["time"] = $time;
  506. $newRssi = [
  507. "front" => $rssi1,
  508. "behind" => $rssi2,
  509. "left" => $rssi3,
  510. "right" => $rssi4,
  511. "time" => $time
  512. ];
  513. $rssisArr = $info["rssi"];
  514. // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
  515. foreach ($rssisArr as $k => $v) {
  516. # code...
  517. if( ($time - $v["time"] ) > $config['expInt'] ){
  518. unset($rssisArr[$k]);
  519. }
  520. }
  521. // var_dump("+++++++++++++++++",$rssisArr);
  522. if(count($rssisArr) == $config['effSigNum']){
  523. array_shift($rssisArr);
  524. array_push($rssisArr, $newRssi);
  525. }else{
  526. array_push($rssisArr, $newRssi);
  527. }
  528. $rssisArr =array_values($rssisArr);
  529. // var_dump("+++++++++++++++++",$rssisArr);
  530. $info["rssi"] = $rssisArr;
  531. // var_dump("------------",$info["rssi"]);
  532. $cacCount = count($rssisArr);
  533. // if($cacCount > $EFF_SIG_NUM){
  534. $sum1 = 0;
  535. $sum2 = 0;
  536. $sum3 = 0;
  537. $sum4 = 0;
  538. foreach ($rssisArr as $k => $v) {
  539. # code...
  540. $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
  541. $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
  542. $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
  543. $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
  544. }
  545. $info["avg"] = [
  546. "front" => number_format( $sum1/$cacCount, 2 ),
  547. "behind" => number_format( $sum2/$cacCount, 2 ),
  548. "left" => number_format( $sum3/$cacCount, 2 ),
  549. "right" => number_format( $sum4/$cacCount, 2 ),
  550. ];
  551. // var_dump($cacCount, $EFF_SIG_NUM, "dddddddd");
  552. if ($cacCount == $config['effSigNum']) {
  553. # code...
  554. $info = $this->compAndPush($info ,$config);
  555. }
  556. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  557. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  558. }
  559. }
  560. $text=substr($text,24);
  561. }else{
  562. $text=substr($text,1);
  563. }
  564. }
  565. return $text ?$text : "";
  566. }
  567. //判断进出和推送
  568. public function compAndPush($info, $config ){
  569. //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
  570. // $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  571. //{"front_in":44,"front_out":44,"behind_in":50,"behind_out":50,"left_in":55,"left_out":55,"right_out":44,"right_out":44}
  572. $avg = $info['avg'];
  573. $status = $info['status'];
  574. $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
  575. $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
  576. $url="http://localhost:8115/api/areaReport";
  577. if($flagIn && $status == 2){
  578. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  579. # code...
  580. return $info;
  581. }
  582. //推送进
  583. $info['status'] = 1;
  584. $url_data = [
  585. "mac" => $info["mac"],
  586. "data" => [
  587. [
  588. "label" => $info["id"],
  589. "time" => $info["time"],
  590. "dirt" => 1,
  591. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  592. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  593. ]
  594. ]
  595. ];
  596. debug_log("pushPareaRes","进推送依赖:".json_encode($info));
  597. $url_res= curl_http_post(json_encode($url_data),$url,false);
  598. $info['pushTime'] = time();
  599. debug_log("pushPareaRes","远程返回结果".$url_res);
  600. }
  601. // if($info['id'] == "E2B15AD5"){
  602. // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
  603. // }
  604. if($flagOut && $status == 1){
  605. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  606. # code...
  607. return $info;
  608. }
  609. //推送出
  610. $info['status'] = 2;
  611. $url_data = [
  612. "mac" => $info["mac"],
  613. "data" => [
  614. [
  615. "label" => $info["id"],
  616. "time" => $info["time"],
  617. "dirt" => 2,
  618. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  619. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  620. ]
  621. ]
  622. ];
  623. debug_log("pushPareaRes","出推送依赖:".json_encode($info));
  624. $url_res= curl_http_post(json_encode($url_data),$url,false);
  625. $info['pushTime'] = time();
  626. debug_log("pushPareaRes","远程返回结果".$url_res);
  627. }
  628. return $info;
  629. }
  630. // $url="http://47.114.185.186:8115/api/accessReport";
  631. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  632. // debug_log("InAndOUT","远程返回结果".$url_res);
  633. public function test(){
  634. try{
  635. $ues_redis=Cache::store('redis')->handler();
  636. $jsonData= $ues_redis->rpop("mqtt_data");
  637. debug_log("InAndOUT","1212");
  638. }catch(Exception $e){
  639. var_dump($e->getMessage());
  640. }
  641. }
  642. /**
  643. * 删除前1天的数据
  644. *
  645. * @return void
  646. */
  647. public function deleteHistory()
  648. {
  649. while(true)
  650. {
  651. $where[] = ['addTime','<',strtotime("-1 day")];
  652. Db::name('tag_history')->where($where)->delete();
  653. sleep(60*60*24);
  654. }
  655. }
  656. /**
  657. * 检测小程序版本版本 function
  658. *
  659. * @return void
  660. */
  661. public function detectionVersion(){
  662. $token = $_GET['token'];
  663. if (!$token || $token != '444333d3') {
  664. json_fail('缺少token或者token不对');
  665. }
  666. $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
  667. $data["version"]="1.1.1";
  668. json_success("查询版本成功",$data);
  669. }
  670. }