This commit is contained in:
Jens Ullmert 2022-07-27 16:58:08 +02:00
parent 2a67fb36e7
commit 0abbe41473

View File

@ -233,61 +233,62 @@ async def notify_delta(delta):
# #
async def websworker(websocket, path): async def websworker(websocket, path):
# register(websocket) sends user_event() to websocket # register(websocket) sends user_event() to websocket
await register(websocket) await register(websocket)
try: try:
await websocket.send(state_event()) await websocket.send(state_event())
while True: while True:
try: # waiting for a new message resceived on the websocket
message = await websocket.recv()
# waiting for a new message received on the websocket # split message from multiple commands in one string
message = await asyncio.wait_for(websocket.recv(), timeout=0.1) data = message.split(";")
# split message from multiple commands in one string # process each command
data = message.split(";") for index in range(len(data)):
# act[0] will be the key
# act[1] will be the value
act = data[index].split("=")
# process each command try:
for index in range(len(data)): # if key is a number, we will have e change on a dmx value
# act[0] will be the key # otherwise, we have a string to check which will be processed in except...
# act[1] will be the value # poor python - there is no other chance to check if a string can be a int without errors
act = data[index].split("=") dmxchannel = int(act[0])
dmxvalue = int(act[1])
try: # We have integer, but are they in a valid range?
# if key is a number, we will have e change on a dmx value if( dmxchannel < 1 or dmxchannel > 512 or dmxvalue < 0 or dmxvalue > 255):
# otherwise, we have a string to check which will be processed in except... # this must be an exception, we will continue in the exception-area anyway now
# poor python - there is no other chance to check if a string can be a int without errors dmxchannel = int("will raise")
dmxchannel = int(act[0])
dmxvalue = int(act[1])
# We have integer, but are they in a valid range? # looks valid, so take them to the buffer
if( dmxchannel < 1 or dmxchannel > 512 or dmxvalue < 0 or dmxvalue > 255): DMXDATA[dmxchannel] = dmxvalue
# this must be an exception, we will continue in the exception-area anyway now
dmxchannel = int("will raise")
# looks valid, so take them to the buffer # notifying our connected users
DMXDATA[dmxchannel] = dmxvalue # we can send the resceived command back since the syntax is matched :)
await notify_delta(data[index])
# notifying our connected users # debug
# we can send the resceived command back since the syntax is matched :) print("DMX value for channel " + act[0] + " changed to " + act[1])
await notify_delta(data[index])
# 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("")
else:
print("received message, but can not handle: ", act)
# 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("")
else:
print("received message, but can not handle: ", act)
finally: finally:
await unregister(websocket) await unregister(websocket)