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