annotate bin/paintserver @ 1799:0bb7b9df12e5

collector warnings and errors. the reactor.crash isn't working. Ignore-this: b7d4a6c08f04c924ffba6112483f9c19
author drewp@bigasterisk.com
date Fri, 08 Jun 2018 03:35:30 +0000
parents f140153c087c
children 7772cc48e016
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 __future__ import division
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4 from run_local import log
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5 import json
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 from twisted.internet import reactor
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7 from light9.greplin_cyclone import StatsForCyclone
1692
6fa4288da8a6 rdfdb is its own package now
drewp@bigasterisk.com
parents: 1626
diff changeset
8 from rdfdb.syncedgraph import SyncedGraph
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
9 from light9 import networking, showconfig
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10 from greplin import scales
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 import optparse, sys, logging
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
12 import cyclone.web
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 import light9.paint.solve
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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
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
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
20
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 class Solve(PrettyErrorHandler, cyclone.web.RequestHandler):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23 def post(self):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
24 painting = json.loads(self.request.body)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
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)
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
27 sample, sampleDist = self.settings.solver.bestMatch(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)
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
32
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
33 self.write(json.dumps({
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
34 'bestMatch': {'uri': sample, 'path': bestPath, 'dist': sampleDist},
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
35 # 'layers': layers,
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
36 # 'out': out,
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1524
diff changeset
37 }))
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
38
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
39 def reloadSolver(self):
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
40 reload(light9.paint.solve)
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
41 self.settings.solver = light9.paint.solve.Solver(self.settings.graph)
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
42 self.settings.solver.loadSamples()
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
43
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
44 class BestMatches(PrettyErrorHandler, cyclone.web.RequestHandler):
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
45 def post(self):
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
46 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
47 painting = body['painting']
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
48 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
49 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
50 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
51 outSettings = self.settings.solver.bestMatches(img, devs)
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
52 self.write(json.dumps({
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
53 'settings': outSettings.asList()
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
54 }))
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
55
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
56 class App(object):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
57 def __init__(self, show, session):
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
58 self.show = show
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
59 self.session = session
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
60
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
61 self.graph = SyncedGraph(networking.rdfdb.url, "paintServer")
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
62 self.graph.initiallySynced.addCallback(self.launch).addErrback(log.error)
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
63
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
64 self.stats = scales.collection('/', scales.PmfStat('solve'),
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
65 )
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
66
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
67 def launch(self, *args):
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
68
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
69 self.solver = light9.paint.solve.Solver(self.graph, sessions=[
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
70 L9['show/dance2017/capture/aura1/cap1876596'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
71 L9['show/dance2017/capture/aura2/cap1876792'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
72 L9['show/dance2017/capture/aura3/cap1877057'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
73 L9['show/dance2017/capture/aura4/cap1877241'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
74 L9['show/dance2017/capture/aura5/cap1877406'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
75 L9['show/dance2017/capture/q1/cap1874255'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
76 L9['show/dance2017/capture/q2/cap1873665'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
77 L9['show/dance2017/capture/q3/cap1876223'],
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
78 ])
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
79 self.solver.loadSamples()
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
80
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
81 self.cycloneApp = cyclone.web.Application(handlers=[
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
82 (r'/stats', StatsForCyclone),
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
83 (r'/solve', Solve),
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1585
diff changeset
84 (r'/bestMatches', BestMatches),
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
85 ],
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
86 debug=True,
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
87 graph=self.graph,
1575
0d2247ec8f49 bestMatch works in paintserver
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
88 solver=self.solver,
1521
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
89 stats=self.stats)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
90 reactor.listenTCP(networking.paintServer.port, self.cycloneApp)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
91 log.info("listening on %s" % networking.paintServer.port)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
92
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
93
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
94 if __name__ == "__main__":
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
95 parser = optparse.OptionParser()
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
96 parser.add_option('--show',
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
97 help='show URI, like http://light9.bigasterisk.com/show/dance2008',
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
98 default=showconfig.showUri())
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
99 parser.add_option("-v", "--verbose", action="store_true",
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
100 help="logging.DEBUG")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
101 parser.add_option("--twistedlog", action="store_true",
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
102 help="twisted logging")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
103 clientsession.add_option(parser)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
104 (options, args) = parser.parse_args()
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
105 log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
106
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
107 if not options.show:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
108 raise ValueError("missing --show http://...")
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
109
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
110 session = clientsession.getUri('paint', options)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
111
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
112 app = App(URIRef(options.show), session)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
113 if options.twistedlog:
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
114 from twisted.python import log as twlog
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
115 twlog.startLogging(sys.stderr)
15f296550447 start paintServer
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
116 reactor.run()