Skip to content

Commit

Permalink
Keep track of server state
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbithell committed Mar 1, 2024
1 parent 7c59231 commit b85dd13
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
30 changes: 18 additions & 12 deletions device/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions server/src/osc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 =
Expand Down Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit b85dd13

Please sign in to comment.