annotate bin/collector @ 1859:f066d6e874db

2to3 with these fixers: all idioms set_literal Ignore-this: cbd28518218c2f0ddce8c4f92d3b8b33
author drewp@bigasterisk.com
date Wed, 22 May 2019 00:08:22 +0000
parents 7772cc48e016
children 5bcb950024af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!bin/python
1590
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
2 """
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
3 Collector receives device attrs from multiple senders, combines
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
4 them, and sends output attrs to hardware. The combining part has
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
5 custom code for some attributes.
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
6
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
7 Input can be over http or zmq.
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
8 """
8268224c1b23 WIP support for choice/enum attrs
drewp@bigasterisk.com
parents: 1564
diff changeset
9
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
10
1697
5c04a54df635 fix startup (this might be breaking mypy)
Drew Perttula <drewp@bigasterisk.com>
parents: 1692
diff changeset
11 from run_local import log
5c04a54df635 fix startup (this might be breaking mypy)
Drew Perttula <drewp@bigasterisk.com>
parents: 1692
diff changeset
12
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
13 from rdflib import URIRef, Literal
1493
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
14 from twisted.internet import reactor, utils
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15 from txzmq import ZmqEndpoint, ZmqFactory, ZmqPullConnection
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
16 import json
1289
5a4e74f1e36a Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents: 1288
diff changeset
17 import logging
1372
f427801da9f6 collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents: 1307
diff changeset
18 import optparse
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
19 import time
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
20 import traceback
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
21 import cyclone.web, cyclone.websocket
1289
5a4e74f1e36a Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents: 1288
diff changeset
22 from greplin import scales
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
24 from lib.cycloneerr import PrettyErrorHandler
1605
64c1bcff604e dummyoutput for collector
drewp@bigasterisk.com
parents: 1596
diff changeset
25 from light9.collector.output import EnttecDmx, Udmx, DummyOutput
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
26 from light9.collector.collector import Collector
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
27 from light9.namespaces import L9
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
28 from light9 import networking
1692
6fa4288da8a6 rdfdb is its own package now
drewp@bigasterisk.com
parents: 1630
diff changeset
29 from rdfdb.syncedgraph import SyncedGraph
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
30 from light9.greplin_cyclone import StatsForCyclone
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
31
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
32
1491
c0742e710eeb refactor collector json parsing
drewp@bigasterisk.com
parents: 1490
diff changeset
33 def parseJsonMessage(msg):
1492
ce97f298bfb8 restore zmq transport to collector
drewp@bigasterisk.com
parents: 1491
diff changeset
34 body = json.loads(msg)
1491
c0742e710eeb refactor collector json parsing
drewp@bigasterisk.com
parents: 1490
diff changeset
35 settings = []
c0742e710eeb refactor collector json parsing
drewp@bigasterisk.com
parents: 1490
diff changeset
36 for device, attr, value in body['settings']:
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
37 if isinstance(value, str) and value.startswith('http'):
1595
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1590
diff changeset
38 value = URIRef(value)
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1590
diff changeset
39 else:
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1590
diff changeset
40 value = Literal(value)
013cbd7a0f08 choice-type attrs in live
Drew Perttula <drewp@bigasterisk.com>
parents: 1590
diff changeset
41 settings.append((URIRef(device), URIRef(attr), value))
1491
c0742e710eeb refactor collector json parsing
drewp@bigasterisk.com
parents: 1490
diff changeset
42 return body['client'], body['clientSession'], settings, body['sendTime']
c0742e710eeb refactor collector json parsing
drewp@bigasterisk.com
parents: 1490
diff changeset
43
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
44
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
45 def startZmq(port, collector):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
46 stats = scales.collection('/zmqServer', scales.PmfStat('setAttr'))
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
47
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
48 zf = ZmqFactory()
1492
ce97f298bfb8 restore zmq transport to collector
drewp@bigasterisk.com
parents: 1491
diff changeset
49 addr = 'tcp://*:%s' % port
ce97f298bfb8 restore zmq transport to collector
drewp@bigasterisk.com
parents: 1491
diff changeset
50 log.info('creating zmq endpoint at %r', addr)
ce97f298bfb8 restore zmq transport to collector
drewp@bigasterisk.com
parents: 1491
diff changeset
51 e = ZmqEndpoint('bind', addr)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
52
1809
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
53 class Pull(ZmqPullConnection):
1826
d6ec468112cb not sure highwatermark setting did what i wanted
drewp@bigasterisk.com
parents: 1822
diff changeset
54 #highWaterMark = 3
1809
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
55 def onPull(self, message):
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
56 with stats.setAttr.time():
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
57 # todo: new compressed protocol where you send all URIs up
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
58 # front and then use small ints to refer to devices and
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
59 # attributes in subsequent requests.
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
60 client, clientSession, settings, sendTime = parseJsonMessage(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
61 message[0])
1809
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
62 collector.setAttrs(client, clientSession, settings, sendTime)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
63
1809
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
64 s = Pull(zf, e)
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
65
1289
5a4e74f1e36a Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents: 1288
diff changeset
66
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
67 class WebListeners(object):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
68
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
69 def __init__(self):
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
70 self.clients = []
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
71 self.pendingMessageForDev = {} # dev: (attrs, outputmap)
1596
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
72 self.lastFlush = 0
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
73
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
74 def addClient(self, client):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
75 self.clients.append([client, {}]) # seen = {dev: attrs}
1806
5668ad92a98e bug in collector log
Drew Perttula <drewp@bigasterisk.com>
parents: 1801
diff changeset
76 log.info('added client %s %s', len(self.clients), client)
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
77
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
78 def delClient(self, client):
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
79 self.clients = [[c, t] for c, t in self.clients if c != client]
1543
c8cffe82b537 collector gui updates
Drew Perttula <drewp@bigasterisk.com>
parents: 1541
diff changeset
80 log.info('delClient %s, %s left', client, len(self.clients))
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
81
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
82 def outputAttrsSet(self, dev, attrs, outputMap):
1596
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
83 """called often- don't be slow"""
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
84
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
85 self.pendingMessageForDev[dev] = (attrs, outputMap)
1809
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
86 try:
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
87 self._flush()
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
88 except Exception:
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
89 traceback.print_exc()
778c67ab70c9 set zmq highWaterMark to dump stale messages, especially those sent when collector isn't running
drewp@bigasterisk.com
parents: 1806
diff changeset
90 raise
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
91
1596
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
92 def _flush(self):
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
93 now = time.time()
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
94 if now < self.lastFlush + .05 or not self.clients:
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
95 return
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
96 self.lastFlush = now
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
97
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
98 while self.pendingMessageForDev:
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
99 dev, (attrs, outputMap) = self.pendingMessageForDev.popitem()
1564
f2e6d96d02de minor comments and refactors, I think, except the removal of 'row update' logging made a big perf difference
Drew Perttula <drewp@bigasterisk.com>
parents: 1543
diff changeset
100
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
101 msg = None # lazy, since makeMsg is slow
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
102
1596
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
103 # this omits repeats, but can still send many
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
104 # messages/sec. Not sure if piling up messages for the browser
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
105 # could lead to slowdowns in the real dmx output.
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
106 for client, seen in self.clients:
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
107 if seen.get(dev) == attrs:
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
108 continue
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
109 if msg is None:
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
110 msg = self.makeMsg(dev, attrs, outputMap)
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
111
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
112 seen[dev] = attrs
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
113 client.sendMessage(msg)
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
114
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
115 def makeMsg(self, dev, attrs, outputMap):
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
116 attrRows = []
1859
f066d6e874db 2to3 with these fixers: all idioms set_literal
drewp@bigasterisk.com
parents: 1858
diff changeset
117 for attr, val in list(attrs.items()):
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
118 output, index = outputMap[(dev, attr)]
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
119 attrRows.append({
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
120 'attr': attr.rsplit('/')[-1],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
121 'val': val,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
122 'chan': (output.shortId(), index + 1)
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
123 })
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
124 attrRows.sort(key=lambda r: r['chan'])
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
125 for row in attrRows:
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
126 row['chan'] = '%s %s' % (row['chan'][0], row['chan'][1])
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
127
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
128 msg = json.dumps({'outputAttrsSet': {
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
129 'dev': dev,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
130 'attrs': attrRows
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
131 }},
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
132 sort_keys=True)
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
133 return msg
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
134
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
135
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
136 class Updates(cyclone.websocket.WebSocketHandler):
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
137
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
138 def connectionMade(self, *args, **kwargs):
1543
c8cffe82b537 collector gui updates
Drew Perttula <drewp@bigasterisk.com>
parents: 1541
diff changeset
139 log.info('socket connect %s', self)
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
140 self.settings.listeners.addClient(self)
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
141
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
142 def connectionLost(self, reason):
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
143 self.settings.listeners.delClient(self)
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
144
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
145 def messageReceived(self, message):
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
146 json.loads(message)
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
147
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
148
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
149 stats = scales.collection('/webServer', scales.PmfStat('setAttr'))
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
150
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
151
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
152 class Attrs(PrettyErrorHandler, cyclone.web.RequestHandler):
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
153
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
154 def put(self):
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
155 with stats.setAttr.time():
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
156 client, clientSession, settings, sendTime = parseJsonMessage(
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
157 self.request.body)
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
158 self.settings.collector.setAttrs(client, clientSession, settings,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
159 sendTime)
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
160 self.set_status(202)
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
161
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
162
1490
649d482737e0 start of a collector --loadtest mode
Drew Perttula <drewp@bigasterisk.com>
parents: 1489
diff changeset
163 def launch(graph, doLoadTest=False):
1530
a5a44077c54c docs and error msgs
drewp@bigasterisk.com
parents: 1493
diff changeset
164 try:
a5a44077c54c docs and error msgs
drewp@bigasterisk.com
parents: 1493
diff changeset
165 # todo: drive outputs with config files
a5a44077c54c docs and error msgs
drewp@bigasterisk.com
parents: 1493
diff changeset
166 outputs = [
1822
0da5fcfe4ea5 collector debug logging
drewp@bigasterisk.com
parents: 1809
diff changeset
167 # EnttecDmx(L9['output/dmxA/'], '/dev/dmx3', 80),
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
168 Udmx(L9['output/dmxA/'], bus=5, numChannels=80),
1822
0da5fcfe4ea5 collector debug logging
drewp@bigasterisk.com
parents: 1809
diff changeset
169 #DummyOutput(L9['output/dmxA/'], 80),
0da5fcfe4ea5 collector debug logging
drewp@bigasterisk.com
parents: 1809
diff changeset
170 Udmx(L9['output/dmxB/'], bus=7, numChannels=500),
1530
a5a44077c54c docs and error msgs
drewp@bigasterisk.com
parents: 1493
diff changeset
171 ]
1799
0bb7b9df12e5 collector warnings and errors. the reactor.crash isn't working.
drewp@bigasterisk.com
parents: 1798
diff changeset
172 except Exception:
0bb7b9df12e5 collector warnings and errors. the reactor.crash isn't working.
drewp@bigasterisk.com
parents: 1798
diff changeset
173 log.error("setting up outputs:")
1530
a5a44077c54c docs and error msgs
drewp@bigasterisk.com
parents: 1493
diff changeset
174 traceback.print_exc()
a5a44077c54c docs and error msgs
drewp@bigasterisk.com
parents: 1493
diff changeset
175 raise
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
176 listeners = WebListeners()
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
177 c = Collector(graph, outputs, listeners)
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
178
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
179 startZmq(networking.collectorZmq.port, c)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
180
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
181 reactor.listenTCP(networking.collector.port,
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
182 cyclone.web.Application(handlers=[
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
183 (r'/()', cyclone.web.StaticFileHandler, {
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
184 "path": "light9/collector/web",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
185 "default_filename": "index.html"
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
186 }),
1541
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
187 (r'/updates', Updates),
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
188 (r'/attrs', Attrs),
c1bf296b0a74 collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents: 1530
diff changeset
189 (r'/stats', StatsForCyclone),
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
190 ],
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
191 collector=c,
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
192 listeners=listeners),
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
193 interface='::')
1289
5a4e74f1e36a Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents: 1288
diff changeset
194 log.info('serving http on %s, zmq on %s', networking.collector.port,
5a4e74f1e36a Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents: 1288
diff changeset
195 networking.collectorZmq.port)
1490
649d482737e0 start of a collector --loadtest mode
Drew Perttula <drewp@bigasterisk.com>
parents: 1489
diff changeset
196 if doLoadTest:
1493
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
197 # in a subprocess since we don't want this client to be
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
198 # cooperating with the main event loop and only sending
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
199 # requests when there's free time
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
200 def afterWarmup():
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
201 log.info('running collector_loadtest')
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
202 d = utils.getProcessValue('bin/python',
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
203 ['bin/collector_loadtest.py'])
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
204
1493
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
205 def done(*a):
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
206 log.info('loadtest done')
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
207 reactor.stop()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
208
1493
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
209 d.addCallback(done)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
210
1493
4294ed82ee16 move collector_loadtest and arrange for collector to be able to run the test itself, but that last part isn't working. you can run collector and collector_loadtest in two shells, though
drewp@bigasterisk.com
parents: 1492
diff changeset
211 reactor.callLater(2, afterWarmup)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
212
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
213
1307
8863b4485fd4 collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1289
diff changeset
214 def main():
1372
f427801da9f6 collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents: 1307
diff changeset
215 parser = optparse.OptionParser()
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
216 parser.add_option("-v",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
217 "--verbose",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
218 action="store_true",
1372
f427801da9f6 collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents: 1307
diff changeset
219 help="logging.DEBUG")
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
220 parser.add_option("--loadtest",
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
221 action="store_true",
1490
649d482737e0 start of a collector --loadtest mode
Drew Perttula <drewp@bigasterisk.com>
parents: 1489
diff changeset
222 help="call myself with some synthetic load then exit")
1372
f427801da9f6 collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents: 1307
diff changeset
223 (options, args) = parser.parse_args()
f427801da9f6 collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents: 1307
diff changeset
224 log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
1307
8863b4485fd4 collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1289
diff changeset
225
1596
7d5d6e7bc526 collector web view speedups- don't json encode all devs all the time
Drew Perttula <drewp@bigasterisk.com>
parents: 1595
diff changeset
226 logging.getLogger('colormath').setLevel(logging.INFO)
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
227
1307
8863b4485fd4 collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1289
diff changeset
228 graph = SyncedGraph(networking.rdfdb.url, "collector")
8863b4485fd4 collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents: 1289
diff changeset
229
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
230 graph.initiallySynced.addCallback(lambda _: launch(graph, options.loadtest)
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
231 ).addErrback(lambda e: reactor.crash())
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
232 reactor.run()
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
233
1858
7772cc48e016 reformat all python
drewp@bigasterisk.com
parents: 1826
diff changeset
234
1288
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
235 if __name__ == '__main__':
5e76c8fd8a03 rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
236 main()