qmsk/dmx/web.py
changeset 88 c923295ee520
parent 84 9ebf1a2cee3a
child 96 e6dfc98ec50f
equal deleted inserted replaced
87:2179a4e28aba 88:c923295ee520
     1 import pvl.web
     1 import pvl.web
     2 from pvl.web import urls, html
     2 from pvl.web import urls, html
     3 
     3 
       
     4 import collections
     4 import logging; log = logging.getLogger('qmsk.dmx.web')
     5 import logging; log = logging.getLogger('qmsk.dmx.web')
     5 
     6 
     6 def colorize (x, red, green, blue, alpha=1.0) :
     7 def colorize (x, red, green, blue, alpha=1.0) :
     7     return x(style='background-color: rgba({red}, {green}, {blue}, {alpha:0.2f})'.format(red=red, green=green, blue=blue, alpha=alpha))
     8     return x(style='background-color: rgba({red}, {green}, {blue}, {alpha:0.2f})'.format(red=red, green=green, blue=blue, alpha=alpha))
     8 
     9 
    59     # test
    60     # test
    60     TITLE = u"DMX Control"
    61     TITLE = u"DMX Control"
    61 
    62 
    62     def process (self) :
    63     def process (self) :
    63         if self.request.method == 'POST' :
    64         if self.request.method == 'POST' :
       
    65             update = collections.defaultdict(dict)
       
    66 
    64             for name, value in self.request.form.iteritems() :
    67             for name, value in self.request.form.iteritems() :
    65                 head_name, attr = name.split('-', 1)
    68                 head_name, attr = name.split('-', 1)
    66                 head = self.app.heads[head_name]
    69                 head = self.app.universe[head_name]
    67 
    70 
    68                 if value :
    71                 if value:
    69                     value = int(value)
    72                     update[head][attr] = int(value)
    70                     
       
    71                     # update head
       
    72                     head[attr] = value
       
    73 
    73 
    74             # update dmx
    74             # update dmx
    75             self.app.update() 
    75             self.app.update(update)
    76 
    76 
    77     def render_head (self, name, head) :
    77     def render_head (self, name, head) :
    78         if head.alpha() is None :
    78         if head.alpha() is None :
    79             head_input = head_slider = None
    79             head_input = head_slider = None
    80         else :
    80         else :
   116                         html.th(class_='dmx-head')(u"Head"),
   116                         html.th(class_='dmx-head')(u"Head"),
   117                         html.th(class_='dmx-value')(u"DMX"),
   117                         html.th(class_='dmx-value')(u"DMX"),
   118                         html.th(class_='dmx-control')(u"Control"),
   118                         html.th(class_='dmx-control')(u"Control"),
   119                         html.th(class_='dmx-head-control')(u"Head Control"),
   119                         html.th(class_='dmx-head-control')(u"Head Control"),
   120                     ),
   120                     ),
   121                     html.tbody(self.render_head(name, head) for name, head in sorted(self.app.heads.iteritems())),
   121                     html.tbody(self.render_head(name, head) for name, head in sorted(self.app.universe)),
   122                 ),
   122                 ),
   123 
   123 
   124                 html.button(type='submit', class_='btn btn-primary')("Go"),
   124                 html.button(type='submit', class_='btn btn-primary')("Go"),
   125             )
   125             )
   126         )
   126         )
   128 class DMXWebApplication (pvl.web.Application) :
   128 class DMXWebApplication (pvl.web.Application) :
   129     URLS = urls.Map((
   129     URLS = urls.Map((
   130         urls.rule('/',          Handler),
   130         urls.rule('/',          Handler),
   131     ))
   131     ))
   132 
   132 
   133     def __init__ (self, dmx, heads, **opts) :
   133     def __init__ (self, universe, **opts) :
       
   134         """
       
   135             universe    - qmsk.dmx.heads.Universe
       
   136         """
       
   137 
   134         super(DMXWebApplication, self).__init__(**opts)
   138         super(DMXWebApplication, self).__init__(**opts)
       
   139         
       
   140         self.universe = universe
   135 
   141 
   136         self.dmx = dmx
   142     def update (self, update) :
   137         self.heads = heads
   143         """
       
   144             Update given {head: {attr: value}}
       
   145         """
       
   146         
       
   147         for head, attrs in update.iteritems():
       
   148             self.universe.update(head, attrs)
   138 
   149 
   139     def update (self) :
       
   140         if not self.dmx :
       
   141             return
       
   142 
       
   143         for head in self.heads.itervalues() :
       
   144             self.dmx[head.channel] = tuple(head)
       
   145