Api.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. '884619516',
  59. '885907612',
  60. '3827256674',
  61. '885833308',
  62. '884019740',
  63. '884765612'
  64. ];
  65. while(1){
  66. $jsonData= $ues_redis->rpop("mqtt_data");
  67. debug_log("InAndOUT",'数据redis'.$jsonData);
  68. if(empty($jsonData)){
  69. debug_log("InAndOUT",'没有数据');
  70. sleep(1);
  71. continue;
  72. }
  73. // debug_log("InAndOUT",$jsonData);
  74. $data=json_decode($jsonData,true);
  75. // debug_log("InAndOUT",$data['cnt']);
  76. $time=$data['time'];
  77. $mac=$data['imei'];
  78. $list=[];
  79. $text=$text.$data['cnt'];
  80. if(strlen($text)<24){
  81. continue;
  82. }
  83. // $arr=['']
  84. while(strlen($text)>=24){
  85. $da=substr($text,0,24);
  86. $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
  87. $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
  88. if($t1&&$t2){
  89. $label= substr($da,0,8);
  90. $rssi1= substr($da,16,2);
  91. $rssi2= substr($da,18,2);
  92. $rssi3= substr($da,20,2);
  93. $rssi4= substr($da,22,2);
  94. $DA=[
  95. 'mac' => $mac,
  96. 'label' =>$label,
  97. 'rssi1' => hexdec($rssi2),
  98. 'rssi2' => hexdec($rssi3),
  99. 'rssi3' => hexdec($rssi1),
  100. 'rssi4' => hexdec($rssi4),
  101. 'report_time'=>$time
  102. ];
  103. // if($DA['rssi1']==255){
  104. // $DA['rssi1']=100;
  105. // }
  106. // if($DA['rssi2']==255){
  107. // $DA['rssi2']=100;
  108. // }
  109. // debug_log("InAndOUT","数据:".json_encode($DA,true));
  110. //保存计算中
  111. //分为8个桶
  112. // $num=Hexdec($label);
  113. // $Identification=$num%8;
  114. // $key="buckets".$Identification;
  115. // $ues_redis->rPush($key,json_encode($DA,true));
  116. $num=Hexdec($label);
  117. $vs=false;
  118. if($DA['rssi1']<255&&$DA['rssi2']<255){
  119. $vs=true;
  120. }
  121. if(in_array($num,$testArray)&&$vs&&(substr($da,8,2)=='01')){
  122. $testData=$DA;
  123. $testData['num']=$num;
  124. debug_log("label_log",json_encode($testData));
  125. debug_log('label_log',$da);
  126. try{
  127. $dispose = new dispose($ues_redis);
  128. $computedata=$dispose->computeData($DA);
  129. //debug_log("InAndOUT","计算完成的数据:".json_encode($computedata,true));
  130. $dispose->computeResout($computedata);
  131. }catch(Exception $e){
  132. debug_log("InAndOUT","抛出异常".json_encode($DA,true));
  133. }
  134. }
  135. $text=substr($text,24);
  136. }else{
  137. $text=substr($text,1);
  138. }
  139. }
  140. // $tage = new Access();
  141. // $tage->saveAll($list);
  142. }
  143. }
  144. public function test(){
  145. try{
  146. $ues_redis=Cache::store('redis')->handler();
  147. $jsonData= $ues_redis->rpop("mqtt_data");
  148. debug_log("InAndOUT","1212");
  149. }catch(Exception $e){
  150. var_dump($e->getMessage());
  151. }
  152. }
  153. /**
  154. * 删除前1天的数据
  155. *
  156. * @return void
  157. */
  158. public function deleteHistory()
  159. {
  160. while(true)
  161. {
  162. $where[] = ['addTime','<',strtotime("-1 day")];
  163. Db::name('tag_history')->where($where)->delete();
  164. sleep(60*60*24);
  165. }
  166. }
  167. /**
  168. * 检测小程序版本版本 function
  169. *
  170. * @return void
  171. */
  172. public function detectionVersion(){
  173. $token = $_GET['token'];
  174. if (!$token || $token != '444333d3') {
  175. json_fail('缺少token或者token不对');
  176. }
  177. $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
  178. $data["version"]="1.1.1";
  179. json_success("查询版本成功",$data);
  180. }
  181. }