Mercurial > code > home > repos > light9
comparison bin/subserver @ 919:33a5a98e9bf1
start subserver webapp with websockets, cyclone, jade, coffee
Ignore-this: d0e47882e0f3edb2bcea660e612d364
author | drewp@bigasterisk.com |
---|---|
date | Mon, 10 Jun 2013 23:03:57 +0000 |
parents | 6a4e99505164 |
children | 8b95d2865643 |
comparison
equal
deleted
inserted
replaced
918:124f4647179f | 919:33a5a98e9bf1 |
---|---|
1 # keep the database of submasters, and mix up the current client | 1 #!bin/python |
2 # requests into dmx levels for dmxserver | 2 """ |
3 live web display of all existing subs with pictures, mainly for | |
4 dragging them into CC | |
5 """ | |
6 from run_local import log | |
7 from twisted.internet import reactor | |
8 import twisted.internet.error | |
9 import sys, optparse, logging, json, os, subprocess | |
10 import cyclone.web, cyclone.httpclient, cyclone.websocket | |
11 from light9.rdfdb.syncedgraph import SyncedGraph | |
12 import pyjade.utils | |
3 | 13 |
4 class SubServe: | 14 try: |
5 """call the server with these messages""" | 15 import sys |
16 sys.path.append("../homeauto/lib") | |
17 sys.path.append("/home/drewp/projects/homeauto/lib") | |
18 from cycloneerr import PrettyErrorHandler | |
19 except ImportError: | |
20 class PrettyErrorHandler(object): | |
21 pass | |
22 | |
23 liveClients = set() | |
24 def sendToLiveClients(d=None, asJson=None): | |
25 j = asJson or json.dumps(d) | |
26 for c in liveClients: | |
27 c.sendMessage(j) | |
28 | |
29 class Live(cyclone.websocket.WebSocketHandler): | |
30 def connectionMade(self, *args, **kwargs): | |
31 log.info("websocket opened") | |
32 liveClients.add(self) | |
33 self.settings.onNewClient() | |
34 | |
35 def connectionLost(self, reason): | |
36 log.info("websocket closed") | |
37 liveClients.remove(self) | |
38 | |
39 def messageReceived(self, message): | |
40 log.info("got message %s" % message) | |
41 self.sendMessage(message) | |
42 | |
43 class Static(PrettyErrorHandler, cyclone.web.StaticFileHandler): | |
44 # .xhtml pages can be get() without .xhtml on them | |
45 def get(self, path, *args, **kw): | |
46 if path == '': | |
47 return self.respondStaticJade("light9/subserver/index.jade") | |
48 | |
49 if path == 'gui.js': | |
50 return self.responseStaticCoffee('light9/subserver/gui.coffee') | |
51 | |
52 oddlyPlaced = { | |
53 "websocket.js": "light9/rdfdb/web/websocket.js", | |
54 "jquery-1.7.2.min.js": "light9/rdfdb/web/lib/jquery-1.7.2.min.js", | |
55 } | |
56 if path in oddlyPlaced: | |
57 self.write(open(oddlyPlaced[path]).read()) | |
58 return | |
59 | |
60 cyclone.web.StaticFileHandler.get(self, path, *args, **kw) | |
61 | |
62 def respondStaticJade(self, src): | |
63 html = pyjade.utils.process(open(src).read()) | |
64 self.write(html) | |
65 | |
66 def responseStaticCoffee(self, src): | |
67 self.write(subprocess.check_output([ | |
68 '/usr/bin/coffee', '--compile', '--print', src])) | |
69 | |
70 if __name__ == "__main__": | |
71 parser = optparse.OptionParser() | |
72 parser.add_option("-v", "--verbose", action="store_true", | |
73 help="logging.DEBUG") | |
74 (options, args) = parser.parse_args() | |
75 | |
76 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
77 | |
78 graph = SyncedGraph("subserver") | |
79 | |
80 d = {} | |
81 def updateSubs(): | |
82 d.clear() | |
83 d.update({'subs':['a', 'b']}) | |
84 sendToLiveClients(d=d) | |
85 def onNewClient(): | |
86 sendToLiveClients(d=d) | |
87 | |
88 graph.addHandler(updateSubs) | |
6 | 89 |
7 def allSubs(self): | |
8 """list of all the known subs""" | |
9 | |
10 def output(self,levels): | |
11 """pass a dict of {sub : level} mappings""" | |
12 | |
13 def editsub | |
14 | |
15 | |
16 class SubClient: | |
17 """each client can receive these messages""" | |
18 | 90 |
19 def subAdded(self,newsub): | 91 port = 8052 |
20 """sub was just added to the db""" | 92 reactor.listenTCP(port, cyclone.web.Application(handlers=[ |
21 | 93 (r'/live', Live), |
22 def subRemove(self,pastsub): | 94 (r'/(.*)', Static, |
23 """this sub is about to be removed from the db""" | 95 {"path" : "light9/subserver", |
24 | 96 "default_filename" : "index.jade"}), |
25 def subChange(self,sub): | 97 ], debug=True, graph=graph, onNewClient=onNewClient)) |
26 """this is a new version of an existing sub""" | 98 log.info("serving on %s" % port) |
99 reactor.run() |