Changeset - f427801da9f6
[Not reviewed]
default
0 3 0
Drew Perttula - 9 years ago 2016-06-07 10:51:23
drewp@bigasterisk.com
collector properly merges repeated attr settings in the same message
Ignore-this: a03dcc65eb0d9300cecf3153cd2a58d3
3 files changed with 34 insertions and 6 deletions:
0 comments (0 inline, 0 general)
bin/collector
Show inline comments
 
@@ -4,12 +4,13 @@ from rdflib import Graph, URIRef, Litera
 
from twisted.internet import reactor
 
from twisted.web.server import Site
 
from txzmq import ZmqEndpoint, ZmqFactory, ZmqPullConnection
 
import json
 
import logging
 
import klein
 
import optparse
 
from greplin import scales
 
from greplin.scales.twistedweb import StatsResource
 

	
 
from run_local import log
 
from light9.collector.output import EnttecDmx, Udmx
 
from light9.collector.collector import Collector
 
@@ -72,14 +73,17 @@ def launch(graph):
 
                      interface='::')
 
    log.info('serving http on %s, zmq on %s', networking.collector.port,
 
             networking.collectorZmq.port)
 
    
 
    
 
def main():
 
    log.setLevel(logging.DEBUG)
 

	
 
    parser = optparse.OptionParser()
 
    parser.add_option("-v", "--verbose", action="store_true",
 
                      help="logging.DEBUG")
 
    (options, args) = parser.parse_args()
 
    log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
 

	
 
    graph = SyncedGraph(networking.rdfdb.url, "collector")
 

	
 
    graph.initiallySynced.addCallback(lambda _: launch(graph))
 
    reactor.run()
 

	
light9/collector/collector.py
Show inline comments
 
@@ -58,12 +58,21 @@ class Collector(object):
 
        for c, (_, t, _) in self.lastRequest.iteritems():
 
            if t < now - self.clientTimeoutSec:
 
                staleClients.append(c)
 
        for c in staleClients:
 
            del self.lastRequest[c]
 

	
 
    def resolvedSettingsDict(self, settingsList):
 
        out = {}
 
        for d, a, v in settingsList:
 
            if (d, a) in out:
 
                out[(d, a)] = resolve(d, a, [out[(d, a)], v])
 
            else:
 
                out[(d, a)] = v
 
        return out
 

	
 
    def setAttrs(self, client, clientSession, settings):
 
        """
 
        settings is a list of (device, attr, value). These attrs are
 
        device attrs. We resolve conflicting values, process them into
 
        output attrs, and call Output.update/Output.flush to send the
 
        new outputs.
 
@@ -77,20 +86,19 @@ class Collector(object):
 
        if row is not None:
 
            sess, _, prevClientSettings = row
 
            if sess != clientSession:
 
                prevClientSettings = {}
 
        else:
 
            prevClientSettings = {}
 
        for d, a, v in settings:
 
            prevClientSettings[(d, a)] = v
 
        prevClientSettings.update(self.resolvedSettingsDict(settings))
 
        self.lastRequest[client] = (clientSession, now, prevClientSettings)
 

	
 

	
 
        deviceAttrs = {} # device: {attr: value}
 
        for _, _, settings in self.lastRequest.itervalues():
 
            for (device, attr), value in settings.iteritems():
 
        for _, _, lastSettings in self.lastRequest.itervalues():
 
            for (device, attr), value in lastSettings.iteritems():
 
                attrs = deviceAttrs.setdefault(device, {})
 
                if attr in attrs:
 
                    value = resolve(device, attr, [attrs[attr], value])
 
                attrs[attr] = value
 

	
 
        outputAttrs = {} # device: {attr: value}
light9/collector/collector_test.py
Show inline comments
 
@@ -192,6 +192,22 @@ class TestCollector(unittest.TestCase):
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], .5)])
 
        c.setAttrs('client1', 'sess1', [(DEV['inst1'], L9['brightness'], 1)])
 
        c.setAttrs('client1', 'sess1', [(DEV['colorStrip'], L9['color'], '#00ff00')])
 

	
 
        self.assertEqual([[0, 255, 0, 215], 'flush'], self.udmx.updates)
 
        self.assertEqual([[127], 'flush', [255], 'flush', [255], 'flush'], self.dmx0.updates)
 

	
 
    def testRepeatedAttributesInOneRequestGetResolved(self):
 
        c = Collector(self.config, outputs=[self.dmx0, self.udmx])
 
        
 
        c.setAttrs('client1', 'sess1', [
 
            (DEV['inst1'], L9['brightness'], .5),
 
            (DEV['inst1'], L9['brightness'], .3),
 
        ])
 
        self.assertEqual([[127], 'flush'], self.dmx0.updates)
 

	
 
        c.setAttrs('client1', 'sess1', [
 
            (DEV['inst1'], L9['brightness'], .3),
 
            (DEV['inst1'], L9['brightness'], .5),
 
        ])
 
        self.assertEqual([[127], 'flush', [127], 'flush'], self.dmx0.updates)
 

	
0 comments (0 inline, 0 general)