From 3714cade341d53b1dffeca3a87e0f8961c730663 Mon Sep 17 00:00:00 2001 From: Matt Webb Date: Wed, 24 Sep 2014 18:56:18 +0100 Subject: [PATCH] reorganisation of the messaging core into its own place --- app/__init__.py | 84 +------------------------------------------ app/core/__init__.py | 86 ++++++++++++++++++++++++++++++++++++++++++++ app/models/core.py | 16 +++++++++ 3 files changed, 103 insertions(+), 83 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index e074577..fe44246 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -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 diff --git a/app/core/__init__.py b/app/core/__init__.py index e69de29..e291967 100644 --- a/app/core/__init__.py +++ b/app/core/__init__.py @@ -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 diff --git a/app/models/core.py b/app/models/core.py index a11a7fb..d90e384 100755 --- a/app/models/core.py +++ b/app/models/core.py @@ -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) -- 2.47.3