Api.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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. public function redis_to_mysql2(){
  146. $redis=Cache::store('redis')->handler();
  147. // $dispose = new dispose($redis);
  148. $text=null;
  149. while(1){
  150. $jsonData= $redis->rpop("asset_after_handle");
  151. if(empty($jsonData)){
  152. sleep(1);
  153. continue;
  154. }
  155. // $jsonData= '{"mac": "01396C","data": [{"label": "230600070007","time": 1691128977,"ant": 1,"rssi": 19}]}';
  156. $list=json_decode($jsonData,true);
  157. $redis=Cache::store('redis')->handler();
  158. $received_state=$redis->hget('api_receiver_asset_state', 'accessClassReport');
  159. $mac=$list['mac'];
  160. foreach($list['data'] as $val){
  161. //1 3外 2 4内
  162. if($val['ant']=='3'){
  163. $val['ant']='1';
  164. }elseif($val['ant']=='4'){
  165. $val['ant']='2';
  166. }
  167. $report_data=[
  168. 'mac' => $mac,
  169. 'label' =>$val['label'],
  170. 'rssi' =>$val['rssi'],
  171. 'ant' =>$val['ant'],
  172. 'time'=>$val['time']
  173. ];
  174. // var_dump($report_data);
  175. debug_log("redis_to_mysql2_data",'mac:'. $mac.' label:'.$val['label'].' ant:'.$val['ant'].' time:'.$val['time'].' rssi:'.$val['rssi']);
  176. if($received_state=='1'){
  177. //如果开启盘点 则有数据就推送
  178. $url="http://localhost:8115/api/assetReceiveReport";
  179. var_dump($report_data);
  180. $postFields = http_build_query($report_data);
  181. $url_res= curl_http_post($postFields,$url,false);
  182. echo 'assetReceiveReport res'.PHP_EOL;
  183. var_dump($url_res);
  184. }
  185. $hashKey='station_access_last_data';
  186. $beforeKey='station_access_before_data';
  187. $key=$mac.'_'.$val['label'];
  188. $last_data=json_decode($redis->hGet($hashKey,$key),true);
  189. // var_dump($old_data);
  190. if($last_data){
  191. //取出历史数据 对比两次数据变化
  192. if($last_data['ant']==$report_data['ant']){
  193. //相同 则更新时间
  194. $report_data['count']=$last_data['count']+1;
  195. $before_data=json_decode($redis->hGet($beforeKey,$key),true);
  196. //存在反向数据 并且连续搜到单边数据5次
  197. if($before_data && ($report_data['count']>5)){
  198. //开始推送数据;
  199. $send_data=[
  200. "mac"=>$mac,
  201. "label"=>$val['label'],
  202. 'time'=>$val['time'],
  203. ];
  204. if($report_data['ant']>$before_data['ant']){
  205. //1->2 外到内 进
  206. $send_data['dirt']=1;
  207. }else{
  208. //2>1 内到外 出
  209. $send_data['dirt']=2;
  210. }
  211. var_dump($send_data);
  212. //推送数据
  213. $url="http://localhost:8115/api/assetReport";
  214. $postFields = http_build_query($send_data);
  215. $url_res= curl_http_post($postFields,$url,false);
  216. echo 'assetReport res'.PHP_EOL;
  217. var_dump($url_res);
  218. //推送完清除数据
  219. $redis->hDel($hashKey,$key);
  220. $redis->hDel($beforeKey,$key);
  221. }else{
  222. $redis->hSet($hashKey,$key,json_encode($report_data));
  223. }
  224. }else{
  225. //比较数据时间
  226. //默认新数据更晚上报
  227. $report_data['count']=1;
  228. $redis->hSet($hashKey,$key,json_encode($report_data));
  229. $redis->hSet($beforeKey,$key,json_encode($last_data));
  230. // $send_data=[
  231. // "mac"=>$mac,
  232. // "label"=>$val['label'],
  233. // 'time'=>$val['time'],
  234. // ];
  235. // if($report_data['ant']>$last_data['ant']){
  236. // //1->2 内到外 出
  237. // $send_data['dirt']=2;
  238. // }else{
  239. // //2>1 外到内 进
  240. // $send_data['dirt']=1;
  241. // }
  242. // var_dump($send_data);
  243. // //推送数据
  244. // $url="http://localhost:8115/api/assetReport";
  245. // $postFields = http_build_query($send_data);
  246. // $url_res= curl_http_post($postFields,$url,false);
  247. // echo 'assetReport res'.PHP_EOL;
  248. // var_dump($url_res);
  249. // $redis->hSet($hashKey,$key,json_encode($report_data));
  250. }
  251. }else{
  252. $report_data['count']=1;
  253. $redis->hSet($hashKey,$key,json_encode($report_data));
  254. }
  255. }
  256. }
  257. }
  258. public function asset_redis(){
  259. while(true){
  260. $redis=Cache::store('redis')->handler();
  261. $hashKey='station_access_last_data';
  262. $beforeKey='station_access_before_data';
  263. $list=$redis->hgetall($hashKey);
  264. foreach($list as $key=>$val){
  265. $report_data = json_decode($val, true);
  266. if ( (time() - $report_data["time"] ) > 10 ) {
  267. $before_data=json_decode($redis->hGet($beforeKey,$key),true);
  268. //存在反向数据 并且连续搜到单边数据5次
  269. if($before_data){
  270. //开始推送数据;
  271. $send_data=[
  272. "mac"=>$report_data['mac'],
  273. "label"=>$report_data['label'],
  274. 'time'=>$report_data['time'],
  275. ];
  276. if($report_data['ant']>$before_data['ant']){
  277. //1->2 外到内 进
  278. $send_data['dirt']=1;
  279. }else{
  280. //2>1 内到外 出
  281. $send_data['dirt']=2;
  282. }
  283. var_dump($send_data);
  284. //推送数据
  285. $url="http://localhost:8115/api/assetReport";
  286. $postFields = http_build_query($send_data);
  287. $url_res= curl_http_post($postFields,$url,false);
  288. echo 'assetReport res'.PHP_EOL;
  289. var_dump($url_res);
  290. //推送完清除数据
  291. $redis->hDel($hashKey,$key);
  292. $redis->hDel($beforeKey,$key);
  293. }
  294. }
  295. }
  296. sleep(1);
  297. }
  298. }
  299. /**
  300. * 清除数据
  301. *
  302. * @return void
  303. */
  304. public function clear_redis(){
  305. //清理超时的标签
  306. while(1){
  307. sleep(1);
  308. $redis=Cache::store('redis')->handler();
  309. $dispose = new dispose($redis);
  310. $keylist= $dispose->getStations();
  311. //获取存储的的基站
  312. debug_log("clear_label","查询数据".json_encode($keylist));
  313. if(empty($keylist)){
  314. continue;
  315. }
  316. foreach($keylist as $station){
  317. //结果key值
  318. $key="res".$station;
  319. //存取旧的数据的reids数据
  320. $catchkey=$station."station";
  321. //获取该基站的所有key
  322. $keys= $redis->hKeys($key);
  323. if(empty($keys)){
  324. continue;
  325. }
  326. foreach($keys as $item){
  327. if( $json_data=$redis->hget($key,$item)){
  328. debug_log("clear_label",'基站'.$station.'获取的数据'.'标签'.$item.$json_data);
  329. $json_data=json_decode($json_data,true);
  330. //列表
  331. $a=$json_data['a'];
  332. //状态
  333. $status=$json_data['status'];
  334. //是否形成进出考勤
  335. $in_and_out=$json_data["in_and_out"];
  336. //总数
  337. $count= count($a);
  338. $time= $dispose->getNowStationTime($station);
  339. debug_log("clear_label","标签最新最新时间". date('Y-m-d H:i:s', $time));
  340. //如果标签间隔超过7s,则超时并判断能否生成进出
  341. if(($time-$json_data["time"])<=7){
  342. debug_log("clear_label", $item.':'. "标签上报间隔没有超过7秒进行跳过");
  343. continue;
  344. }
  345. if($count<5){
  346. //如果信号条数小于等于2条不参与计算,信号条数大于2小于5条,则直接进行判断是否形成进出
  347. if($count>2){
  348. debug_log("clear_label", $item.':'. "标签信号个数等于超过2个,参与计算");
  349. $frist=$a[0]['dirt'];
  350. $end=$a[$count-1]['dirt'];
  351. if($frist!=$end){
  352. debug_log("clear_label", $item.':'. "标签信号个数等于超过2个,参与计算,得出结果:".$frist);
  353. $dispose->network_push($station,$item,$a[$count-1]['time'],$frist==1?1:2);
  354. }else{
  355. debug_log("clear_label", $item.':'. "标签信号个数没有超过2个,全部去清除");
  356. }
  357. }
  358. }else{
  359. //如果信号条数大于5条,则判断最后三条数据的信号强的方向是否和初始方向相反
  360. $front=0;
  361. $back=0;
  362. $res=0;
  363. if($a[$count-3]['dirt']==1){
  364. $front+=1;
  365. }else{
  366. $back+=1;
  367. }
  368. if($a[$count-2]['dirt']==1){
  369. $front+=1;
  370. }else{
  371. $back+=1;
  372. }
  373. if($a[$count-1]['dirt']==1){
  374. $front+=1;
  375. }else{
  376. $back+=1;
  377. }
  378. if($front>$back){
  379. $res=1;
  380. }else{
  381. $res=2;
  382. }
  383. if($status['dirt']!=$res){
  384. //最后三条数据的信号强的方向和初始方向相反,则形成进出考勤
  385. debug_log("clear_label", $item.':'. "最后一个信号,方向相反,得出结果,清除缓存数据");
  386. $dispose->network_push($station,$item,$a[$count-1]['time'],$status['dirt']==1?1:2);
  387. }else{
  388. //最后三条数据的信号强的方向和初始方向不相反,并且在连续的时间内没有生成考勤,则开始第二次判断
  389. if(empty($in_and_out)){
  390. //判断第二次能否生成考勤结果
  391. $res=$dispose->second_create_direction($station,$item);
  392. if(!empty($res)){
  393. //获取该标签的之前最新的考勤,和当前形成考勤时间间隔是否大于10,
  394. //如果大于10则生成考勤结果
  395. $res_time= $dispose->get_time_results($station,$item);
  396. $res_time= empty($res_time)?0:$res_time;
  397. $diff_time=$res['time']-$res_time;
  398. debug_log("clear_label", $item.':'. "间隔时间".$diff_time);
  399. //判断如果结果超过时间
  400. if($diff_time>10){
  401. debug_log("clear_label", $item.':'. "二次生成进出");
  402. $dispose->network_push($station,$item,$res['time'],$res['dirt']);
  403. }
  404. }else{
  405. //二次判断如果也没生成考勤,如果朝前的信号最大强度低于65,
  406. //朝后的信号最大强度也低于65,则记录下来最后的朝向,
  407. //存入没有生成考勤时的初始数据
  408. //获取朝前的最大值
  409. //获取朝后的最大值
  410. $frontMax= $dispose->get_label_history(4,$station,$item);
  411. $backMax= $dispose->get_label_history(5,$station,$item);
  412. $records=false;
  413. if($status['dirt']==1){
  414. $records=$frontMax<65;
  415. }
  416. if($status['dirt']==2){
  417. $records=$backMax<65;
  418. }
  419. if($records){
  420. $dispose->no_check_data($station,$item,$status['dirt'],$a[$count-1]['time'],1);
  421. }
  422. }
  423. }
  424. //二次进出
  425. debug_log("clear_label", $item.':'. "最后一个信号,方向没有变化,清除缓存数据");
  426. }
  427. }
  428. //清除对应的键值对
  429. //清空头部尾部历史
  430. $dispose->get_label_history(3,$station,$item);
  431. $redis->hdel($key,$item);
  432. $redis->hdel($catchkey,$item);
  433. }
  434. }
  435. }
  436. }
  437. }
  438. /**
  439. * 远程推送数据
  440. *
  441. * @return void
  442. */
  443. public function remotePush(){
  444. while(true){
  445. $redis=Cache::store('redis')->handler();
  446. $key="push_data";
  447. $url="http://localhost:8115/api/accessReport";
  448. sleep(2);
  449. while($data=$redis->lPop($key)){
  450. if(!empty($data)){
  451. debug_log($key,"远程推送的数据".$data);
  452. $url_res= curl_http_post($data,$url,false);
  453. $num=1;
  454. while($url_res==false){
  455. $num++;
  456. $url_res= curl_http_post($data,$url,false);
  457. if($num>=3){
  458. debug_log($key,"远程推送的地址".$url);
  459. debug_log($key,"连续推送三次失败,远程推送的数据".$data);
  460. break;
  461. }
  462. }
  463. debug_log($key,"远程返回结果".$url_res);
  464. }
  465. }
  466. }
  467. }
  468. public function redis_to_mysql_parea(){
  469. $ues_redis=Cache::store('redis')->handler();
  470. $text="";
  471. $dispose = new dispose($ues_redis);
  472. while(1){
  473. $jsonData= $ues_redis->rpop("mqtt_data_parea4rssi");
  474. debug_log("pare_InAndOUT",'数据redis'.$jsonData);
  475. if(empty($jsonData)){
  476. debug_log("pare_InAndOUT",'没有数据');
  477. sleep(1);
  478. continue;
  479. }
  480. // debug_log("InAndOUT",$jsonData);
  481. $data=json_decode($jsonData,true);
  482. // debug_log("InAndOUT",$data['cnt']);
  483. $time=$data['time'];
  484. $mac=$data['devId'];
  485. $text=$text.$data['cnt'];
  486. if(strlen($text)<24){
  487. continue;
  488. }
  489. $text = $this->handleMessage($text, $mac, $time, $ues_redis);
  490. }
  491. }
  492. public function redis_to_mysql_indoor(){
  493. $ues_redis=Cache::store('redis')->handler();
  494. $text="";
  495. $dispose = new dispose($ues_redis);
  496. while(1){
  497. $jsonData= $ues_redis->lpop("mqtt_data_indoor4rssi");
  498. debug_log("indoor_InAndOUT",'mqtt数据redis'.$jsonData);
  499. if(!$jsonData){
  500. debug_log("indoor_InAndOUT",'mqtt没有数据');
  501. sleep(1);
  502. continue;
  503. }
  504. // debug_log("InAndOUT",$jsonData);
  505. $data=json_decode($jsonData,true);
  506. // debug_log("InAndOUT",$data['cnt']);
  507. $time=$data['time'];
  508. $mac=$data['devId'];
  509. $text=$text.$data['cnt'];
  510. if(strlen($text)<24){
  511. continue;
  512. }
  513. // var_dump("before",microtime());
  514. $text = $this->handleMessageIndoor($text, $mac, $time, $ues_redis);
  515. // var_dump("after",microtime());
  516. }
  517. }
  518. //打印日志
  519. public function temporary_label_log(){
  520. $redis=Cache::store('redis')->handler();
  521. // $key="temporary_label";
  522. // $list= $redis->HgetAll($key);
  523. // debug_log($key,json_encode($list));
  524. // debug_log($key,"所有的标签");
  525. // foreach($list as $k=>$value){
  526. // debug_log($key,json_encode([$k=>$value]));
  527. // }
  528. // debug_log($key,"所有总数".count($list));
  529. // debug_log($key,"上报的标签");
  530. // $i=0;
  531. // foreach($list as $k=>$value){
  532. // if($value==2){
  533. // debug_log($key,json_encode([$k=>$value]));
  534. // $i++;
  535. // }
  536. // }
  537. // debug_log($key,"正常上报的标签总数".$i);
  538. // debug_log($key,"补出的标签");
  539. // $t=0;
  540. // foreach($list as $k=>$value){
  541. // if($value==3){
  542. // $t++;
  543. // debug_log($key,json_encode([$k=>$value]));
  544. // }
  545. // }
  546. // debug_log($key,"补出的标签总数".$t);
  547. // debug_log($key,"没有上报的的标签");
  548. // $f=0;
  549. // foreach($list as $k=>$value){
  550. // if($value==1){
  551. // debug_log($key,json_encode([$k=>$value]));
  552. // $f++;
  553. // }
  554. // }
  555. // debug_log($key,"没有上报的的标签总数".$f);
  556. // // $redis->del($key);
  557. $dis= new dispose($redis);
  558. $dis->analysisLog();
  559. }
  560. // public function temporary_label($redis,$label,$type){
  561. // $key="temporary_label";
  562. // if($type==1){
  563. // $res= $redis->hGet($key,$label);
  564. // if($res==false){
  565. // $redis->hSet($key,$label,1);
  566. // }
  567. // }else{
  568. // $redis->hSet($key,$label,3);
  569. // }
  570. // }
  571. //消息处理--重点区域
  572. public function handleMessage($text, $mac, $time, $ues_redis){
  573. while(strlen($text)>=24){
  574. $da=substr($text,0,24);
  575. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  576. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  577. if($t1&&$t2){
  578. // var_dump($text);
  579. $rfid= substr($da,0,8);
  580. $rssi1= hexdec(substr($da,16,2));
  581. $rssi2= hexdec(substr($da,18,2));
  582. $rssi3= hexdec(substr($da,20,2));
  583. $rssi4= hexdec(substr($da,22,2));
  584. // $rc1 = ($rssi1 == 255 ? 0 : 1);
  585. // $rc2 = ($rssi2 == 255 ? 0 : 1);
  586. // $rc3 = ($rssi3 == 255 ? 0 : 1);
  587. // $rc4 = ($rssi4 == 255 ? 0 : 1);
  588. $DA=[
  589. 'mac' => $mac,
  590. 'label' =>$rfid,
  591. 'rssi1' => $rssi1,
  592. 'rssi2' => $rssi2,
  593. 'rssi3' => $rssi3,
  594. 'rssi4' => $rssi4,
  595. 'report_time'=>$time
  596. ];
  597. $field = $mac."_".$rfid;
  598. $info = $ues_redis->hget("parea_rfidinfos",$field);
  599. debug_log("pare_InAndOUT","数据:".json_encode($DA,true));
  600. if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
  601. debug_log("pare_InAndOUT","无效数据:".json_encode($DA,true));
  602. }else{
  603. if(!$info){
  604. $info = [
  605. "mac" => $mac,
  606. "id" => $rfid,
  607. "firs_time" => $time,
  608. "time" => $time,
  609. "status" => 2,//2出状态,1进状态
  610. "rssi" => [
  611. [
  612. "front" => $rssi1,
  613. "behind" => $rssi2,
  614. "left" => $rssi3,
  615. "right" => $rssi4,
  616. "time" => $time
  617. ]
  618. ]
  619. ];
  620. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  621. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  622. }else{
  623. $info = json_decode($info,true);
  624. $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  625. if (!$config) {
  626. # code...
  627. $config = [
  628. "front_in" => 80,
  629. "behind_in" => 80,
  630. "left_in" => 80,
  631. "right_in" => 80,
  632. "front_out" => 255,
  633. "behind_out" => 255,
  634. "left_out" => 255,
  635. "right_out" => 255,
  636. "expInt" => 12,
  637. "effSigNum" => 6
  638. ];
  639. }else{
  640. $config = json_decode($config,true);
  641. }
  642. // var_dump("+++++++++++++++++",$config);
  643. $info["time"] = $time;
  644. $newRssi = [
  645. "front" => $rssi1,
  646. "behind" => $rssi2,
  647. "left" => $rssi3,
  648. "right" => $rssi4,
  649. "time" => $time
  650. ];
  651. $rssisArr = $info["rssi"];
  652. // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
  653. foreach ($rssisArr as $k => $v) {
  654. # code...
  655. if( ($time - $v["time"] ) > $config['expInt'] ){
  656. unset($rssisArr[$k]);
  657. }
  658. }
  659. // var_dump("+++++++++++++++++",$rssisArr);
  660. if(count($rssisArr) == $config['effSigNum']){
  661. array_shift($rssisArr);
  662. array_push($rssisArr, $newRssi);
  663. }else{
  664. array_push($rssisArr, $newRssi);
  665. }
  666. $rssisArr =array_values($rssisArr);
  667. // var_dump("+++++++++++++++++",$rssisArr);
  668. $info["rssi"] = $rssisArr;
  669. // var_dump("------------",$info["rssi"]);
  670. $cacCount = count($rssisArr);
  671. // if($cacCount > $EFF_SIG_NUM){
  672. $sum1 = 0;
  673. $sum2 = 0;
  674. $sum3 = 0;
  675. $sum4 = 0;
  676. foreach ($rssisArr as $k => $v) {
  677. # code...
  678. $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
  679. $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
  680. $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
  681. $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
  682. }
  683. $info["avg"] = [
  684. "front" => number_format( $sum1/$cacCount, 2 ),
  685. "behind" => number_format( $sum2/$cacCount, 2 ),
  686. "left" => number_format( $sum3/$cacCount, 2 ),
  687. "right" => number_format( $sum4/$cacCount, 2 ),
  688. ];
  689. // var_dump($cacCount, $EFF_SIG_NUM, "dddddddd");
  690. if ($cacCount == $config['effSigNum']) {
  691. # code...
  692. $info = $this->compAndPush($info ,$config, $ues_redis);
  693. }
  694. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  695. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  696. }
  697. }
  698. $text=substr($text,24);
  699. }else{
  700. $text=substr($text,1);
  701. }
  702. }
  703. return $text ?$text : "";
  704. }
  705. //消息处理--室内
  706. public function handleMessageIndoor($text, $mac, $time, $ues_redis){
  707. while(strlen($text)>=24){
  708. $da=substr($text,0,24);
  709. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  710. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  711. if($t1&&$t2){
  712. // var_dump($text);
  713. $rfid= substr($da,0,8);
  714. $rssi1= hexdec(substr($da,16,2));
  715. $rssi2= hexdec(substr($da,18,2));
  716. $rssi3= hexdec(substr($da,20,2));
  717. $rssi4= hexdec(substr($da,22,2));
  718. $DA=[
  719. 'mac' => $mac,
  720. 'label' =>$rfid,
  721. 'rssi1' => $rssi1,
  722. 'rssi2' => $rssi2,
  723. 'rssi3' => $rssi3,
  724. 'rssi4' => $rssi4,
  725. 'report_time'=>$time
  726. ];
  727. $field = $mac."_".$rfid;
  728. $info = $ues_redis->hget("indoor_rfidinfos",$field);
  729. debug_log("indoor_InAndOUT","数据:".json_encode($DA,true));
  730. if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
  731. debug_log("indoor_InAndOUT","无效数据:".json_encode($DA,true));
  732. }else{
  733. if(!$info){
  734. $info = [
  735. "mac" => $mac,
  736. "id" => $rfid,
  737. "first_time" => $time,
  738. "time" => $time,
  739. "status" => 2,//2出状态,1进状态
  740. "rssi" => [
  741. [
  742. "front" => $rssi1,
  743. "behind" => $rssi2,
  744. "left" => $rssi3,
  745. "right" => $rssi4,
  746. "time" => $time
  747. ]
  748. ]
  749. ];
  750. $ues_redis->hset("indoor_rfidinfos",$field, json_encode($info,true));
  751. debug_log("indoor_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  752. }else{
  753. $info = json_decode($info,true);
  754. $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  755. if (!$config) {
  756. # code...
  757. $config = [
  758. "front_in" => 80,
  759. "behind_in" => 80,
  760. "left_in" => 80,
  761. "right_in" => 80,
  762. "front_out" => 255,
  763. "behind_out" => 255,
  764. "left_out" => 255,
  765. "right_out" => 255,
  766. "expInt" => 12,
  767. "effSigNum" => 6
  768. ];
  769. }else{
  770. $config = json_decode($config,true);
  771. }
  772. // var_dump("+++++++++++++++++",$config);
  773. $info["time"] = $time;
  774. $newRssi = [
  775. "front" => $rssi1,
  776. "behind" => $rssi2,
  777. "left" => $rssi3,
  778. "right" => $rssi4,
  779. "time" => $time
  780. ];
  781. $rssisArr = $info["rssi"];
  782. // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
  783. foreach ($rssisArr as $k => $v) {
  784. # code...
  785. if( ($time - $v["time"] ) > $config['expInt'] ){
  786. unset($rssisArr[$k]);
  787. }
  788. }
  789. // var_dump("+++++++++++++++++",$rssisArr);
  790. if(count($rssisArr) == $config['effSigNum']){
  791. array_shift($rssisArr);
  792. array_push($rssisArr, $newRssi);
  793. }else{
  794. array_push($rssisArr, $newRssi);
  795. }
  796. $rssisArr =array_values($rssisArr);
  797. // var_dump("+++++++++++++++++",$rssisArr);
  798. $info["rssi"] = $rssisArr;
  799. // var_dump("------------",$info["rssi"]);
  800. $cacCount = count($rssisArr);
  801. // if($cacCount > $EFF_SIG_NUM){
  802. $sum1 = 0;
  803. $sum2 = 0;
  804. $sum3 = 0;
  805. $sum4 = 0;
  806. foreach ($rssisArr as $k => $v) {
  807. # code...
  808. $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
  809. $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
  810. $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
  811. $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
  812. }
  813. $info["avg"] = [
  814. "front" => number_format( $sum1/$cacCount, 2 ),
  815. "behind" => number_format( $sum2/$cacCount, 2 ),
  816. "left" => number_format( $sum3/$cacCount, 2 ),
  817. "right" => number_format( $sum4/$cacCount, 2 ),
  818. ];
  819. // var_dump($cacCount, $EFF_SIG_NUM, "dddddddd");
  820. if ($cacCount == $config['effSigNum']) {
  821. # code...
  822. $info = $this->compAndPushIndoor($info ,$config, $ues_redis);
  823. }
  824. $ues_redis->hset("indoor_rfidinfos",$field, json_encode($info,true));
  825. debug_log("indoor_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  826. }
  827. }
  828. $text=substr($text,24);
  829. }else{
  830. $text=substr($text,1);
  831. }
  832. }
  833. return $text ?$text : "";
  834. }
  835. //判断进出和推送--室内考勤
  836. public function compAndPushIndoor($info, $config, $ues_redis){
  837. //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
  838. // $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  839. //{"front_in":44,"front_out":44,"behind_in":50,"behind_out":50,"left_in":55,"left_out":55,"right_out":44,"right_out":44}
  840. $avg = $info['avg'];
  841. $status = $info['status'];
  842. $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
  843. $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
  844. $url="http://localhost:8115/api/api/accessClassReport";
  845. // $url="http://61.175.203.188:10001/api/accessClassReport";
  846. // if($flagIn && $status == 2){
  847. if($flagIn){
  848. if ($info["pushTime"] && (time()-$info['pushTime']) < 120 ) {
  849. # code...
  850. return $info;
  851. }
  852. //推送进
  853. $info['status'] = 1;
  854. $url_data = [
  855. "mac" => $info["mac"],
  856. "data" => [
  857. [
  858. "label" => $info["id"],
  859. "time" => $info["time"],
  860. "dirt" => 1,
  861. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  862. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  863. ]
  864. ]
  865. ];
  866. $ues_redis->lpush("indoor_inoutres_push", json_encode($url_data));
  867. $info['pushTime'] = time();
  868. // debug_log("push_indoor_res","进推送依赖:".json_encode($info));
  869. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  870. // debug_log("push_indoor_res","远程返回结果".$url_res);
  871. }
  872. // if($info['id'] == "E2B15AD5"){
  873. // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
  874. // }
  875. if($flagOut && $status == 1){
  876. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  877. # code...
  878. return $info;
  879. }
  880. //推送出
  881. $info['status'] = 2;
  882. $url_data = [
  883. "mac" => $info["mac"],
  884. "data" => [
  885. [
  886. "label" => $info["id"],
  887. "time" => $info["time"],
  888. "dirt" => 2,
  889. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  890. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  891. ]
  892. ]
  893. ];
  894. $ues_redis->lpush("indoor_inoutres_push", json_encode($url_data));
  895. // debug_log("push_indoor_res","出推送依赖:".json_encode($info));
  896. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  897. $info['pushTime'] = time();
  898. // debug_log("push_indoor_res","远程返回结果".$url_res);
  899. }
  900. return $info;
  901. }
  902. //判断进出和推送
  903. public function compAndPush($info, $config,$ues_redis){
  904. //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
  905. $avg = $info['avg'];
  906. $status = $info['status'];
  907. $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
  908. $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
  909. if($flagIn && $status == 2){
  910. // if($flagIn){
  911. $inoutIntFilter = $ues_redis->hget("anbang_four_wire", "anbang_4rssi_intFilter");
  912. $inoutIntFilter = $inoutIntFilter ? $inoutIntFilter : 20;
  913. if ($info["pushTime"] && (time()-$info['pushTime']) < $inoutIntFilter ) {
  914. # code...
  915. return $info;
  916. }
  917. //推送进
  918. $info['status'] = 1;
  919. $url_data = [
  920. "mac" => $info["mac"],
  921. "data" => [
  922. [
  923. "label" => $info["id"],
  924. "time" => $info["time"],
  925. "first_time" => $info["first_time"],
  926. "dirt" => 1,
  927. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  928. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  929. ]
  930. ]
  931. ];
  932. // debug_log("pushPareaRes","进推送依赖:".json_encode($info));
  933. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  934. $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
  935. $info['pushTime'] = time();
  936. // debug_log("pushPareaRes","远程返回结果".$url_res);
  937. }
  938. // if($info['id'] == "E2B15AD5"){
  939. // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
  940. // }
  941. if($flagOut && $status == 1){
  942. if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
  943. # code...
  944. return $info;
  945. }
  946. //推送出
  947. $info['status'] = 2;
  948. $url_data = [
  949. "mac" => $info["mac"],
  950. "data" => [
  951. [
  952. "label" => $info["id"],
  953. "time" => $info["time"],
  954. "dirt" => 2,
  955. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  956. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  957. ]
  958. ]
  959. ];
  960. // debug_log("pushPareaRes","出推送依赖:".json_encode($info));
  961. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  962. $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
  963. $info['pushTime'] = time();
  964. // debug_log("pushPareaRes","远程返回结果".$url_res);
  965. }
  966. if($flagIn && $info['status'] == 1){
  967. $inoutIntFilter = $ues_redis->hget("anbang_four_wire", "anbang_4rssi_intFilter");
  968. $inoutIntFilter = $inoutIntFilter ? $inoutIntFilter : 20;
  969. if ($info["camPushTime"] && (time()-$info['camPushTime']) < $inoutIntFilter ) {
  970. # code...
  971. return $info;
  972. }
  973. //推送进
  974. $url_data = [
  975. "mac" => $info["mac"],
  976. "first_time" => $info["first_time"],
  977. "data" => [
  978. [
  979. "label" => $info["id"],
  980. "time" => $info["time"],
  981. "dirt" => 1,
  982. "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
  983. "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
  984. ]
  985. ]
  986. ];
  987. $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
  988. $info["camPushTime"] = time();
  989. }
  990. return $info;
  991. }
  992. public function test(){
  993. try{
  994. $ues_redis=Cache::store('redis')->handler();
  995. $jsonData= $ues_redis->rpop("mqtt_data");
  996. debug_log("InAndOUT","1212");
  997. }catch(Exception $e){
  998. var_dump($e->getMessage());
  999. }
  1000. }
  1001. /**
  1002. * 删除前1天的数据
  1003. *
  1004. * @return void
  1005. */
  1006. public function deleteHistory()
  1007. {
  1008. while(true)
  1009. {
  1010. $where[] = ['addTime','<',strtotime("-1 day")];
  1011. Db::name('tag_history')->where($where)->delete();
  1012. sleep(60*60*24);
  1013. }
  1014. }
  1015. /**
  1016. * 检测小程序版本版本 function
  1017. *
  1018. * @return void
  1019. */
  1020. public function detectionVersion(){
  1021. $token = $_GET['token'];
  1022. if (!$token || $token != '444333d3') {
  1023. json_fail('缺少token或者token不对');
  1024. }
  1025. $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
  1026. $data["version"]="1.1.1";
  1027. json_success("查询版本成功",$data);
  1028. }
  1029. }