Mercurial > code > home > repos > light9
annotate bin/paintserver @ 1859:f066d6e874db
2to3 with these fixers: all idioms set_literal
Ignore-this: cbd28518218c2f0ddce8c4f92d3b8b33
author | drewp@bigasterisk.com |
---|---|
date | Wed, 22 May 2019 00:08:22 +0000 |
parents | 7772cc48e016 |
children | 5bcb950024af |
rev | line source |
---|---|
1521 | 1 #!bin/python |
2 | |
1859
f066d6e874db
2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents:
1858
diff
changeset
|
3 |
1521 | 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 |
1859
f066d6e874db
2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents:
1858
diff
changeset
|
18 import imp |
1521 | 19 |
1522
69088fe2865e
more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents:
1521
diff
changeset
|
20 |
1858 | 21 class Solve(PrettyErrorHandler, cyclone.web.RequestHandler): |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
22 |
1521 | 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) |
1858 | 27 sample, sampleDist = self.settings.solver.bestMatch( |
28 img, device=DEV['aura2']) | |
1570
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
29 with self.settings.graph.currentState() as g: |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
30 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
|
31 #out = solver.solve(painting) |
0480fc790527
paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents:
1524
diff
changeset
|
32 #layers = solver.simulationLayers(out) |
1858 | 33 |
34 self.write( | |
35 json.dumps({ | |
36 'bestMatch': { | |
37 'uri': sample, | |
38 'path': bestPath, | |
39 'dist': sampleDist | |
40 }, | |
41 # 'layers': layers, | |
42 # 'out': out, | |
43 })) | |
1521 | 44 |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
45 def reloadSolver(self): |
1859
f066d6e874db
2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents:
1858
diff
changeset
|
46 imp.reload(light9.paint.solve) |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
47 self.settings.solver = light9.paint.solve.Solver(self.settings.graph) |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
48 self.settings.solver.loadSamples() |
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
49 |
1858 | 50 |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
51 class BestMatches(PrettyErrorHandler, cyclone.web.RequestHandler): |
1858 | 52 |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
53 def post(self): |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
54 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
|
55 painting = body['painting'] |
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 outSettings = self.settings.solver.bestMatches(img, devs) |
1858 | 60 self.write(json.dumps({'settings': outSettings.asList()})) |
61 | |
62 | |
1521 | 63 class App(object): |
1858 | 64 |
1521 | 65 def __init__(self, show, session): |
66 self.show = show | |
67 self.session = session | |
68 | |
69 self.graph = SyncedGraph(networking.rdfdb.url, "paintServer") | |
1858 | 70 self.graph.initiallySynced.addCallback(self.launch).addErrback( |
71 log.error) | |
72 | |
73 self.stats = scales.collection( | |
74 '/', | |
75 scales.PmfStat('solve'), | |
76 ) | |
77 | |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
78 def launch(self, *args): |
1521 | 79 |
1858 | 80 self.solver = light9.paint.solve.Solver( |
81 self.graph, | |
82 sessions=[ | |
83 L9['show/dance2017/capture/aura1/cap1876596'], | |
84 L9['show/dance2017/capture/aura2/cap1876792'], | |
85 L9['show/dance2017/capture/aura3/cap1877057'], | |
86 L9['show/dance2017/capture/aura4/cap1877241'], | |
87 L9['show/dance2017/capture/aura5/cap1877406'], | |
88 L9['show/dance2017/capture/q1/cap1874255'], | |
89 L9['show/dance2017/capture/q2/cap1873665'], | |
90 L9['show/dance2017/capture/q3/cap1876223'], | |
91 ]) | |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
92 self.solver.loadSamples() |
1858 | 93 |
1521 | 94 self.cycloneApp = cyclone.web.Application(handlers=[ |
95 (r'/stats', StatsForCyclone), | |
96 (r'/solve', Solve), | |
1626
5d2dcae1a7c6
paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents:
1585
diff
changeset
|
97 (r'/bestMatches', BestMatches), |
1521 | 98 ], |
99 debug=True, | |
100 graph=self.graph, | |
1575
0d2247ec8f49
bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents:
1570
diff
changeset
|
101 solver=self.solver, |
1521 | 102 stats=self.stats) |
103 reactor.listenTCP(networking.paintServer.port, self.cycloneApp) | |
104 log.info("listening on %s" % networking.paintServer.port) | |
105 | |
106 | |
107 if __name__ == "__main__": | |
108 parser = optparse.OptionParser() | |
1858 | 109 parser.add_option( |
110 '--show', | |
1521 | 111 help='show URI, like http://light9.bigasterisk.com/show/dance2008', |
1858 | 112 default=showconfig.showUri()) |
113 parser.add_option("-v", | |
114 "--verbose", | |
115 action="store_true", | |
1521 | 116 help="logging.DEBUG") |
1858 | 117 parser.add_option("--twistedlog", |
118 action="store_true", | |
1521 | 119 help="twisted logging") |
120 clientsession.add_option(parser) | |
121 (options, args) = parser.parse_args() | |
122 log.setLevel(logging.DEBUG if options.verbose else logging.INFO) | |
123 | |
124 if not options.show: | |
125 raise ValueError("missing --show http://...") | |
1858 | 126 |
1521 | 127 session = clientsession.getUri('paint', options) |
128 | |
129 app = App(URIRef(options.show), session) | |
130 if options.twistedlog: | |
131 from twisted.python import log as twlog | |
132 twlog.startLogging(sys.stderr) | |
133 reactor.run() |