Api.php 54 KB

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