-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
78 lines (58 loc) · 1.99 KB
/
server.py
File metadata and controls
78 lines (58 loc) · 1.99 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
import json
import queue
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.websocket
from tornado.options import define, options
import serialporttask
define("port", default=8080, help="port to listen on", type=int)
clients = []
serialport = None
input_queue = queue.Queue()
output_queue = queue.Queue()
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("./public/index.html")
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
print("connection opened")
clients.append(self)
def on_message(self, message):
data = json.loads(message)
msg = serialport.command(data)
if msg:
self.write_message(msg)
else:
input_queue.put(data['args']['data'])
def on_close(self):
print("connection closed")
clients.remove(self)
# check the queue for pending messages to all connected clients
def checkQueue():
if not output_queue.empty():
message = output_queue.get()
for c in clients:
c.write_message(message)
if __name__ == "__main__":
# start the serial port task in background (as a deamon)
serialport = serialporttask.SerialPortThread(input_queue, output_queue)
serialport.daemon = True
serialport.start()
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[
(r"/", IndexHandler),
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": "./public/"}),
(r"/ws", WebSocketHandler)
]
)
httpServer = tornado.httpserver.HTTPServer(app)
httpServer.listen(options.port)
print("Listening on port:", options.port)
mainLoop = tornado.ioloop.IOLoop.instance()
# adjust the scheduler_interval according to the frames sent by the serial port
scheduler_interval = 100
scheduler = tornado.ioloop.PeriodicCallback(checkQueue, scheduler_interval)
scheduler.start()
mainLoop.start()