2014-10-23 18:46:44 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-10-20 02:07:45 +00:00
|
|
|
import os
|
2014-10-23 08:20:53 +00:00
|
|
|
import re
|
|
|
|
import time
|
2014-10-20 02:02:12 +00:00
|
|
|
import json
|
2014-10-22 19:11:46 +00:00
|
|
|
import traceback
|
2014-10-23 04:46:35 +00:00
|
|
|
import inspect
|
|
|
|
|
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-23 08:20:53 +00:00
|
|
|
import paths
|
2014-10-23 04:46:35 +00:00
|
|
|
|
2014-10-23 18:46:44 +00:00
|
|
|
cone_symbol = re.compile(r'\^([0-9]{1,3})')
|
|
|
|
|
2014-10-20 02:02:12 +00:00
|
|
|
class ClientSocket(websocket.WebSocketHandler):
|
|
|
|
def initialize(self, parent):
|
|
|
|
self.parent = parent
|
|
|
|
|
|
|
|
def open(self):
|
2014-10-23 04:46:35 +00:00
|
|
|
self.parent.clients.append(self)
|
2014-10-20 02:02:12 +00:00
|
|
|
|
|
|
|
def on_close(self):
|
2014-10-23 04:46:35 +00:00
|
|
|
self.parent.clients.remove(self)
|
2014-10-20 02:16:49 +00:00
|
|
|
|
2014-10-23 08:20:53 +00:00
|
|
|
class ManagerHandler(tornado.web.RequestHandler):
|
|
|
|
def initialize(self, manager):
|
|
|
|
self.manager = manager
|
|
|
|
|
|
|
|
class MainHandler(ManagerHandler):
|
|
|
|
def get(self):
|
|
|
|
files = os.listdir(paths.profile_path)
|
2014-10-23 18:46:44 +00:00
|
|
|
fixname = lambda x: cone_symbol.sub(r'Δ\1', os.path.splitext(x)[0].replace("_", " "))
|
2014-10-23 08:20:53 +00:00
|
|
|
profiles = dict((fname, fixname(fname)) for fname in files)
|
2014-11-30 04:27:48 +00:00
|
|
|
|
2014-10-23 08:20:53 +00:00
|
|
|
return self.render(os.path.join(paths.html_templates, "main.html"),
|
|
|
|
state=self.manager.state.__class__.__name__,
|
2014-11-30 04:51:32 +00:00
|
|
|
state_data=json.dumps(self.manager.state.status),
|
2014-10-23 08:20:53 +00:00
|
|
|
profiles=profiles,
|
|
|
|
)
|
|
|
|
|
2014-10-23 04:46:35 +00:00
|
|
|
class DataRequest(ManagerHandler):
|
2014-10-20 02:16:49 +00:00
|
|
|
def get(self):
|
2014-11-29 18:59:24 +00:00
|
|
|
self.set_header("Content-Type", "application/json")
|
2014-10-22 19:11:46 +00:00
|
|
|
data = list(self.manager.history)
|
2014-10-22 01:08:10 +00:00
|
|
|
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-23 08:20:53 +00:00
|
|
|
class ProfileHandler(tornado.web.RequestHandler):
|
|
|
|
def get(self, name):
|
|
|
|
try:
|
|
|
|
with open(os.path.join(paths.profile_path, name)) as fp:
|
|
|
|
self.write(fp.read())
|
|
|
|
except IOError:
|
|
|
|
self.write_error(404)
|
|
|
|
|
|
|
|
def post(self, name):
|
|
|
|
try:
|
2014-11-02 06:07:27 +00:00
|
|
|
schedule = json.loads(self.get_argument("schedule"))
|
2014-10-23 08:20:53 +00:00
|
|
|
fname = os.path.join(paths.profile_path, name)
|
|
|
|
with open(fname, 'w') as fp:
|
|
|
|
json.dump(schedule, fp)
|
|
|
|
self.write(dict(type="success"))
|
|
|
|
except IOError:
|
|
|
|
self.write_error(404)
|
|
|
|
except Exception as e:
|
|
|
|
self.write(dict(type="error", error=repr(e), msg=traceback.format_exc()))
|
|
|
|
|
2014-10-23 04:46:35 +00:00
|
|
|
class DoAction(ManagerHandler):
|
|
|
|
def _run(self, name, argfunc):
|
|
|
|
func = getattr(self.manager.state, name)
|
|
|
|
#Introspect the function, get the arguments
|
|
|
|
args, varargs, keywords, defaults = inspect.getargspec(func)
|
|
|
|
|
|
|
|
kwargs = dict()
|
|
|
|
if defaults is not None:
|
|
|
|
#keyword arguments
|
|
|
|
for arg, d in zip(args[-len(defaults):], defaults):
|
|
|
|
kwargs[arg] = argfunc(arg, default=d)
|
|
|
|
end = len(defaults)
|
|
|
|
else:
|
|
|
|
end = len(args)
|
|
|
|
|
|
|
|
#required arguments
|
|
|
|
for arg in args[1:end]:
|
|
|
|
kwargs[arg] = argfunc(arg)
|
|
|
|
|
|
|
|
realfunc = getattr(self.manager, name)
|
|
|
|
realfunc(**kwargs)
|
2014-10-21 21:21:43 +00:00
|
|
|
|
|
|
|
def get(self, action):
|
2014-10-22 19:11:46 +00:00
|
|
|
try:
|
2014-10-23 04:46:35 +00:00
|
|
|
self._run(action, self.get_query_argument)
|
2014-10-22 19:11:46 +00:00
|
|
|
self.write(json.dumps(dict(type="success")))
|
2014-10-23 04:46:35 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.write(json.dumps(dict(type="error", error=repr(e), msg=traceback.format_exc())))
|
2014-10-22 19:11:46 +00:00
|
|
|
|
|
|
|
def post(self, action):
|
|
|
|
try:
|
2014-10-23 04:46:35 +00:00
|
|
|
self._run(action, self.get_argument)
|
2014-10-22 19:11:46 +00:00
|
|
|
self.write(json.dumps(dict(type="success")))
|
2014-10-23 04:46:35 +00:00
|
|
|
except Exception as e:
|
2014-10-23 08:20:53 +00:00
|
|
|
self.write(json.dumps(dict(type="error", error=repr(e), msg=traceback.format_exc())))
|
2014-10-21 21:21:43 +00:00
|
|
|
|
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 = [
|
2014-10-23 08:20:53 +00:00
|
|
|
(r"^/$", MainHandler, dict(manager=manager)),
|
|
|
|
(r"^/ws/?$", ClientSocket, dict(parent=self)),
|
|
|
|
(r"^/temperature.json$", DataRequest, dict(manager=manager)),
|
|
|
|
(r"^/do/(.*)/?$", DoAction, dict(manager=manager)),
|
|
|
|
(r"^/profile/?(.*)$", ProfileHandler),
|
|
|
|
(r"^/(.*)$", tornado.web.StaticFileHandler, dict(path=paths.html_static)),
|
2014-10-20 02:02:12 +00:00
|
|
|
]
|
2014-10-23 04:46:35 +00:00
|
|
|
self.clients = []
|
2014-10-20 02:02:12 +00:00
|
|
|
self.port = port
|
|
|
|
|
|
|
|
def send(self, data):
|
2014-11-30 04:51:32 +00:00
|
|
|
jsondat = json.dumps(data)
|
2014-10-23 04:46:35 +00:00
|
|
|
for sock in self.clients:
|
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
|
2014-12-17 06:34:47 +00:00
|
|
|
kiln = manager.Manager(simulate=False)
|
2014-10-22 01:08:10 +00:00
|
|
|
app = WebApp(kiln)
|
2014-10-22 19:11:46 +00:00
|
|
|
kiln._send = app.send
|
2014-10-20 04:34:17 +00:00
|
|
|
|
|
|
|
app.run()
|
2014-10-20 03:35:01 +00:00
|
|
|
except KeyboardInterrupt:
|
2014-10-22 19:11:46 +00:00
|
|
|
kiln.manager_stop()
|