forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terrariumConfig.py
468 lines (355 loc) · 16.3 KB
/
terrariumConfig.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# -*- coding: utf-8 -*-
import terrariumLogging
logger = terrariumLogging.logging.getLogger(__name__)
import ConfigParser
from glob import glob
import datetime
from terrariumUtils import terrariumUtils
class terrariumConfig(object):
DEFAULT_CONFIG = 'defaults.cfg'
CUSTOM_CONFIG = 'settings.cfg'
'''Class for loading the configuration for terrariumPI software.
The configuration is based on two configuration files.
- default.cfg holds system defaults for first run
- settigs.cfg holds the user defined config files
So the default.cfg file is read first, and overwritten by the settings
from the settings.cfg file.
Changes will always be written to settings.cfg.'''
def __init__(self):
'''Load terrariumPI config object'''
logger.info('Setting up configuration')
self.__cache_available_languages = None
self.__config = ConfigParser.SafeConfigParser()
# Read defaults config file
self.__config.readfp(open(terrariumConfig.DEFAULT_CONFIG))
logger.info('Loaded default settings from %s' % (terrariumConfig.DEFAULT_CONFIG,))
# Read new version number
version = self.get_system()['version']
# Read custom config file
self.__config.read(terrariumConfig.CUSTOM_CONFIG)
logger.info('Loaded custom settings from %s' % (terrariumConfig.CUSTOM_CONFIG,))
# Upgrade config and save new version number
self.__upgrade_config(version)
logger.info('TerrariumPI Config is ready')
# Private functions
def __upgrade_config(self,to_version):
# Set minimal version to 3.0.0
current_version = 300
new_version = int(to_version.replace('.',''))
if int(self.get_system()['version'].replace('.','')) >= current_version:
current_version = int(self.get_system()['version'].replace('.',''))
if not current_version < new_version:
logger.info('Configuration is up to date')
else:
logger.info('Configuration is out of date. Running updates from %s to %s' % (current_version,new_version))
for version in xrange(current_version+1,new_version+1):
if version == 300:
logger.info('Updating configuration file to version: %s' % (version,))
# Upgrade: Move temperature indicator from weather to system
temperature_indicator = self.__get_config('weather')
if 'temperature' in temperature_indicator:
self.__config.set('terrariumpi', 'temperature_indicator', str(temperature_indicator['temperature']))
self.__config.remove_option('weather','temperature')
# Upgrade: Change profile image path to new path and config location
data = self.__get_config('terrariumpi')
if 'image' in data and '/static/images/gecko.jpg' == data['image']:
self.__config.set('profile', 'image', '/static/images/profile_image.jpg')
self.__config.remove_option('terrariumpi','image')
# Upgrade: Change profile name path to new config location
data = self.__get_config('terrariumpi')
if 'person' in data:
self.__config.set('profile', 'name', data['person'])
self.__config.remove_option('terrariumpi','person')
# Upgrade: Remove default available languages variable
data = self.__get_config('terrariumpi')
if 'available_languages' in data:
self.__config.remove_option('terrariumpi','available_languages')
elif version == 310:
logger.info('Updating configuration file to version: %s' % (version,))
# Upgrade: Rename active_language to just language
data = self.__get_config('terrariumpi')
if 'active_language' in data:
self.__config.set('terrariumpi', 'language', data['active_language'])
self.__config.remove_option('terrariumpi','active_language')
# Update the GPIO pinnumbering for PWM dimmers and DHT like sensors
for section in self.__config.sections():
if section[:6] == 'sensor':
sensor_data = self.__get_config(section)
if 'dht' in sensor_data['hardwaretype'] or 'am2302' == sensor_data['hardwaretype']:
self.__config.set(section, 'address', str(terrariumUtils.to_BOARD_port_number(sensor_data['address'])))
if section[:6] == 'switch':
switch_data = self.__get_config(section)
if 'pwm-dimmer' == switch_data['hardwaretype']:
self.__config.set(section, 'address', str(terrariumUtils.to_BOARD_port_number(switch_data['address'])))
elif version == 312:
logger.info('Updating configuration file to version: %s' % (version,))
data = self.__get_config('terrariumpi')
if 'soundcard' in data and data['soundcard'] == '0':
self.__config.set('terrariumpi', 'soundcard', 'bcm2835 ALSA')
elif version == 330:
logger.info('Updating configuration file to version: %s' % (version,))
for section in self.__config.sections():
if section[:8] == 'playlist':
playlist_data = self.__get_config(section)
self.__config.set(section, 'start', str(datetime.datetime.fromtimestamp(float(playlist_data['start'])).strftime('%H:%M')))
self.__config.set(section, 'stop', str(datetime.datetime.fromtimestamp(float(playlist_data['stop'])).strftime('%H:%M')))
if section == 'environment':
environment_data = self.__get_config(section)
self.__config.set(section, 'light_on', str(datetime.datetime.fromtimestamp(float(environment_data['light_on'])).strftime('%H:%M')))
self.__config.set(section, 'light_off', str(datetime.datetime.fromtimestamp(float(environment_data['light_off'])).strftime('%H:%M')))
self.__config.set(section, 'heater_on', str(datetime.datetime.fromtimestamp(float(environment_data['heater_on'])).strftime('%H:%M')))
self.__config.set(section, 'heater_off', str(datetime.datetime.fromtimestamp(float(environment_data['heater_off'])).strftime('%H:%M')))
self.__config.set(section, 'cooler_on', str(datetime.datetime.fromtimestamp(float(environment_data['cooler_on'])).strftime('%H:%M')))
self.__config.set(section, 'cooler_off', str(datetime.datetime.fromtimestamp(float(environment_data['cooler_off'])).strftime('%H:%M')))
# Update version number
self.__config.set('terrariumpi', 'version', str(to_version))
self.__save_config()
self.__config.read(terrariumConfig.CUSTOM_CONFIG)
logger.info('Updated configuration. Set version to: %s' % (to_version,))
def __reload_config(self):
self.__config.read(terrariumConfig.CUSTOM_CONFIG)
def __save_config(self):
'''Write terrariumPI config to settings.cfg file'''
with open(terrariumConfig.CUSTOM_CONFIG, 'wb') as configfile:
self.__config.write(configfile)
return True
def __update_config(self,section,data,exclude = []):
'''Update terrariumPI config with new values
Keyword arguments:
section -- section in configuration. If not exists it will be created
data -- data to save in dict form'''
if not self.__config.has_section(section):
self.__config.add_section(section)
keys = data.keys()
keys.sort()
for setting in keys:
if setting in exclude:
continue
if type(data[setting]) is list:
data[setting] = ','.join(data[setting])
if isinstance(data[setting], basestring):
try:
data[setting] = data[setting].encode('utf-8')
except Exception, ex:
'Not sure what to do... but it seams already utf-8...??'
pass
self.__config.set(section, str(setting), str(data[setting]))
config_ok = self.__save_config()
if config_ok:
self.__reload_config()
return config_ok
def __get_config(self,section):
'''Get terrariumPI config based on section. Return empty dict when not exists
Keyword arguments:
section -- section to read from the config'''
config = {}
if not self.__config.has_section(section):
return config
for config_part in self.__config.items(section):
config[config_part[0]] = config_part[1]
return config
def __get_all_config(self,part):
data = []
for section in self.__config.sections():
if section[:len(part)] == part:
data.append(self.__get_config(section))
return data
# End private functions
def get_system(self):
'''Get terrariumPI configuration section 'terrariumpi'
'''
data = self.__get_config('terrariumpi')
data['available_languages'] = self.get_available_languages()
return data
def set_system(self,data):
'''Set terrariumPI configuration section 'terrariumpi'
Make sure that the fields cur_password and new_password are never stored
'''
return self.__update_config('terrariumpi',data,['cur_password','new_password','available_languages','location','windspeed'])
def get_available_languages(self):
'''Get terrariumPI available languages'''
if self.__cache_available_languages is None:
self.__cache_available_languages = [language.replace('locales/','').replace('/','') for language in glob("locales/*/")]
return self.__cache_available_languages
def get_language(self):
'''Get terrariumPI language'''
config = self.get_system()
if 'language' not in config:
config['language'] = self.get_available_languages()[0]
return config['language']
def get_weather_location(self):
data = self.get_weather()
return data['location'] if 'location' in data else None
def get_weather_windspeed(self):
data = self.get_weather()
return data['windspeed'] if 'windspeed' in data else None
def get_temperature_indicator(self):
config = self.get_system()
return config['temperature_indicator']
def get_distance_indicator(self):
config = self.get_system()
return config['distance_indicator']
def get_admin(self):
'''Get terrariumPI admin name'''
config = self.get_system()
return config['admin']
def get_password(self):
'''Get terrariumPI admin password'''
config = self.get_system()
return config['password']
def get_active_soundcard(self):
config = self.get_system()
return config['soundcard']
def get_external_calender_url(self):
config = self.get_system()
if 'external_calendar_url' in config and config['external_calendar_url'] != '':
return config['external_calendar_url']
return ''
def get_pi_power_wattage(self):
'''Get terrariumPI power usage'''
config = self.get_system()
return float(config['power_usage'])
def get_power_price(self):
'''Get terrariumPI power price. Price is entered as euro/kWh'''
config = self.get_system()
return float(config['power_price'])
def get_water_price(self):
'''Get terrariumPI water price. Price is entered as euro/m3'''
config = self.get_system()
return float(config['water_price'])
def get_hostname(self):
config = self.get_system()
return config['host']
def get_port_number(self):
config = self.get_system()
return config['port']
# Environment functions
def save_environment(self,data):
'''Save the terrariumPI environment config
'''
config = {}
for environment_part in data:
for part in data[environment_part]:
# Do not save the following settings
if part in ['enabled','time_table','state','amount','current','temperature','humidity','distance','alarm','alarm_min','alarm_max','limit_max','limit_min','type','night_modus','error','lastaction']:
continue
if data[environment_part][part] is None:
data[environment_part][part] = ''
config[environment_part + '_' + part] = data[environment_part][part]
self.__config.remove_section('environment')
return self.__update_config('environment',config,[])
def get_environment(self):
config = self.__get_config('environment')
data = {'light' : {}, 'sprayer' : {}, 'heater' : {} , 'cooler' : {}, 'watertank' : {}, 'moisture' : {}, 'ph' : {}}
for key in config:
config_keys = key.split('_')
part = config_keys[0]
del(config_keys[0])
data[part]['_'.join(config_keys)] = config[key]
return data
# End Environment functions
# Profile functions
def get_profile(self):
return self.__get_config('profile')
def get_profile_image(self):
config = self.get_profile()
return config['image']
def get_profile_name(self):
config = self.get_profile()
return config['name']
def save_profile(self,data):
return self.__update_config('profile',data)
# End profile functions
# Weather config functions
def save_weather(self,data):
return self.__update_config('weather',data,['type'])
def get_weather(self):
return self.__get_config('weather')
# End weather config functions
# Sensor config functions
def get_owfs_port(self):
return int(self.get_system()['owfs_port'])
def save_sensor(self,data):
return self.__update_config('sensor' + data['id'],data,['current','indicator'])
def save_sensors(self,data):
update_ok = True
for sensor in self.get_sensors():
self.__config.remove_section('sensor' + sensor['id'])
for sensorid in data:
update_ok = update_ok and self.save_sensor(data[sensorid].get_data())
if len(data) == 0:
update_ok = update_ok and self.__save_config()
return update_ok
def get_sensors(self):
return self.__get_all_config('sensor')
# End sensor config functions
# Switches config functions
def save_power_switch(self,data):
clearfields = ['state','current_power_wattage','current_water_flow']
if data['hardwaretype'] != 'pwm-dimmer':
clearfields += ['dimmer_duration','dimmer_off_duration','dimmer_off_percentage','dimmer_on_duration','dimmer_on_percentage']
return self.__update_config('switch' + data['id'],data,clearfields)
def save_power_switches(self,data):
update_ok = True
for power_switch in self.get_power_switches():
self.__config.remove_section('switch' + power_switch['id'])
for power_switch_id in data:
update_ok = update_ok and self.save_power_switch(data[power_switch_id].get_data())
if len(data) == 0:
update_ok = update_ok and self.__save_config()
return update_ok
def get_power_switches(self):
return self.__get_all_config('switch')
# End switches config functions
# Door config functions
def save_door(self,data):
return self.__update_config('door' + data['id'],data,['state'])
def save_doors(self,data):
update_ok = True
for door in self.get_doors():
self.__config.remove_section('door' + door['id'])
for door_id in data:
update_ok = update_ok and self.save_door(data[door_id].get_data())
if len(data) == 0:
update_ok = update_ok and self.__save_config()
return update_ok
def get_doors(self):
return self.__get_all_config('door')
# End door config functions
# Webcam config functions
def save_webcam(self,data):
if 'resolution' in data:
data['resolution_width'] = data['resolution']['width']
data['resolution_height'] = data['resolution']['height']
del(data['resolution'])
return self.__update_config('webcam' + data['id'],data,['state','image','max_zoom','last_update','preview','archive_images'])
def save_webcams(self,data):
update_ok = True
for webcam in self.get_webcams():
self.__config.remove_section('webcam' + webcam['id'])
for webcam_id in data:
update_ok = update_ok and self.save_webcam(data[webcam_id].get_data())
if len(data) == 0:
update_ok = update_ok and self.__save_config()
return update_ok
def get_webcams(self):
return self.__get_all_config('webcam')
# End webcam config functions
# Audio playlist config functions
def save_audio_playlist(self,data):
return self.__update_config('playlist' + data['id'],data,['running','songs_duration','duration'])
def save_audio_playlists(self,data):
update_ok = True
for audio_playlist in self.get_audio_playlists():
self.__config.remove_section('playlist' + audio_playlist['id'])
for audio_playlist_id in data:
update_ok = update_ok and self.save_audio_playlist(data[audio_playlist_id].get_data())
if len(data) == 0:
update_ok = update_ok and self.__save_config()
return update_ok
def get_audio_playlists(self):
data = self.__get_all_config('playlist')
for playlist in data:
playlist['files'] = playlist['files'].split(',')
return data
# End audio playlist config functions