123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368 |
- <?php
- namespace catchAdmin\api\controller;
- use catchAdmin\api\service\dispose;
- use catchAdmin\tag_history\model\Access;
- use catchAdmin\tag_history\model\Dormitory;
- use catchAdmin\tag_history\model\TagHistory;
- use catchAdmin\worklocation\model\WorkRecord as ModelWorkRecord;
- use catcher\base\CatchRequest as Request;
- use catcher\CatchResponse;
- use catcher\base\CatchController;
- use Exception;
- use think\facade\Cache;
- use \think\facade\Db;
- error_reporting(0);
- class Api extends CatchController
- {
- public function report()
- {
- $token = $_GET['token'];
- if (!$token || $token != '22723927C') {
- json_fail('缺少token或者token不对');
- }
- $param = json_decode(optimize_json(file_get_contents("php://input")), true);
- debug_log("上报数据",json_encode($param));
- if (json_last_error() != 0) {
- json_fail('解析异常', json_last_error_msg());
- }
- $mac = $param['mac'];
- if (empty($mac)) {
- json_fail('mac地址不存在');
- }
- $data = [];
- foreach ($param['list'] as $item) {
- // $onetime= TagHistory::where("mac",$mac)->where('lable',$item['label'])->order("id","desc")->value("time");
- $data[] = [
- 'mac' => $mac,
- 'lable' => $item['label'],
- 'rssi' => $item['rssi'],
- 'time' => $item['time'],
- 'addTime' => time(),
- 'move' => $item['move'],
- 'step' => empty($item['step']) ? 0 : $item['step'],
- 'temperature' => empty($item['temperature']) ? 0 : $item['temperature'],
- //告警
- 'temperWarn' => empty($item['temperWarn']) ? 00 : $item['temperWarn'],
- // "interval_time"=>empty($onetime) ?0:($item['time']-$onetime)
- ];
- }
- $tage = new TagHistory();
- $tage->saveAll($data);
- json_success('上传成功');
- }
- //储存数据到redis
- public function redis_to_mysql(){
- $ues_redis=Cache::store('redis')->handler();
- $dispose = new dispose($ues_redis);
- $text=null;
-
-
- while(1){
- $jsonData= $ues_redis->rpop("mqtt_data");
- if(empty($jsonData)){
- sleep(1);
- continue;
- }
- debug_log("InAndOUT",'数据redis'.$jsonData);
-
- $data=json_decode($jsonData,true);
- $time=$data['time'];
- $mac=$data['devId'];
- //存入基站的最新时间
- $dispose->setStations($mac,$time);
- $list=[];
- $text=$text.$data['cnt'];
- $assetDataArr = [];
-
- if(strlen($text)<24){
- continue;
- }
-
- while(strlen($text)>=24){
-
- $da=substr($text,0,24);
- $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
- $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
-
- if($t1&&$t2){
-
- $label= substr($da,0,8);
- $rssi1= substr($da,16,2);
- $rssi2= substr($da,18,2);
- $rssi3= substr($da,20,2);
- $rssi4= substr($da,22,2);
- $DA=[
- 'mac' => $mac,
- 'label' =>$label,
- 'rssi1' => hexdec($rssi1),
- 'rssi2' => hexdec($rssi2),
- 'rssi3' => hexdec($rssi3),
- 'rssi4' => hexdec($rssi4),
- 'report_time'=>$time
- ];
- $vs=false;
- $history_status=false;
-
- $DA['other_time']=date("Y-m-d H:i:s",$time);
- //打印原始的数据日志
- debug_log("label_log","cssh: ".json_encode($DA));
- $rssiHi = 85;
- if ($mac == '638007') {
- # code...
- $rssiHi = 60;
- }
- if($DA['rssi1']<=$rssiHi||$DA['rssi2']<=$rssiHi||$DA['rssi3']<=$rssiHi||$DA['rssi4']<=$rssiHi){
- //原始数据只要其中有一条信号小于85 都存入redis 历史数据
- $history_status=true;
- }
- $rssiIn = 72;
- //安装调测代码
- $de_macs = ['638004','638001','638003'];
- $de_rfids = ['52A50A09','52A4E019'];
- if (in_array($mac, $de_macs) && in_array($label, $de_rfids)){
- $deDa = $DA;
- $deDa['time'] = date("Y-m-d H:i:s",$time);
- $deLkey = 'debug_'.$mac.'_'. hexdec($label);
- $ues_redis->lpush($deLkey,json_encode($deDa));
- }
- //资产盘点调测代码
- // if (in_array($mac, $de_macs) && in_array($label, $de_rfids)) {
- // # code...
- // $assetData = [
- // 'label' => $label,
- // 'ant' => 1,
- // 'rssi' => $DA['rssi1'],
- // 'time' => $time,
- // ];
- // array_push($assetDataArr, $assetData);
- // }else{
- if ($mac == '638007') {
- # code...
- $rssiIn = 60;
- }
-
- if($DA['rssi1']<=$rssiIn||$DA['rssi2']<=$rssiIn||$DA['rssi3']<=$rssiIn||$DA['rssi4']<=$rssiIn){
- //原始数据只要其中有一条信号小于72 都参与计算
- $vs=true;
- }
- $DA['rssi1']=$DA['rssi1']==255?100:$DA['rssi1'];
- $DA['rssi2']=$DA['rssi2']==255?100:$DA['rssi2'];
- $DA['rssi3']=$DA['rssi3']==255?100:$DA['rssi3'];
- $DA['rssi4']=$DA['rssi4']==255?100:$DA['rssi4'];
-
- $DA["rssi1"]=$DA["rssi1"]<=$DA["rssi3"]?$DA["rssi1"]:$DA["rssi3"];
- $DA["rssi2"]=$DA["rssi2"]<=$DA["rssi4"]?$DA["rssi2"]:$DA["rssi4"];
- /**
- * 缓存到redis中历史数据
- */
- if(substr($da,8,2)=='01'&&$history_status&&$DA["rssi1"]!=$DA["rssi2"]){
-
- debug_log("label_log","cache: ".json_encode($DA));
- $diff= $DA["rssi1"]-$DA["rssi2"];
- if(abs($diff)>2){
- //历史数据朝前和朝后的信号的差值大于2参与存储
- $dispose->set_label_history($DA);
- }
- debug_log("label_log","cache_diff: ".$diff);
- }
- /**
- * 参与计算的数据
- */
-
- if($vs&&(substr($da,8,2)=='01')){
- try{
- $DA= $dispose->check_data($DA);
- //处理的数据参与计算
- debug_log("label_log","xsj:".json_encode($DA));
-
- $dispose->computeResout($DA);
- }catch(Exception $e){
- debug_log("InAndOUT","抛出异常:".$e->getMessage());
- }
- }
-
- // }
- $text=substr($text,24);
- }else{
- $text=substr($text,1);
- }
-
- }
- if (!empty($assetDataArr)) {
- # code...
- $assetPushData = [
- 'mac' => $mac,
- 'data' => $assetDataArr
- ];
- $ues_redis->lpush('asset_after_handle',json_encode($assetPushData));
- }
-
-
- }
-
- }
- public function redis_to_mysql2(){
- $redis=Cache::store('redis')->handler();
- // $dispose = new dispose($redis);
- $text=null;
-
-
- while(1){
- $jsonData= $redis->rpop("asset_after_handle");
- if(empty($jsonData)){
- sleep(1);
- continue;
- }
-
- // $jsonData= '{"mac": "01396C","data": [{"label": "230600070007","time": 1691128977,"ant": 1,"rssi": 19}]}';
- $list=json_decode($jsonData,true);
- $redis=Cache::store('redis')->handler();
- $received_state=$redis->hget('api_receiver_asset_state', 'accessClassReport');
- $mac=$list['mac'];
- foreach($list['data'] as $val){
- if ($received_state == '1') {
- # code...
- $antData = json_decode( $redis->hGet('lab_ant_tmp',$val['label']) );
- if (!$antData) {
- # code...
- $antData = [
- $val['ant'] => 1
- ];
- }elseif(!$antData[$val['ant']]){
- $antData[$val['ant']] = 1;
- }else {
- # code...
- $antData[$val['ant']] += 1;
- }
- $redis->hset('lab_ant_tmp',$val['label'], json_encode($antData) );
- }else{
- $redis->del('lab_ant_tmp');
- }
- //1 3外 2 4内
- if($val['ant']=='3'){
- $val['ant']='1';
- }elseif($val['ant']=='4'){
- $val['ant']='2';
- }
- $report_data=[
- 'mac' => $mac,
- 'label' =>$val['label'],
- 'rssi' =>$val['rssi'],
- 'ant' =>$val['ant'],
- 'time'=>$val['time']
- ];
- $countKey='station_receive_count_data';
- $key=$mac.'_'.$val['label'];
- $count_data=$redis->hGet($countKey,$key);
- if($count_data){
- $count_data++;
- }else{
- $count_data=1;
- }
- $redis->hSet($countKey,$key,$count_data);
- // var_dump($report_data);
- 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);
- if($received_state=='1'){
- //如果开启盘点 则有数据就推送
- $url="http://localhost:8115/api/assetReceiveReport";
- var_dump($report_data);
- $postFields = http_build_query($report_data);
- $url_res= curl_http_post($postFields,$url,false);
- echo 'assetReceiveReport res'.PHP_EOL;
- var_dump($url_res);
- }
- //推送给闸机用
- $url="http://localhost:8115/api/gateReport";
- var_dump($report_data);
- $postFields = http_build_query($report_data);
- $url_res= curl_http_post($postFields,$url,false);
- echo 'gateReceiveReport res'.PHP_EOL;
- var_dump($url_res);
- $hashKey='station_access_last_data';
- $beforeKey='station_access_before_data';
- $key=$mac.'_'.$val['label'];
- $last_data=json_decode($redis->hGet($hashKey,$key),true);
- // var_dump($old_data);
- if($last_data){
- //取出历史数据 对比两次数据变化
- if($last_data['ant']==$report_data['ant']){
- //相同 则更新时间
- $report_data['count']=$last_data['count']+1;
- $before_data=json_decode($redis->hGet($beforeKey,$key),true);
- //存在反向数据 并且连续搜到单边数据5次
- if($before_data && ($report_data['count']>5)){
- //开始推送数据;
- $send_data=[
- "mac"=>$mac,
- "label"=>$val['label'],
- 'time'=>$val['time'],
- ];
- if($report_data['ant']>$before_data['ant']){
- //1->2 外到内 进
- $send_data['dirt']=1;
- }else{
- //2>1 内到外 出
- $send_data['dirt']=2;
- }
- var_dump($send_data);
- //推送数据
- $url="http://localhost:8115/api/assetReport";
- $postFields = http_build_query($send_data);
- $url_res= curl_http_post($postFields,$url,false);
- echo 'assetReport res'.PHP_EOL;
- var_dump($url_res);
- //推送完清除数据
- $redis->hDel($hashKey,$key);
- $redis->hDel($beforeKey,$key);
-
- }else{
- $redis->hSet($hashKey,$key,json_encode($report_data));
- }
-
- }else{
- //比较数据时间
- //默认新数据更晚上报
- $report_data['count']=1;
- $redis->hSet($hashKey,$key,json_encode($report_data));
- $redis->hSet($beforeKey,$key,json_encode($last_data));
- // $send_data=[
- // "mac"=>$mac,
- // "label"=>$val['label'],
- // 'time'=>$val['time'],
- // ];
- // if($report_data['ant']>$last_data['ant']){
- // //1->2 内到外 出
- // $send_data['dirt']=2;
- // }else{
- // //2>1 外到内 进
- // $send_data['dirt']=1;
- // }
- // var_dump($send_data);
- // //推送数据
- // $url="http://localhost:8115/api/assetReport";
- // $postFields = http_build_query($send_data);
- // $url_res= curl_http_post($postFields,$url,false);
- // echo 'assetReport res'.PHP_EOL;
- // var_dump($url_res);
- // $redis->hSet($hashKey,$key,json_encode($report_data));
- }
-
- }else{
- $report_data['count']=1;
- $redis->hSet($hashKey,$key,json_encode($report_data));
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- }
-
- }
- public function asset_redis(){
- while(true){
- $redis=Cache::store('redis')->handler();
- $hashKey='station_access_last_data';
- $beforeKey='station_access_before_data';
- $list=$redis->hgetall($hashKey);
- foreach($list as $key=>$val){
- $report_data = json_decode($val, true);
- if ( (time() - $report_data["time"] ) > 10 ) {
- $before_data=json_decode($redis->hGet($beforeKey,$key),true);
- //存在反向数据 并且连续搜到单边数据5次
- if($before_data){
- //开始推送数据;
- $send_data=[
- "mac"=>$report_data['mac'],
- "label"=>$report_data['label'],
- 'time'=>$report_data['time'],
- ];
-
- if($report_data['ant']>$before_data['ant']){
- //1->2 外到内 进
- $send_data['dirt']=1;
- }else{
- //2>1 内到外 出
- $send_data['dirt']=2;
- }
- var_dump($send_data);
- //推送数据
- $url="http://localhost:8115/api/assetReport";
- $postFields = http_build_query($send_data);
- $url_res= curl_http_post($postFields,$url,false);
- echo 'assetReport res'.PHP_EOL;
- var_dump($url_res);
- //推送完清除数据
- $redis->hDel($hashKey,$key);
- $redis->hDel($beforeKey,$key);
-
- }
- }
- }
- sleep(1);
- }
- }
- /**
- * 清除数据
- *
- * @return void
- */
- public function clear_redis(){
- //清理超时的标签
- while(1){
- sleep(1);
- $redis=Cache::store('redis')->handler();
- $dispose = new dispose($redis);
- $keylist= $dispose->getStations();
- //获取存储的的基站
- debug_log("clear_label","查询数据".json_encode($keylist));
- if(empty($keylist)){
- continue;
- }
- foreach($keylist as $station){
- //结果key值
- $key="res".$station;
- //存取旧的数据的reids数据
- $catchkey=$station."station";
- //获取该基站的所有key
- $keys= $redis->hKeys($key);
- if(empty($keys)){
- continue;
- }
- foreach($keys as $item){
- if( $json_data=$redis->hget($key,$item)){
-
- debug_log("clear_label",'基站'.$station.'获取的数据'.'标签'.$item.$json_data);
- $json_data=json_decode($json_data,true);
- //列表
- $a=$json_data['a'];
- //状态
- $status=$json_data['status'];
- //是否形成进出考勤
- $in_and_out=$json_data["in_and_out"];
-
- //总数
- $count= count($a);
- $time= $dispose->getNowStationTime($station);
-
- debug_log("clear_label","标签最新最新时间". date('Y-m-d H:i:s', $time));
- //如果标签间隔超过7s,则超时并判断能否生成进出
- if(($time-$json_data["time"])<=7){
-
- debug_log("clear_label", $item.':'. "标签上报间隔没有超过7秒进行跳过");
- continue;
- }
-
- if($count<5){
- //如果信号条数小于等于2条不参与计算,信号条数大于2小于5条,则直接进行判断是否形成进出
- if($count>2){
- debug_log("clear_label", $item.':'. "标签信号个数等于超过2个,参与计算");
- $frist=$a[0]['dirt'];
- $end=$a[$count-1]['dirt'];
- if($frist!=$end){
- debug_log("clear_label", $item.':'. "标签信号个数等于超过2个,参与计算,得出结果:".$frist);
- $dispose->network_push($station,$item,$a[$count-1]['time'],$frist==1?1:2);
- }else{
- debug_log("clear_label", $item.':'. "标签信号个数没有超过2个,全部去清除");
- }
- }
- }else{
- //如果信号条数大于5条,则判断最后三条数据的信号强的方向是否和初始方向相反
- $front=0;
- $back=0;
- $res=0;
- if($a[$count-3]['dirt']==1){
- $front+=1;
- }else{
- $back+=1;
- }
- if($a[$count-2]['dirt']==1){
- $front+=1;
- }else{
- $back+=1;
- }
- if($a[$count-1]['dirt']==1){
- $front+=1;
- }else{
- $back+=1;
- }
- if($front>$back){
- $res=1;
- }else{
- $res=2;
- }
-
- if($status['dirt']!=$res){
- //最后三条数据的信号强的方向和初始方向相反,则形成进出考勤
- debug_log("clear_label", $item.':'. "最后一个信号,方向相反,得出结果,清除缓存数据");
- $dispose->network_push($station,$item,$a[$count-1]['time'],$status['dirt']==1?1:2);
- }else{
- //最后三条数据的信号强的方向和初始方向不相反,并且在连续的时间内没有生成考勤,则开始第二次判断
- if(empty($in_and_out)){
- //判断第二次能否生成考勤结果
- $res=$dispose->second_create_direction($station,$item);
- if(!empty($res)){
- //获取该标签的之前最新的考勤,和当前形成考勤时间间隔是否大于10,
- //如果大于10则生成考勤结果
- $res_time= $dispose->get_time_results($station,$item);
- $res_time= empty($res_time)?0:$res_time;
- $diff_time=$res['time']-$res_time;
- debug_log("clear_label", $item.':'. "间隔时间".$diff_time);
- //判断如果结果超过时间
- if($diff_time>10){
- debug_log("clear_label", $item.':'. "二次生成进出");
- $dispose->network_push($station,$item,$res['time'],$res['dirt']);
- }
- }else{
- //二次判断如果也没生成考勤,如果朝前的信号最大强度低于65,
- //朝后的信号最大强度也低于65,则记录下来最后的朝向,
- //存入没有生成考勤时的初始数据
- //获取朝前的最大值
- //获取朝后的最大值
- $frontMax= $dispose->get_label_history(4,$station,$item);
- $backMax= $dispose->get_label_history(5,$station,$item);
- $records=false;
- if($status['dirt']==1){
- $records=$frontMax<65;
- }
- if($status['dirt']==2){
- $records=$backMax<65;
- }
- if($records){
- $dispose->no_check_data($station,$item,$status['dirt'],$a[$count-1]['time'],1);
- }
- }
- }
- //二次进出
- debug_log("clear_label", $item.':'. "最后一个信号,方向没有变化,清除缓存数据");
-
- }
-
-
- }
- //清除对应的键值对
- //清空头部尾部历史
- $dispose->get_label_history(3,$station,$item);
- $redis->hdel($key,$item);
- $redis->hdel($catchkey,$item);
-
- }
-
- }
- }
-
- }
- }
- /**
- * 远程推送数据
- *
- * @return void
- */
- public function remotePush(){
- while(true){
- $redis=Cache::store('redis')->handler();
- $key="push_data";
- $url="http://localhost:8115/api/accessReport";
-
- sleep(2);
- while($data=$redis->lPop($key)){
- if(!empty($data)){
- debug_log($key,"远程推送的数据".$data);
- $url_res= curl_http_post($data,$url,false);
- $num=1;
- while($url_res==false){
- $num++;
- $url_res= curl_http_post($data,$url,false);
- if($num>=3){
- debug_log($key,"远程推送的地址".$url);
- debug_log($key,"连续推送三次失败,远程推送的数据".$data);
- break;
- }
- }
- debug_log($key,"远程返回结果".$url_res);
- }
- }
- }
- }
-
- public function redis_to_mysql_parea(){
- $ues_redis=Cache::store('redis')->handler();
- $text="";
- $dispose = new dispose($ues_redis);
- while(1){
- $jsonData= $ues_redis->rpop("mqtt_data_parea4rssi");
- debug_log("pare_InAndOUT",'数据redis'.$jsonData);
-
- if(empty($jsonData)){
- debug_log("pare_InAndOUT",'没有数据');
- sleep(1);
- continue;
-
- }
- // debug_log("InAndOUT",$jsonData);
- $data=json_decode($jsonData,true);
- // debug_log("InAndOUT",$data['cnt']);
- $time=$data['time'];
- $mac=$data['devId'];
- $text=$text.$data['cnt'];
-
- if(strlen($text)<24){
- continue;
- }
- $text = $this->handleMessage($text, $mac, $time, $ues_redis);
- }
- }
- public function redis_to_mysql_indoor(){
- $ues_redis=Cache::store('redis')->handler();
- $text="";
- $dispose = new dispose($ues_redis);
- while(1){
- $jsonData= $ues_redis->lpop("mqtt_data_indoor4rssi");
- debug_log("indoor_InAndOUT",'mqtt数据redis'.$jsonData);
-
- if(!$jsonData){
- debug_log("indoor_InAndOUT",'mqtt没有数据');
- sleep(1);
- continue;
-
- }
- // debug_log("InAndOUT",$jsonData);
- $data=json_decode($jsonData,true);
- // debug_log("InAndOUT",$data['cnt']);
- $time=$data['time'];
- $mac=$data['devId'];
- $text=$text.$data['cnt'];
-
- if(strlen($text)<24){
- continue;
- }
- // var_dump("before",microtime());
- $text = $this->handleMessageIndoor($text, $mac, $time, $ues_redis);
- // var_dump("after",microtime());
- }
- }
- //打印日志
- public function temporary_label_log(){
- $redis=Cache::store('redis')->handler();
- // $key="temporary_label";
- // $list= $redis->HgetAll($key);
- // debug_log($key,json_encode($list));
- // debug_log($key,"所有的标签");
- // foreach($list as $k=>$value){
- // debug_log($key,json_encode([$k=>$value]));
- // }
- // debug_log($key,"所有总数".count($list));
- // debug_log($key,"上报的标签");
- // $i=0;
- // foreach($list as $k=>$value){
- // if($value==2){
- // debug_log($key,json_encode([$k=>$value]));
- // $i++;
- // }
- // }
- // debug_log($key,"正常上报的标签总数".$i);
- // debug_log($key,"补出的标签");
- // $t=0;
- // foreach($list as $k=>$value){
- // if($value==3){
- // $t++;
- // debug_log($key,json_encode([$k=>$value]));
- // }
- // }
- // debug_log($key,"补出的标签总数".$t);
- // debug_log($key,"没有上报的的标签");
- // $f=0;
- // foreach($list as $k=>$value){
- // if($value==1){
- // debug_log($key,json_encode([$k=>$value]));
- // $f++;
- // }
-
- // }
- // debug_log($key,"没有上报的的标签总数".$f);
- // // $redis->del($key);
- $dis= new dispose($redis);
- $dis->analysisLog();
-
- }
- // public function temporary_label($redis,$label,$type){
- // $key="temporary_label";
- // if($type==1){
- // $res= $redis->hGet($key,$label);
- // if($res==false){
- // $redis->hSet($key,$label,1);
- // }
- // }else{
- // $redis->hSet($key,$label,3);
- // }
- // }
- //消息处理--重点区域
- public function handleMessage($text, $mac, $time, $ues_redis){
- while(strlen($text)>=24){
-
- $da=substr($text,0,24);
- $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
- $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
-
- if($t1&&$t2){
- // var_dump($text);
- $rfid= substr($da,0,8);
- $rssi1= hexdec(substr($da,16,2));
- $rssi2= hexdec(substr($da,18,2));
- $rssi3= hexdec(substr($da,20,2));
- $rssi4= hexdec(substr($da,22,2));
- // $rc1 = ($rssi1 == 255 ? 0 : 1);
- // $rc2 = ($rssi2 == 255 ? 0 : 1);
- // $rc3 = ($rssi3 == 255 ? 0 : 1);
- // $rc4 = ($rssi4 == 255 ? 0 : 1);
- $DA=[
- 'mac' => $mac,
- 'label' =>$rfid,
- 'rssi1' => $rssi1,
- 'rssi2' => $rssi2,
- 'rssi3' => $rssi3,
- 'rssi4' => $rssi4,
- 'report_time'=>$time
- ];
-
- $field = $mac."_".$rfid;
- $info = $ues_redis->hget("parea_rfidinfos",$field);
- debug_log("pare_InAndOUT","数据:".json_encode($DA,true));
- if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
- debug_log("pare_InAndOUT","无效数据:".json_encode($DA,true));
- }else{
- if(!$info){
- $info = [
- "mac" => $mac,
- "id" => $rfid,
- "first_time" => $time,
- "time" => $time,
- "status" => 2,//2出状态,1进状态
- "rssi" => [
- [
- "front" => $rssi1,
- "behind" => $rssi2,
- "left" => $rssi3,
- "right" => $rssi4,
- "time" => $time
- ]
- ]
- ];
- $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
- debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
- }else{
- $info = json_decode($info,true);
- $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
-
- if (!$config) {
- # code...
- $config = [
- "front_in" => 80,
- "behind_in" => 80,
- "left_in" => 80,
- "right_in" => 80,
- "front_out" => 255,
- "behind_out" => 255,
- "left_out" => 255,
- "right_out" => 255,
- "expInt" => 12,
- "effSigNum" => 6
- ];
- }else{
- $config = json_decode($config,true);
- }
- // var_dump("+++++++++++++++++",$config);
-
- $info["time"] = $time;
- $newRssi = [
- "front" => $rssi1,
- "behind" => $rssi2,
- "left" => $rssi3,
- "right" => $rssi4,
- "time" => $time
- ];
- $rssisArr = $info["rssi"];
-
- // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
-
- foreach ($rssisArr as $k => $v) {
- # code...
- if( ($time - $v["time"] ) > $config['expInt'] ){
- unset($rssisArr[$k]);
- }
- }
- // var_dump("+++++++++++++++++",$rssisArr);
- if(count($rssisArr) == $config['effSigNum']){
- array_shift($rssisArr);
- array_push($rssisArr, $newRssi);
- }else{
- array_push($rssisArr, $newRssi);
- }
-
- $rssisArr =array_values($rssisArr);
- // var_dump("+++++++++++++++++",$rssisArr);
- $info["rssi"] = $rssisArr;
-
- // var_dump("------------",$info["rssi"]);
-
- $cacCount = count($rssisArr);
- // if($cacCount > $EFF_SIG_NUM){
- $sum1 = 0;
- $sum2 = 0;
- $sum3 = 0;
- $sum4 = 0;
- foreach ($rssisArr as $k => $v) {
- # code...
- $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
- $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
- $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
- $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
- }
- $info["avg"] = [
- "front" => number_format( $sum1/$cacCount, 2 ),
- "behind" => number_format( $sum2/$cacCount, 2 ),
- "left" => number_format( $sum3/$cacCount, 2 ),
- "right" => number_format( $sum4/$cacCount, 2 ),
- ];
- // var_dump($cacCount, $EFF_SIG_NUM, "dddddddd");
- if ($cacCount == $config['effSigNum']) {
- # code...
- $info = $this->compAndPush($info ,$config, $ues_redis);
- }
- $ues_redis->hset("parea_rfidinfos",$field, json_encode($info,true));
-
- debug_log("parea_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
- }
- }
- $text=substr($text,24);
- }else{
- $text=substr($text,1);
- }
- }
- return $text ?$text : "";
- }
- //消息处理--室内
- public function handleMessageIndoor($text, $mac, $time, $ues_redis){
- while(strlen($text)>=24){
-
- $da=substr($text,0,24);
- $t1 = substr($da,8,2)=='00'||substr($da,8,2)=='10'||substr($da,8,2)=='01' ? true:false;
- $t2 =substr($da,10,6)=="FFFFFF"||substr($da,10,6)=="000000" ?true:false;
-
- if($t1&&$t2){
- // var_dump($text);
- $rfid= substr($da,0,8);
- $rssi1= hexdec(substr($da,16,2));
- $rssi2= hexdec(substr($da,18,2));
- $rssi3= hexdec(substr($da,20,2));
- $rssi4= hexdec(substr($da,22,2));
-
- $DA=[
- 'mac' => $mac,
- 'label' =>$rfid,
- 'rssi1' => $rssi1,
- 'rssi2' => $rssi2,
- 'rssi3' => $rssi3,
- 'rssi4' => $rssi4,
- 'report_time'=>$time
- ];
-
- $field = $mac."_".$rfid;
- $info = $ues_redis->hget("indoor_rfidinfos",$field);
- debug_log("indoor_InAndOUT","数据:".json_encode($DA,true));
- if($rssi1 ==255 && $rssi2 == 255 && $rssi3 == 255 && $rssi4 == 255){
- debug_log("indoor_InAndOUT","无效数据:".json_encode($DA,true));
- }else{
- if(!$info){
- $info = [
- "mac" => $mac,
- "id" => $rfid,
- "first_time" => $time,
- "time" => $time,
- "status" => 2,//2出状态,1进状态
- "rssi" => [
- [
- "front" => $rssi1,
- "behind" => $rssi2,
- "left" => $rssi3,
- "right" => $rssi4,
- "time" => $time
- ]
- ]
- ];
- $ues_redis->hset("indoor_rfidinfos",$field, json_encode($info,true));
- debug_log("indoor_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
- }else{
- $info = json_decode($info,true);
- $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
-
- if (!$config) {
- # code...
- $config = [
- "front_in" => 80,
- "behind_in" => 80,
- "left_in" => 80,
- "right_in" => 80,
- "front_out" => 255,
- "behind_out" => 255,
- "left_out" => 255,
- "right_out" => 255,
- "expInt" => 12,
- "effSigNum" => 6
- ];
- }else{
- $config = json_decode($config,true);
- }
- // var_dump("+++++++++++++++++",$config);
-
- $info["time"] = $time;
- $newRssi = [
- "front" => $rssi1,
- "behind" => $rssi2,
- "left" => $rssi3,
- "right" => $rssi4,
- "time" => $time
- ];
- $rssisArr = $info["rssi"];
-
- // var_dump("+++++++++++++++++", $time, $newRssi["time"]);
-
- foreach ($rssisArr as $k => $v) {
- # code...
- if( ($time - $v["time"] ) > $config['expInt'] ){
- unset($rssisArr[$k]);
- }
- }
- // var_dump("+++++++++++++++++",$rssisArr);
- if(count($rssisArr) == $config['effSigNum']){
- array_shift($rssisArr);
- array_push($rssisArr, $newRssi);
- }else{
- array_push($rssisArr, $newRssi);
- }
-
- $rssisArr =array_values($rssisArr);
- // var_dump("+++++++++++++++++",$rssisArr);
- $info["rssi"] = $rssisArr;
-
- // var_dump("------------",$info["rssi"]);
-
- $cacCount = count($rssisArr);
- // if($cacCount > $EFF_SIG_NUM){
- $sum1 = 0;
- $sum2 = 0;
- $sum3 = 0;
- $sum4 = 0;
- foreach ($rssisArr as $k => $v) {
- # code...
- $sum1 += ($v["front"] == 255 ? 90 : $v["front"]);
- $sum2 += ($v["behind"] == 255 ? 90 : $v["behind"]);
- $sum3 += ($v["left"] == 255 ? 90 : $v["left"]);
- $sum4 += ($v["right"] == 255 ? 90 : $v["right"]);
- }
- $info["avg"] = [
- "front" => number_format( $sum1/$cacCount, 2 ),
- "behind" => number_format( $sum2/$cacCount, 2 ),
- "left" => number_format( $sum3/$cacCount, 2 ),
- "right" => number_format( $sum4/$cacCount, 2 ),
- ];
- // var_dump($cacCount, $EFF_SIG_NUM, "dddddddd");
- if ($cacCount == $config['effSigNum']) {
- # code...
- $info = $this->compAndPushIndoor($info ,$config, $ues_redis);
- }
- $ues_redis->hset("indoor_rfidinfos",$field, json_encode($info,true));
-
- debug_log("indoor_rfidinfos_redis_set_record","redis更新记录".json_encode($info,true));
- }
- }
- $text=substr($text,24);
- }else{
- $text=substr($text,1);
- }
- }
- return $text ?$text : "";
- }
- //判断进出和推送--室内考勤
- public function compAndPushIndoor($info, $config, $ues_redis){
- //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
- // $config = $ues_redis->hget("anbang_four_wire", $info["mac"]);
-
- //{"front_in":44,"front_out":44,"behind_in":50,"behind_out":50,"left_in":55,"left_out":55,"right_out":44,"right_out":44}
-
- $avg = $info['avg'];
- $status = $info['status'];
- $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
- $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
-
- $url="http://localhost:8115/api/api/accessClassReport";
- // $url="http://61.175.203.188:10001/api/accessClassReport";
- // if($flagIn && $status == 2){
- if($flagIn){
- if ($info["pushTime"] && (time()-$info['pushTime']) < 120 ) {
- # code...
- return $info;
- }
- //推送进
- $info['status'] = 1;
-
- $url_data = [
- "mac" => $info["mac"],
- "data" => [
- [
- "label" => $info["id"],
- "time" => $info["time"],
- "dirt" => 1,
- "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
- "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
- ]
- ]
- ];
- $ues_redis->lpush("indoor_inoutres_push", json_encode($url_data));
- $info['pushTime'] = time();
- // debug_log("push_indoor_res","进推送依赖:".json_encode($info));
- // $url_res= curl_http_post(json_encode($url_data),$url,false);
- // debug_log("push_indoor_res","远程返回结果".$url_res);
- }
- // if($info['id'] == "E2B15AD5"){
- // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
- // }
- if($flagOut && $status == 1){
- if ($info["pushTime"] && (time()-$info['pushTime']) < 5 ) {
- # code...
- return $info;
- }
- //推送出
- $info['status'] = 2;
- $url_data = [
- "mac" => $info["mac"],
- "data" => [
- [
- "label" => $info["id"],
- "time" => $info["time"],
- "dirt" => 2,
- "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
- "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
- ]
- ]
- ];
- $ues_redis->lpush("indoor_inoutres_push", json_encode($url_data));
- // debug_log("push_indoor_res","出推送依赖:".json_encode($info));
- // $url_res= curl_http_post(json_encode($url_data),$url,false);
- $info['pushTime'] = time();
- // debug_log("push_indoor_res","远程返回结果".$url_res);
- }
- return $info;
- }
-
- //判断进出和推送
- public function compAndPush($info, $config,$ues_redis){
- //标签多,数据多时候,判断完成后放入redis,异步推送,当前暂时直接推
- $avg = $info['avg'];
- $status = $info['status'];
- $flagIn = $avg["front"] < $config["front_in"] || $avg["behind"] < $config["behind_in"] || $avg["left"] < $config["left_in"] || $avg["right"] < $config["right_in"];
- $flagOut = $avg["front"] > $config["front_out"] && $avg["behind"] > $config["behind_out"] && $avg["left"] > $config["left_out"] && $avg["right"] > $config["right_out"];
- if ($info['mac'] == '638101') {
- # code...
- echo '-------------label:'.$info['id'].'------------------'.PHP_EOL;
- var_dump($info['first_in_time'],$info['time'], $info['pushTime'], $flagIn, $flagOut);
- }
- if($flagIn && $status == 2){
- // if($flagIn && ($status == 2 || $status == 3) ){
- // if($flagIn){
- $inoutIntFilter = $ues_redis->hget("anbang_four_wire", "anbang_4rssi_intFilter");
- $inoutIntFilter = $inoutIntFilter ? $inoutIntFilter : 120;
- if(!$info['first_in_time']){
- $info['first_in_time'] = time();
- }
-
- if ($info["pushTime"] && (time()-$info['pushTime']) < 120 ) {
- # code...
- return $info;
- }
- // // 临时功能代码
- // var_dump($info["id"]);
- // $labels = ['0200C1D1', '0200C1D2']; //测试设备
- // if (in_array(strtoupper($info['id']), $labels)) {
- // # code...
- // //card_black
- // $where = [];
- // $where[] = ['type', '=','basic_config'];
- // $where[] = ['field', '=','card_black'];
- // $bkList = Db::name('sys_config')->where($where)->find();
- // $bkList = explode(',', $bkList['fieldValue']);
- // // var_dump($info['id'],in_array(strtoupper($info['id']), $bkList),$bkList);
- // if (in_array(strtoupper($info['id']), $bkList)) {
- // # code...
- // $info['status'] = 3;
- // }
-
- // }
- $expInt =$info["time"]-$info["first_in_time"];
-
- if ($expInt >= 100 ) {
- //推送进
- $info['status'] = 1;
- # code...
- // var_dump('-------------expint------',$info["id"],$expInt);
- $url_data = [
- "mac" => $info["mac"],
- "data" => [
- [
- "label" => $info["id"],
- "time" => $info["time"],
- "first_time" => $info["first_time"],
- "dirt" => $info['status'],
- "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
- "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
- ]
- ]
- ];
- // debug_log("pushPareaRes","进推送依赖:".json_encode($info));
- $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
- $info['pushTime'] = time();
- }
- // debug_log("pushPareaRes","远程返回结果".$url_res);
- }
- // if($info['id'] == "E2B15AD5"){
- // var_dump($flagIn, $flagOut, $status, $avg["front"],$avg["behind"],$avg["left"], $avg["right"]);
- // }
- if($flagOut && $status == 1){
- $info["first_in_time"] = null;
- if ($info["pushTime"] && (time()-$info['pushTime']) < 2 ) {
- # code...
- return $info;
- }
- //推送出
- $info['status'] = 2;
- $url_data = [
- "mac" => $info["mac"],
- "data" => [
- [
- "label" => $info["id"],
- "time" => $info["time"],
- "dirt" => 2,
- "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
- "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
- ]
- ]
- ];
- // debug_log("pushPareaRes","出推送依赖:".json_encode($info));
- // $url_res= curl_http_post(json_encode($url_data),$url,false);
- $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
- $info['pushTime'] = time();
-
- // debug_log("pushPareaRes","远程返回结果".$url_res);
- }
- // if($flagIn && $info['status'] == 1){
- // $inoutIntFilter = $ues_redis->hget("anbang_four_wire", "anbang_4rssi_intFilter");
- // $inoutIntFilter = $inoutIntFilter ? $inoutIntFilter : 20;
- // if ($info["camPushTime"] && (time()-$info['camPushTime']) < $inoutIntFilter ) {
- // # code...
- // return $info;
- // }
- // //推送进
- // $url_data = [
- // "mac" => $info["mac"],
- // "first_time" => $info["first_time"],
- // "data" => [
- // [
- // "label" => $info["id"],
- // "time" => $info["time"],
- // "dirt" => 1,
- // "rssi" => $info["rssi"],//数组 [{"front":72,"behind":255,"left":255,"right":255,"time":1685067709},{"front":76,"behind":255,"left":255,"right":255,"time":1685067710}]
- // "avg" => $info["avg"] //{"front":"77.17","behind":"90.00","left":"90.00","right":"90.00"}
- // ]
- // ]
- // ];
- // $ues_redis->lpush("parea_inoutres_push", json_encode($url_data));
- // $info["camPushTime"] = time();
- // }
- return $info;
- }
- public function test(){
- try{
- $ues_redis=Cache::store('redis')->handler();
- $jsonData= $ues_redis->rpop("mqtt_data");
- debug_log("InAndOUT","1212");
- }catch(Exception $e){
- var_dump($e->getMessage());
- }
-
- }
- /**
- * 删除前1天的数据
- *
- * @return void
- */
- public function deleteHistory()
- {
- while(true)
- {
-
- $where[] = ['addTime','<',strtotime("-1 day")];
- Db::name('tag_history')->where($where)->delete();
- sleep(60*60*24);
- }
-
- }
- /**
- * 检测小程序版本版本 function
- *
- * @return void
- */
- public function detectionVersion(){
- $token = $_GET['token'];
- if (!$token || $token != '444333d3') {
- json_fail('缺少token或者token不对');
- }
- $data["url"]="http://116.62.220.88:8112/static/app/蓝牙小程序/1.1.1/rl-release.apk";
- $data["version"]="1.1.1";
- json_success("查询版本成功",$data);
- }
-
- //储存数据
- public function livestockDataToMysql(){
- $ues_redis=Cache::store('redis')->handler();
- $dispose = new dispose($ues_redis);
- $text=null;
-
-
- // while(1){
- $jsonData= $ues_redis->rpop("mqtt_data_livestock");
- // if(empty($jsonData)){
- // sleep(1);
- // continue;
- // }
- debug_log("LiveStockData",'数据redis'.$jsonData);
-
- $data=json_decode($jsonData,true);
- $url="http://127.0.0.1/api/reportMessage";
- var_dump($data);
- $postFields = http_build_query($data);
- $url_res= curl_http_post($postFields,$url,false);
- var_dump($url_res);
-
-
-
- // }
-
- }
- }
|