2014-10-15 18:23:03 +00:00
|
|
|
import time
|
2014-10-20 02:07:45 +00:00
|
|
|
import os
|
2014-10-20 02:02:12 +00:00
|
|
|
import json
|
2014-10-15 18:23:03 +00:00
|
|
|
import tornado.ioloop
|
|
|
|
import tornado.web
|
2014-10-20 02:07:45 +00:00
|
|
|
from tornado import websocket
|
2014-10-15 18:23:03 +00:00
|
|
|
|
2014-10-20 02:02:12 +00:00
|
|
|
cwd = os.path.split(os.path.abspath(__file__))[0]
|
2014-10-15 18:23:03 +00:00
|
|
|
|
2014-10-20 02:02:12 +00:00
|
|
|
class ClientSocket(websocket.WebSocketHandler):
|
|
|
|
def initialize(self, parent):
|
|
|
|
self.parent = parent
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
self.parent.sockets.append(self)
|
|
|
|
|
|
|
|
def on_close(self):
|
|
|
|
self.parent.sockets.remove(self)
|
|
|
|
|
2014-10-20 02:16:49 +00:00
|
|
|
class DataRequest(tornado.web.RequestHandler):
|
2014-10-22 01:08:10 +00:00
|
|
|
def initialize(self, manager):
|
|
|
|
self.manager = manager
|
2014-10-20 02:16:49 +00:00
|
|
|
|
|
|
|
def get(self):
|
2014-10-22 01:08:10 +00:00
|
|
|
data = self.manager.thermocouple.history
|
|
|
|
output = [dict(time=ts.time, temp=ts.temp) for ts in data]
|
2014-10-20 02:16:49 +00:00
|
|
|
self.write(json.dumps(output))
|
|
|
|
|
2014-10-21 21:21:43 +00:00
|
|
|
class DoAction(tornado.web.RequestHandler):
|
2014-10-22 01:08:10 +00:00
|
|
|
def initialize(self, manager):
|
|
|
|
self.manager = manager
|
2014-10-21 21:21:43 +00:00
|
|
|
|
|
|
|
def get(self, action):
|
|
|
|
pass
|
|
|
|
|
2014-10-20 02:02:12 +00:00
|
|
|
class WebApp(object):
|
2014-10-22 01:08:10 +00:00
|
|
|
def __init__(self, manager, port=8888):
|
2014-10-20 02:02:12 +00:00
|
|
|
self.handlers = [
|
|
|
|
(r"/ws/", ClientSocket, dict(parent=self)),
|
2014-10-22 01:08:10 +00:00
|
|
|
(r"/temperature.json", DataRequest, dict(manager=manager)),
|
|
|
|
(r"/do/(.*)", DoAction, dict(manager=manager)),
|
2014-10-20 02:02:12 +00:00
|
|
|
(r"/(.*)", tornado.web.StaticFileHandler, dict(path=cwd)),
|
|
|
|
]
|
|
|
|
self.sockets = []
|
|
|
|
self.port = port
|
|
|
|
|
|
|
|
def send(self, data):
|
|
|
|
jsondat = json.dumps(data)
|
|
|
|
for sock in self.sockets:
|
2014-10-20 03:35:01 +00:00
|
|
|
sock.write_message(jsondat)
|
2014-10-20 02:02:12 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.app = tornado.web.Application(self.handlers, gzip=True)
|
|
|
|
self.app.listen(8888)
|
|
|
|
tornado.ioloop.IOLoop.instance().start()
|
2014-10-15 18:23:03 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-10-20 03:35:01 +00:00
|
|
|
try:
|
2014-10-22 01:08:10 +00:00
|
|
|
import manager
|
|
|
|
kiln = manager.Manager()
|
|
|
|
app = WebApp(kiln)
|
|
|
|
kiln.register(app)
|
|
|
|
kiln.start()
|
2014-10-20 04:34:17 +00:00
|
|
|
|
|
|
|
app.run()
|
2014-10-20 03:35:01 +00:00
|
|
|
except KeyboardInterrupt:
|
2014-10-22 01:08:10 +00:00
|
|
|
kiln.stop()
|