Skip to content

Commit

Permalink
Allow hosting websocket server locally
Browse files Browse the repository at this point in the history
Config has changed to include settings for locally hosting
  • Loading branch information
XDelta committed Mar 25, 2021
1 parent e8139ce commit 78b5f72
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
9 changes: 6 additions & 3 deletions Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from Logging import wssl
import __meta__ as meta

configSpec = 1
configSpec = 2

class Config(object):

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion __meta__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
author = "Delta"
version = "0.1.0"
version = "0.2.0"
8 changes: 5 additions & 3 deletions config/config.toml
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
45 changes: 28 additions & 17 deletions sACN_Websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()))
Expand Down

0 comments on commit 78b5f72

Please sign in to comment.