]> git.sven.stormbind.net Git - sven/scripts.git/blobdiff - home/portalmonitor.py
Add a state tracking to envertech portalmonitor
[sven/scripts.git] / home / portalmonitor.py
index 13a7b98ba55207ad7861809264eea7857cad400f..3acd3f1766e677469fff2021e60f853af0b1253a 100755 (executable)
@@ -39,26 +39,49 @@ def isDaylight(lat, lon, toleranceSeconds):
 
 def getCurrentPower(userName, password, stationId):
     with requests.Session() as s:
-        r = s.post('https://www.envertecportal.com/apiaccount/login',
-                   data={
-                       'userName': userName,
-                       'pwd': password
-                   },
-                   timeout=(10, 30))
-
-        r = s.post('https://www.envertecportal.com/ApiStations/getStationInfo',
-                   data={
-                       'stationId': stationId
-                   },
-                   timeout=(10, 60)).json()
-        power = r['Data']['Power']
-
-        r = s.post('https://www.envertecportal.com/apiAccount/Logout',
-                   timeout=(10, 30))
+        try:
+            r = s.post('https://www.envertecportal.com/apiaccount/login',
+                       data={
+                           'userName': userName,
+                           'pwd': password
+                       },
+                       timeout=(10, 30))
+
+            r = s.post(
+                'https://www.envertecportal.com/ApiStations/getStationInfo',
+                data={
+                    'stationId': stationId
+                },
+                timeout=(10, 60)).json()
+            power = r['Data']['Power']
+
+            r = s.post('https://www.envertecportal.com/apiAccount/Logout',
+                       timeout=(10, 30))
+        except requests.exceptions.RequestException as e:
+            raise SystemExit(e)
 
     return float(power)
 
 
+# use our stateFile to determine if we have a state change
+# used to decide if we print something - thus generate a mail - later on
+def stateCheck(newState, stateFile):
+    try:
+        with open(stateFile, "r") as f:
+            oldState = f.read()
+    except FileNotFoundError:
+        oldState = "NULL"
+
+    if newState == oldState:
+        change = False
+    else:
+        change = True
+        with open(stateFile, "w") as f:
+            f.write(newState)
+
+    return change
+
+
 # read configuration file
 conf = configparser.ConfigParser()
 conf.read('portalmonitor.ini')
@@ -71,8 +94,12 @@ if isDaylight(conf['config'].getfloat('lat'), conf['config'].getfloat('lon'),
                                    conf['config']['stationId'])
 
     if options.printStatus:
-        print('Current Power: ' + str(currentPower))
+        print(f"Current Power: {currentPower}")
 
     if currentPower == 0:
-        print('Error: Power dropped to 0 but we should have daylight!')
+        if stateCheck('FAILED', conf['config']['stateFile']):
+            print('Error: Power dropped to 0 but we should have daylight!')
         sys.exit(1)
+    else:
+        if stateCheck('OK', conf['config']['stateFile']):
+            print(f"Resolved: Inverter reports {currentPower}W")