From b85dd13ffd3dde28e70ea40d82ea65c543f7b028 Mon Sep 17 00:00:00 2001 From: James Bithell Date: Fri, 1 Mar 2024 18:35:34 +0000 Subject: [PATCH] Keep track of server state --- device/main.py | 30 ++++++++++++++++++------------ server/src/osc.ts | 13 +++++++++---- server/src/server.ts | 2 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/device/main.py b/device/main.py index 14f66fd..fe91eb4 100644 --- a/device/main.py +++ b/device/main.py @@ -70,7 +70,8 @@ ''' Main State Logic ''' -state = 0 +state = 0 # The state we are in +serverState = -1 # The state that the server thinks we are in - minus 1 is unknown state - server isn't sure def translateState(stateInt): # Translates the state integer into a human readable string if (stateInt == 0): @@ -137,7 +138,7 @@ def setState(newState): LEDFlash(getLEDIdByName("OUTSTATION-GO")) def transmitState(): - oscClient.send("/cueb/outstationState/" + deviceUniqueId, state) + oscClient.send("/cueb/outstationState", state, deviceUniqueId) ''' Setup Buttons & LEDs - Interrupts @@ -488,13 +489,14 @@ async def oscServer(host, port, cb, **params): setState(0) print("[OSC] Server shutdown") -async def broadcastState(): +async def retransmitState(): + # Rebroadcast the state every 200ms if the server has ended up in a different state - this is normally if they misheard while True: - # Broadcast state every 2 seconds - await asyncio.sleep_ms(2000) - transmitState() + if (state != serverState): + transmitState() + await asyncio.sleep_ms(200) -asyncio.create_task(broadcastState()) +asyncio.create_task(retransmitState()) async def handle_request(sock, data, caddr, **params): handle_osc(data, caddr, **params) @@ -504,18 +506,22 @@ async def handle_request(sock, data, caddr, **params): ''' lastOSCMessageReceived = 0 def oscMessageRecieved(timetag, data): - global lastOSCMessageReceived + global lastOSCMessageReceived, serverState oscaddr, tags, args, src = data if (oscaddr.startswith("/reply/")): return lastOSCMessageReceived = time.ticks_ms() - if (oscaddr == "/cueb/setOutstationState/" and len(args) == 1 and tags == "f"): + if (oscaddr == "/cueb/outstationState" and len(args) == 1 and tags == "f" and src != deviceIp): + serverState = int(args[0]) if (state != int(args[0])): - print("[OSC] Recieved state message from", src, "with state", int(args[0])) setState(int(args[0])) - elif (oscaddr == "/cueb/ping/" and len(args) == 0 and tags == "" and src != deviceIp): + print("[OSC] Recieved state message from", src, "with state", int(args[0])) + elif (oscaddr == "/cueb/outstationState" and len(args) == 0 and tags == "" and src != deviceIp): + serverState = -1 + transmitState() + elif (oscaddr == "/cueb/ping" and len(args) == 0 and tags == "" and src != deviceIp): print("[OSC] Ping from", src) - oscClient.send("/cueb/pong/" + deviceUniqueId) + oscClient.send("/cueb/pong", deviceUniqueId) else: print(oscaddr) print(tags) diff --git a/server/src/osc.ts b/server/src/osc.ts index 7bedd51..b817602 100644 --- a/server/src/osc.ts +++ b/server/src/osc.ts @@ -35,8 +35,8 @@ export class OSC { OSC.servers[port] = createOSCServer(port); OSC.servers[port].on("message", (msg, rinfo) => { if ( - msg[0].startsWith("/cueb/outstationState") && - msg.length === 2 && + msg[0] == "/cueb/outstationState" && + msg.length === 3 && typeof msg[1] === "number" ) { const deviceId = OSC.ipsToDevices[rinfo.address]; @@ -45,10 +45,15 @@ export class OSC { Date.now(); if (OSC.deviceStatus[deviceId] !== msg[1]) { OSC.deviceStatus[deviceId] = msg[1]; + OSC.messageDevice( + deviceId, + "/cueb/outstationState", + OSC.deviceStatus[deviceId] + ); // Echo the state back to the device to confirm it was received eventEmitter.emit("trpc.deviceStatus"); } } - } else if (msg[0].startsWith("/cueb/pong/") && msg.length === 1) { + } else if (msg[0] == "/cueb/pong" && msg.length === 2) { const deviceId = OSC.ipsToDevices[rinfo.address]; if (deviceId) { OSC.devicePingChecks[deviceId].lastPingReceivedTimestamp = @@ -80,7 +85,7 @@ export class OSC { Date.now() - OSC.devicePingChecks[deviceId].lastPingSentTimestamp >= 3500 ) { - OSC.messageDevice(deviceId, "/cueb/ping/"); + OSC.messageDevice(deviceId, "/cueb/ping"); } }, 500), checkInterval: setInterval(() => { diff --git a/server/src/server.ts b/server/src/server.ts index 3f98fb3..b4f25d5 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -124,7 +124,7 @@ const devicesRouter = router({ setState: publicProcedure .input(z.object({ id: z.number(), newState: z.number() })) .mutation(({ input }) => { - OSC.messageDevice(input.id, "/cueb/setOutstationState/", input.newState); + OSC.messageDevice(input.id, "/cueb/outstationState", input.newState); return {}; }), scanForDevices: publicProcedure.mutation(() => {