feat: Implement type casting for channel states and events, refine Shelly device online/offline logic, and improve event handling on server startup.

This commit is contained in:
sebseb7
2026-01-17 00:50:32 -05:00
parent 55e4dc56c3
commit 2468f7b5e0
3 changed files with 67 additions and 13 deletions

View File

@@ -28,6 +28,14 @@ db.serialize(() => {
// Events table - references channels
db.run("CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY AUTOINCREMENT, channel_id INTEGER, event TEXT, timestamp TEXT, FOREIGN KEY(channel_id) REFERENCES channels(id))");
// Insert offline events for all online channels on startup (to prevent deduplication issues after restart)
db.run(`INSERT INTO events (channel_id, event, timestamp)
SELECT c.id, 'false', datetime('now')
FROM channels c
WHERE c.field = 'online'
AND EXISTS (SELECT 1 FROM events e WHERE e.channel_id = c.id AND e.event = 'true'
AND e.id = (SELECT MAX(e2.id) FROM events e2 WHERE e2.channel_id = c.id))`);
});
// Upstream Client Integration