-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
executable file
·62 lines (44 loc) · 1.32 KB
/
app.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
#!/usr/bin/env python
import os, json
from lib.socket_controller import SocketController
from bottle import route, run, static_file
import config
ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
STATIC_PATH = ROOT_PATH + '/client'
sc = SocketController(config)
@route('/')
def index():
return static_file('index.html', root=STATIC_PATH)
@route('/<filename:re:.*\.(html|js|css)>')
def index(filename):
return static_file(filename, root=STATIC_PATH)
@route('/manifest.appcache')
def manifest():
return static_file('manifest.appcache', root=STATIC_PATH, mimetype='text/cache-manifest')
@route('/status')
def status():
return sc.state
@route('/sockets')
def status():
return {
1: config.SOCKET_NAMES[0],
2: config.SOCKET_NAMES[1],
3: config.SOCKET_NAMES[2],
4: config.SOCKET_NAMES[3],
}
@route('/<socket:re:([1234]|all)>/<state:re:[01]>')
def switch_socket(socket, state):
"""
define catch all endpoint for sending signals to sockets like:
/1/1 => turn_1_on
/1/0 => turn_1_off
"""
method = "switch_%s_%s" % (socket, ('on' if state == '1' else 'off'))
getattr(sc, method)()
return sc.state
if __name__ == "__main__":
# Init SocketController and Run server
sc.start()
run(host='0.0.0.0', port=8080)
# Clean up
sc.stop()