-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (36 loc) · 1.08 KB
/
main.py
File metadata and controls
57 lines (36 loc) · 1.08 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import tornado.ioloop
import tornado.web
import tornado.websocket
import led
from tornado.options import define, options, parse_command_line
define("port", default=8888, help="run on the given port")
root = os.path.dirname(__file__)
gpioHandler = led.Handler()
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def open(self):
print("WebSocket opened")
def on_message(self, message):
if(message == "red"):
gpioHandler.toggleRedLed()
if(message == "green"):
gpioHandler.toggleGreenLed()
if(message == "yellow"):
gpioHandler.toggleYellowLed()
if(message == "blue"):
gpioHandler.toggleBlueLed()
def on_close(self):
print("WebSocket closed")
app = tornado.web.Application([
#~ (r'/', HtmlHandler),
(r'/websocket', WebSocketHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": root, "default_filename": "index.html"})
])
if __name__ == '__main__':
parse_command_line()
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()