7 from suntime import Sun
8 from optparse import OptionParser
10 parser = OptionParser(usage="usage: %prog [options]")
11 parser.add_option("-s",
15 help="Print Status Information",
17 parser.add_option("-f",
21 help="Force retrieval of Power value",
23 (options, args) = parser.parse_args()
26 def isDaylight(lat, lon, toleranceSeconds):
29 sunriseTimestamp = int(sun.get_local_sunrise_time().timestamp())
30 sunsetTimestamp = int(sun.get_local_sunset_time().timestamp())
31 nowTimestamp = int(time.time())
33 if ((sunriseTimestamp + toleranceSeconds) < nowTimestamp) and (
34 (sunsetTimestamp - toleranceSeconds) > nowTimestamp):
40 def getCurrentPower(userName, password, stationId):
41 with requests.Session() as s:
43 r = s.post('https://www.envertecportal.com/apiaccount/login',
51 'https://www.envertecportal.com/ApiStations/getStationInfo',
53 'stationId': stationId
55 timeout=(10, 60)).json()
56 power = r['Data']['Power']
58 r = s.post('https://www.envertecportal.com/apiAccount/Logout',
60 except requests.exceptions.RequestException as e:
66 # read configuration file
67 conf = configparser.ConfigParser()
68 conf.read('portalmonitor.ini')
70 # retrieve current power value as reported by envertecportal
71 if isDaylight(conf['config'].getfloat('lat'), conf['config'].getfloat('lon'),
72 conf['config'].getint('toleranceSeconds')) or options.force:
73 currentPower = getCurrentPower(conf['config']['userName'],
74 conf['config']['password'],
75 conf['config']['stationId'])
77 if options.printStatus:
78 print('Current Power: ' + str(currentPower))
81 print('Error: Power dropped to 0 but we should have daylight!')