diff --git a/bin/subserver b/bin/subserver --- a/bin/subserver +++ b/bin/subserver @@ -4,17 +4,18 @@ live web display of all existing subs wi 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 sys, optparse, logging, json, subprocess, datetime +from dateutil.tz import tzlocal +from twisted.internet import reactor, defer import cyclone.web, cyclone.httpclient, cyclone.websocket +from rdflib import RDF, URIRef, Literal +import pyjade.utils from light9.rdfdb.syncedgraph import SyncedGraph -from rdflib import RDF -from light9.namespaces import L9 -import pyjade.utils +from light9.rdfdb.patch import Patch +from light9.namespaces import L9, DCTERMS +from light9 import networking, showconfig try: - import sys sys.path.append("../homeauto/lib") sys.path.append("/home/drewp/projects/homeauto/lib") from cycloneerr import PrettyErrorHandler @@ -67,6 +68,35 @@ class Static(PrettyErrorHandler, cyclone def responseStaticCoffee(self, src): self.write(subprocess.check_output([ '/usr/bin/coffee', '--compile', '--print', src])) + +class Snapshot(PrettyErrorHandler, cyclone.web.RequestHandler): + @defer.inlineCallbacks + def post(self): + about = URIRef(self.get_argument("about")) + response = yield cyclone.httpclient.fetch(networking.vidref.path("snapshot"), method="POST") + + snapUri = URIRef(json.loads(response.body)['snapshot']) + # vidref could write about when it was taken, etc. would it be + # better for us to tell vidref where to attach the result in + # the graph, and then it doesn't even have to return anything? + + ctx = showconfig.showUri() + "/snapshots" + + self.settings.graph.patch(Patch(addQuads=[ + (about, L9['image'], snapUri, ctx), + (snapUri, DCTERMS['created'], + Literal(datetime.datetime.now(tzlocal())), ctx), + ])) + + self.write(json.dumps({'snapshot': snapUri})) + +def newestImage(subject): + newest = (None, None) + for img in graph.objects(subject, L9['image']): + created = graph.value(img, DCTERMS['created']) + if created > newest[0]: + newest = (created, img) + return newest[1] if __name__ == "__main__": parser = optparse.OptionParser() @@ -80,13 +110,13 @@ if __name__ == "__main__": d = {} def updateSubs(): - subs = [] - for sub in graph.subjects(RDF.type, L9['Submaster']): + for sub in sorted(graph.subjects(RDF.type, L9['Submaster'])): rec = {'uri' : sub} rec['isLocal'] = graph.contains((sub, RDF.type, L9['LocalSubmaster'])) rec['label'] = graph.label(sub) + rec['img'] = newestImage(sub) subs.append(rec) d.clear() @@ -101,6 +131,7 @@ if __name__ == "__main__": port = 8052 reactor.listenTCP(port, cyclone.web.Application(handlers=[ (r'/live', Live), + (r'/snapshot', Snapshot), (r'/(.*)', Static, {"path" : "light9/subserver", "default_filename" : "index.jade"}),