forked from jakobbp/WeatherIcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweathericon.py
96 lines (72 loc) · 2.55 KB
/
weathericon.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/env python
import datetime
import json
from urllib import request
from bottle import route, run, debug, static_file
SETTINGS_FILE_PATH = 'wi_settings.json'
THRESHOLDS_FILE_PATH = 'radiation_threshold_images.json'
ERROR_IMAGE_FILE_PATH = 'img/error.png'
KEY_HOST = 'host'
KEY_PORT = 'port'
KEY_DATA_SOURCE = 'dataSource'
KEY_AVERAGE_SAMPLES = 'averageSamples'
DATA_VALUE_SEPARATOR = ','
DATA_INDEX_MEASURED_RADIATION = 18
DATA_INDEX_MAX_RADIATION = 22
@route('/current')
def current_reading():
single_value = calculate_average_over_n(1)
print("Current ratio value: {}".format(single_value))
return static_file(filename=get_image_for_ratio(single_value), root='.')
@route('/average')
def average_reading():
average_value = calculate_average_over_n(int(get_settings()[KEY_AVERAGE_SAMPLES]))
print("Average ratio value: {}".format(average_value))
return static_file(filename=get_image_for_ratio(average_value), root='.')
def calculate_average_over_n(n):
data_lines = get_data()
if len(data_lines) < n:
n = len(data_lines)
if n > 0:
try:
ratio_sum = 0.0
for line in data_lines[-n:]:
ratio_sum += calculate_ratio_from_line(line)
return ratio_sum/n
except IOError:
return -1.0
else:
return -1.0
def calculate_ratio_from_line(line):
values = line.split(DATA_VALUE_SEPARATOR)
measurement = float(values[DATA_INDEX_MEASURED_RADIATION])
maximum = float(values[DATA_INDEX_MAX_RADIATION])
if maximum == 0:
return 0.0
return measurement/maximum
def get_image_for_ratio(ratio):
radiation_threshold_images = get_radiation_threshold_images()
img_path = radiation_threshold_images[-1][1]
for rt in radiation_threshold_images:
if ratio <= rt[0]:
img_path = rt[1]
return img_path
def get_data():
with request.urlopen(get_data_source()) as data:
return [str(data_line) for data_line in data.readlines()]
def get_data_source():
ds_template = get_settings()[KEY_DATA_SOURCE]
current_date = datetime.datetime.now()
return current_date.strftime(ds_template)
def get_settings():
with open(SETTINGS_FILE_PATH) as settings_file:
return json.load(settings_file)
def get_radiation_threshold_images():
with open(THRESHOLDS_FILE_PATH) as thresholds_file:
threshold_ratio_images = json.load(thresholds_file)
radiation_threshold_images = [(float(tr), threshold_ratio_images[tr]) for tr in threshold_ratio_images]
radiation_threshold_images.append((-1.0, ERROR_IMAGE_FILE_PATH))
radiation_threshold_images.sort(reverse=True)
return radiation_threshold_images
debug(True)
run(host=get_settings()[KEY_HOST], port=get_settings()[KEY_PORT])