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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!bin/python
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 from run_local import log
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 import json
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5 from twisted.internet import reactor
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 from light9.greplin_cyclone import StatsForCyclone
1692
6fa4288da8a6 rdfdb is its own package now
drewp@bigasterisk.com
parents: 1626
diff changeset
7 from rdfdb.syncedgraph import SyncedGraph
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 from light9 import networking, showconfig
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
9 from greplin import scales
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10 import optparse, sys, logging
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 import cyclone.web
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 import light9.paint.solve
1861
40cc863d2b63 start py3 and other dep fixes
drewp@bigasterisk.com
parents: 1860
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
18
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1521
diff changeset
19
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 def post(self):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23 painting = json.loads(self.request.body)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
26 sample, sampleDist = self.settings.solver.bestMatch(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
32
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
33 self.write(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
34 json.dumps({
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
35 'bestMatch': {
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
36 'uri': sample,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
37 'path': bestPath,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
38 'dist': sampleDist
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
39 },
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
40 # 'layers': layers,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
41 # 'out': out,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
42 }))
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
59 self.write(json.dumps({'settings': outSettings.asList()}))
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
60
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
61
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
62 class App(object):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
63
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
64 def __init__(self, show, session):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
65 self.show = show
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
66 self.session = session
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
68 self.graph = SyncedGraph(networking.rdfdb.url, "paintServer")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
69 self.graph.initiallySynced.addCallback(self.launch).addErrback(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
70 log.error)
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
71
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
72 self.stats = scales.collection(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
73 '/',
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
74 scales.PmfStat('solve'),
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
75 )
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
76
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
77 def launch(self, *args):
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
78
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
79 self.solver = light9.paint.solve.Solver(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
80 self.graph,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
81 sessions=[
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
82 L9['show/dance2017/capture/aura1/cap1876596'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
83 L9['show/dance2017/capture/aura2/cap1876792'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
84 L9['show/dance2017/capture/aura3/cap1877057'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
85 L9['show/dance2017/capture/aura4/cap1877241'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
86 L9['show/dance2017/capture/aura5/cap1877406'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
87 L9['show/dance2017/capture/q1/cap1874255'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
88 L9['show/dance2017/capture/q2/cap1873665'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
89 L9['show/dance2017/capture/q3/cap1876223'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
90 ])
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
91 self.solver.loadSamples()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
92
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
93 self.cycloneApp = cyclone.web.Application(handlers=[
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
94 (r'/stats', StatsForCyclone),
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
97 ],
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
98 debug=True,
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
99 graph=self.graph,
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
100 solver=self.solver,
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
101 stats=self.stats)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
102 reactor.listenTCP(networking.paintServer.port, self.cycloneApp)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
103 log.info("listening on %s" % networking.paintServer.port)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
104
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
105
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
106 if __name__ == "__main__":
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
107 parser = optparse.OptionParser()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
108 parser.add_option(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
109 '--show',
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
110 help='show URI, like http://light9.bigasterisk.com/show/dance2008',
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
111 default=showconfig.showUri())
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
112 parser.add_option("-v",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
113 "--verbose",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
114 action="store_true",
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
115 help="logging.DEBUG")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
116 parser.add_option("--twistedlog",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
117 action="store_true",
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
118 help="twisted logging")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
119 clientsession.add_option(parser)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
120 (options, args) = parser.parse_args()
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
121 log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
122
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
123 if not options.show:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
124 raise ValueError("missing --show http://...")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
125
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
126 session = clientsession.getUri('paint', options)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
127
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
128 app = App(URIRef(options.show), session)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
129 if options.twistedlog:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
130 from twisted.python import log as twlog
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
131 twlog.startLogging(sys.stderr)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
132 reactor.run()