]> git.aero2k.de Git - sirius.git/commitdiff
feels super kludgy to stick the command sender into the app object like this... but...
authorMatt Webb <matt@interconnected.org>
Fri, 26 Sep 2014 21:55:44 +0000 (22:55 +0100)
committerMatt Webb <matt@interconnected.org>
Fri, 26 Sep 2014 21:55:44 +0000 (22:55 +0100)
app/__init__.py
app/core/command_sender.py [deleted file]
app/core/events.py
app/main/__init__.py
app/queues.py

index 215cca1dfb5555f5761dafe19293cd8f847195f3..62f17c758f676bcee797c0af8dadaa3ceafcb1e1 100755 (executable)
@@ -41,10 +41,12 @@ def create_app(config_name):
     
     #from .core import coresocket
     from .core.events import process_event
-    from .core.command_sender import CommandSender
+    #from .core.command_sender import CommandSender
 
+    from queues import CommandSender
     sender = CommandSender()
     sender.run()
+    app.sender = sender
 
     @sockets.route('/api/v1/connection') 
     def coresocket(ws):
diff --git a/app/core/command_sender.py b/app/core/command_sender.py
deleted file mode 100644 (file)
index f23dcab..0000000
+++ /dev/null
@@ -1,136 +0,0 @@
-from ..models.core import BridgeCommand, DeviceCommand
-
-import gevent
-
-class CommandSender(object):
-       """ Single object per process to send bridge and device commands
-       down the Websocket connection.
-       @TODO use Reddis to pass information around between processes.
-       """
-       
-       def __init__(self):
-               self.queues = {}  # ws: _CommandQueue
-               self.did_send_image = False # temporary
-       
-       def register(self, ws, bridge):
-               # when a power_on event is received from a bridge,
-               # register its websocket so that bridge (and devices
-               # connected to that bridge) can receive commands
-               for k, v in self.queues.items():
-                       if bridge.bridge_address == v.bridge:
-                               self.queues.pop(k)
-               q = _CommandQueue(bridge=bridge)
-               self.queues[ws] = q
-               # queue old bridge commands
-               commands = BridgeCommand.query.filter_by(state='ready', bridge_address=bridge.bridge_address).all()
-               for c in commands:
-                       q.queue_bridge_command(c)
-       
-       def device_connect(self, bridge, device):
-               # device can be connected only to one bridge at a time
-               qb = self.for_bridge(bridge)
-               for q in [qq for qq in self.queues.values() if qq != qb]:
-                       if q.has_device(device):
-                               q.remove_device(device)
-               qb.add_device(device)
-               # queue old device commands
-               commands = DeviceCommand.query.filter_by(state='ready', device_address=device.device_address).all()
-               for c in commands:
-                       qb.queue_device_command(c)
-       
-       def device_disconnect(self, bridge, device):
-           q = self.for_bridge(bridge)
-           q.remove_device(device)
-       
-       def for_bridge(self, bridge):
-               queues = [q for q in self.queues.values() if q.bridge == bridge.bridge_address]
-               if queues:
-                       return queues[0]
-               else:
-                       return None
-       
-       def for_device(self, device):
-               queues = [q for q in self.queues.values() if q.has_device(device)]
-               if queues:
-                       return queues[0]
-               else:
-                       return None
-       
-       def send_commands(self):
-               """ called periodically by run """
-               for ws, q in self.queues.items():
-                       for c in q.prepare_commands_to_send():
-                               ws.send(c)
-       
-       def _run(self):
-               while True:
-                       self.send_commands()
-                       gevent.sleep(0.5)
-       
-       def run(self):
-               """ spawns and loops, calling send_commands periodically """
-               gevent.spawn(self._run)
-       
-       
-class _CommandQueue(object):
-       """ There is one _CommandQueue per websocket connection,
-       and it handles the command queues and pending commands.
-       Uses bridge_address and device_address internally. """
-       
-       def __init__(self, bridge):
-               self.bridge = bridge.bridge_address
-               self.devices = set()
-               self.bridge_queue = []
-               self.device_queue = {} # device_address: [DeviceCommand]
-               self.bridge_pending = None
-               self.device_pending = {} # device_address: None|DeviceCommand
-           
-       def add_device(self, device):
-               da = device.device_address
-               if da not in self.devices:
-                       print "==> Adding device %s to queue for bridge %s" % (da, self.bridge)
-                       self.devices.add(da)
-                       self.device_queue[da] = []
-                       self.device_pending[da] = None
-       
-       def remove_device(self, device):
-               da = device.device_address
-               self.devices.remove(da)
-               self.device_queue.pop(da, None)
-               self.device_pending.pop(da, None)
-       
-       def has_device(self, device):
-               return device.device_address in self.devices
-       
-       def queue_bridge_command(self, command):
-               self.bridge_queue.insert(0, (command.id, command.to_json()))
-       
-       def queue_device_command(self, command):
-               da = command.device_address
-               self.device_queue[da].insert(0, (command.id, command.to_json()))
-       
-       def handle_bridge_response(self, command):
-               if self.bridge_pending[0] == command.id:
-                       self.bridge_pending = None
-               self.bridge_queue = [c for c in self.bridge_queue if c[0] != command.id]
-       
-       def handle_device_response(self, command):
-               da = command.device_address
-               if self.device_pending[da][0] == command.id:
-                       self.device_pending[da] = None
-               self.device_queue[da] = \
-                       [c for c in self.device_queue[da] if c[0] != command.id]
-       
-       def prepare_commands_to_send(self):
-               commands_to_send = []
-               if self.bridge_pending is None and len(self.bridge_queue) > 0:
-                       c = self.bridge_queue.pop()
-                       self.bridge_pending = c
-                       commands_to_send.append(c[1])
-               for da in self.devices:
-                       if self.device_pending[da] is None and \
-                         len(self.device_queue[da]) > 0:
-                               c = self.device_queue[da].pop()
-                               self.device_pending[da] = c
-                               commands_to_send.append(c[1])
-               return commands_to_send
\ No newline at end of file
index 2a1d24902b1c2548a5d483e7d143bbaa8f2d4255..107b99e563e69ab950eefd63c02505607f8ca93c 100644 (file)
@@ -55,8 +55,8 @@ def process_event(ws, event, sender):
                 if device is not None:
                     q = sender.for_device(device)
                     if q is not None:
-                        dc = commands.set_delivery_and_print(event['device_address'])
-                        q.queue_device_command(dc)
+                        #dc = commands.set_delivery_and_print(event['device_address'])
+                        #q.queue_device_command(dc)
                         sender.did_send_image = True
     elif event['type'] == 'BridgeEvent':
         # unwrap here. use device_connect and device_disconnect to
index 998fa37d3f590bf126528d5ef03c534c2a1cd366..b4a4f0537505a658ea376721f58fe6d9b23e2d49 100644 (file)
@@ -1,4 +1,4 @@
-from flask import Blueprint, render_template, flash
+from flask import Blueprint, render_template, flash, current_app
 from flask.ext.wtf import Form
 from wtforms import StringField, TextAreaField, BooleanField, SelectField,\
     SubmitField
@@ -23,9 +23,12 @@ def index():
 def device(device_address):
        device = Device.query.get_or_404(device_address)
        form = SendDeliveryForm()
+       sender = current_app.sender
        
        if form.validate_on_submit():
                flash('Form submitted')
-               #dc = set_delivery_and_print(device_address)
-               # now queue it, but where?
+               q = sender.for_device(device)
+               if q is not None:
+                       dc = set_delivery_and_print(device.device_address)
+                       q.queue_device_command(dc)
        return render_template('dump.html', dump=pprint.pformat(device), form=form)
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..38c179ee5d6be25fe1fecee6718b74cec3c74d39 100644 (file)
@@ -0,0 +1,136 @@
+from app.models.core import BridgeCommand, DeviceCommand
+
+import gevent
+
+class CommandSender(object):
+       """ Single object per process to send bridge and device commands
+       down the Websocket connection.
+       @TODO use Reddis to pass information around between processes.
+       """
+       
+       def __init__(self):
+               self.queues = {}  # ws: _CommandQueue
+               self.did_send_image = False # temporary
+       
+       def register(self, ws, bridge):
+               # when a power_on event is received from a bridge,
+               # register its websocket so that bridge (and devices
+               # connected to that bridge) can receive commands
+               for k, v in self.queues.items():
+                       if bridge.bridge_address == v.bridge:
+                               self.queues.pop(k)
+               q = _CommandQueue(bridge=bridge)
+               self.queues[ws] = q
+               # queue old bridge commands
+               commands = BridgeCommand.query.filter_by(state='ready', bridge_address=bridge.bridge_address).all()
+               for c in commands:
+                       q.queue_bridge_command(c)
+       
+       def device_connect(self, bridge, device):
+               # device can be connected only to one bridge at a time
+               qb = self.for_bridge(bridge)
+               for q in [qq for qq in self.queues.values() if qq != qb]:
+                       if q.has_device(device):
+                               q.remove_device(device)
+               qb.add_device(device)
+               # queue old device commands
+               commands = DeviceCommand.query.filter_by(state='ready', device_address=device.device_address).all()
+               for c in commands:
+                       qb.queue_device_command(c)
+       
+       def device_disconnect(self, bridge, device):
+           q = self.for_bridge(bridge)
+           q.remove_device(device)
+       
+       def for_bridge(self, bridge):
+               queues = [q for q in self.queues.values() if q.bridge == bridge.bridge_address]
+               if queues:
+                       return queues[0]
+               else:
+                       return None
+       
+       def for_device(self, device):
+               queues = [q for q in self.queues.values() if q.has_device(device)]
+               if queues:
+                       return queues[0]
+               else:
+                       return None
+       
+       def send_commands(self):
+               """ called periodically by run """
+               for ws, q in self.queues.items():
+                       for c in q.prepare_commands_to_send():
+                               ws.send(c)
+       
+       def _run(self):
+               while True:
+                       self.send_commands()
+                       gevent.sleep(0.5)
+       
+       def run(self):
+               """ spawns and loops, calling send_commands periodically """
+               gevent.spawn(self._run)
+       
+       
+class _CommandQueue(object):
+       """ There is one _CommandQueue per websocket connection,
+       and it handles the command queues and pending commands.
+       Uses bridge_address and device_address internally. """
+       
+       def __init__(self, bridge):
+               self.bridge = bridge.bridge_address
+               self.devices = set()
+               self.bridge_queue = []
+               self.device_queue = {} # device_address: [DeviceCommand]
+               self.bridge_pending = None
+               self.device_pending = {} # device_address: None|DeviceCommand
+           
+       def add_device(self, device):
+               da = device.device_address
+               if da not in self.devices:
+                       print "==> Adding device %s to queue for bridge %s" % (da, self.bridge)
+                       self.devices.add(da)
+                       self.device_queue[da] = []
+                       self.device_pending[da] = None
+       
+       def remove_device(self, device):
+               da = device.device_address
+               self.devices.remove(da)
+               self.device_queue.pop(da, None)
+               self.device_pending.pop(da, None)
+       
+       def has_device(self, device):
+               return device.device_address in self.devices
+       
+       def queue_bridge_command(self, command):
+               self.bridge_queue.insert(0, (command.id, command.to_json()))
+       
+       def queue_device_command(self, command):
+               da = command.device_address
+               self.device_queue[da].insert(0, (command.id, command.to_json()))
+       
+       def handle_bridge_response(self, command):
+               if self.bridge_pending[0] == command.id:
+                       self.bridge_pending = None
+               self.bridge_queue = [c for c in self.bridge_queue if c[0] != command.id]
+       
+       def handle_device_response(self, command):
+               da = command.device_address
+               if self.device_pending[da][0] == command.id:
+                       self.device_pending[da] = None
+               self.device_queue[da] = \
+                       [c for c in self.device_queue[da] if c[0] != command.id]
+       
+       def prepare_commands_to_send(self):
+               commands_to_send = []
+               if self.bridge_pending is None and len(self.bridge_queue) > 0:
+                       c = self.bridge_queue.pop()
+                       self.bridge_pending = c
+                       commands_to_send.append(c[1])
+               for da in self.devices:
+                       if self.device_pending[da] is None and \
+                         len(self.device_queue[da]) > 0:
+                               c = self.device_queue[da].pop()
+                               self.device_pending[da] = c
+                               commands_to_send.append(c[1])
+               return commands_to_send
\ No newline at end of file