qmsk.dmx: move dmx-updating logic to qmsk.dmx.heads.Universe
authorTero Marttila <terom@paivola.fi>
Mon, 02 Jun 2014 18:27:08 +0300
changeset 88 c923295ee520
parent 87 2179a4e28aba
child 89 1b3cea759eff
qmsk.dmx: move dmx-updating logic to qmsk.dmx.heads.Universe
bin/dmx-web.py
qmsk/dmx/heads.py
qmsk/dmx/web.py
--- a/bin/dmx-web.py	Mon Jun 02 18:26:41 2014 +0300
+++ b/bin/dmx-web.py	Mon Jun 02 18:27:08 2014 +0300
@@ -9,16 +9,16 @@
 
 import optparse
 
-def dmx_heads (options) :
+def dmx_universe (options, dmx) :
     from qmsk.dmx import heads
     
-    return {
+    return heads.Universe(dmx, {
         'ledpar':   heads.Stairville_LEDPar56(1),
         'par1':     heads.Dimmer(5),
         'par2':     heads.Dimmer(6),
         'ledbar_roof':      heads.AmericanDJ_MegaTri60_Mode2(10),
         'ledbar_floor':     heads.AmericanDJ_MegaTri60_Mode2(20),
-    }
+    })
 
 def main (argv) :
     """
@@ -42,9 +42,11 @@
         #dmx.zero()
     else :
         dmx = None
+
+    universe = dmx_universe(options, dmx)
     
     # app
-    app = qmsk.dmx.web.DMXWebApplication(dmx, dmx_heads(options))
+    app = qmsk.dmx.web.DMXWebApplication(universe)
 
     pvl.web.args.main(options, app)
 
--- a/qmsk/dmx/heads.py	Mon Jun 02 18:26:41 2014 +0300
+++ b/qmsk/dmx/heads.py	Mon Jun 02 18:27:08 2014 +0300
@@ -77,3 +77,30 @@
         'alpha',
     ]
 
+
+class Universe (object) :
+    """
+        An universe of Heads for DMX output.
+    """
+
+    def __init__ (self, dmx, heads) :
+        self.dmx = dmx
+        self.heads = heads
+
+    def __getitem__ (self, name) :
+        return self.heads[name]
+
+    def __iter__ (self) :
+        return self.heads.iteritems()
+
+    def update (self, head, attrs) :
+        """
+            Update per-head values..
+        """
+
+        for attr, value in attrs.iteritems():
+            head[attr] = value
+        
+        # output
+        self.dmx.set(head.channel, *head)
+
--- a/qmsk/dmx/web.py	Mon Jun 02 18:26:41 2014 +0300
+++ b/qmsk/dmx/web.py	Mon Jun 02 18:27:08 2014 +0300
@@ -1,6 +1,7 @@
 import pvl.web
 from pvl.web import urls, html
 
+import collections
 import logging; log = logging.getLogger('qmsk.dmx.web')
 
 def colorize (x, red, green, blue, alpha=1.0) :
@@ -61,18 +62,17 @@
 
     def process (self) :
         if self.request.method == 'POST' :
+            update = collections.defaultdict(dict)
+
             for name, value in self.request.form.iteritems() :
                 head_name, attr = name.split('-', 1)
-                head = self.app.heads[head_name]
+                head = self.app.universe[head_name]
 
-                if value :
-                    value = int(value)
-                    
-                    # update head
-                    head[attr] = value
+                if value:
+                    update[head][attr] = int(value)
 
             # update dmx
-            self.app.update() 
+            self.app.update(update)
 
     def render_head (self, name, head) :
         if head.alpha() is None :
@@ -118,7 +118,7 @@
                         html.th(class_='dmx-control')(u"Control"),
                         html.th(class_='dmx-head-control')(u"Head Control"),
                     ),
-                    html.tbody(self.render_head(name, head) for name, head in sorted(self.app.heads.iteritems())),
+                    html.tbody(self.render_head(name, head) for name, head in sorted(self.app.universe)),
                 ),
 
                 html.button(type='submit', class_='btn btn-primary')("Go"),
@@ -130,16 +130,20 @@
         urls.rule('/',          Handler),
     ))
 
-    def __init__ (self, dmx, heads, **opts) :
-        super(DMXWebApplication, self).__init__(**opts)
-
-        self.dmx = dmx
-        self.heads = heads
+    def __init__ (self, universe, **opts) :
+        """
+            universe    - qmsk.dmx.heads.Universe
+        """
 
-    def update (self) :
-        if not self.dmx :
-            return
+        super(DMXWebApplication, self).__init__(**opts)
+        
+        self.universe = universe
 
-        for head in self.heads.itervalues() :
-            self.dmx[head.channel] = tuple(head)
+    def update (self, update) :
+        """
+            Update given {head: {attr: value}}
+        """
+        
+        for head, attrs in update.iteritems():
+            self.universe.update(head, attrs)