From 0abbe41473b43a947af568bc87ca0127a70c46c8 Mon Sep 17 00:00:00 2001 From: Jens Ullmert Date: Wed, 27 Jul 2022 16:58:08 +0200 Subject: [PATCH] wies war --- Python/sock.py | 89 +++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/Python/sock.py b/Python/sock.py index 4af5b4b..d12c174 100644 --- a/Python/sock.py +++ b/Python/sock.py @@ -232,62 +232,63 @@ async def notify_delta(delta): # Websocket working thread # async def websworker(websocket, path): + + # register(websocket) sends user_event() to websocket + await register(websocket) + try: + await websocket.send(state_event()) + while True: + # waiting for a new message resceived on the websocket + message = await websocket.recv() - # register(websocket) sends user_event() to websocket - await register(websocket) - try: - await websocket.send(state_event()) - while True: - try: + # split message from multiple commands in one string + data = message.split(";") - # waiting for a new message received on the websocket - message = await asyncio.wait_for(websocket.recv(), timeout=0.1) + # process each command + for index in range(len(data)): + # act[0] will be the key + # act[1] will be the value + act = data[index].split("=") - # split message from multiple commands in one string - data = message.split(";") + try: + # if key is a number, we will have e change on a dmx value + # otherwise, we have a string to check which will be processed in except... + # poor python - there is no other chance to check if a string can be a int without errors + dmxchannel = int(act[0]) + dmxvalue = int(act[1]) - # process each command - for index in range(len(data)): - # act[0] will be the key - # act[1] will be the value - act = data[index].split("=") + # We have integer, but are they in a valid range? + if( dmxchannel < 1 or dmxchannel > 512 or dmxvalue < 0 or dmxvalue > 255): + # this must be an exception, we will continue in the exception-area anyway now + dmxchannel = int("will raise") - try: - # if key is a number, we will have e change on a dmx value - # otherwise, we have a string to check which will be processed in except... - # poor python - there is no other chance to check if a string can be a int without errors - dmxchannel = int(act[0]) - dmxvalue = int(act[1]) + # looks valid, so take them to the buffer + DMXDATA[dmxchannel] = dmxvalue - # We have integer, but are they in a valid range? - if( dmxchannel < 1 or dmxchannel > 512 or dmxvalue < 0 or dmxvalue > 255): - # this must be an exception, we will continue in the exception-area anyway now - dmxchannel = int("will raise") + # notifying our connected users + # we can send the resceived command back since the syntax is matched :) + await notify_delta(data[index]) - # looks valid, so take them to the buffer - DMXDATA[dmxchannel] = dmxvalue - - # notifying our connected users - # we can send the resceived command back since the syntax is matched :) - await notify_delta(data[index]) - - # debug - print("DMX value for channel " + act[0] + " changed to " + act[1]) + # debug + print("DMX value for channel " + act[0] + " changed to " + act[1]) + + - # It does not look like we have a dmx key-value, so we have to process the command - except Exception: - if act[0] == "manual": - sendDMX(DMXDATA) - print("Manual send data: ", bytearray(DMXDATA)) - elif act[0] == "sec": - print("") + # It does not look like we have a dmx key-value, so we have to process the command + except Exception: - else: - print("received message, but can not handle: ", act) - + if act[0] == "manual": + sendDMX(DMXDATA) + print("Manual send data: ", bytearray(DMXDATA)) + + elif act[0] == "sec": + print("") + + else: + print("received message, but can not handle: ", act) finally: await unregister(websocket)