feat: Implement real-time rule status updates and dashboard display via WebSockets.

This commit is contained in:
sebseb7
2026-01-18 05:52:54 -05:00
parent a381b0e121
commit e226032d0b
4 changed files with 247 additions and 6 deletions

View File

@@ -71,6 +71,14 @@ function getState(mac) {
}
export default {
getStatus() {
const state = getState(WATER_BUTTON_MAC);
return {
storedDuration: state.storedDuration,
countMode: state.countMode,
timerActive: state.timer !== null
};
},
async run(ctx) {
// Auto-on for water button when it connects (only if remote switch is online)
if (ctx.trigger.mac === WATER_BUTTON_MAC && ctx.trigger.field === 'online' && ctx.trigger.event === true) {
@@ -159,6 +167,14 @@ export default {
state.countMode = false;
ctx.log(`Count mode ended. Stored duration: ${elapsed}ms`);
// Push status update to dashboard
ctx.updateStatus({
storedDuration: state.storedDuration,
countMode: false,
timerActive: false,
lastAction: 'Duration saved'
});
// Persist the new duration
persistedState[mac] = { storedDuration: elapsed };
saveState(persistedState);
@@ -169,10 +185,21 @@ export default {
// Normal mode - schedule light to turn on after stored duration
ctx.log(`Light off. Will turn on in ${state.storedDuration}ms`);
// Push status update to dashboard
ctx.updateStatus({
storedDuration: state.storedDuration,
countMode: false,
timerActive: true,
lastAction: 'Timer started'
});
state.timer = setTimeout(async () => {
ctx.log(`Timer elapsed. Turning light on.`);
await setLight(ctx, mac, true, 20);
state.timer = null;
// Can't use ctx.updateStatus here since ctx is out of scope
// The timer completing will be reflected in next getStatus() call
}, state.storedDuration);
}
}
@@ -202,6 +229,14 @@ export default {
state.countMode = true;
state.countStart = Date.now();
ctx.log('Count mode active. Light stays off. Press button to set duration and turn on.');
// Push status update to dashboard
ctx.updateStatus({
storedDuration: state.storedDuration,
countMode: true,
timerActive: false,
lastAction: 'Counting...'
});
}
}
};