-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathPlugwise-2-web.py
More file actions
executable file
·365 lines (318 loc) · 13.3 KB
/
Plugwise-2-web.py
File metadata and controls
executable file
·365 lines (318 loc) · 13.3 KB
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
#!/usr/bin/env python3
# Copyright (C) 2012,2013,2014,2015,2016,2017,2018,2019,2020 Seven Watt <info@sevenwatt.com>
# <http://www.sevenwatt.com>
#
# This file is part of Plugwise-2.
#
# Plugwise-2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plugwise-2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plugwise-2 If not, see <http://www.gnu.org/licenses/>.
#
import sys
import time
import logging
import logging.handlers
import string
import cgi
import urllib.parse
import mimetypes
import os
import glob
import json
import threading
from socketserver import ThreadingMixIn
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
import ssl
from base64 import b64encode
from swutil import *
from swutil.util import *
from swutil.pwmqtt import *
from swutil.HTTPWebSocketsHandler import HTTPWebSocketsHandler
#webroot is the config folder in Plugwise-2-py.
#webserver can only serve files from webroot and subfolders.
#the webroot needs to be the current folder
webroot = os.curdir + os.sep + "config" + os.sep
os.chdir(webroot)
cfg = json.load(open("pw-hostconfig.json"))
#global var
pw_logger = None
logpath = cfg['log_path']+'/'
init_logger(logpath+"pw-web.log", "pw-web")
if 'log_level' in cfg:
if cfg['log_level'].strip().lower() == 'debug':
log_level(logging.DEBUG)
elif cfg['log_level'].strip().lower() == 'info':
log_level(logging.INFO)
elif cfg['log_level'].strip().lower() == 'error':
log_level(logging.ERROR)
else:
log_level(logging.INFO)
info('Number of arguments: %d' % (len(sys.argv),))
info('Argument List: %s' % (sys.argv,))
#setup mqtt (if mosquitto bindings are found)
import queue
import threading
#from pwmqttweb import *
mqtt = True
try:
import paho.mqtt.client as mosquitto
except:
mqtt = False
qpub = queue.Queue()
qsub = queue.Queue()
broadcast = []
last_topics = {}
#bcmutex = threading.Lock()
mqtt_t = None
if not mqtt:
error("No MQTT python binding installed (mosquitto-python)")
elif 'mqtt_ip' in cfg and 'mqtt_port' in cfg:
#connect to server and start worker thread.
if 'mqtt_user' in cfg and 'mqtt_password' in cfg:
mqttclient = Mqtt_client(cfg['mqtt_ip'], cfg['mqtt_port'], qpub, qsub,"Plugwise-2-web",cfg['mqtt_user'],cfg['mqtt_password'])
else:
mqttclient = Mqtt_client(cfg['mqtt_ip'], cfg['mqtt_port'], qpub, qsub, "Plugwise-2-web")
mqttclient.subscribe("plugwise2py/state/#")
mqtt_t = threading.Thread(target=mqttclient.run)
mqtt_t.setDaemon(True)
mqtt_t.start()
info("MQTT thread started")
else:
error("No MQTT broker and port configured")
mqtt = False
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = 8000
if len(sys.argv) > 2:
secure = str(sys.argv[2]).lower()=="secure"
else:
secure = False
if len(sys.argv) > 3:
credentials = str(sys.argv[3]).encode()
else:
credentials = b""
def broadcaster():
while True:
while not qsub.empty():
rcv = qsub.get()
topic = rcv[0]
payl = rcv[1]
#debug("mqtt broadcaster: %s %s" % (topic, payl))
#bcmutex.acquire()
last_topics[topic] = payl
for bq in broadcast:
bq.put(rcv)
#bcmutex.release()
time.sleep(0.1)
bc_t = threading.Thread(target=broadcaster)
bc_t.setDaemon(True)
bc_t.start()
info("Broadcast thread started")
class PW2PYwebHandler(HTTPWebSocketsHandler):
def log_message(self, format, *args):
if not args:
debug(self.address_string()+' '+format)
else:
debug(self.address_string()+' '+format % args)
def log_error(self, format, *args):
error(self.address_string()+' '+format % args)
def log_request(self, code='-', size='-'):
#self.log_message('"%s" %s %s', self.requestline, str(code), str(size))
info(self.address_string()+' "%s" %s %s' % (self.requestline, str(code), str(size)))
def end_headers(self):
self.send_header("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
HTTPWebSocketsHandler.end_headers(self)
def do_GET(self):
self.log_message("PW2PYwebHandler do_GET")
#debug("GET " + self.path)
if self.path in ['', '/', '/index']:
self.path = '/index.html'
#for this specific application this entry point:
if self.path == '/index.html':
self.path = '/pw2py.html'
#parse url
purl = urllib.parse.urlparse(self.path)
path = purl.path
debug("PW2PYwebHandler.do_GET() parsed: " + path)
if path == '/schedules':
#retrieve list of schedules
schedules = [os.path.splitext(os.path.basename(x))[0] for x in glob.glob(os.curdir + os.sep + 'schedules' + os.sep + '*.json')]
#print schedules
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(schedules))
return
#
#only allow certain file types to be retrieved
elif any(path.endswith(x) for x in ('.ws','.html','.js','.css','.png','.jpg','.gif', '.svg', '.ttf', '.woff', '.txt','.map','.json')):
HTTPWebSocketsHandler.do_GET(self)
else:
self.send_error(404,'Plugwise-2-py-web Page not found')
def do_POST(self):
self.log_message("PW2PYwebHandler do_POST")
#self.logRequest()
path = self.path
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if (ctype == 'application/x-www-form-urlencoded'):
clienttype = "ajax "
else:
clienttype = "angular "
info("%s POST: Path %s" % (clienttype, path))
debug("%s POST: Content-type %s" % (clienttype, ctype))
if ((ctype == 'application/x-www-form-urlencoded') or (ctype == 'application/json')):
if (path.startswith('/schedules/') and path.endswith('.json')) or path == '/pw-control.json':
#Write a config or schedule JSON file
debug("POST write a config schedule JSON file")
length = int(self.headers.getheader('content-length'))
raw = self.rfile.read(length)
#print raw
if (ctype == 'application/x-www-form-urlencoded'):
postvars = urllib.parse.parse_qs(raw, keep_blank_values=1)
if 'data' not in postvars:
debug("ajaxserver.POST: missing input parameter: %s" % (postvars,))
self.send_response(404, "data invalid format")
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({ "error": "missing data parameter" }))
return
#TODO: check for ajax POST client whether postvars['data'][0] is already the string to write!
#print postvars['data'][0]
ndata = json.loads(postvars['data'][0])
data = json.dumps(data);
else:
data = raw
#save schedule json file
fnsched = os.curdir + self.path
debug("POST save schedule to file path: " + fnsched)
try:
with open(fnsched, 'w') as outfile:
#json.dump(data, outfile)
outfile.write(data)
except IOError as err:
error("POST exception during saving schedule: %s" % (err,))
self.send_error(404,'Unable to store: %s' % fnsched)
return
self.send_response(200, "ok")
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({ "result": "ok"}))
return
elif path == '/schedules':
length = int(self.headers.getheader('content-length'))
raw = self.rfile.read(length)
debug("POST delete schedule %s" % (json.loads(raw)['delete'],))
try:
fnsched = path[1:]+"/"+json.loads(raw)['delete']
os.remove(fnsched)
except OSError:
self.send_error(404,'Unable to remove: %s' % fnsched)
return
self.send_response(200, "ok")
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({ "result": "ok"}))
return
elif path == '/mqtt/':
length = int(self.headers.getheader('content-length'))
raw = self.rfile.read(length)
data = json.loads(raw)
info("POST mqtt message: %s" % data)
topic = str(data['topic'])
msg = json.dumps(data['payload'])
qpub.put((topic, msg))
self.send_response(200, "ok")
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({ "result": "ok"}))
return
info("POST unhandled. Send 404.")
self.send_response(404, "unsupported POST")
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({ "error": "only accepting application/json or application/x-www-form-urlencoded" }))
return
def on_ws_message(self, message):
if message is None:
message = ''
#self.log_message('websocket received "%s"',str(message))
try:
data = json.loads(message)
topic = str(data['topic'])
payload = json.dumps(data['payload'])
qpub.put((topic, payload))
except Exception as err:
self.log_error("Error parsing incoming websocket message: %s", err)
def on_ws_connected(self):
self.q = queue.Queue()
self.t = threading.Thread(target=self.process_mqtt)
self.t.daemon = True
self.t.start()
#bcmutex.acquire()
broadcast.append(self.q)
#bcmutex.release()
self.log_message("process_mqtt running on worker thread %d" % self.t.ident)
def on_ws_closed(self):
#bcmutex.acquire()
broadcast.remove(self.q)
#bcmutex.release()
#join gives issues. threads seems to be reused, so threads end anyways.
#self.t.join()
#self.log_message("on_ws_closed websocket closed for handler %s" % str(self))
def process_mqtt(self):
#allow some time for websockets to become fully operational
time.sleep(2.0)
#send last known state to webpage
topics = list(last_topics.items())
for topic in topics:
self.q.put(topic)
while self.connected:
while not self.q.empty():
rcv = self.q.get()
topic = rcv[0]
payl = rcv[1]
info("process_mqtt_commands: %s %s" % (topic, payl))
if self.connected:
self.send_message(payl)
time.sleep(0.5)
self.log_message("process_mqtt exiting worker thread %d" % self.t.ident)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
def main():
try:
server = ThreadedHTTPServer(('', port), PW2PYwebHandler)
server.daemon_threads = True
server.auth = b64encode(credentials)
if secure:
if sys.hexversion < 0x02071000:
#server.socket = ssl.wrap_socket (server.socket, certfile='./server.pem', server_side=True, ssl_version=ssl.PROTOCOL_TLSv1_2)
server.socket = ssl.wrap_socket (server.socket, certfile='./server.pem', server_side=True, ssl_version=ssl.PROTOCOL_TLSv1)
else:
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ctx.load_cert_chain(certfile="./server.pem")
ctx.options |= ssl.OP_NO_TLSv1
ctx.options |= ssl.OP_NO_TLSv1_1
ctx.options |= ssl.OP_CIPHER_SERVER_PREFERENCE
ctx.set_ciphers('ECDHE-RSA-AES256-GCM-SHA384 ECDHE-RSA-AES256-SHA384 ECDHE-RSA-AES256-SHA')
server.socket = ctx.wrap_socket(server.socket, server_side=True)
info('started secure https server at port %d' % (port,))
else:
info('started http server at port %d' % (port,))
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down server')
server.shutdown()
print("exit after server.shutdown()")
if __name__ == '__main__':
main()