Mercurial > code > home > repos > light9
annotate light9/collector/collector.py @ 1541:c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Ignore-this: 6dda48ab8d89344e0c8271429c8175af
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Wed, 17 May 2017 07:39:21 +0000 |
parents | 37cbb245d93c |
children | 60e559cb1a5e |
rev | line source |
---|---|
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
1 from __future__ import division |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
2 import time |
1289
5a4e74f1e36a
Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents:
1288
diff
changeset
|
3 import logging |
1448
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
4 from rdflib import Literal |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
5 from light9.namespaces import L9, RDF |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
6 from light9.collector.output import setListElem |
1302
24357038de9f
resolver does per-channel max() for color attributes
Drew Perttula <drewp@bigasterisk.com>
parents:
1300
diff
changeset
|
7 from light9.collector.device import toOutputAttrs, resolve |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
8 |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
9 # types only |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
10 from rdflib import Graph, URIRef |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
11 from typing import List, Dict, Tuple, Any, TypeVar, Generic |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
12 from light9.collector.output import Output |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
13 |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
14 ClientType = TypeVar('ClientType') |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
15 ClientSessionType = TypeVar('ClientSessionType') |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
16 |
1289
5a4e74f1e36a
Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents:
1288
diff
changeset
|
17 log = logging.getLogger('collector') |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
18 |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
19 def outputMap(graph, outputs): |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
20 # type: (Graph, List[Output]) -> Dict[Tuple[URIRef, URIRef], Tuple[Output, int]] |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
21 """From rdf config graph, compute a map of |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
22 (device, outputattr) : (output, index) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
23 that explains which output index to set for any device update. |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 """ |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 ret = {} |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
27 outputByUri = {} # universeUri : output |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 for out in outputs: |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
29 outputByUri[out.uri] = out |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
31 for dc in graph.subjects(RDF.type, L9['DeviceClass']): |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
32 log.info('mapping DeviceClass %s', dc) |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
33 for dev in graph.subjects(RDF.type, dc): |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
34 log.info(' mapping device %s', dev) |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
35 universe = graph.value(dev, L9['dmxUniverse']) |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
36 try: |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
37 output = outputByUri[universe] |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
38 except Exception: |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
39 log.warn('dev %r :dmxUniverse %r', dev, universe) |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
40 raise |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
41 dmxBase = int(graph.value(dev, L9['dmxBase']).toPython()) |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
42 for row in graph.objects(dc, L9['attr']): |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
43 outputAttr = graph.value(row, L9['outputAttr']) |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
44 offset = int(graph.value(row, L9['dmxOffset']).toPython()) |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
45 index = dmxBase + offset - 1 |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
46 ret[(dev, outputAttr)] = (output, index) |
1541
c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents:
1506
diff
changeset
|
47 log.debug(' map %s to %s,%s', outputAttr, output, index) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
48 return ret |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
50 class Collector(Generic[ClientType, ClientSessionType]): |
1541
c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents:
1506
diff
changeset
|
51 def __init__(self, graph, outputs, listeners=None, clientTimeoutSec=10): |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
52 # type: (Graph, List[Output], float) -> None |
1307
8863b4485fd4
collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents:
1302
diff
changeset
|
53 self.graph = graph |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 self.outputs = outputs |
1541
c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents:
1506
diff
changeset
|
55 self.listeners = listeners |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
56 self.clientTimeoutSec = clientTimeoutSec |
1307
8863b4485fd4
collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents:
1302
diff
changeset
|
57 |
8863b4485fd4
collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents:
1302
diff
changeset
|
58 self.graph.addHandler(self.rebuildOutputMap) |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
59 |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
60 # client : (session, time, {(dev,devattr): latestValue}) |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
61 self.lastRequest = {} # type: Dict[ClientType, Tuple[ClientSessionType, float, Dict[Tuple[URIRef, URIRef], float]]] |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
62 |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
63 # (dev, devAttr): value to use instead of 0 |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
64 self.stickyAttrs = {} # type: Dict[Tuple[URIRef, URIRef], float] |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
65 |
1307
8863b4485fd4
collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents:
1302
diff
changeset
|
66 def rebuildOutputMap(self): |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
67 self.outputMap = outputMap(self.graph, self.outputs) # (device, outputattr) : (output, index) |
1307
8863b4485fd4
collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents:
1302
diff
changeset
|
68 self.deviceType = {} # uri: type that's a subclass of Device |
1448
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
69 self.remapOut = {} # (device, deviceAttr) : (start, end) |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
70 for dc in self.graph.subjects(RDF.type, L9['DeviceClass']): |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
71 for dev in self.graph.subjects(RDF.type, dc): |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
72 self.deviceType[dev] = dc |
1307
8863b4485fd4
collector uses rdfdb
Drew Perttula <drewp@bigasterisk.com>
parents:
1302
diff
changeset
|
73 |
1448
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
74 for remap in self.graph.objects(dev, L9['outputAttrRange']): |
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
75 attr = self.graph.value(remap, L9['outputAttr']) |
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
76 start = float(self.graph.value(remap, L9['start'])) |
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
77 end = float(self.graph.value(remap, L9['end'])) |
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
78 self.remapOut[(dev, attr)] = start, end |
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
79 |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
80 def _forgetStaleClients(self, now): |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
81 # type: (float) -> None |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
82 staleClients = [] |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
83 for c, (_, t, _2) in self.lastRequest.iteritems(): |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
84 if t < now - self.clientTimeoutSec: |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
85 staleClients.append(c) |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
86 for c in staleClients: |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
87 log.info('forgetting stale client %r', c) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
88 del self.lastRequest[c] |
1300
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
89 |
1372
f427801da9f6
collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents:
1307
diff
changeset
|
90 def resolvedSettingsDict(self, settingsList): |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
91 # type: (List[Tuple[URIRef, URIRef, float]]) -> Dict[Tuple[URIRef, URIRef], float] |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
92 out = {} # type: Dict[Tuple[URIRef, URIRef], float] |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
93 for d, da, v in settingsList: |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
94 if (d, da) in out: |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
95 out[(d, da)] = resolve(d, da, [out[(d, da)], v]) |
1372
f427801da9f6
collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents:
1307
diff
changeset
|
96 else: |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
97 out[(d, da)] = v |
1372
f427801da9f6
collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents:
1307
diff
changeset
|
98 return out |
f427801da9f6
collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents:
1307
diff
changeset
|
99 |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
100 def _warnOnLateRequests(self, client, now, sendTime): |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
101 requestLag = now - sendTime |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
102 if requestLag > .1: |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
103 log.warn('collector.setAttrs from %s is running %.1fms after the request was made', |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
104 client, requestLag * 1000) |
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
105 |
1489
31612b323436
pass packet creation time so collector can see how much lag there is
Drew Perttula <drewp@bigasterisk.com>
parents:
1475
diff
changeset
|
106 def setAttrs(self, client, clientSession, settings, sendTime): |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
107 """ |
1300
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
108 settings is a list of (device, attr, value). These attrs are |
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
109 device attrs. We resolve conflicting values, process them into |
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
110 output attrs, and call Output.update/Output.flush to send the |
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
111 new outputs. |
1289
5a4e74f1e36a
Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents:
1288
diff
changeset
|
112 |
5a4e74f1e36a
Fixed client session clearing bugs.
Drew Perttula <drewp@bigasterisk.com>
parents:
1288
diff
changeset
|
113 Call with settings=[] to ping us that your session isn't dead. |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
114 """ |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
115 now = time.time() |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
116 self._warnOnLateRequests(client, now, sendTime) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
117 |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
118 self._forgetStaleClients(now) |
1446
ba6594263fa7
collector: client's prev settings are no longer mixed with its new request. this breaks live page
drewp@bigasterisk.com
parents:
1437
diff
changeset
|
119 |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
120 uniqueSettings = self.resolvedSettingsDict(settings) |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
121 self.lastRequest[client] = (clientSession, now, uniqueSettings) |
1300
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
122 |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
123 deviceAttrs = {} # device: {deviceAttr: value} |
1372
f427801da9f6
collector properly merges repeated attr settings in the same message
Drew Perttula <drewp@bigasterisk.com>
parents:
1307
diff
changeset
|
124 for _, _, lastSettings in self.lastRequest.itervalues(): |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
125 for (device, deviceAttr), value in lastSettings.iteritems(): |
1448
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
126 if (device, deviceAttr) in self.remapOut: |
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
127 start, end = self.remapOut[(device, deviceAttr)] |
931d2dafca12
new feature: values can have their range remapped in the device processing
drewp@bigasterisk.com
parents:
1446
diff
changeset
|
128 value = Literal(start + float(value) * (end - start)) |
1450
ddb7622698a8
don't mix remapped values with unremapped ones
drewp@bigasterisk.com
parents:
1448
diff
changeset
|
129 |
ddb7622698a8
don't mix remapped values with unremapped ones
drewp@bigasterisk.com
parents:
1448
diff
changeset
|
130 attrs = deviceAttrs.setdefault(device, {}) |
ddb7622698a8
don't mix remapped values with unremapped ones
drewp@bigasterisk.com
parents:
1448
diff
changeset
|
131 if deviceAttr in attrs: |
ddb7622698a8
don't mix remapped values with unremapped ones
drewp@bigasterisk.com
parents:
1448
diff
changeset
|
132 value = resolve(device, deviceAttr, [attrs[deviceAttr], value]) |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
133 attrs[deviceAttr] = value |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
134 # list should come from the graph. these are attrs |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
135 # that should default to holding the last position, |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
136 # not going to 0. |
1475 | 137 if deviceAttr in [L9['rx'], L9['ry'], L9['zoom'], L9['focus']]: |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
138 self.stickyAttrs[(device, deviceAttr)] = value |
1300
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
139 |
1470
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
140 |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
141 # e.g. don't let an unspecified rotation go to 0 |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
142 for (d, da), v in self.stickyAttrs.iteritems(): |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
143 daDict = deviceAttrs.setdefault(d, {}) |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
144 if da not in daDict: |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
145 daDict[da] = v |
b1d8abc96f06
don't run rotations to zero when no one is requesting them.
drewp@bigasterisk.com
parents:
1450
diff
changeset
|
146 |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
147 outputAttrs = {} # device: {outputAttr: value} |
1300
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
148 for d in deviceAttrs: |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
149 try: |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
150 devType = self.deviceType[d] |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
151 except KeyError: |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
152 log.warn("request for output to unconfigured device %s" % d) |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
153 continue |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
154 outputAttrs[d] = toOutputAttrs(devType, deviceAttrs[d]) |
1541
c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents:
1506
diff
changeset
|
155 if self.listeners: |
c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents:
1506
diff
changeset
|
156 self.listeners.outputAttrsSet(d, outputAttrs[d], self.outputMap) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
157 |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
158 pendingOut = {} # output : values |
1437 | 159 for out in self.outputs: |
160 pendingOut[out] = [0] * out.numChannels | |
1506
37cbb245d93c
fix tests. add logging, some mypy types.
Drew Perttula <drewp@bigasterisk.com>
parents:
1492
diff
changeset
|
161 |
1300
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
162 for device, attrs in outputAttrs.iteritems(): |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
163 for outputAttr, value in attrs.iteritems(): |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
164 self.setAttr(device, outputAttr, value, pendingOut) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
165 |
1436
d0d5900a8031
collector run the blocking dmx output calls in another thread, so they don't add delay to our http server
drewp@bigasterisk.com
parents:
1426
diff
changeset
|
166 dt1 = 1000 * (time.time() - now) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
167 self.flush(pendingOut) |
1436
d0d5900a8031
collector run the blocking dmx output calls in another thread, so they don't add delay to our http server
drewp@bigasterisk.com
parents:
1426
diff
changeset
|
168 dt2 = 1000 * (time.time() - now) |
d0d5900a8031
collector run the blocking dmx output calls in another thread, so they don't add delay to our http server
drewp@bigasterisk.com
parents:
1426
diff
changeset
|
169 if dt1 > 10: |
1541
c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents:
1506
diff
changeset
|
170 log.warn("slow setAttrs: %.1fms -> flush -> %.1fms. lr %s da %s oa %s" % ( |
1446
ba6594263fa7
collector: client's prev settings are no longer mixed with its new request. this breaks live page
drewp@bigasterisk.com
parents:
1437
diff
changeset
|
171 dt1, dt2, len(self.lastRequest), len(deviceAttrs), len(outputAttrs) |
1541
c1bf296b0a74
collector uses cyclone and gets a web ui showing output attrs
Drew Perttula <drewp@bigasterisk.com>
parents:
1506
diff
changeset
|
172 )) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
173 |
1416
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
174 def setAttr(self, device, outputAttr, value, pendingOut): |
ab7b40d20af0
rewrite theaterConfig to a better data format
Drew Perttula <drewp@bigasterisk.com>
parents:
1412
diff
changeset
|
175 output, index = self.outputMap[(device, outputAttr)] |
1437 | 176 outList = pendingOut[output] |
1300
d51014267bfd
move device-specific code out of collector. resolver isn't done yet. live.html can edit colors
Drew Perttula <drewp@bigasterisk.com>
parents:
1289
diff
changeset
|
177 setListElem(outList, index, value, combine=max) |
1288
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
178 |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
179 def flush(self, pendingOut): |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
180 """write any changed outputs""" |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
181 for out, vals in pendingOut.iteritems(): |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
182 out.update(vals) |
5e76c8fd8a03
rewrite dmx outputter to a new service
Drew Perttula <drewp@bigasterisk.com>
parents:
diff
changeset
|
183 out.flush() |