forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrariumWebserver.py
439 lines (341 loc) · 16.5 KB
/
terrariumWebserver.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
# -*- coding: utf-8 -*-
import terrariumLogging
logger = terrariumLogging.logging.getLogger(__name__)
import gettext
gettext.install('terrariumpi', 'locales/', unicode=True)
from bottle import BaseRequest, Bottle, request, abort, static_file, template, error, response, auth_basic, HTTPError
#Increase bottle memory to max 5MB to process images in WYSIWYG editor
BaseRequest.MEMFILE_MAX = 5 * 1024 * 1024
from bottle.ext.websocket import GeventWebSocketServer
from bottle.ext.websocket import websocket
from Queue import Queue
from terrariumTranslations import terrariumTranslations
from terrariumAudio import terrariumAudioPlayer
from terrariumUtils import terrariumUtils
import thread
import json
import os
import datetime
import hashlib
import functools
from gevent import monkey, sleep
monkey.patch_all()
class terrariumWebserverHeaders(object):
name = 'webserver_headers'
api = 2
def apply(self, fn, context):
def webserver_headers(*args, **kwargs):
template_file = 'views' + request.fullpath[:-5] + '.tpl'
if os.path.isfile(template_file):
t = os.path.getmtime(template_file)
#response.headers['Expires'] = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%a, %d %b %Y %H:%M:%S GMT')
response.headers['Last-Modified'] = datetime.datetime.fromtimestamp(t).strftime( '%a, %d %b %Y %H:%M:%S GMT')
response.headers['Etag'] = hashlib.md5(response.headers['Last-Modified']).hexdigest()
return fn(*args, **kwargs)
return webserver_headers
class terrariumWebserver():
app = Bottle()
app.install(terrariumWebserverHeaders())
def __init__(self, terrariumEngine):
self.__terrariumEngine = terrariumEngine
self.__app = terrariumWebserver.app
self.__config = self.__terrariumEngine.get_config('system')
self.__caching_days = 30
terrariumWebserver.app.terrarium = self.__terrariumEngine
# Load language
gettext.translation('terrariumpi', 'locales/', languages=[self.__terrariumEngine.config.get_language()]).install(True)
self.__translations = terrariumTranslations()
self.__routes()
# Custom HTTP authentication routine. This way there is an option to optional secure the hole web interface
def __auth_basic2(self, check, required, realm="private", text="Access denied"):
""" Callback decorator to require HTTP auth (basic).
TODO: Add route(check_auth=...) parameter. """
def decorator(func):
@functools.wraps(func)
def wrapper(*a, **ka):
user, password = request.auth or (None, None)
if required or terrariumUtils.is_true(self.__terrariumEngine.config.get_system()['always_authenticate']):
if user is None or not check(user, password):
err = HTTPError(401, text)
err.add_header('WWW-Authenticate', 'Basic realm="%s"' % realm)
return err
return func(*a, **ka)
return wrapper
return decorator
def __authenticate(self, required):
return self.__auth_basic2(self.__terrariumEngine.authenticate,required,_('TerrariumPI') + ' ' + _('Authentication'),_('Authenticate to make any changes'))
def __logout_authenticate(self, user, password):
return True
def __routes(self):
self.__app.route('/',
method="GET",
callback=self.__render_page,
apply=self.__authenticate(False))
self.__app.route('/<template_name:re:[^/]+\.html$>',
method="GET",
callback=self.__render_page,
apply=self.__authenticate(False))
self.__app.route('/<filename:re:robots\.txt>',
method="GET",
callback=self.__static_file,
apply=self.__authenticate(False))
self.__app.route('/<root:re:(static|gentelella|webcam|audio|log)>/<filename:path>',
method="GET",
callback=self.__static_file,
apply=self.__authenticate(False))
self.__app.route('/api/<path:path>',
method=['GET'],
callback=self.__get_api_call,
apply=self.__authenticate(False))
self.__app.route('/api/switch/toggle/<switchid:path>',
method=['POST'],
callback=self.__toggle_switch,
apply=self.__authenticate(True)
)
self.__app.route('/api/switch/state/<switchid:path>/<value:int>',
method=['POST'],
callback=self.__state_switch,
apply=self.__authenticate(True)
)
self.__app.route('/api/config/<path:re:(system|weather|switches|sensors|webcams|doors|audio|environment|profile)>',
method=['PUT','POST','DELETE'],
callback=self.__update_api_call,
apply=self.__authenticate(True)
)
self.__app.route('/api/audio/player/<action:re:(start|stop|volumeup|volumedown|mute|unmute)>',
method=['POST'],
callback=self.__player_commands,
apply=self.__authenticate(True)
)
self.__app.route('/api/audio/file',
method=['POST'],
callback=self.__upload_audio_file,
apply=self.__authenticate(True)
)
self.__app.route('/api/audio/file/<audiofileid:path>',
method=['DELETE'],
callback=self.__delete_audio_file,
apply=self.__authenticate(True)
)
self.__app.route('/logout',
method=['GET'],
callback=self.__logout_url,
apply=auth_basic(self.__logout_authenticate,_('TerrariumPI') + ' ' + _('Authentication'),_('Authenticate to make any changes'))
)
def __template_variables(self, template):
variables = { 'lang' : self.__terrariumEngine.config.get_language(),
'title' : self.__config['title'],
'version' : self.__config['version'],
'page_title' : _(template.replace('_',' ').capitalize()),
'temperature_indicator' : self.__terrariumEngine.get_temperature_indicator(),
'distance_indicator' : self.__terrariumEngine.get_distance_indicator(),
'translations': self.__translations }
if 'index' == template or 'profile' == template:
variables['person_name'] = self.__terrariumEngine.get_profile_name()
variables['person_image'] = self.__terrariumEngine.get_profile_image()
return variables
def __render_page(self,template_name = 'index.html'):
template_name = template_name[:-5]
if not os.path.isfile('views/' + template_name + '.tpl'):
template_name = '404'
return template(template_name,**self.__template_variables(template_name))
def __static_file(self,filename, root = 'static'):
if filename == 'js/terrariumpi.js':
response.headers['Content-Type'] = 'application/javascript; charset=UTF-8'
response.headers['Expires'] = (datetime.datetime.utcnow() + datetime.timedelta(days=self.__caching_days)).strftime('%a, %d %b %Y %H:%M:%S GMT')
return template(filename,template_lookup=[root])
staticfile = static_file(filename, root=root)
if isinstance(staticfile,HTTPError):
return staticfile
if 'webcam' == root or 'log' == root:
staticfile.add_header('Expires',datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT'))
if 'log' == root:
staticfile.add_header('Content-Type','text/text; charset=UTF-8')
staticfile.add_header('Content-Disposition','Attachment;filename=' + filename)
else:
staticfile.add_header('Expires',(datetime.datetime.utcnow() + datetime.timedelta(days=self.__caching_days)).strftime('%a, %d %b %Y %H:%M:%S GMT'))
if staticfile.get_header('Last-Modified') is not None:
staticfile.add_header('Etag',hashlib.md5(staticfile.get_header('Last-Modified')).hexdigest())
return staticfile
def __player_commands(self,action):
result = {'ok' : False, 'title' : _('Error!'), 'message' : _('Player command could ot be executed!')}
if 'start' == action:
self.__terrariumEngine.audio_player_start()
elif 'stop' == action:
self.__terrariumEngine.audio_player_stop()
elif 'volumeup' == action:
self.__terrariumEngine.audio_player_volume_up()
result = {'ok' : True, 'title' : _('OK!'), 'message' : _('Player command executed!')}
elif 'volumedown' == action:
self.__terrariumEngine.audio_player_volume_down()
result = {'ok' : True, 'title' : _('OK!'), 'message' : _('Player command executed!')}
elif 'mute' == action:
pass
elif 'unmute' == action:
pass
return result;
def __upload_audio_file(self):
result = {'ok' : False, 'title' : _('Error!'), 'message' : _('File is not uploaded!')}
upload = request.files.get('file')
try:
upload.save(terrariumAudioPlayer.AUDIO_FOLDER)
self.__terrariumEngine.reload_audio_files()
result = {'ok' : True, 'title' : _('Success!'), 'message' : _('File \'%s\' is uploaded' % (upload.filename,))}
except IOError, message:
result['message'] = _('Duplicate file \'%s\'' % (upload.filename,))
return result
def __delete_audio_file(self,audiofileid):
result = {'ok' : False, 'title' : _('Error!'), 'message' : _('Action could not be satisfied')}
if self.__terrariumEngine.delete_audio_file(audiofileid):
result = {'ok' : True, 'title' : _('Success!'), 'message' : _('Audio file is deleted')}
return result
def __update_api_call(self,path):
result = {'ok' : False, 'title' : _('Error!'), 'message' : _('Data could not be saved')}
postdata = {}
if request.json is not None:
postdata = request.json
result['ok'] = self.__terrariumEngine.set_config(path,postdata,request.files)
if result['ok']:
result['title'] = _('Data saved')
result['message'] = _('Your changes are saved')
# Reload language if needed
if 'language' in postdata:
gettext.translation('terrariumpi', 'locales/', languages=[self.__terrariumEngine.config.get_language()]).install(True)
self.__translations.reload()
return result
def __get_api_call(self,path):
response.headers['Expires'] = (datetime.datetime.utcnow() + datetime.timedelta(minutes=1)).strftime('%a, %d %b %Y %H:%M:%S GMT')
response.headers['Access-Control-Allow-Origin'] = '*'
result = {}
parameters = path.strip('/').split('/')
action = parameters[0]
del(parameters[0])
if 'switches' == action:
result = self.__terrariumEngine.get_switches(parameters)
elif 'doors' == action:
if len(parameters) > 0 and parameters[0] == 'status':
result = {'status' : self.__terrariumEngine.get_doors_status()}
else:
result = self.__terrariumEngine.get_doors()
elif 'profile' == action:
result = self.__terrariumEngine.get_profile()
elif 'sensors' == action:
result = self.__terrariumEngine.get_sensors(parameters)
elif 'webcams' == action:
result = self.__terrariumEngine.get_webcams(parameters)
elif 'audio' == action:
if len(parameters) > 0 and parameters[0] == 'files':
del(parameters[0])
result = self.__terrariumEngine.get_audio_files(parameters)
elif len(parameters) > 0 and parameters[0] == 'playing':
del(parameters[0])
result = self.__terrariumEngine.get_audio_playing()
elif len(parameters) > 0 and parameters[0] == 'hardware':
del(parameters[0])
result = {'audiohardware' : terrariumAudioPlayer.get_sound_cards()}
else:
result = self.__terrariumEngine.get_audio_playlists(parameters)
elif 'environment' == action:
result = self.__terrariumEngine.get_environment(parameters)
elif 'weather' == action:
result = self.__terrariumEngine.get_weather(parameters)
elif 'uptime' == action:
result = self.__terrariumEngine.get_uptime()
elif 'power_usage' == action:
result = self.__terrariumEngine.get_power_usage_water_flow()['power']
elif 'water_usage' == action:
result = self.__terrariumEngine.get_power_usage_water_flow()['water']
elif 'system' == action:
result = self.__terrariumEngine.get_system_stats()
elif 'history' == action or 'export' == action:
response.headers['Expires'] = (datetime.datetime.utcnow() + datetime.timedelta(minutes=5)).strftime('%a, %d %b %Y %H:%M:%S GMT')
if 'export' == action:
parameters.append('all')
result = self.__terrariumEngine.get_history(parameters)
if 'export' == action:
csv = ''
export_name = 'error'
for datatype in result:
for dataid in result[datatype]:
export_name = datatype + '_' + dataid + '.csv'
# Header
fields = result[datatype][dataid].keys()
if 'totals' in fields:
fields.remove('totals')
csv = '"' + '","'.join(['timestamp'] + fields) + "\"\n"
for counter in xrange(0,len(result[datatype][dataid][fields[0]])):
# Timestamp
row = [datetime.datetime.fromtimestamp(int(str(int(result[datatype][dataid][fields[0]][counter][0]/1000)))).strftime('%Y-%m-%d %H:%M:%S')]
for field in fields:
# Row values
row.append(str(result[datatype][dataid][field][counter][1]))
csv += '"' + '","'.join(row) + "\"\n"
response.headers['Content-Type'] = 'application/csv';
response.headers['Content-Disposition'] = 'attachment; filename=' + export_name;
return csv
elif 'config' == action:
# TODO: New way of data processing.... fix other config options
result = self.__terrariumEngine.get_config(parameters[0] if len(parameters) == 1 else None)
return result
def __toggle_switch(self,switchid):
if switchid in self.__terrariumEngine.power_switches:
self.__terrariumEngine.power_switches[switchid].toggle()
return {'ok' : True}
return {'ok' : False}
def __state_switch(self,switchid,value):
if switchid in self.__terrariumEngine.power_switches:
self.__terrariumEngine.power_switches[switchid].set_state(value)
return {'ok' : True}
return {'ok' : False}
def __logout_url(self):
return {'ok' : True,
'title' : _('Log out'),
'message' : _('You are now logged out')
}
@app.error(404)
def error404(error):
config = terrariumWebserver.app.terrarium.get_config('system')
variables = { 'lang' : terrariumWebserver.app.terrarium.config.get_language(),
'title' : config['title'],
'page_title' : config['title'] + ' | 404'
}
return template('404',**variables)
@app.route('/live', apply=[websocket])
def handle_websocket(socket):
messages = Queue()
def listen_for_messages(messages,socket):
while True:
message = messages.get()
try:
socket.send(json.dumps(message))
except Exception, err:
# Socket connection is lost, stop looping....
break
messages.task_done()
while True:
try:
message = socket.receive()
except Exception, err:
break
if message is not None:
message = json.loads(message)
if message['type'] == 'client_init':
thread.start_new_thread(listen_for_messages, (messages,socket))
terrariumWebserver.app.terrarium.subscribe(messages)
terrariumWebserver.app.terrarium.get_doors_status(socket=True)
terrariumWebserver.app.terrarium.get_uptime(socket=True)
terrariumWebserver.app.terrarium.get_environment(socket=True)
terrariumWebserver.app.terrarium.get_sensors(['average'],socket=True)
terrariumWebserver.app.terrarium.get_power_usage_water_flow(socket=True)
def start(self):
# Start the webserver
logger.info('Running webserver at %s:%s' % (self.__config['host'],self.__config['port']))
print '%s - INFO - terrariumWebserver - Running webserver at %s:%s' % (datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S,000'),
self.__config['host'],
self.__config['port'])
self.__app.run(host=self.__config['host'],
port=self.__config['port'],
server=GeventWebSocketServer,
debug=True,
reloader=False,
quiet=True)