Browse Source

- add a status file that is touched every minute as long as something has been received

FloKra 11 months ago
parent
commit
0d9f24e1db
3 changed files with 38 additions and 7 deletions
  1. 3 0
      CHANGELOG.txt
  2. 30 7
      cul2mqtt.py
  3. 5 0
      cul2mqtt_example.ini

+ 3 - 0
CHANGELOG.txt

@@ -1,3 +1,6 @@
+2023-05-30:
+ - add a status file that is touched every minute as long as something has been received
+
 2023-05-30:
  - add no-receive-timeout after which the program will be exited (and will be restarted by systemd)
  - minor improvements

+ 30 - 7
cul2mqtt.py

@@ -57,6 +57,12 @@ if TX_interface_prefer is None: # compatibility with old ini file structure
 
 repeat_received_commands = False # not yet implemented
 
+dataReceiveTimeout = config['main'].getint('dataReceiveTimeout')
+
+lastReceivedStatusFile = config['main'].get('lastReceivedStatusFile')
+lastReceivedStatusFileUpdateTime = 0
+
+
 if len(sys.argv) >= 2:
     if sys.argv[1] == "-q":
         verbose = False
@@ -580,11 +586,28 @@ def encodeInterTechnoRX(itname, cmd):
         if debug: log_write("    unknown or invalid IT code '" + rx_code + "'")
 
 def cul_received(payload, source_cul):
-    global lastReceivedTime, lastReceivedMaxAge, lastDataReceiveTime
+    global lastReceivedTime, lastReceivedMaxAge, lastDataReceiveTime, lastReceivedStatusFile, lastReceivedStatusFileUpdateTime
     
     lastDataReceiveTime = time.time()
     if debug:
-        print("lastDataReceiveTime=" + str(lastDataReceiveTime))
+        print("DEBUG: lastDataReceiveTime=" + str(lastDataReceiveTime))
+    
+    # touch lastReceivedStatusFile if configured
+    if lastReceivedStatusFile is not None:
+        if (time.time() - lastReceivedStatusFileUpdateTime) >= 60:
+            # try updating lastReceivedStatusFile if last time was >60s ago
+            lastReceivedStatusFileUpdateTime = time.time()
+            try:
+                touch(lastReceivedStatusFile)
+                if debug:
+                    print("DEBUG: lastReceivedStatusFile updated")
+            except:
+                if debug:
+                    print("ERROR: could not update lastReceivedStatusFile")
+    else:
+        if debug:
+            print("DEBUG: lastReceivedStatusFile not defined")
+            
     
     if payload[:2] == 'is': # msg is reply from CUL to raw send command
         pass
@@ -882,12 +905,12 @@ try:
         # check if receive timeout is exceeded 
         # - if so there is possibly something wrong
         # -> quit program (will be restarted by systemd)
-        if config['main'].get('dataReceiveTimeout') is not None:
-            if (time.time() - lastDataReceiveTime) >= float(config['main'].get('dataReceiveTimeout')):
-                print("WARNING: received no data from CUL for >" + str(config['main'].get('dataReceiveTimeout')) + "s -> something has gone wrong -> quitting program.")
+        if dataReceiveTimeout is not None:
+            if (time.time() - lastDataReceiveTime) >= dataReceiveTimeout:
+                print("WARNING: received no data from CUL for >" + str(dataReceiveTimeout) + "s -> something has gone wrong -> quitting program.")
                 quit()
-        #else:
-        #    print("dataReceiveTimeout not configured")
+        ##else:
+        ##    print("dataReceiveTimeout not configured")
             
         
         sleep(0.05)

+ 5 - 0
cul2mqtt_example.ini

@@ -14,6 +14,11 @@ lastSentMinInterval = 1500
 # -> don't set too low if there is not much activity on CUL compatible RF in the location!
 dataReceiveTimeout = 180
 
+# a status file that is touched every minute as long as something has been received
+# ONLY set this to a location in a RAM drive!!! (i.E. /tmp/cul2mqtt_lastReceived on RaspberryOS or /run/user/XXXX/cul2mqtt_lastReceived where XXXX is the user id)
+lastReceivedStatusFile = /run/user/1000/cul2mqtt_lastReceived
+
+
 [cul]
 serialPort = /dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0
 serialBaudrate = 38400