changeset 1302:24357038de9f

resolver does per-channel max() for color attributes Ignore-this: 6bba98291492058d190ef4948f9ec7fc
author Drew Perttula <drewp@bigasterisk.com>
date Mon, 30 May 2016 20:30:43 +0000
parents c13e0705a011
children 052e31de680c
files light9/collector/collector.py light9/collector/device.py light9/collector/device_test.py
diffstat 3 files changed, 35 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/light9/collector/collector.py	Mon May 30 17:45:56 2016 +0000
+++ b/light9/collector/collector.py	Mon May 30 20:30:43 2016 +0000
@@ -3,7 +3,7 @@
 import logging
 from light9.namespaces import L9, RDF, DEV
 from light9.collector.output import setListElem
-from light9.collector.device import toOutputAttrs
+from light9.collector.device import toOutputAttrs, resolve
 
 log = logging.getLogger('collector')
 
@@ -85,8 +85,10 @@
         deviceAttrs = {} # device: {attr: value}
         for _, _, settings in self.lastRequest.itervalues():
             for (device, attr), value in settings.iteritems():
-                # resolving conflicts goes around here
-                deviceAttrs.setdefault(device, {})[attr] = value
+                attrs = deviceAttrs.setdefault(device, {})
+                if attr in attrs:
+                    value = resolve(device, attr, [attrs[attr], value])
+                attrs[attr] = value
 
         outputAttrs = {} # device: {attr: value}
         for d in deviceAttrs:
--- a/light9/collector/device.py	Mon May 30 17:45:56 2016 +0000
+++ b/light9/collector/device.py	Mon May 30 20:30:43 2016 +0000
@@ -2,7 +2,7 @@
 import logging
 import math
 from light9.namespaces import L9, RDF, DEV
-from webcolors import hex_to_rgb
+from webcolors import hex_to_rgb, rgb_to_hex
 
 log = logging.getLogger('device')
 
@@ -35,9 +35,15 @@
 def resolve(deviceType, deviceAttr, values):
     """
     return one value to use for this attr, given a set of them that
-    have come in simultaneously
+    have come in simultaneously. len(values) >= 1.
     """
-    raise NotImplementedError
+    if len(values) == 1:
+        return values[0]
+    if deviceAttr == L9['color']:
+        rgbs = [hex_to_rgb(v) for v in values]
+        return rgb_to_hex([max(*component) for component in zip(*rgbs)])
+        
+    return max(values)
     
 def toOutputAttrs(deviceType, deviceAttrSettings):
     """
@@ -55,7 +61,8 @@
             L9['blue']: b
             }
     elif deviceType == L9['Dimmer']:
-        return {L9['brightness']: _8bit(deviceAttrSettings.get(L9['brightness'], 0))}
+        return {L9['brightness']:
+                _8bit(deviceAttrSettings.get(L9['brightness'], 0))}
     elif deviceType == L9['Mini15']:
         inp = deviceAttrSettings
         rx8 = float(inp.get(L9['rx'], 0)) / 540 * 255
--- a/light9/collector/device_test.py	Mon May 30 17:45:56 2016 +0000
+++ b/light9/collector/device_test.py	Mon May 30 20:30:43 2016 +0000
@@ -2,7 +2,11 @@
 from rdflib import Literal
 from light9.namespaces import L9
 
-from light9.collector.device import toOutputAttrs
+from light9.collector.device import toOutputAttrs, resolve
+
+class TestUnknownDevice(unittest.TestCase):
+    def testFails(self):
+        self.assertRaises(NotImplementedError, toOutputAttrs, L9['bogus'], {})
 
 class TestColorStrip(unittest.TestCase):
     def testConvertDeviceToOutputAttrs(self):
@@ -29,7 +33,20 @@
     def testConvertRotation(self):
         out = toOutputAttrs(L9['Mini15'], {L9['rx']: Literal(90), L9['ry']: Literal(45)})
         self.assertEqual(42, out[L9['xRotation']])
-        self.assertEqual(31, out[L9['xFine']])
+        self.assertEqual(127, out[L9['xFine']])
         self.assertEqual(47, out[L9['yRotation']])
         self.assertEqual(51, out[L9['yFine']])
         self.assertEqual(0, out[L9['rotationSpeed']])
+        
+class TestResolve(unittest.TestCase):
+    def testMaxes1Color(self):
+        # do not delete - this one catches a bug in the rgb_to_hex(...) lines
+        self.assertEqual('#ff0300',
+                         resolve(None, L9['color'], ['#ff0300']))
+    def testMaxes2Colors(self):
+        self.assertEqual('#ff0400',
+                         resolve(None, L9['color'], ['#ff0300', '#000400']))
+    def testMaxes3Colors(self):
+        self.assertEqual('#112233',
+                         resolve(None, L9['color'], ['#110000', '#002200', '#000033']))
+