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