]> git.aero2k.de Git - sirius.git/commitdiff
reorganisation of the messaging core into its own place
authorMatt Webb <matt@interconnected.org>
Wed, 24 Sep 2014 17:56:18 +0000 (18:56 +0100)
committerMatt Webb <matt@interconnected.org>
Wed, 24 Sep 2014 17:56:18 +0000 (18:56 +0100)
app/__init__.py
app/core/__init__.py
app/models/core.py

index e074577d8c9378bac3e49bd708536f8334eb09e9..fe442466a2c2c6910b67c0bd5335bc64a6c4f356 100755 (executable)
@@ -12,9 +12,6 @@ bootstrap = Bootstrap()
 db = SQLAlchemy()
 sockets = Sockets()
 
-from core import commands
-from core.events import process_event
-
 
 # @TODO
 # for this to work, I have to put the following line at line 38 of
@@ -40,85 +37,6 @@ def create_app(config_name):
     from .main import main as main_blueprint
     app.register_blueprint(main_blueprint)
     
-    class CommandBackend(object):
-        """ Interface for registering and updating Websocket clients
-        to receive commands. """
-        
-        def __init__(self):
-            self.clients = list()
-            self.bridge_commands_queue = {} # bridge_address: [bc]
-            self.pending_bridge_command = {} # bridge_address: bc
-            self.device_commands_queue = {} # device_address: [dc]
-            self.pending_device_command = {} # device_address: dc
-            
-            self.did_send_image = False
-        
-        def register(self, ws):
-            self.clients.append( WebsocketClient(ws) )
-        
-        def add_bridge_address(self, bridge_address, ws):
-            for c in self.clients:
-                if c.ws == ws:
-                    c.bridge_address = bridge_address
-        
-        def add_device(self, device_address, ws):
-            [c.add_device(device_address) for c in self.clients if c.ws == ws]
-        
-        def remove_device(self, device_address, ws):
-            [c.remove_device(device_address) for c in self.clients if c.ws == ws]
-
-        def send_to_bridge(self, bridge_address, command):
-            matches = [c for c in self.clients if c.bridge_address == bridge_address]
-            if matches:
-                matches[0].ws.send(command.to_json())
-            
-        def send_to_device(self, device_address, command):
-            matches = [c for c in self.clients if c.has_device(device_address)]
-            if matches:
-                matches[0].ws.send(command.to_json())
-        
-    command_sender = CommandBackend()
-    #command_sender.run()            
-    
-    class WebsocketClient(object):
-        
-        def __init__(self, ws):
-            self.ws = ws
-            self.bridge_address = None
-            self.connected_devices = set() # device_address
-        
-        def add_device(self, device_address):
-            self.connected_devices.add(device_address)
-        
-        def remove_device(self, device_address):
-            self.connected_devices.remove(device_address)
-        
-        def has_device(self, device_address):
-            return device_address in self.connected_devices
-    
-    # shouldn't be here, should be attached as a blueprint
-    # there needs to be a single backend object per process
-    # that allows sockets to register themselves, and remember
-    # what bridge_address and device_address are present
-    # then messages can be sent via this object.
-    # at some point, the object should pass messages around itself
-    # using redis, so it works over multiple processes
-    # see https://devcenter.heroku.com/articles/python-websockets
-    # for a good pattern
-    @sockets.route('/api/v1/connection') 
-    def echo_socket(ws):
-        print "here"
-        done_once = False
-        counting = 0
-        command_sender.register(ws)
-        while True: 
-            message = ws.receive()            
-            #pprint(message)
-           
-            try:
-                event = json.loads(message)
-                process_event(ws, event, command_sender)
-            except Exception, e:
-                print "Exception: %r" % e
+    from .core import socket, command_sender
       
     return app
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e29196742c1e65b15e9eddd3475f3eb59f3d0fa4 100644 (file)
@@ -0,0 +1,86 @@
+from app import sockets
+
+from .events import process_event
+
+import json
+
+class CommandBackend(object):
+    """ Interface for registering and updating Websocket clients
+    to receive commands. """
+    
+    def __init__(self):
+        self.clients = list()
+        self.bridge_commands_queue = {} # bridge_address: [bc]
+        self.pending_bridge_command = {} # bridge_address: bc
+        self.device_commands_queue = {} # device_address: [dc]
+        self.pending_device_command = {} # device_address: dc
+        
+        self.did_send_image = False
+    
+    def register(self, ws):
+        self.clients.append( WebsocketClient(ws) )
+    
+    def add_bridge_address(self, bridge_address, ws):
+        for c in self.clients:
+            if c.ws == ws:
+                c.bridge_address = bridge_address
+    
+    def add_device(self, device_address, ws):
+        [c.add_device(device_address) for c in self.clients if c.ws == ws]
+    
+    def remove_device(self, device_address, ws):
+        [c.remove_device(device_address) for c in self.clients if c.ws == ws]
+
+    def send_to_bridge(self, bridge_address, command):
+        matches = [c for c in self.clients if c.bridge_address == bridge_address]
+        if matches:
+            matches[0].ws.send(command.to_json())
+        
+    def send_to_device(self, device_address, command):
+        matches = [c for c in self.clients if c.has_device(device_address)]
+        if matches:
+            matches[0].ws.send(command.to_json())
+    
+command_sender = CommandBackend()
+#command_sender.run()            
+
+class WebsocketClient(object):
+    
+    def __init__(self, ws):
+        self.ws = ws
+        self.bridge_address = None
+        self.connected_devices = set() # device_address
+    
+    def add_device(self, device_address):
+        self.connected_devices.add(device_address)
+    
+    def remove_device(self, device_address):
+        self.connected_devices.remove(device_address)
+    
+    def has_device(self, device_address):
+        return device_address in self.connected_devices
+
+# shouldn't be here, should be attached as a blueprint
+# there needs to be a single backend object per process
+# that allows sockets to register themselves, and remember
+# what bridge_address and device_address are present
+# then messages can be sent via this object.
+# at some point, the object should pass messages around itself
+# using redis, so it works over multiple processes
+# see https://devcenter.heroku.com/articles/python-websockets
+# for a good pattern
+@sockets.route('/api/v1/connection') 
+def socket(ws):
+    print "here"
+    done_once = False
+    counting = 0
+    command_sender.register(ws)
+    while True: 
+        message = ws.receive()            
+        #pprint(message)
+       
+        try:
+            event = json.loads(message)
+            process_event(ws, event, command_sender)
+        except Exception, e:
+            print "Exception: %r" % e
\ No newline at end of file
index a11a7fbc0624985d644af65cb9537a083d7d0394..d90e384d2e29830bf5e3a30ecd63087066f38d03 100755 (executable)
@@ -53,3 +53,19 @@ class DeviceCommand(db.Model):
             'binary_payload': self.binary_payload
         }
         return json.dumps(json_hash, encoding='utf-8')
+
+class Bridge(db.Model):
+    __tablename__ = 'bridges'
+    bridge_address = db.Column(db.String, primary_key=True)
+    last_power_on = db.Column(db.DateTime, nullable=False)
+
+class Device(db.Model):
+    __tablename__ = 'devices'
+    device_address = db.Column(db.String, primary_key=True)
+    hardware_xor = db.Column(db.String, nullable=False)
+
+class PendingClaim(db.Model):
+    __tablename__ = 'pending_claims'
+    id = db.Column(db.Integer, primary_key=True)
+    claim_code = db.Column(db.String, nullable=False)
+    hardware_xor = db.Column(db.String, nullable=False)