tongshanglei 4 months ago
parent
commit
49cf87f56a
1 changed files with 82 additions and 10 deletions
  1. 82 10
      task_script/RLSTA_DOOR_MQTT_PUBLISH.php

+ 82 - 10
task_script/RLSTA_DOOR_MQTT_PUBLISH.php

@@ -81,19 +81,94 @@ function sendConfig($topic,$config)
     $mqtt = new MqttClient($server, $port, $clientId);
 
     $mqtt->connect($connectionSettings, $clean_session);
-    echo 'connect OK'.PHP_EOL;
-    echo 'topic:'.$topic.PHP_EOL;
-    echo 'config:'.$config.PHP_EOL;
+    rlog("[MqttClient] connect OK" );
+    rlog("[MqttClient] topic: " .$topic);
+    rlog("[MqttClient] config: " .$config);
     $res=$mqtt->publish(
         $topic,
         $config,
         1
     );
-    echo 'publish end'.PHP_EOL;
+    rlog("[MqttClient] publish endK" );
     $mqtt->loop(true,true);
     $mqtt->disconnect();
     return $res;
 }
+function rlog(...$args)
+{
+    if (empty($args[0])) {
+        return;
+    }
+    static $LOG_CONSOLE = false; //是否输出到控制台
+    static $LOG_NAME = "rlsta_door_publish.log"; //值为空时 不写入文件
+    static $LOG_SIZE = 64 * 1024 * 1024; //文件最大尺寸
+
+    static $LOG_CACHE = false; //是否缓存日志内容 用于批量写入文件
+    static $CACHE_DURATION = 10; //缓存最大时间 秒
+    static $CACHE_SIZE = 1024; //缓存大小
+    static $cacheStartTime = 0;
+    static $cacheBuf = '';
+
+    static $LOG_TIMES = 10; //调用这个函数最大次数 超过次数后判断下文件大小
+    static $logCount = 0;
+
+
+    $buf = '';
+    if (count($args) == 1 && $args[0] == "\n") { //只有换行时 不写入时间戳了
+        $buf = "\n";
+    } else {
+        $pid = ''; //进程id
+        if (function_exists('posix_getpid')) {
+            $pid = ' ' . posix_getpid() . ' ';
+        }
+        $fileLine = ''; //文件名:行号
+        {
+            $debug = debug_backtrace();
+            $fileLine = ($pid == '' ? ' ' : '') . basename($debug[0]['file']) . ':' . $debug[0]['line'] . ' ';
+        }
+        $buf = date("y-m-d H:i:s") . "{$pid}{$fileLine}" . implode(' ', $args) . "\n";
+    }
+
+    $logCount++;
+    if (!empty($LOG_NAME)) {
+        if ($LOG_CACHE) {
+            $cacheBuf .= $buf;
+            //超过缓存尺寸 或者 超过缓存时长 写缓存到文件
+            if (strlen($cacheBuf) > $CACHE_SIZE || time() - $cacheStartTime > $CACHE_DURATION) {
+                $cacheStartTime = time();
+                goto write;
+            } else {
+                goto skipWrite;
+            }
+        } else {
+            $cacheBuf = $buf;
+        }
+        write: {
+            //超过尺寸后 删除旧文件 把新文件重命名为旧文件  多进程同时操作 不加锁问题不大
+            if ($logCount > $LOG_TIMES && filesize($LOG_NAME) > $LOG_SIZE) {
+                $oldLogName = $LOG_NAME . '.old';
+                if (file_exists($oldLogName)) {
+                    if (!unlink($oldLogName)) {
+                        echo "unlink err\n";
+                    }
+                }
+                if (!rename($LOG_NAME, $oldLogName)) {
+                    echo "rename err\n";
+                }
+                $logCount = 0;
+            }
+            if (!file_put_contents($LOG_NAME, $cacheBuf, FILE_APPEND)) {
+                echo "file_put_contents err\n";
+            }
+            $cacheBuf = '';
+        }
+        skipWrite: {
+        }
+    }
+    if ($LOG_CONSOLE) {
+        echo $buf;
+    }
+}
 function updateSendResult($msgid){
     // $conn = new mysqli('rm-bp1h3uqkzy66ckt8yro.mysql.rds.aliyuncs.com', 'dev', '711e7D69f9d0c3f1', 'smart_livestock');
     $conn = new mysqli('127.0.0.1', 'root', 'root', 'smart_livestock');
@@ -118,7 +193,7 @@ while (1) {
         continue;
     }
     $data=json_decode($jsonData,true);
-    var_dump($data);
+    rlog("[Redis]  rpop: " . json_encode($data));
     $topic="rlsta/door/bklist/set/".$data['device_id'];
     // {
     //     "msgid": "id123",
@@ -129,17 +204,14 @@ while (1) {
     //     "time": 1724145895
     // }
     $config=$data['config'];
-    var_dump($config);
+    rlog("[Config] : " . json_encode($config));
     
     foreach($config['bkList']['list'] as &$val){
         $val = str_pad($val, 8, "0", STR_PAD_LEFT);
     }
     $config_json=json_encode($config);
     
-    // if(isset($data['msgid'])){
-    //     updateSendResult($data['msgid']);
-    // }
-    var_dump($config_json);
+    rlog("[json_config] : " . $config_json);
     sendConfig($topic,$config_json);
  
 }