annotate bin/paintserver @ 2046:9aa046cc9b33

replace greplin with prometheus throughout (untested)
author drewp@bigasterisk.com
date Tue, 10 May 2022 23:01:26 -0700
parents f29e26811206
children ccdfdc8183ad
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
1692
6fa4288da8a6 rdfdb is its own package now
drewp@bigasterisk.com
parents: 1626
diff changeset
6 from rdfdb.syncedgraph import SyncedGraph
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7 from light9 import networking, showconfig
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 import optparse, sys, logging
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
9 import cyclone.web
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
12 import light9.paint.solve
1861
40cc863d2b63 start py3 and other dep fixes
drewp@bigasterisk.com
parents: 1860
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
17
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1521
diff changeset
18
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
21 def post(self):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
25 sample, sampleDist = self.settings.solver.bestMatch(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
31
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
32 self.write(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
33 json.dumps({
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
34 'bestMatch': {
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
35 'uri': sample,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
36 'path': bestPath,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
37 'dist': sampleDist
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
38 },
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
39 # 'layers': layers,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
40 # 'out': out,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
41 }))
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
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
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
58 self.write(json.dumps({'settings': outSettings.asList()}))
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
59
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
60
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
61 class App(object):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
62
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
63 def __init__(self, show, session):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
64 self.show = show
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
65 self.session = session
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
66
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67 self.graph = SyncedGraph(networking.rdfdb.url, "paintServer")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
68 self.graph.initiallySynced.addCallback(self.launch).addErrback(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
69 log.error)
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
70
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
71
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
72 def launch(self, *args):
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
73
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
74 self.solver = light9.paint.solve.Solver(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
75 self.graph,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
76 sessions=[
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
77 L9['show/dance2017/capture/aura1/cap1876596'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
78 L9['show/dance2017/capture/aura2/cap1876792'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
79 L9['show/dance2017/capture/aura3/cap1877057'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
80 L9['show/dance2017/capture/aura4/cap1877241'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
81 L9['show/dance2017/capture/aura5/cap1877406'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
82 L9['show/dance2017/capture/q1/cap1874255'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
83 L9['show/dance2017/capture/q2/cap1873665'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
84 L9['show/dance2017/capture/q3/cap1876223'],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
85 ])
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
86 self.solver.loadSamples()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
87
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
88 self.cycloneApp = cyclone.web.Application(handlers=[
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
92 ],
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
93 debug=True,
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
94 graph=self.graph,
2046
9aa046cc9b33 replace greplin with prometheus throughout (untested)
drewp@bigasterisk.com
parents: 1937
diff changeset
95 solver=self.solver)
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
96 reactor.listenTCP(networking.paintServer.port, self.cycloneApp)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
97 log.info("listening on %s" % networking.paintServer.port)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
98
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
99
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
100 if __name__ == "__main__":
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
101 parser = optparse.OptionParser()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
102 parser.add_option(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
103 '--show',
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
104 help='show URI, like http://light9.bigasterisk.com/show/dance2008',
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
105 default=showconfig.showUri())
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
106 parser.add_option("-v",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
107 "--verbose",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
108 action="store_true",
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
109 help="logging.DEBUG")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
110 parser.add_option("--twistedlog",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
111 action="store_true",
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
112 help="twisted logging")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
113 clientsession.add_option(parser)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
114 (options, args) = parser.parse_args()
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
115 log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
116
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
117 if not options.show:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
118 raise ValueError("missing --show http://...")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1698
diff changeset
119
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
120 session = clientsession.getUri('paint', options)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
121
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
122 app = App(URIRef(options.show), session)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
123 if options.twistedlog:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
124 from twisted.python import log as twlog
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
125 twlog.startLogging(sys.stderr)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
126 reactor.run()