From f3cde97f6e775414519f87835c3a2cdddfcf2293 Mon Sep 17 00:00:00 2001 From: Sven Hoexter Date: Mon, 23 Aug 2021 21:10:09 +0200 Subject: [PATCH] Add envertec portal monitor script with ini config file --- home/portalmonitor.ini | 7 ++++ home/portalmonitor.py | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 home/portalmonitor.ini create mode 100755 home/portalmonitor.py diff --git a/home/portalmonitor.ini b/home/portalmonitor.ini new file mode 100644 index 0000000..93b78ff --- /dev/null +++ b/home/portalmonitor.ini @@ -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 index 0000000..13a7b98 --- /dev/null +++ b/home/portalmonitor.py @@ -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) -- 2.39.5