Mercurial > code > home > repos > light9
comparison bin/collector @ 1288:5e76c8fd8a03
rewrite dmx outputter to a new service
Ignore-this: cb2bcb14f7cae9059e515b1752ef8976
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Sun, 29 May 2016 10:17:26 +0000 |
parents | |
children | 5a4e74f1e36a |
comparison
equal
deleted
inserted
replaced
1287:1382cb43a3ca | 1288:5e76c8fd8a03 |
---|---|
1 #!bin/python | |
2 from __future__ import division | |
3 from rdflib import Graph, URIRef, Literal | |
4 from twisted.internet import reactor | |
5 from twisted.web.server import Site | |
6 from txzmq import ZmqEndpoint, ZmqFactory, ZmqPullConnection | |
7 import json | |
8 import klein | |
9 | |
10 import run_local | |
11 from light9.collector.output import EnttecDmx, Udmx | |
12 from light9.collector.collector import Collector | |
13 from light9.namespaces import L9 | |
14 from light9 import networking | |
15 | |
16 class WebServer(object): | |
17 app = klein.Klein() | |
18 def __init__(self, collector): | |
19 self.collector = collector | |
20 | |
21 @app.route('/attrs', methods=['PUT']) | |
22 def putAttrs(self, request): | |
23 body = json.load(request.content) | |
24 settings = [] | |
25 for device, attr, value in body['settings']: | |
26 settings.append((URIRef(device), URIRef(attr), Literal(value))) | |
27 self.collector.setAttrs(body['client'], | |
28 body['clientSession'], | |
29 settings) | |
30 request.setResponseCode(202) | |
31 | |
32 def startZmq(port, collector): | |
33 zf = ZmqFactory() | |
34 e = ZmqEndpoint('bind', 'tcp://*:%s' % port) | |
35 s = ZmqPullConnection(zf, e) | |
36 def onPull(message): | |
37 # todo: new compressed protocol where you send all URIs up | |
38 # front and then use small ints to refer to devices and | |
39 # attributes in subsequent requests. | |
40 message[0] | |
41 collector.setAttrs() | |
42 s.onPull = onPull | |
43 | |
44 def main(): | |
45 config = Graph() | |
46 # todo: replace with rdfdb's loaded graph, and notice changes | |
47 config.parse('show/dance2016/output.n3', format='n3') | |
48 | |
49 # todo: drive outputs with config files | |
50 outputs = [EnttecDmx(L9['output/dmx0/'], 70, '/dev/dmx0'), | |
51 Udmx(L9['output/udmx/'], 70)] | |
52 c = Collector(config, outputs) | |
53 | |
54 server = WebServer(c) | |
55 startZmq(networking.collectorZmq.port, c) | |
56 | |
57 reactor.listenTCP(networking.collector.port, | |
58 Site(server.app.resource()), | |
59 interface='::') | |
60 reactor.run() | |
61 | |
62 if __name__ == '__main__': | |
63 main() |