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)
|
|
|
|
|
|
|
|
class WebApp(object):
|
|
|
|
def __init__(self, handlers, port=8888):
|
|
|
|
self.handlers = [
|
|
|
|
(r"/ws/", ClientSocket, dict(parent=self)),
|
|
|
|
(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:
|
|
|
|
socket.write_message(jsondat)
|
|
|
|
|
|
|
|
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 02:02:12 +00:00
|
|
|
import thermo
|
|
|
|
monitor = thermo.Monitor()
|
2014-10-16 07:46:04 +00:00
|
|
|
|
2014-10-20 02:02:12 +00:00
|
|
|
app = WebApp([])
|
|
|
|
def send_temp(time, temp):
|
|
|
|
app.send(dict(time=time, temp=temp))
|
|
|
|
monitor.callback = send_temp
|
|
|
|
monitor.start()
|
|
|
|
app.run()
|