-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·96 lines (81 loc) · 3.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python3
import requests
import os
import sys
import json
import time
import logging
import datetime
from logging.handlers import RotatingFileHandler
logger = logging.getLogger(__name__)
def check_weather(data):
logger.debug("checking weather")
try:
page = requests.get(data['api_url'])
if page.status_code == 200:
res = json.loads(page.text)
sum_sunpower = float(0)
max_windstoten = float(0)
max_wind = float(0)
sum_temp = float(0)
sum_humidity = float(0)
logger.debug("find weather stations")
for ws in res['actual']['stationmeasurements']:
if ws['regio'] in data['regions']:
try:
sum_sunpower += float(ws['sunpower'])
wind = float(ws['windspeed'])
if (wind > max_wind):
max_wind = wind
windstoten = float(ws['windgusts'])
if windstoten > max_windstoten:
max_windstoten = windstoten
sum_temp += float(ws['temperature'])
sum_humidity += float(ws['humidity'])
except BaseException:
logger.error("Error parsing reponse from KNMI")
if data['pilight_enabled']:
avg_temp = round(float(sum_temp / float(len(data['regions']))),2)
avg_sunpower = round(float(sum_sunpower / float(len(data['regions']))),2)
#avg_humidity = round(float(sum_humidity / float(len(data['regions']))),2)
try:
os.system("pilight-send -p generic_label -i %s -l '%s MW2'"%(data['pilight_label'], avg_sunpower))
logger.debug("update sun power")
os.system("pilight-send -p generic_label -i %s -l '%s MS, %s MS'"%(data['pilight_wind_label'], max_wind, max_windstoten))
logger.debug("update wind")
os.system("pilight-send -p generic_label -i %s -l '%s Celsius'"%(data['pilight_temp_label'], avg_temp))
os.system("pilight-send -p generic_label -i %s -l '%s'"%(data['pilight_timestamp_label'], datetime.datetime.now().timestamp()))
except:
logger.error("Failed to update pilight")
pass
else:
logger.error("failed to get weather data")
logger.debug("weather data updated")
except:
logger.error("failed to get response from knmi")
check_weather.last_sunpower = 0
if __name__ == '__main__':
handler = RotatingFileHandler(
'/var/log/weather_check.log',
maxBytes=100000,
backupCount=0)
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)-22s - %(levelname)-8s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
handler_stdout = logging.StreamHandler(sys.stdout)
logger.addHandler(handler_stdout)
logger.setLevel(logging.INFO)
logger.info("Starting Olisto Weather checker")
try:
with open('config.json') as data_file:
data = json.load(data_file)
except FileNotFoundError:
logger.error("Failed to open configuration, create the configuration file: weather_check.json")
sys.exit()
except ValueError:
logger.error("Failed to parse configuration")
sys.exit()
while True:
check_weather(data)
time.sleep(data['interval'])