Api.php 49 KB

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