Api.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. $text=null;
  57. $testArray=[
  58. '52A51239','52A50A09','52A4B3F9','52A53899','52A4FA19','52A56489','52A4B839','52A535C9','2E2C17B2','52A4F279','52A4B879','2E37D1A2','52A57409','2E2C2802','52A52649','52A55059','52A519F9','52A4B109','52A54539','52A4CB09','52A55839','52A56C59','52A4E059','52A51979','52A55FB9','52A4E849','2E2C1FE2','52A4EFC9','2E245942','2E40F632','2E25C912','52A52969','52A4B8F9','52A53D59','52A4E7C9','52A52E09','52A54D49','52A4D059','52A4AC59','52A519B9','52A50719','52A54D09','2E2EE362','52A4C389','52A4D7D9','2E3AE732','2E2137B2','52A56C19','52A58439','52A511C9'
  59. ];
  60. while(1){
  61. $jsonData= $ues_redis->rpop("mqtt_data");
  62. debug_log("InAndOUT",'数据redis'.$jsonData);
  63. if(empty($jsonData)){
  64. debug_log("InAndOUT",'没有数据');
  65. sleep(1);
  66. continue;
  67. }
  68. $data=json_decode($jsonData,true);
  69. $time=$data['time'];
  70. $mac=$data['devId'];
  71. $list=[];
  72. $text=$text.$data['cnt'];
  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. $num=$label;
  96. $vs=false;
  97. $DA['num']=$num;
  98. $DA['other_time']=date("Y-m-d H:i:s",$time);
  99. if($DA['rssi1']<=75||$DA['rssi2']<=75||$DA['rssi3']<=75||$DA['rssi4']<=75){
  100. $vs=true;
  101. }else{
  102. debug_log("label_log","delete:".json_encode($DA));
  103. }
  104. $this->temporary_label($ues_redis,$label,1);
  105. //in_array($num,$testArray)&&
  106. if($vs&&(substr($da,8,2)=='01')){
  107. if($DA['rssi1']==255){
  108. $DA['rssi1']=90;
  109. }
  110. if($DA['rssi2']==255){
  111. $DA['rssi2']=90;
  112. }
  113. if($DA['rssi3']==255){
  114. $DA['rssi3']=90;
  115. }
  116. if($DA['rssi4']==255){
  117. $DA['rssi4']=90;
  118. }
  119. debug_log("label_log","cssh: ".json_encode($DA));
  120. $DA["rssi1"]=$DA["rssi1"]<=$DA["rssi3"]?$DA["rssi1"]:$DA["rssi3"];
  121. $DA["rssi2"]=$DA["rssi2"]<=$DA["rssi4"]?$DA["rssi2"]:$DA["rssi4"];
  122. // debug_log("label_log","标签总数量为".count($arr));
  123. try{
  124. $dispose = new dispose($ues_redis);
  125. // $computedata=$dispose->computeData($DA);
  126. //$dispose->check_data($DA);
  127. $DA= $dispose->check_data($DA);
  128. debug_log("label_log","xsj:".json_encode($DA));
  129. $dispose->computeResout($DA);
  130. }catch(Exception $e){
  131. debug_log("InAndOUT","抛出异常".json_encode($DA,true));
  132. }
  133. }
  134. $text=substr($text,24);
  135. }else{
  136. $text=substr($text,1);
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * 清除数据
  143. *
  144. * @return void
  145. */
  146. public function clear_redis(){
  147. while(1){
  148. sleep(1);
  149. $redis=Cache::store('redis')->handler();
  150. $dispose = new dispose($redis);
  151. $keylist= $dispose->getStations();
  152. if(empty($keylist)){
  153. continue;
  154. }
  155. foreach($keylist as $station){
  156. //结果key值
  157. $key="res".$station;
  158. //存取旧的数据的reids数据
  159. $catchkey=$station."station";
  160. //获取该基站的所有key
  161. $keys= $redis->hKeys($key);
  162. if(empty($keys)){
  163. continue;
  164. }
  165. foreach($keys as $item){
  166. if( $json_data=$redis->hget($key,$item)){
  167. debug_log("clear_label",'获取的数据'.$json_data);
  168. $json_data=json_decode($json_data,true);
  169. $a=$json_data['a'];
  170. $status=$json_data['status'];
  171. $count= count($a);
  172. if((time()-$json_data["time"])<20){
  173. debug_log("clear_label", $item.':'. "标签上报间隔没有超过20秒进行跳过");
  174. continue;
  175. }
  176. if($count<5){
  177. // $redis->hdel($key,$item);
  178. debug_log("clear_label", $item.':'. "标签信号个数没有超过5个,全部去清除");
  179. //continue;
  180. }else{
  181. $front=0;
  182. $back=0;
  183. $res=0;
  184. if($a[$count-3]['dirt']==1){
  185. $front+=1;
  186. }else{
  187. $back+=1;
  188. }
  189. if($a[$count-2]['dirt']==1){
  190. $front+=1;
  191. }else{
  192. $back+=1;
  193. }
  194. if($a[$count-1]['dirt']==1){
  195. $front+=1;
  196. }else{
  197. $back+=1;
  198. }
  199. if($front>$back){
  200. $res=1;
  201. }else{
  202. $res=2;
  203. }
  204. if($status['dirt']!=$res){
  205. debug_log("clear_label", $item.':'. "最后一个信号,方向相反,出来结果,清除缓存数据");
  206. $redis->hdel($key,$item);
  207. $data_array=[];
  208. $data_array[]=[
  209. "label"=>$item,
  210. 'time'=>$a[$count-1]['time'],
  211. 'dirt'=> $status['dirt']==1?1:2 //1是进 2是出
  212. ];
  213. $url_data=[
  214. "mac"=>$station,
  215. "data"=>$data_array
  216. ];
  217. // debug_log("InAndOUT","得出计算结果:".json_encode($arr));
  218. debug_log("clear_label","发送给远程".json_encode($url_data));
  219. $url="http://47.114.185.186:8115/api/accessReport";
  220. $url_res= curl_http_post(json_encode($url_data),$url,false);
  221. debug_log("clear_label","远程返回结果".$url_res);
  222. debug_log("result","补充计算出的结果计算返回的结果".json_encode($url_data));
  223. $this->temporary_label($redis,$item,2);
  224. }else{
  225. debug_log("clear_label", $item.':'. "最后一个信号,方向没有变化,清除缓存数据");
  226. }
  227. }
  228. //清除对应的键值对
  229. $redis->hdel($key,$item);
  230. $redis->hdel($catchkey,$item);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. public function redis_to_mysql_parea(){
  237. $ues_redis=Cache::store('redis')->handler();
  238. $text="";
  239. $dispose = new dispose($ues_redis);
  240. while(1){
  241. $jsonData= $ues_redis->rpop("mqtt_data_parea4rssi");
  242. debug_log("pare_InAndOUT",'数据redis'.$jsonData);
  243. if(empty($jsonData)){
  244. debug_log("pare_InAndOUT",'没有数据');
  245. sleep(1);
  246. continue;
  247. }
  248. // debug_log("InAndOUT",$jsonData);
  249. $data=json_decode($jsonData,true);
  250. // debug_log("InAndOUT",$data['cnt']);
  251. $time=$data['time'];
  252. $mac=$data['devId'];
  253. $text=$text.$data['cnt'];
  254. if(strlen($text)<24){
  255. continue;
  256. }
  257. $text = $this->handleMessage($text, $mac, $time, $ues_redis);
  258. // foreach ($jsonData as $k => $v) {
  259. // $data = json_decode($v, true);
  260. // $config = $this->selectHash("anbang_four_wire",$data['mac']);
  261. // if(!$config){
  262. // $config = [
  263. // "front" => 70,
  264. // "behind" => 70,
  265. // "left" => 70,
  266. // "right" => 70
  267. // ];
  268. // }else{
  269. // $config = json_decode($config);
  270. // }
  271. // $field = "";
  272. // $infos = $ues_redis->hget("parea_rfidinfos",$field);
  273. // if(!$infos){
  274. // }
  275. // //判断进出段
  276. // //1。当前时间 - 上次时间 > 设定时间
  277. // if( (time() - $v["time"]) > $setInt ){
  278. // //推送离开
  279. // }
  280. // }
  281. // sleep(2);
  282. }
  283. }
  284. // //http://47.114.185.186:8115/api/areaReport
  285. // //{“mac”:”013560”,”data”:[{“label”:”258962f”,”time”:1682317268,”dirt”:2},{“label”:”258962f”,”time”:1682317268,”dirt”:1}]
  286. // }
  287. //打印日志
  288. public function temporary_label_log(){
  289. $redis=Cache::store('redis')->handler();
  290. $key="temporary_label";
  291. $list= $redis->HgetAll($key);
  292. debug_log($key,json_encode($list));
  293. debug_log($key,"所有的标签");
  294. foreach($list as $k=>$value){
  295. debug_log($key,json_encode([$k=>$value]));
  296. }
  297. debug_log($key,"所有总数".count($list));
  298. debug_log($key,"上报的标签");
  299. $i=0;
  300. foreach($list as $k=>$value){
  301. if($value==2){
  302. debug_log($key,json_encode([$k=>$value]));
  303. $i++;
  304. }
  305. }
  306. debug_log($key,"正常上报的标签总数".$i);
  307. debug_log($key,"补出的标签");
  308. $t=0;
  309. foreach($list as $k=>$value){
  310. if($value==3){
  311. $t++;
  312. debug_log($key,json_encode([$k=>$value]));
  313. }
  314. }
  315. debug_log($key,"补出的标签总数".$t);
  316. debug_log($key,"没有上报的的标签");
  317. $f=0;
  318. foreach($list as $k=>$value){
  319. if($value==1){
  320. debug_log($key,json_encode([$k=>$value]));
  321. $f++;
  322. }
  323. }
  324. debug_log($key,"没有上报的的标签总数".$f);
  325. // $redis->del($key);
  326. }
  327. public function temporary_label($redis,$label,$type){
  328. $key="temporary_label";
  329. if($type==1){
  330. $res= $redis->hGet($key,$label);
  331. if($res==false){
  332. $redis->hSet($key,$label,1);
  333. }
  334. }else{
  335. $redis->hSet($key,$label,3);
  336. }
  337. }
  338. //消息处理
  339. public function handleMessage($text, $mac, $time, $ues_redis){
  340. $EXP_TIME = 20;
  341. $EFF_SIG_NUM = 10;
  342. while(strlen($text)>=24){
  343. $da=substr($text,0,24);
  344. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  345. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  346. if($t1&&$t2){
  347. // var_dump($text);
  348. $rfid= substr($da,0,8);
  349. $rssi1= hexdec(substr($da,16,2));
  350. $rssi2= hexdec(substr($da,18,2));
  351. $rssi3= hexdec(substr($da,20,2));
  352. $rssi4= hexdec(substr($da,22,2));
  353. // $rc1 = ($rssi1 == 255 ? 0 : 1);
  354. // $rc2 = ($rssi2 == 255 ? 0 : 1);
  355. // $rc3 = ($rssi3 == 255 ? 0 : 1);
  356. // $rc4 = ($rssi4 == 255 ? 0 : 1);
  357. $DA=[
  358. 'mac' => $mac,
  359. 'label' =>$rfid,
  360. 'rssi1' => $rssi1,
  361. 'rssi2' => $rssi2,
  362. 'rssi3' => $rssi3,
  363. 'rssi4' => $rssi4,
  364. 'report_time'=>$time
  365. ];
  366. $field = $mac."_".$rfid;
  367. $info = $ues_redis->hget("parea_rfidinfos",$field);
  368. debug_log("pare_InAndOUT","数据:".json_encode($DA,true));
  369. if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
  370. debug_log("pare_InAndOUT","无效数据:".json_encode($DA,true));
  371. }else{
  372. if(!$info){
  373. $info = [
  374. "mac" => $mac,
  375. "id" => $rfid,
  376. "firs_time" => $time,
  377. "time" => $time,
  378. "status" => 2,//2出状态,1进状态
  379. "rssi" => [
  380. [
  381. "front" => $rssi1,
  382. "behind" => $rssi2,
  383. "left" => $rssi3,
  384. "right" => $rssi4,
  385. "time" => $time
  386. ]
  387. ]
  388. ];
  389. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  390. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  391. }else{
  392. $info = json_decode($info,true);
  393. $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  394. if (!$config) {
  395. # code...
  396. $config = [
  397. "front_in" => 65,
  398. "behind_in" => 70,
  399. "left_in" => 80,
  400. "right_in" => 70,
  401. "front_out" => 70,
  402. "behind_out" => 75,
  403. "left_out" => 82,
  404. "right_out" => 70,
  405. "expInt" => 20,
  406. "effSigNum" => 10
  407. ];
  408. }else{
  409. $config = json_decode($config,true);
  410. }
  411. // var_dump("+++++++++++++++++",$config);
  412. $info["time"] = $time;
  413. $newRssi = [
  414. "front" => $rssi1,
  415. "behind" => $rssi2,
  416. "left" => $rssi3,
  417. "right" => $rssi4,
  418. "time" => $time
  419. ];
  420. $rssisArr = $info["rssi"];
  421. // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
  422. foreach ($rssisArr as $k => $v) {
  423. # code...
  424. if( ($time - $v["time"] ) > $config['expInt'] ){
  425. unset($rssisArr[$k]);
  426. }
  427. }
  428. // var_dump("+++++++++++++++++",$rssisArr);
  429. if(count($rssisArr) == $config['effSigNum']){
  430. array_shift($rssisArr);
  431. array_push($rssisArr, $newRssi);
  432. }else{
  433. array_push($rssisArr, $newRssi);
  434. }
  435. $rssisArr =array_values($rssisArr);
  436. // var_dump("+++++++++++++++++",$rssisArr);
  437. $info["rssi"] = $rssisArr;
  438. // var_dump("------------",$info["rssi"]);
  439. $cacCount = count($rssisArr);
  440. // if($cacCount > $EFF_SIG_NUM){
  441. $sum1 = 0;
  442. $sum2 = 0;
  443. $sum3 = 0;
  444. $sum4 = 0;
  445. foreach ($rssisArr as $k => $v) {
  446. # code...
  447. $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
  448. $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
  449. $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
  450. $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
  451. }
  452. $info["avg"] = [
  453. "front" => number_format( $sum1/$cacCount, 2 ),
  454. "behind" => number_format( $sum2/$cacCount, 2 ),
  455. "left" => number_format( $sum3/$cacCount, 2 ),
  456. "right" => number_format( $sum4/$cacCount, 2 ),
  457. ];
  458. if ($cacCount == $EFF_SIG_NUM) {
  459. # code...
  460. $info = $this->compAndPush($info ,$config);
  461. }
  462. $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
  463. debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
  464. }
  465. }
  466. $text=substr($text,24);
  467. }else{
  468. $text=substr($text,1);
  469. }
  470. }
  471. return $text ?$text : "";
  472. }
  473. //判断进出和推送
  474. public function compAndPush($info, $config ){
  475. //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
  476. // $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
  477. //{"front_in":44,"front_out":44,"behind_in":50,"behind_out":50,"left_in":55,"left_out":55,"right_out":44,"right_out":44}
  478. $avg = $info['avg'];
  479. $status = $info['status'];
  480. $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
  481. $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
  482. $url="http://47.114.185.186:8115/api/areaReport";
  483. if($flagIn && $status == 2){
  484. //推送进
  485. $info['status'] = 1;
  486. $url_data = [
  487. "mac" => $info["mac"],
  488. "data" => [
  489. [
  490. "label" => $info["id"],
  491. "time" => $info["time"],
  492. "dirt" => 1
  493. ]
  494. ]
  495. ];
  496. debug_log("pushPareaRes","进推送依赖:".json_encode($info));
  497. $url_res= curl_http_post(json_encode($url_data),$url,false);
  498. debug_log("pushPareaRes","远程返回结果".$url_res);
  499. }
  500. // if($info['id'] == "E2B15AD5"){
  501. // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
  502. // }
  503. if($flagOut && $status == 1){
  504. //推送出
  505. $info['status'] = 2;
  506. $url_data = [
  507. "mac" => $info["mac"],
  508. "data" => [
  509. [
  510. "label" => $info["id"],
  511. "time" => $info["time"],
  512. "dirt" => 2
  513. ]
  514. ]
  515. ];
  516. debug_log("pushPareaRes","出推送依赖:".json_encode($info));
  517. $url_res= curl_http_post(json_encode($url_data),$url,false);
  518. debug_log("pushPareaRes","远程返回结果".$url_res);
  519. }
  520. return $info;
  521. }
  522. // $url="http://47.114.185.186:8115/api/accessReport";
  523. // $url_res= curl_http_post(json_encode($url_data),$url,false);
  524. // debug_log("InAndOUT","远程返回结果".$url_res);
  525. public function test(){
  526. try{
  527. $ues_redis=Cache::store('redis')->handler();
  528. $jsonData= $ues_redis->rpop("mqtt_data");
  529. debug_log("InAndOUT","1212");
  530. }catch(Exception $e){
  531. var_dump($e->getMessage());
  532. }
  533. }
  534. /**
  535. * 删除前1天的数据
  536. *
  537. * @return void
  538. */
  539. public function deleteHistory()
  540. {
  541. while(true)
  542. {
  543. $where[] = ['addTime','<',strtotime("-1 day")];
  544. Db::name('tag_history')->where($where)->delete();
  545. sleep(60*60*24);
  546. }
  547. }
  548. /**
  549. * 检测小程序版本版本 function
  550. *
  551. * @return void
  552. */
  553. public function detectionVersion(){
  554. $token = $_GET['token'];
  555. if (!$token || $token != '444333d3') {
  556. json_fail('缺少token或者token不对');
  557. }
  558. $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
  559. $data["version"]="1.1.1";
  560. json_success("查询版本成功",$data);
  561. }
  562. }