Api.php 55 KB

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