-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWebinterface.py
81 lines (66 loc) · 2.79 KB
/
Webinterface.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
from jinja2 import Environment, FileSystemLoader
from tornado.web import RequestHandler, HTTPError
import os
import logging
import json
import config
import Database
templateFile = "index.html"
templateLoader = FileSystemLoader(searchpath=os.path.join(config.WebDir, 'templates'))
templateEnv = Environment(loader=templateLoader)
template = templateEnv.get_template(templateFile)
class IndexHandler(RequestHandler):
def initialize(self, openhab):
self._logging = logging.getLogger('pymygw')
self._db = Database.Database()
self._openhab = openhab
self._output = template.render(items=self._db.get(),
openhab=self.__prepareOpenhabItems())
def get(self):
self.write(self._output)
def __prepareOpenhabItems(self):
l = []
for e, v in self._openhab.Items().iteritems():
if str(v) in ('NumberItem'):
l.append(e)
return l
class CommandHandler(RequestHandler):
def initialize(self, openhab):
self._logging = logging.getLogger('pymygw')
self._db = Database.Database()
self._openhab = openhab
def post(self):
try:
data = json.loads(self.request.body)
self._action = data['action']
self._sensor = data['sensor']
self._ohitem = data['openhab']
except:
raise HTTPError(400, 'Bad Request {0}'.format(data))
self.__checkSensor()
self.__checkOpenhab()
self._logging.info('Webinterface Action {0}, Sensor {1}, Openhab {2}'.format(self._action, self._sensor, self._ohitem))
if self._action == 'update':
self._logging.info('Would update {0}'.format((self._action, self._sensor, self._ohitem)))
self.__update()
elif self._action == 'delete':
self._logging.info('Would delete {0}'.format((self._action, self._sensor, self._ohitem)))
self.__delete()
else:
raise HTTPError(400, 'Action unknown {0}'.format(self._action))
def __checkSensor(self):
if self._db.get(Node=self._sensor) is None:
raise HTTPError(400, 'Sensor unknown {0}'.format(self._sensor))
def __checkOpenhab(self):
if not self._ohitem in self._openhab.Items():
raise HTTPError(400, 'Openhab Item unknown {0}'.format(self._ohitem))
def __delete(self):
r = self._db.update(node=self._sensor)
if r is None:
raise HTTPError(400, 'Delete failed {0}'.format(self._sensor))
self.set_status(200, reason='OK')
def __update(self):
r = self._db.update(node=self._sensor, openhab=self._ohitem)
if r is None:
raise HTTPError(400, 'Update failed {0}, {1}'.format(self._sensor, self._ohitem))
self.set_status(200, reason='OK')