Skip to content

Commit

Permalink
Various bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbithell committed Feb 27, 2024
1 parent ccaeb63 commit d5d2b78
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
12 changes: 8 additions & 4 deletions device/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'''
CONFIG
'''
VERSION = '8.0.1'
VERSION = '8.0.2'
SERIAL = 'CUEBGEN2-?'
CUSTOMMAC = 'none' #set to string "none" to disable, format is without : - so "0271835c629b"
# Buttons & LEDs - Pinout
Expand Down Expand Up @@ -92,19 +92,22 @@ def translateState(stateInt):
else:
return "Unknown"


try:
autoGreenOffTimer = int(configStore.getConfig("mainlogic-autogreenoff"))
except:
# Just in case it wasn't an int for some reason
autoGreenOffTimer = 3

# Used to track when the last green light was turned on, because if you send a green twice within the autoGreenOffTimer value it will turn it off too early the second time because the sleep is finished
autoGreenOffLastGreen = 0
# Automatically turn off the green light after x seconds
async def autoGreenOff():
await asyncio.sleep_ms(autoGreenOffTimer * 1000)
if (state == 5):
if (state == 5 and time.ticks_diff(time.ticks_ms(), autoGreenOffLastGreen) >= (autoGreenOffTimer * 1000)):
setState(1)

def setState(newState):
global state
global state, autoGreenOffLastGreen
print("[STATE] State changed from ", translateState(state), "to", translateState(newState))
state = newState
transmitState()
Expand All @@ -124,6 +127,7 @@ def setState(newState):
LEDOff(getLEDIdByName("OUTSTATION-STANDBY"))
LEDOn(getLEDIdByName("OUTSTATION-GO"))
if (autoGreenOffTimer > 0):
autoGreenOffLastGreen = time.ticks_ms()
asyncio.create_task(autoGreenOff())
elif (newState == 6):
LEDOn(getLEDIdByName("OUTSTATION-STANDBY"))
Expand Down
24 changes: 15 additions & 9 deletions server/src/osc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class OSC {
} = {};
static devicePingChecks: {
[deviceId: number]: {
lastPingTimestamp: number;
lastPingReceivedTimestamp: number;
lastPingSentTimestamp: number;
sendInterval: NodeJS.Timeout;
checkInterval: NodeJS.Timeout;
};
Expand All @@ -40,7 +41,8 @@ export class OSC {
) {
const deviceId = OSC.ipsToDevices[rinfo.address];
if (deviceId) {
OSC.devicePingChecks[deviceId].lastPingTimestamp = Date.now();
OSC.devicePingChecks[deviceId].lastPingReceivedTimestamp =
Date.now();
if (OSC.deviceStatus[deviceId] !== msg[1]) {
OSC.deviceStatus[deviceId] = msg[1];
eventEmitter.emit("trpc.deviceStatus");
Expand All @@ -49,7 +51,8 @@ export class OSC {
} else if (msg[0].startsWith("/cueb/pong/") && msg.length === 1) {
const deviceId = OSC.ipsToDevices[rinfo.address];
if (deviceId) {
OSC.devicePingChecks[deviceId].lastPingTimestamp = Date.now();
OSC.devicePingChecks[deviceId].lastPingReceivedTimestamp =
Date.now();
}
} else {
console.log("Unknown message", msg, rinfo);
Expand All @@ -74,23 +77,25 @@ export class OSC {
sendInterval: setInterval(() => {
// Devices timeout after 5000ms of no pings received from a server
if (
Date.now() - OSC.devicePingChecks[deviceId].lastPingTimestamp >
4000
Date.now() - OSC.devicePingChecks[deviceId].lastPingSentTimestamp >=
3500
) {
OSC.messageDevice(deviceId, "/cueb/ping/");
}
}, 500),
checkInterval: setInterval(() => {
// We should mark a device as timed out if we haven't heard from it for 3 seconds - it should be transmitting its state every 2 seconds
// We should mark a device as timed out if we haven't heard from it for 4 seconds - it should be transmitting its state every 2 seconds
if (
Date.now() - OSC.devicePingChecks[deviceId].lastPingTimestamp >
3000
Date.now() -
OSC.devicePingChecks[deviceId].lastPingReceivedTimestamp >
4000
) {
OSC.deviceStatus[deviceId] = false;
eventEmitter.emit("trpc.deviceStatus");
}
}, 500),
lastPingTimestamp: 0,
lastPingReceivedTimestamp: 0,
lastPingSentTimestamp: 0,
};
}
static deleteClient(deviceId: number) {
Expand All @@ -113,5 +118,6 @@ export class OSC {
) {
const msg = new Message(message, ...args);
OSC.devices[deviceId].send(msg);
OSC.devicePingChecks[deviceId].lastPingSentTimestamp = Date.now();
}
}
3 changes: 3 additions & 0 deletions server/src/utils/scanForDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const scanForDevices = () => {
const ips: string[] = [];
const block = new Netmask(thisIp, "255.255.255.0");
block.forEach((ip: string) => ips.push(ip));

//TODO hunt through existing devices to double check no clashes first

console.log("Scanning for devices on the network");
return Promise.allSettled(
ips.map((ip) => {
Expand Down

0 comments on commit d5d2b78

Please sign in to comment.