X-Git-Url: http://git.sven.stormbind.net/?a=blobdiff_plain;f=home%2Fportalmonitor.py;h=c8b732b46afb64181df4c0cc7cefed2e16747209;hb=751a1490b1b8f20b913b58519ff8baacb5abba3c;hp=13a7b98ba55207ad7861809264eea7857cad400f;hpb=f3cde97f6e775414519f87835c3a2cdddfcf2293;p=sven%2Fscripts.git diff --git a/home/portalmonitor.py b/home/portalmonitor.py index 13a7b98..c8b732b 100755 --- a/home/portalmonitor.py +++ b/home/portalmonitor.py @@ -1,26 +1,26 @@ #!/usr/bin/env python3 +import argparse 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", +parser = argparse.ArgumentParser() +parser.add_argument("-s", "--status", action="store_true", dest="printStatus", help="Print Status Information", default=False) -parser.add_option("-f", +parser.add_argument("-f", "--force", action="store_true", dest="force", help="Force retrieval of Power value", default=False) -(options, args) = parser.parse_args() +args = parser.parse_args() def isDaylight(lat, lon, toleranceSeconds): @@ -39,40 +39,73 @@ 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=(20, 40)) + + r = s.post( + 'https://www.envertecportal.com/ApiStations/getStationInfo', + data={ + 'stationId': stationId + }, + timeout=(20, 60)).json() + power = r['Data']['Power'] + + r = s.post('https://www.envertecportal.com/apiAccount/Logout', + timeout=(20, 40)) + + # connect timeouts occur so frequently since the portal relaunch, + # ignore them for the time beeing completely + except requests.exceptions.ConnectTimeout as eTimeout: + sys.exit(1) + except requests.exceptions.RequestException as e: + print(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') # 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: + conf['config'].getint('toleranceSeconds')) or args.force: currentPower = getCurrentPower(conf['config']['userName'], conf['config']['password'], conf['config']['stationId']) - if options.printStatus: - print('Current Power: ' + str(currentPower)) + if args.printStatus: + 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")