u
This commit is contained in:
91
server.js
91
server.js
@@ -23,10 +23,18 @@ db.serialize(() => {
|
||||
|
||||
// Drop old events table to enforce new schema
|
||||
db.run("DROP TABLE IF EXISTS events");
|
||||
// Create new events table with 'field' column
|
||||
db.run("CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY AUTOINCREMENT, mac TEXT, component TEXT, field TEXT, event TEXT, timestamp TEXT)");
|
||||
// Create new events table with 'field' and 'type' columns
|
||||
db.run("CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY AUTOINCREMENT, mac TEXT, component TEXT, field TEXT, type TEXT, event TEXT, timestamp TEXT)");
|
||||
});
|
||||
|
||||
// Upstream Client Integration
|
||||
import { TischlerClient } from './tischler_client.js';
|
||||
const UPSTREAM_URL = 'wss://dash.bosewolf.de/agentapi/';
|
||||
const UPSTREAM_KEY = 'd2fba4ff8cd18b87735bef34088a5fe78e52049145bc336f30adf2da371ff431';
|
||||
|
||||
//const upstreamClient = new TischlerClient(UPSTREAM_URL, UPSTREAM_KEY);
|
||||
//upstreamClient.connect().catch(err => console.error('Failed to connect to upstream:', err));
|
||||
|
||||
// Map to track connection ID to device MAC
|
||||
const connectionDeviceMap = new Map();
|
||||
|
||||
@@ -34,12 +42,24 @@ const connectionDeviceMap = new Map();
|
||||
const macConnectionsMap = new Map();
|
||||
|
||||
// Helper to deduplicate stateful events
|
||||
function checkAndLogEvent(mac, component, field, event, connectionId = null) {
|
||||
// type: 'enum' (button events), 'range' (level 0-100), 'boolean' (on/off, online)
|
||||
function checkAndLogEvent(mac, component, field, type, event, connectionId = null) {
|
||||
// Function to forward event to upstream
|
||||
const forwardToUpstream = () => {
|
||||
const channel = `${component}_${field}`; // e.g. light:0_on, system_online
|
||||
//upstreamClient.sendReadings([{
|
||||
// device: mac,
|
||||
// channel: channel,
|
||||
// value: event
|
||||
//}]);
|
||||
};
|
||||
|
||||
if (field === 'button') {
|
||||
if (connectionId) console.log(`[ID: ${connectionId}] Event logged: ${event} on ${component} (${field})`);
|
||||
const stmt = db.prepare("INSERT INTO events (mac, component, field, event, timestamp) VALUES (?, ?, ?, ?, ?)");
|
||||
stmt.run(mac, component, field, String(event), new Date().toISOString());
|
||||
const stmt = db.prepare("INSERT INTO events (mac, component, field, type, event, timestamp) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
stmt.run(mac, component, field, type, String(event), new Date().toISOString());
|
||||
stmt.finalize();
|
||||
forwardToUpstream();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,9 +75,10 @@ function checkAndLogEvent(mac, component, field, event, connectionId = null) {
|
||||
|
||||
if (!row || row.event !== currentEventStr) {
|
||||
if (connectionId) console.log(`[ID: ${connectionId}] Status change logged: ${event} on ${component} (${field})`);
|
||||
const stmt = db.prepare("INSERT INTO events (mac, component, field, event, timestamp) VALUES (?, ?, ?, ?, ?)");
|
||||
stmt.run(mac, component, field, currentEventStr, new Date().toISOString());
|
||||
const stmt = db.prepare("INSERT INTO events (mac, component, field, type, event, timestamp) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
stmt.run(mac, component, field, type, currentEventStr, new Date().toISOString());
|
||||
stmt.finalize();
|
||||
forwardToUpstream();
|
||||
} else {
|
||||
// console.log(`[ID: ${connectionId}] Duplicate event suppressed: ${event} on ${component} (${field})`);
|
||||
}
|
||||
@@ -121,27 +142,24 @@ wss.on('connection', (ws, req) => {
|
||||
const possibleMac = data.params.sys ? data.params.sys.mac : null;
|
||||
|
||||
// Log initial output states
|
||||
if ((key.startsWith('light') || key.startsWith('switch')) && typeof value.output !== 'undefined') {
|
||||
if (key.startsWith('switch') && typeof value.output !== 'undefined') {
|
||||
const eventVal = value.output; // true/false
|
||||
if (possibleMac) {
|
||||
checkAndLogEvent(possibleMac, key, 'on', eventVal, connectionId);
|
||||
checkAndLogEvent(possibleMac, key, 'on', 'boolean', eventVal, connectionId);
|
||||
}
|
||||
}
|
||||
// Log initial brightness
|
||||
if (key.startsWith('light') && typeof value.brightness !== 'undefined') {
|
||||
// Suppress if output is explicitly false (off)
|
||||
if (value.output !== false) {
|
||||
const eventVal = value.brightness;
|
||||
if (possibleMac) {
|
||||
checkAndLogEvent(possibleMac, key, 'brightness', eventVal, connectionId);
|
||||
}
|
||||
// Log initial level for analog light outputs (0 = off, 1-100 = brightness)
|
||||
if (key.startsWith('light') && typeof value.output !== 'undefined') {
|
||||
const level = value.output === false ? 0 : (value.brightness || 0);
|
||||
if (possibleMac) {
|
||||
checkAndLogEvent(possibleMac, key, 'level', 'range', level, connectionId);
|
||||
}
|
||||
}
|
||||
// Log initial input states (if state is boolean)
|
||||
if (key.startsWith('input') && typeof value.state === 'boolean') {
|
||||
const eventVal = value.state; // true/false
|
||||
if (possibleMac) {
|
||||
checkAndLogEvent(possibleMac, key, 'input', eventVal, connectionId);
|
||||
checkAndLogEvent(possibleMac, key, 'input', 'boolean', eventVal, connectionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,12 +215,18 @@ wss.on('connection', (ws, req) => {
|
||||
stmtDevice.finalize();
|
||||
|
||||
// Log online event
|
||||
checkAndLogEvent(mac, 'system', 'online', true, connectionId);
|
||||
checkAndLogEvent(mac, 'system', 'online', 'boolean', true, connectionId);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.params && data.params.sys && data.params.sys.available_updates) {
|
||||
console.log(`[ID: ${connectionId}] Firmware update available for ${deviceId}:`, JSON.stringify(data.params.sys.available_updates));
|
||||
// Only notify if a stable firmware update is available
|
||||
const updates = data.params.sys.available_updates;
|
||||
const hasStableUpdate = updates.stable && updates.stable.version;
|
||||
|
||||
if (hasStableUpdate) {
|
||||
console.log(`[ID: ${connectionId}] Firmware update available for ${deviceId}:`, JSON.stringify(data.params.sys.available_updates));
|
||||
}
|
||||
}
|
||||
|
||||
if (data.method === 'NotifyStatus') {
|
||||
@@ -210,16 +234,20 @@ wss.on('connection', (ws, req) => {
|
||||
const mac = connectionDeviceMap.get(connectionId);
|
||||
if (mac) {
|
||||
for (const [key, value] of Object.entries(data.params)) {
|
||||
// Check for components like light:0, switch:0 etc.
|
||||
if (key.startsWith('light') || key.startsWith('switch')) {
|
||||
// Check for switch components
|
||||
if (key.startsWith('switch')) {
|
||||
if (typeof value.output !== 'undefined') {
|
||||
checkAndLogEvent(mac, key, 'on', value.output, connectionId);
|
||||
checkAndLogEvent(mac, key, 'on', 'boolean', value.output, connectionId);
|
||||
}
|
||||
|
||||
// Log brightness changes, suppressed if turning off
|
||||
if (typeof value.brightness !== 'undefined') {
|
||||
if (value.output !== false) {
|
||||
checkAndLogEvent(mac, key, 'brightness', value.brightness, connectionId);
|
||||
}
|
||||
// Check for light components (analog) - log level 0-100
|
||||
if (key.startsWith('light')) {
|
||||
// Calculate level: 0 if explicitly off, otherwise use brightness
|
||||
if (typeof value.output !== 'undefined' || typeof value.brightness !== 'undefined') {
|
||||
const isOff = value.output === false;
|
||||
const level = isOff ? 0 : (value.brightness !== undefined ? value.brightness : null);
|
||||
if (level !== null) {
|
||||
checkAndLogEvent(mac, key, 'level', 'range', level, connectionId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,7 +263,7 @@ wss.on('connection', (ws, req) => {
|
||||
if (mac) {
|
||||
data.params.events.forEach(evt => {
|
||||
// Pass the button event (btn_down/up/etc) as values
|
||||
checkAndLogEvent(mac, evt.component, 'button', evt.event, connectionId);
|
||||
checkAndLogEvent(mac, evt.component, 'button', 'enum', evt.event, connectionId);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -243,7 +271,8 @@ wss.on('connection', (ws, req) => {
|
||||
|
||||
const logFile = path.join(LOG_DIR, `${deviceId}.log`);
|
||||
const timestamp = new Date().toISOString();
|
||||
const logEntry = `[${timestamp}] ${msgString}\n`;
|
||||
const prettyJson = JSON.stringify(data, null, 2);
|
||||
const logEntry = `[${timestamp}]\n${prettyJson}\n\n`;
|
||||
|
||||
// Ensure log directory exists (in case it was deleted at runtime)
|
||||
if (!fs.existsSync(LOG_DIR)) {
|
||||
@@ -289,7 +318,7 @@ wss.on('connection', (ws, req) => {
|
||||
db.run("UPDATE devices SET connected = 0 WHERE mac = ?", [mac]);
|
||||
|
||||
// Log offline event
|
||||
checkAndLogEvent(mac, 'system', 'online', false, connectionId);
|
||||
checkAndLogEvent(mac, 'system', 'online', 'boolean', false, connectionId);
|
||||
|
||||
macConnectionsMap.delete(mac);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user