Api.php 55 KB

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