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

View File

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