Procházet zdrojové kódy

2023-08-28:
- option to log successfully received commands (from configured/known senders) while keeping debug logging disabled

FloKra před 1 rokem
rodič
revize
bc05c0a34e
3 změnil soubory, kde provedl 68 přidání a 6 odebrání
  1. 3 0
      CHANGELOG.txt
  2. 59 5
      cul2mqtt.py
  3. 6 1
      cul2mqtt_example.ini

+ 3 - 0
CHANGELOG.txt

@@ -1,3 +1,6 @@
+2023-08-28:
+ - option to log successfully received commands (from configured/known senders) while keeping debug logging disabled
+ 
 2023-06-11:
  - add support for DIY DS18B20 sensor sending via 433MHz cheap transmitter
  

+ 59 - 5
cul2mqtt.py

@@ -42,6 +42,7 @@ lastDataReceiveTime = time.time()
 deviceConfigFile = config['main'].get('devices_config_yml')
 
 log_enable = config['main'].getboolean('log_enable')
+logSuccessfulCommands = config['main'].getboolean('logSuccessfulCommands')
 log_path = config['main'].get('log_path')
 if not os.path.exists(log_path):
     os.makedirs(log_path)
@@ -123,7 +124,6 @@ yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicat
 log_last_date = None
 logfilehandle = False
 
-
 def log_start():
     global logfilehandle, log_last_date
     if log_enable:
@@ -132,7 +132,7 @@ def log_start():
             
         try:
             _log_current_date = strftime("%Y%m%d")
-            _logfilename = _log_current_date + ".log"
+            _logfilename = _log_current_date + "_debug.log"
             logfilehandle = open(log_path + '/' + _logfilename, 'a')
             log_last_date = _log_current_date
         except:
@@ -147,12 +147,12 @@ def log_rotate():
         if log_last_date != _log_current_date:
             try:
                 logfilehandle.close()
-                _logfilename = _log_current_date + ".log"
+                _logfilename = _log_current_date + "_debug.log"
                 logfilehandle = open(log_path + '/' + _logfilename, 'a')
                 log_last_date = _log_current_date
             except:
                 pass
-            
+
             
 def log_write(_msg):
     global logfilehandle
@@ -165,8 +165,59 @@ def log_write(_msg):
         except:
             # guat dann hoit ned...
             pass
+            
+            
+# Log successfully received (known) codes
+
+logSC_last_date = None
+logSCfilehandle = False
+
+def logSC_start():
+    global logSCfilehandle, logSC_last_date
+    if logSuccessfulCommands:
+        if not os.path.exists(log_path):
+            os.makedirs(log_path)
+            
+        try:
+            _log_current_date = strftime("%Y%m%d")
+            _logfilename = _log_current_date + "_received.log"
+            logSCfilehandle = open(log_path + '/' + _logfilename, 'a')
+            logSC_last_date = _log_current_date
+        except:
+            pass
+
+
+def logSC_rotate():
+    global logSCfilehandle, logSC_last_date
+    if logSuccessfulCommands:
+        _log_current_date = strftime("%Y%m%d")
+        
+        if logSC_last_date != _log_current_date:
+            try:
+                logSCfilehandle.close()
+                _logfilename = _log_current_date + "_received.log"
+                logSCfilehandle = open(log_path + '/' + _logfilename, 'a')
+                logSC_last_date = _log_current_date
+            except:
+                pass
+            
+            
+def logSC_write(_msg):
+    global logSCfilehandle
+    if not quiet: print(_msg)
+    log_rotate()
+    if logSuccessfulCommands:
+        try:
+            logSCfilehandle.write("[" + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + "] " + _msg + "\n")
+            logSCfilehandle.flush()
+        except:
+            # guat dann hoit ned...
+            pass
+            
+###
 
 log_start()
+logSC_start()
 
 log_write("CUL2MQTT v" + str(version))
 log_write("=====================================")
@@ -374,7 +425,10 @@ def publish_device_statusupdate(device, cmd, value):
             else: 
                 if verbose: log_write("    MQTT publish: '" + cmd + "' -> '" + statTopic + "'")
                 mqttc.publish(statTopic, cmd, qos=0, retain=False)
-        
+            
+            if logSuccessfulCommands: 
+                logSC_write("received from device: " + device + ", cmd: " + cmd + ", value: " + str(value) + ", topic: " + statTopic)
+            
         if 'add_statTopics_on' in devdata[device].keys():
             if verbose: log_write("    MQTT publish add_statTopics_on:")
             for res in devdata[device].get('add_statTopics_on'):

+ 6 - 1
cul2mqtt_example.ini

@@ -1,7 +1,12 @@
 [main]
 devices_config_yml = cul2mqtt_devices.yml
 log_path = /home/pi/logs/cul2mqtt
-log_enable = true
+
+# debug logging:
+log_enable = false
+# log successful commands:
+logSuccessfulCommands = true
+
 filterSelfSentIncomingTimeout = 100
 
 # also used as min repeat time, in ms