Browse Source

## 2023-01-27
* add exit program if nothing has been received from JeeLink for 5 minutes (will be restarted by systemd)

FloKra 1 year ago
parent
commit
c46dc31293
2 changed files with 21 additions and 0 deletions
  1. 3 0
      CHANGELOG.md
  2. 18 0
      jeelinklog/jeelinklog.py

+ 3 - 0
CHANGELOG.md

@@ -1,5 +1,8 @@
 # JeeLinkLogMQTT - change log
 
+## 2023-01-27
+* add exit program if nothing has been received from JeeLink for 5 minutes (will be restarted by systemd)
+
 ## 2022-11-14
 
 * fix config mqtt/enable did not work (was always true)

+ 18 - 0
jeelinklog/jeelinklog.py

@@ -24,6 +24,10 @@ import math
 # Change working dir to the same dir as this script
 os.chdir(sys.path[0])
 
+
+# stop program if nothing is received from JeeLink for 5 min (will be restarted by systemd)
+serialReceived_maxAge = 300
+
 config = configparser.ConfigParser()
 config.read('jeelinklog.ini')
 
@@ -293,6 +297,9 @@ ser = serial.Serial(port=serialport,
 
 
 checkLastUpdateInterval_lastRun = time.time() # first check after timeout expired
+
+# set serialReceivedLastTime to current time so that we can detect if nothing is received for some time
+serialReceivedLastTime = int(time.time())
     
 try:
     while True:
@@ -311,6 +318,10 @@ try:
 
         if(serLine):
             if serLine.find('OK 9') != -1:
+                
+                # set serialReceivedLastTime to current time so that we can detect if nothing is received for some time
+                serialReceivedLastTime = int(time.time())
+                
                 if verbosemode:
                     print(serLine + " = LaCrosse sensor")
 
@@ -1124,6 +1135,13 @@ try:
         
         # handle outdated sensor values once a minute
         if (int(time.time()) - checkLastUpdateInterval_lastRun) > checkLastUpdateInterval:
+        
+            # exit program if nothing has been received from JeeLink for 5 min - will be restarted by systemd
+            now = int(time.time())
+            if (now - serialReceivedLastTime) > serialReceived_maxAge:
+                print("Nothing received from JeeLink for " + str(now - serialReceivedLastTime) + "s - there is something wrong. Exiting program so that it will be restarted by systemd.")
+                quit()
+                
             checkLastUpdateInterval_lastRun = int(time.time())
             #print("check lastUpdate")
             for key in sensors_yaml: