From 78b5f72bc71b521fc2ea0a92dbe9bf43d611fc41 Mon Sep 17 00:00:00 2001 From: Delta Date: Thu, 25 Mar 2021 01:14:28 -0700 Subject: [PATCH] Allow hosting websocket server locally Config has changed to include settings for locally hosting --- Config.py | 9 ++++++--- __meta__.py | 2 +- config/config.toml | 8 +++++--- sACN_Websockets.py | 45 ++++++++++++++++++++++++++++----------------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/Config.py b/Config.py index a7d1bcc..0beea6b 100644 --- a/Config.py +++ b/Config.py @@ -5,7 +5,7 @@ from Logging import wssl import __meta__ as meta -configSpec = 1 +configSpec = 2 class Config(object): @@ -21,8 +21,11 @@ def setValDefault(self, val, default): #set a default in case the value is missi def setConfigFile(self, configFile): tomlData = toml.load(open(join(self.app_dir, 'config', configFile))) - self.ws_uri = self.setValDefault(tomlData.get('settings').get('ws_uri'), "ws://localhost") - self.sACN_port = self.setValDefault(tomlData.get('dmx').get('sACN_port'), "5568") + self.ws_uri = self.setValDefault(tomlData.get('destination').get('ws_uri'), "ws://localhost") + self.locally_host = self.setValDefault(tomlData.get('destination').get('locally_host'), True) + self.local_port = self.setValDefault(tomlData.get('destination').get('local_port'), True) + + self.sACN_port = self.setValDefault(tomlData.get('dmx').get('sACN_port'), 5568) self.sACN_ip = self.setValDefault(tomlData.get('dmx').get('sACN_ip'), "127.0.0.1") self.dmx_fps = self.setValDefault(tomlData.get('dmx').get('dmx_fps'), 10) self.sACN_universe = self.setValDefault(tomlData.get('dmx').get('sACN_universe'), 1) diff --git a/__meta__.py b/__meta__.py index a25b528..7268535 100644 --- a/__meta__.py +++ b/__meta__.py @@ -1,2 +1,2 @@ author = "Delta" -version = "0.1.0" +version = "0.2.0" diff --git a/config/config.toml b/config/config.toml index 89d88fa..f7689dd 100644 --- a/config/config.toml +++ b/config/config.toml @@ -1,5 +1,7 @@ -[settings] -ws_uri = "ws://localhost" #Websocket host, (default: ws://localhost) +[destination] +ws_uri = "ws://localhost" # Websocket host (default: ws://localhost) +locally_host = true # Hosts ws server on localhost (default: true) +local_port = 80 # Websocket port when hosting locally (default: 80) [dmx] sACN_port = 5568 # sACN/udp port (default: 5568) @@ -8,4 +10,4 @@ sACN_universe = 1 # sACN universe to listen on (default: 1) dmx_fps = 10 # Send up to x dmx frames per second (default: 10) [debug] -configSpec = 1 # Version of this config +configSpec = 2 # Version of this config diff --git a/sACN_Websockets.py b/sACN_Websockets.py index 773262c..e4a3ebb 100644 --- a/sACN_Websockets.py +++ b/sACN_Websockets.py @@ -39,28 +39,30 @@ def lastDMX(): pre = ''.join(format(x, '02x') for x in LastPacket) return pre -wssl.log(("Listening on {0}:{1}").format(config.sACN_ip, config.sACN_port)) -wssl.log(("Sending on {0}").format(config.ws_uri)) +async def localServer(websocket, path): + await dmxSender(websocket) +async def remoteSender(): #connect to external ws server + async with websockets.connect(config.ws_uri) as websocket: + await dmxSender(websocket) -async def sender(): +async def dmxSender(websocket): running = True global hasDataToSend dmx_fps = config.dmx_fps - async with websockets.connect(config.ws_uri) as websocket: - while running: - if hasDataToSend: - try: - data = lastDMX() - await websocket.send(data) - hasDataToSend = False - await asyncio.sleep(1/dmx_fps) - except Exception as e: - wssl.log(str(e)) - running = False - else: + while running: + if hasDataToSend: + try: + data = lastDMX() + await websocket.send(data) + hasDataToSend = False await asyncio.sleep(1/dmx_fps) + except Exception as e: + wssl.log(str(e)) + running = False + else: + await asyncio.sleep(1/dmx_fps) @receiver.listen_on('universe', universe=config.sACN_universe) def callback(packet): @@ -69,14 +71,23 @@ def callback(packet): LastPacket = packet.dmxData hasDataToSend = True +wssl.log(("Listening on {0}:{1}").format(config.sACN_ip, config.sACN_port)) receiver.join_multicast(config.sACN_universe) try: - asyncio.get_event_loop().run_until_complete(sender()) + if config.locally_host: + start_server = websockets.serve(localServer, "localhost", config.local_port) + asyncio.get_event_loop().run_until_complete(start_server) + wssl.log(("Sending locally on ws://localhost:{0}").format(config.local_port)) + asyncio.get_event_loop().run_forever() + else: + wssl.log(("Sending on {0}").format(config.ws_uri)) + asyncio.get_event_loop().run_until_complete(remoteSender()) + except KeyboardInterrupt as e: wssl.log("Closed by console interrupt") except socket.gaierror as e: - wssl.log("Unable to connect to wss host") + wssl.log("Unable to connect to websocket host") wssl.log(str(sys.exc_info())) except Exception as e: wssl.log(str(sys.exc_info()))