-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
129 lines (118 loc) · 4.38 KB
/
weather.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from darksky.api import DarkSky, DarkSkyAsync
from darksky.types import languages, units, weather
from yaml_config import config
from sensors import gps_data
import global_vars
import logging
import colorlog
logger = colorlog.getLogger(__name__)
logger.addHandler(global_vars.file_handler)
logger.addHandler(global_vars.handler)
logger.setLevel(global_vars.log_level)
weather_data = {}
weather_data['site'] = {}
weather_data['current'] = {}
weather_data['hour'] = {}
weather_data['day'] = {}
weather_data['alert'] = {}
# There's a lot of data we aren't interested in
interesting_items = [
'time',
'summary',
'icon',
'precip_intensity',
'precip_intensity_max',
'precip_probability',
'precip_type',
'precipAccumulation',
'temperature',
'temperature_high',
'temperature_low',
'apparent_temperature',
'apparent_temperature_high',
'apparent_temperature_low',
'dew_point',
'humidity',
'pressure',
'wind_speed',
'wind_gust',
'wind_bearing',
'cloud_cover',
'uv_index',
'visibility'
'ozone',
'nearest_storm_distance',
'nearest_storm_bearing',
]
# Items we're interested in but they need converting to a real (0-100) percentage
percentage_items = [
'precip_probability',
'cloud_cover',
'humidity'
]
alert_items = [
'title',
'severity',
'description',
]
def get_weather_forecast():
try:
if config['sensors']['gps_enable'] and ( gps_data['mode'] >= 2 ):
weather_data['site']['latitude'] = gps_data['latitude']
weather_data['site']['longitude'] = gps_data['longitude']
weather_data['site']['timezone'] = gps_data['timezone']
weather_data['site']['elevation'] = gps_data['altitude']
else:
weather_data['site']['latitude'] = config['field']['latitude']
weather_data['site']['longitude'] = config['field']['longitude']
weather_data['site']['timezone'] = config['field']['timezone']
weather_data['site']['elevation'] = config['field']['elevation']
darksky = DarkSky(config['weather']['api_key'])
forecast = darksky.get_forecast(
weather_data['site']['latitude'],
weather_data['site']['longitude'],
lang=config['weather']['language'],
units=config['weather']['units'],
exclude=[weather.MINUTELY, weather.FLAGS],
timezone=weather_data['site']['timezone'],
)
# We're not interested in ALL the data
all_weather = {
# Only current weather
'current': forecast.currently.__dict__,
# Weather in the next hour
'hour': forecast.hourly.data[1].__dict__,
# And the weather for the day
'day': forecast.daily.data[0].__dict__,
}
# And the weather for the day
for time_period in all_weather:
for forecast_item in all_weather[time_period]:
if forecast_item in interesting_items:
if forecast_item == 'time':
time_iso = all_weather[time_period][forecast_item].replace(tzinfo=None).isoformat()
weather_data[time_period][forecast_item] = time_iso
elif forecast_item in percentage_items:
weather_data[time_period][forecast_item] = int(all_weather[time_period][forecast_item] * 100)
else:
weather_data[time_period][forecast_item] = all_weather[time_period][forecast_item]
if forecast.alerts:
for item in alert_items:
weather_data['alert'][item] = forecast.alerts[0].__dict__[item]
if 'red' in weather_data['alert']['title'].lower():
weather_data['alert']['colour'] = 'red'
elif 'amber' in weather_data['alert']['title'].lower():
weather_data['alert']['colour'] = 'amber'
elif 'orange' in weather_data['alert']['title'].lower():
weather_data['alert']['colour'] = 'amber'
elif 'yellow' in weather_data['alert']['title'].lower():
weather_data['alert']['colour'] = 'yellow'
else:
weather_data['alert']['colour'] = 'info'
else:
weather_data['alert']['title'] = None
weather_data['alert']['colour'] = None
weather_data['alert']['severity'] = None
weather_data['alert']['description'] = None
except Exception as e:
logger.error('Unhandled exception in retrieving weather forecast: ' + str(e))