Changeset - 931d2dafca12
[Not reviewed]
default
0 3 0
drewp@bigasterisk.com - 9 years ago 2016-06-11 22:08:37
drewp@bigasterisk.com
new feature: values can have their range remapped in the device processing
Ignore-this: 542047e6307a304f2aa52a69d134ba21
3 files changed with 22 insertions and 1 deletions:
0 comments (0 inline, 0 general)
light9/collector/collector.py
Show inline comments
 
from __future__ import division
 
import time
 
import logging
 
from rdflib import Literal
 
from light9.namespaces import L9, RDF, DEV
 
from light9.collector.output import setListElem
 
from light9.collector.device import toOutputAttrs, resolve
 

	
 
log = logging.getLogger('collector')
 

	
 
@@ -39,16 +40,23 @@ class Collector(object):
 
        self.graph.addHandler(self.rebuildOutputMap)
 
        self.lastRequest = {} # client : (session, time, {(dev,attr): latestValue})
 

	
 
    def rebuildOutputMap(self):
 
        self.outputMap = outputMap(self.graph, self.outputs) # (device, attr) : (output, index)
 
        self.deviceType = {} # uri: type that's a subclass of Device
 
        self.remapOut = {} # (device, deviceAttr) : (start, end)
 
        for dc in self.graph.subjects(RDF.type, L9['DeviceClass']):
 
            for dev in self.graph.subjects(RDF.type, dc):
 
                self.deviceType[dev] = dc
 

	
 
                for remap in self.graph.objects(dev, L9['outputAttrRange']):
 
                    attr = self.graph.value(remap, L9['outputAttr'])
 
                    start = float(self.graph.value(remap, L9['start']))
 
                    end = float(self.graph.value(remap, L9['end']))
 
                    self.remapOut[(dev, attr)] = start, end
 

	
 
    def _forgetStaleClients(self, now):
 
        staleClients = []
 
        for c, (_, t, _) in self.lastRequest.iteritems():
 
            if t < now - self.clientTimeoutSec:
 
                staleClients.append(c)
 
        for c in staleClients:
 
@@ -87,20 +95,25 @@ class Collector(object):
 
        else: # client always provides all the nonzero settings it wants
 
            prevClientSettings = {}
 
            
 
        prevClientSettings.update(self.resolvedSettingsDict(settings))
 
        self.lastRequest[client] = (clientSession, now, prevClientSettings)
 

	
 

	
 
        # inputs that are omitted, implying a zero, should be added
 
        # back here if they would remap to something nonzero.
 
        
 
        deviceAttrs = {} # device: {deviceAttr: value}
 
        for _, _, lastSettings in self.lastRequest.itervalues():
 
            for (device, deviceAttr), value in lastSettings.iteritems():
 
                attrs = deviceAttrs.setdefault(device, {})
 
                if deviceAttr in attrs:
 
                    value = resolve(device, deviceAttr, [attrs[deviceAttr],
 
                                                         value])
 
                if (device, deviceAttr) in self.remapOut:
 
                    start, end = self.remapOut[(device, deviceAttr)]
 
                    value = Literal(start + float(value) * (end - start))
 
                attrs[deviceAttr] = value
 

	
 
        outputAttrs = {} # device: {outputAttr: value}
 
        for d in deviceAttrs:
 
            try:
 
                devType = self.deviceType[d]
light9/collector/device.py
Show inline comments
 
@@ -55,12 +55,14 @@ def resolve(deviceType, deviceAttr, valu
 
    
 
def toOutputAttrs(deviceType, deviceAttrSettings):
 
    """
 
    Given device attr settings like {L9['color']: Literal('#ff0000')},
 
    return a similar dict where the keys are output attrs (like
 
    L9['red']) and the values are suitable for Collector.setAttr
 

	
 
    :outputAttrRange happens before we get here.
 
    """
 
    def floatAttr(attr, default=0):
 
        out = deviceAttrSettings.get(attr)
 
        if out is None:
 
            return default
 
        return float(out.toPython())
show/dance2016/theaterLightConfig.n3
Show inline comments
 
@@ -18,14 +18,20 @@ dev:f2 a :SimpleDimmer; :dmxUniverse dmx
 
dev:f3 a :SimpleDimmer; :dmxUniverse dmxA:; :dmxBase 10 .
 

	
 
dev:cycR a :SimpleDimmer; :dmxUniverse dmxA:; :dmxBase 42 .
 
dev:cycL a :SimpleDimmer; :dmxUniverse dmxA:; :dmxBase 44 .
 

	
 
dev:q1 a :MacQuantum; :dmxUniverse udmxB:; :dmxBase 325 .
 
dev:q1 :outputAttrRange dev:q1rx . dev:q1rx :outputAttr :rx; :start 0.143; :end 0.271 .
 
dev:q1 :outputAttrRange dev:q1ry . dev:q1ry :outputAttr :ry; :start 0.71; :end 0.821 .
 
dev:q2 a :MacQuantum; :dmxUniverse udmxB:; :dmxBase 352 .
 
dev:q2 :outputAttrRange dev:q2rx . dev:q2rx :outputAttr :rx; :start 0.097; :end 0.227 .
 
dev:q2 :outputAttrRange dev:q2ry . dev:q2ry :outputAttr :ry; :start 0.739; :end 0.824 .
 
dev:q3 a :MacQuantum; :dmxUniverse udmxB:; :dmxBase 379 .
 
dev:q3 :outputAttrRange dev:q3rx . dev:q3rx :outputAttr :rx; :start 0.064; :end 0.198 .
 
dev:q3 :outputAttrRange dev:q3ry . dev:q3ry :outputAttr :ry; :start 0.765; :end 0.822 .
 

	
 
dev:aura1 a :MacAura; :dmxUniverse udmxB:; :dmxBase 406 .
 
dev:aura2 a :MacAura; :dmxUniverse udmxB:; :dmxBase 420 .
 
dev:aura3 a :MacAura; :dmxUniverse udmxB:; :dmxBase 434 .
 
dev:aura4 a :MacAura; :dmxUniverse udmxB:; :dmxBase 448 .
 
dev:aura5 a :MacAura; :dmxUniverse udmxB:; :dmxBase 462 .
0 comments (0 inline, 0 general)