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:
42 r = s.post('https://www.envertecportal.com/apiaccount/login',
49 r = s.post('https://www.envertecportal.com/ApiStations/getStationInfo',
51 'stationId': stationId
53 timeout=(10, 60)).json()
54 power = r['Data']['Power']
56 r = s.post('https://www.envertecportal.com/apiAccount/Logout',
62 # read configuration file
63 conf = configparser.ConfigParser()
64 conf.read('portalmonitor.ini')
66 # retrieve current power value as reported by envertecportal
67 if isDaylight(conf['config'].getfloat('lat'), conf['config'].getfloat('lon'),
68 conf['config'].getint('toleranceSeconds')) or options.force:
69 currentPower = getCurrentPower(conf['config']['userName'],
70 conf['config']['password'],
71 conf['config']['stationId'])
73 if options.printStatus:
74 print('Current Power: ' + str(currentPower))
77 print('Error: Power dropped to 0 but we should have daylight!')