Mercurial > code > home > repos > light9
annotate bin/paintserver @ 1777:299d49de85a8
effectSequencer info display
Ignore-this: 88d2b52511206f38b90b2658bc63ec9c
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Mon, 04 Jun 2018 07:05:42 +0000 |
parents | f140153c087c |
children | 7772cc48e016 |
rev | line source |
---|---|
1521 | 1 #!bin/python |
2 | |
3 from __future__ import division | |
4 from run_local import log | |
5 import json | |
6 from twisted.internet import reactor | |
7 from light9.greplin_cyclone import StatsForCyclone | |
1692 | 8 from rdfdb.syncedgraph import SyncedGraph |
1521 | 9 from light9 import networking, showconfig |
10 from greplin import scales | |
11 import optparse, sys, logging | |
12 import cyclone.web | |
13 from rdflib import URIRef | |
1698
f140153c087c
bring back clientsession which doesn't belong in rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents:
1692
diff
changeset
|
14 from light9 import clientsession |
1521 | 15 import light9.paint.solve |
16 from lib.cycloneerr import PrettyErrorHandler | |
1570
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
17 from light9.namespaces import RDF, L9, DEV |
1521 | 18 |
1522
69088fe2865e
more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
19 |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
20 |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
21 |
1521 | 22 class Solve(PrettyErrorHandler, cyclone.web.RequestHandler): |
23 def post(self): | |
24 painting = json.loads(self.request.body) | |
25 with self.settings.stats.solve.time(): | |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
26 img = self.settings.solver.draw(painting) |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
27 sample, sampleDist = self.settings.solver.bestMatch(img, device=DEV['aura2']) |
1570
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
28 with self.settings.graph.currentState() as g: |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
29 bestPath = g.value(sample, L9['imagePath']).replace(L9[''], '') |
1570
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
30 #out = solver.solve(painting) |
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
31 #layers = solver.simulationLayers(out) |
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
32 |
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
33 self.write(json.dumps({ |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
34 'bestMatch': {'uri': sample, 'path': bestPath, 'dist': sampleDist}, |
1570
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
35 # 'layers': layers, |
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
36 # 'out': out, |
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
37 })) |
1521 | 38 |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
39 def reloadSolver(self): |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
40 reload(light9.paint.solve) |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
41 self.settings.solver = light9.paint.solve.Solver(self.settings.graph) |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
42 self.settings.solver.loadSamples() |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
43 |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
44 class BestMatches(PrettyErrorHandler, cyclone.web.RequestHandler): |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
45 def post(self): |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
46 body = json.loads(self.request.body) |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
47 painting = body['painting'] |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
48 devs = [URIRef(d) for d in body['devices']] |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
49 with self.settings.stats.solve.time(): |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
50 img = self.settings.solver.draw(painting) |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
51 outSettings = self.settings.solver.bestMatches(img, devs) |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
52 self.write(json.dumps({ |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
53 'settings': outSettings.asList() |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
54 })) |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
55 |
1521 | 56 class App(object): |
57 def __init__(self, show, session): | |
58 self.show = show | |
59 self.session = session | |
60 | |
61 self.graph = SyncedGraph(networking.rdfdb.url, "paintServer") | |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
62 self.graph.initiallySynced.addCallback(self.launch).addErrback(log.error) |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
63 |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
64 self.stats = scales.collection('/', scales.PmfStat('solve'), |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
65 ) |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
66 |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
67 def launch(self, *args): |
1521 | 68 |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
69 self.solver = light9.paint.solve.Solver(self.graph, sessions=[ |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
70 L9['show/dance2017/capture/aura1/cap1876596'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
71 L9['show/dance2017/capture/aura2/cap1876792'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
72 L9['show/dance2017/capture/aura3/cap1877057'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
73 L9['show/dance2017/capture/aura4/cap1877241'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
74 L9['show/dance2017/capture/aura5/cap1877406'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
75 L9['show/dance2017/capture/q1/cap1874255'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
76 L9['show/dance2017/capture/q2/cap1873665'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
77 L9['show/dance2017/capture/q3/cap1876223'], |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
78 ]) |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
79 self.solver.loadSamples() |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
80 |
1521 | 81 self.cycloneApp = cyclone.web.Application(handlers=[ |
82 (r'/stats', StatsForCyclone), | |
83 (r'/solve', Solve), | |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
84 (r'/bestMatches', BestMatches), |
1521 | 85 ], |
86 debug=True, | |
87 graph=self.graph, | |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
88 solver=self.solver, |
1521 | 89 stats=self.stats) |
90 reactor.listenTCP(networking.paintServer.port, self.cycloneApp) | |
91 log.info("listening on %s" % networking.paintServer.port) | |
92 | |
93 | |
94 if __name__ == "__main__": | |
95 parser = optparse.OptionParser() | |
96 parser.add_option('--show', | |
97 help='show URI, like http://light9.bigasterisk.com/show/dance2008', | |
98 default=showconfig.showUri()) | |
99 parser.add_option("-v", "--verbose", action="store_true", | |
100 help="logging.DEBUG") | |
101 parser.add_option("--twistedlog", action="store_true", | |
102 help="twisted logging") | |
103 clientsession.add_option(parser) | |
104 (options, args) = parser.parse_args() | |
105 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
106 | |
107 if not options.show: | |
108 raise ValueError("missing --show http://...") | |
109 | |
110 session = clientsession.getUri('paint', options) | |
111 | |
112 app = App(URIRef(options.show), session) | |
113 if options.twistedlog: | |
114 from twisted.python import log as twlog | |
115 twlog.startLogging(sys.stderr) | |
116 reactor.run() |