-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
52 lines (43 loc) · 1.13 KB
/
webserver.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
#!/usr/bin/python3
#
# Webserver for the PLC Dimmer class
import hid
import os
import logging
from flask import Flask, request, abort
from plc import PLC, Dimmer
logger = logging.getLogger(__name__)
app = Flask(__name__)
PASSWORD = os.getenv('PASSWORD')
VID = 0x10bf
PID = 0x0004
@app.route('/dimmer/<address>/<cmd>')
def dimmer(address, cmd):
password = request.args.get('pass')
level = request.args.get('level', None)
if password != PASSWORD:
abort(403)
d = Dimmer(p, address)
try:
if cmd == 'set':
rv = d.set_level(round(255*int(level)/100.))
elif cmd == 'get':
rv = round(100*d.get_level()/255.)
elif cmd == 'status':
if d.get_level() == 0:
rv = 0
else:
rv = 1
return f"{rv}"
except TimeoutError as e:
logger.exception(f"{e}")
abort(504)
abort(404)
h = hid.Device(VID, PID)
logger.info(f'Device manufacturer: {h.manufacturer}')
logger.info(f'Product: {h.product}')
p = PLC(h)
if not PASSWORD:
logger.warning("No password is set.")
if __name__ == '__main__':
app.run()