Api.php 54 KB

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