]> git.sven.stormbind.net Git - sven/scripts.git/commitdiff
Add envertec portal monitor script with ini config file
authorSven Hoexter <sven@stormbind.net>
Mon, 23 Aug 2021 19:10:09 +0000 (21:10 +0200)
committerSven Hoexter <sven@stormbind.net>
Mon, 23 Aug 2021 19:10:09 +0000 (21:10 +0200)
home/portalmonitor.ini [new file with mode: 0644]
home/portalmonitor.py [new file with mode: 0755]

diff --git a/home/portalmonitor.ini b/home/portalmonitor.ini
new file mode 100644 (file)
index 0000000..93b78ff
--- /dev/null
@@ -0,0 +1,7 @@
+[config]
+lat = 51.1234
+lon = 3.1234
+toleranceSeconds = 3600
+stationId = C0FFE123456789
+userName = yourname
+password = yourpassword
\ No newline at end of file
diff --git a/home/portalmonitor.py b/home/portalmonitor.py
new file mode 100755 (executable)
index 0000000..13a7b98
--- /dev/null
@@ -0,0 +1,78 @@
+#!/usr/bin/env python3
+
+import requests
+import time
+import sys
+import configparser
+from suntime import Sun
+from optparse import OptionParser
+
+parser = OptionParser(usage="usage: %prog [options]")
+parser.add_option("-s",
+                  "--status",
+                  action="store_true",
+                  dest="printStatus",
+                  help="Print Status Information",
+                  default=False)
+parser.add_option("-f",
+                  "--force",
+                  action="store_true",
+                  dest="force",
+                  help="Force retrieval of Power value",
+                  default=False)
+(options, args) = parser.parse_args()
+
+
+def isDaylight(lat, lon, toleranceSeconds):
+    daylight = False
+    sun = Sun(lat, lon)
+    sunriseTimestamp = int(sun.get_local_sunrise_time().timestamp())
+    sunsetTimestamp = int(sun.get_local_sunset_time().timestamp())
+    nowTimestamp = int(time.time())
+
+    if ((sunriseTimestamp + toleranceSeconds) < nowTimestamp) and (
+        (sunsetTimestamp - toleranceSeconds) > nowTimestamp):
+        daylight = True
+
+    return daylight
+
+
+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))
+
+    return float(power)
+
+
+# read configuration file
+conf = configparser.ConfigParser()
+conf.read('portalmonitor.ini')
+
+# retrieve current power value as reported by envertecportal
+if isDaylight(conf['config'].getfloat('lat'), conf['config'].getfloat('lon'),
+              conf['config'].getint('toleranceSeconds')) or options.force:
+    currentPower = getCurrentPower(conf['config']['userName'],
+                                   conf['config']['password'],
+                                   conf['config']['stationId'])
+
+    if options.printStatus:
+        print('Current Power: ' + str(currentPower))
+
+    if currentPower == 0:
+        print('Error: Power dropped to 0 but we should have daylight!')
+        sys.exit(1)