]> git.sven.stormbind.net Git - sven/scripts.git/blob - home/portalmonitor.py
Catch requests exceptions
[sven/scripts.git] / home / portalmonitor.py
1 #!/usr/bin/env python3
2
3 import requests
4 import time
5 import sys
6 import configparser
7 from suntime import Sun
8 from optparse import OptionParser
9
10 parser = OptionParser(usage="usage: %prog [options]")
11 parser.add_option("-s",
12                   "--status",
13                   action="store_true",
14                   dest="printStatus",
15                   help="Print Status Information",
16                   default=False)
17 parser.add_option("-f",
18                   "--force",
19                   action="store_true",
20                   dest="force",
21                   help="Force retrieval of Power value",
22                   default=False)
23 (options, args) = parser.parse_args()
24
25
26 def isDaylight(lat, lon, toleranceSeconds):
27     daylight = False
28     sun = Sun(lat, lon)
29     sunriseTimestamp = int(sun.get_local_sunrise_time().timestamp())
30     sunsetTimestamp = int(sun.get_local_sunset_time().timestamp())
31     nowTimestamp = int(time.time())
32
33     if ((sunriseTimestamp + toleranceSeconds) < nowTimestamp) and (
34         (sunsetTimestamp - toleranceSeconds) > nowTimestamp):
35         daylight = True
36
37     return daylight
38
39
40 def getCurrentPower(userName, password, stationId):
41     with requests.Session() as s:
42         try:
43             r = s.post('https://www.envertecportal.com/apiaccount/login',
44                        data={
45                            'userName': userName,
46                            'pwd': password
47                        },
48                        timeout=(10, 30))
49         except requests.exceptions.RequestException as e:
50             raise SystemExit(e)
51
52         try:
53             r = s.post(
54                 'https://www.envertecportal.com/ApiStations/getStationInfo',
55                 data={
56                     'stationId': stationId
57                 },
58                 timeout=(10, 60)).json()
59             power = r['Data']['Power']
60         except requests.exceptions.RequestException as e:
61             raise SystemExit(e)
62
63         try:
64             r = s.post('https://www.envertecportal.com/apiAccount/Logout',
65                        timeout=(10, 30))
66         except requests.exceptions.RequestException as e:
67             raise SystemExit(e)
68
69     return float(power)
70
71
72 # read configuration file
73 conf = configparser.ConfigParser()
74 conf.read('portalmonitor.ini')
75
76 # retrieve current power value as reported by envertecportal
77 if isDaylight(conf['config'].getfloat('lat'), conf['config'].getfloat('lon'),
78               conf['config'].getint('toleranceSeconds')) or options.force:
79     currentPower = getCurrentPower(conf['config']['userName'],
80                                    conf['config']['password'],
81                                    conf['config']['stationId'])
82
83     if options.printStatus:
84         print('Current Power: ' + str(currentPower))
85
86     if currentPower == 0:
87         print('Error: Power dropped to 0 but we should have daylight!')
88         sys.exit(1)