-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyVisuApp.py
339 lines (259 loc) · 10.7 KB
/
MyVisuApp.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf-8 -*-
'''
Visualizes temperature and humidity in rooms and provides a weather forecast.
Classes:
MyScreens: Used to switch between the screens which provide the different contents
Scrn1: Start screen of the app. Provides navigation to the other screens
Scrn2: Shows temperatrue and humidity. Uses Two_Scales_Widget
Two_Scales_Widget: Widget providing functions to get temperature and humidity of a room and to visualize it
Scrn3: Shows the current weather at a selectable location as well as a five day forecast. Uses Weather_Widget
Weather_Widget: Widget providing functions to get and visualize the weather and forecast for a location
Scrn4: Shows the temperature and humidity graph of the last 24 hours. Uses TwoPlotWidgets
TwoPlotsWidget: Matplotlib Backend visualizing two graphs with shared x-Axis
Scrn5: Shows a corona widget displaying current and cumulative infections
Scrn6: Shows a widget representing the most common pollen for a chosen region including a forecast
See details and more explanations at: http://kraisnet.de/index.php/gebaeudedaten-erfassen-und-mit-kivy-visualisieren-2/18-gebaeudedaten-erfassen-und-mit-kivy-visualisieren
'''
import json
import os
from datetime import datetime
from time import mktime, strptime
from kivy.app import App
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.settings import SettingsWithSidebar
from TwoScalesWidgetApp import TwoScalesWidget as TwoScalesWidget
from WeatherWidgetApp import WeatherWidget as WeatherWidget
from TwoPlotsSharedXWidgetApp import TwoPlotsSharedXWidget as TwoPlotsSharedXWidget
from CoronaWidgetApp import CoronaWidget as CoronaWidget
from PollenWidgetApp import PollenWidget as PollenWidget
from kivy.resources import resource_add_path
class MyScreens(ScreenManager):
'''
The ScreenManager MyScreens takes care of changing between the available screens
The functions goto_screen_1 .... can be called by all elements on the screens
'''
def __init__(self, **kwargs):
'''__init__() can perform actions on instantiation. None so far'''
super(MyScreens, self).__init__(**kwargs)
def goto_scrn1(self):
'''Switches to screen 1'''
self.current = 'scrn1'
def goto_scrn2(self):
'''switches to screen 2'''
self.current = 'scrn2'
def goto_scrn3(self):
'''switches to screen 3'''
self.current = 'scrn3'
def goto_scrn4(self):
'''switches to screen 4'''
self.current = 'scrn4'
def goto_scrn5(self):
'''switches to screen 5'''
self.current = 'scrn5'
def goto_scrn6(self):
'''switches to screen 6'''
self.current = 'scrn6'
class Scrn1(Screen):
'''
Shows the start screen
Attributes:
None
'''
pass
class Scrn2(Screen):
'''
Shows the screen containing the temperature and humidity widget
Attributes:
None
'''
timestamp = ListProperty([])
temperature = ListProperty([])
humidity = ListProperty([])
def __init__(self, **kwargs):
'''
Start updating the screen regularly.
A clock will call the update function in a selectable interval.
Put all functions you want to update into the update function
Args:
**kwargs (): not used. For further development.
Returns:
Nothing.
'''
super(Scrn2, self).__init__(**kwargs)
Clock.schedule_interval(self.update_scales, 60)
Clock.schedule_once(self.update_scales)
Clock.schedule_interval(self.update_graph, 60)
Clock.schedule_once(self.update_graph)
def update_scales(self, dt):
'''
updates the scales
Args:
dt (int): interval in seconds in which the funtion will be called
Returns:
nothing
'''
low1 = float(App.get_running_app().config.get('Two Scales Widget', 'temp_lower_limit'))
high1 = float(App.get_running_app().config.get('Two Scales Widget', 'temp_upper_limit'))
low2 = float(App.get_running_app().config.get('Two Scales Widget', 'humidity_lower_limit'))
high2 = float(App.get_running_app().config.get('Two Scales Widget', 'humidity_upper_limit'))
filename = App.get_running_app().config.get('Two Scales Widget', 'data_source_scales')
self.ids.widget1.show_data(filename=filename, low1=low1, high1=high1, low2=low2, high2=high2)
def update_graph(self, dt):
'''
updates the plot
Args:
dt (int): interval in seconds in which the funtion will be called
Returns:
nothing
'''
# Read the data to show from a file and store it
filename = App.get_running_app().config.get('Two Scales Widget', 'data_source_graph')
try:
with open(filename, 'r') as read_file:
data = json.load(read_file)
print(data)
except FileNotFoundError:
print('File not found for temperature and humidity graph')
return
self.timestamp.clear()
self.temperature.clear()
self.humidity.clear()
for item in data:
self.timestamp.append(datetime.fromtimestamp(mktime(strptime(item['time_code'], '%Y-%m-%d %H:%M:%S'))))
self.temperature.append(float(item['temperature']))
self.humidity.append(float(item['humidity']))
self.ids.widget2.update_plot()
class Scrn3(Screen):
def __init__(self, **kwargs):
'''
Start updating the screen regularly.
A clock will call the update function in a selectable interval.
Put all functions you want to update into the update function
Args:
**kwargs (): not used. For further development.
Returns:
Nothing.
'''
super(Scrn3, self).__init__(**kwargs)
Clock.schedule_interval(self.update, 1800)
Clock.schedule_once(self.update)
def update(self, dt):
'''
calls funtions to update the screen.
Args:
dt (int): interval in seconds in which the funtion will be called
Returns:
(float): Scaled value.
'''
city = App.get_running_app().config.get('Weather Widget', 'city')
self.ids.widget1.download_current_weather(city=city)
self.ids.widget1.download_forecast(city=city)
class Scrn5(Screen):
def __init__(self, **kwargs):
super(Scrn5, self).__init__(**kwargs)
'''
Shows the corona widget.
A clock will call the update function in a selectable interval.
Put all functions you want to update into the update function.
During init is the update() function called. This will download the current dataset from
the ECDC. The data ist updated once a day. So the interval should be large enough.
Args:
**kwargs (): not used. For further development.
Returns:
Nothing.
'''
Clock.schedule_interval(self.update, 86400)
Clock.schedule_once(self.update)
def update(self, dt):
'''
calls funtions to update the screen.
Args:
dt (int): interval in seconds in which the funtion will be called
Returns:
(float): Scaled value.
'''
self.ids['wdgt1'].download_data_infection()
self.ids['wdgt1'].download_data_vaccination()
class Scrn6(Screen):
def __init__(self, **kwargs):
super(Scrn6, self).__init__(**kwargs)
'''
Shows the pollen widget.
A clock will call the update function in a selectable interval.
Put all functions you want to update into the update function.
During init is the update() function called. This will download the current dataset from
the DWD. The data ist updated once a day. So the interval should be large enough.
Args:
**kwargs (): not used. For further development.
Returns:
Nothing.
'''
Clock.schedule_interval(self.update, 86400)
Clock.schedule_once(self.update)
def update(self, dt):
'''
calls funtions to update the screen.
Args:
dt (int): interval in seconds in which the funtion will be called
Returns:
(float): Scaled value.
'''
self.ids['wdgt1'].download_dataset(url='https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json')
class MyVisuApp(App):
def build(self):
'''
overwrites the build() function.
The appearance of the settings is set here.
Choose from the available layouts: https://kivy.org/doc/stable/api-kivy.uix.settings.html#different-panel-layouts
The preset values for the settings are loaded by the config.read() function
Args:
None
Returns:
class MyScreens().
'''
self.settings_cls = SettingsWithSidebar
fileDir = os.path.dirname(os.path.abspath(__file__))
absFilename = os.path.join(fileDir, 'mysettings.ini')
self.config.read(absFilename)
return MyScreens()
def build_settings(self, settings):
'''
overwrites the build_settings() function.
Add all necessary panels here by loading from the corresponding file.
Args:
settings
Returns:
Nothing.
'''
fileDir = os.path.dirname(os.path.abspath(__file__))
absFilename1 = os.path.join(fileDir, 'settings_weather_widget.json')
absFilename2 = os.path.join(fileDir, 'settings_two_scales_widget.json')
settings.add_json_panel('Weather Widget', self.config, absFilename1)
settings.add_json_panel('Two Scales Widget', self.config, absFilename2)
def on_config_change(self, config, section, key, value):
'''
overwrites the on_config_change() function.
define actions that shall happen when specific entries in the configuration change here.
Args:
config (kivy.config.ConfigParser):
current configuration.
section (str):
name of the section where the key belongs to.
key (str):
key as specified in the json panels.
value ():
value of the key. return value depending on the type of variable.
Returns:
class MyScreens().
'''
app = self.get_running_app()
if key == 'city':
app.root.ids['scrn3'].update(1)
if section == 'Two Scales Widget':
app.root.ids['scrn2'].update_plots(1)
app.root.ids['scrn2'].update_graph(1)
if __name__ == '__main__':
resource_add_path(r'C:\Users\49172\PycharmProjects')
MyVisuApp().run()