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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user