Files
@ b0337e6f68f1
Branch filter:
Location: light9/bin/subserver
b0337e6f68f1
3.3 KiB
text/plain
refactor to videorecorder.py
Ignore-this: 3b3eeaa3e817b8ebea688e239624683b
Ignore-this: 3b3eeaa3e817b8ebea688e239624683b
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #!bin/python
"""
live web display of all existing subs with pictures, mainly for
dragging them into CC
"""
from run_local import log
from twisted.internet import reactor
import twisted.internet.error
import sys, optparse, logging, json, os, subprocess
import cyclone.web, cyclone.httpclient, cyclone.websocket
from light9.rdfdb.syncedgraph import SyncedGraph
from rdflib import RDF
from light9.namespaces import L9
import pyjade.utils
try:
import sys
sys.path.append("../homeauto/lib")
sys.path.append("/home/drewp/projects/homeauto/lib")
from cycloneerr import PrettyErrorHandler
except ImportError:
class PrettyErrorHandler(object):
pass
liveClients = set()
def sendToLiveClients(d=None, asJson=None):
j = asJson or json.dumps(d)
for c in liveClients:
c.sendMessage(j)
class Live(cyclone.websocket.WebSocketHandler):
def connectionMade(self, *args, **kwargs):
log.info("websocket opened")
liveClients.add(self)
self.settings.onNewClient()
def connectionLost(self, reason):
log.info("websocket closed")
liveClients.remove(self)
def messageReceived(self, message):
log.info("got message %s" % message)
self.sendMessage(message)
class Static(PrettyErrorHandler, cyclone.web.StaticFileHandler):
def get(self, path, *args, **kw):
if path == '':
return self.respondStaticJade("light9/subserver/index.jade")
if path == 'gui.js':
return self.responseStaticCoffee('light9/subserver/gui.coffee')
oddlyPlaced = {
"websocket.js": "light9/rdfdb/web/websocket.js",
"jquery-1.7.2.min.js": "light9/rdfdb/web/lib/jquery-1.7.2.min.js",
}
if path in oddlyPlaced:
self.write(open(oddlyPlaced[path]).read())
return
cyclone.web.StaticFileHandler.get(self, path, *args, **kw)
def respondStaticJade(self, src):
html = pyjade.utils.process(open(src).read())
self.write(html)
def responseStaticCoffee(self, src):
self.write(subprocess.check_output([
'/usr/bin/coffee', '--compile', '--print', src]))
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true",
help="logging.DEBUG")
(options, args) = parser.parse_args()
log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
graph = SyncedGraph("subserver")
d = {}
def updateSubs():
subs = []
for sub in graph.subjects(RDF.type, L9['Submaster']):
rec = {'uri' : sub}
rec['isLocal'] = graph.contains((sub, RDF.type,
L9['LocalSubmaster']))
rec['label'] = graph.label(sub)
subs.append(rec)
d.clear()
d.update({'subs': subs})
sendToLiveClients(d=d)
def onNewClient():
sendToLiveClients(d=d)
graph.addHandler(updateSubs)
port = 8052
reactor.listenTCP(port, cyclone.web.Application(handlers=[
(r'/live', Live),
(r'/(.*)', Static,
{"path" : "light9/subserver",
"default_filename" : "index.jade"}),
], debug=True, graph=graph, onNewClient=onNewClient))
log.info("serving on %s" % port)
reactor.run()
|