381 lines
14 KiB
JavaScript
381 lines
14 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Dibby Wemo Manager — Docker entry point
|
|
*
|
|
* Runs the DWM scheduler + REST/WebSocket API server without Electron.
|
|
* Configure via environment variables:
|
|
* DATA_DIR — path to persistent data directory (default: /data)
|
|
* PORT — HTTP port (default: 3456)
|
|
*/
|
|
|
|
const http = require('http');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
|
|
const DwmStore = require('./lib/store');
|
|
const DwmScheduler = require('./lib/scheduler');
|
|
const wemo = require('./lib/wemo-client');
|
|
|
|
const DATA_DIR = process.env.DATA_DIR || '/data';
|
|
const PORT = parseInt(process.env.PORT || '3456', 10);
|
|
const WEB_DIR = path.join(__dirname, 'web');
|
|
|
|
// ── Bootstrap ─────────────────────────────────────────────────────────────────
|
|
|
|
const store = new DwmStore(DATA_DIR);
|
|
const scheduler = new DwmScheduler({ store, wemoClient: wemo, log: console });
|
|
|
|
let _wss = null;
|
|
|
|
function broadcast(type, data) {
|
|
if (!_wss) return;
|
|
const msg = JSON.stringify({ type, data });
|
|
for (const client of _wss.clients) {
|
|
if (client.readyState === 1 /* OPEN */) client.send(msg);
|
|
}
|
|
}
|
|
|
|
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
|
|
function getLocalIP() {
|
|
for (const iface of Object.values(os.networkInterfaces())) {
|
|
for (const addr of iface) {
|
|
if (addr.family === 'IPv4' && !addr.internal) return addr.address;
|
|
}
|
|
}
|
|
return 'localhost';
|
|
}
|
|
|
|
function json(res, data, status = 200) {
|
|
const body = JSON.stringify(data);
|
|
res.writeHead(status, {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
});
|
|
res.end(body);
|
|
}
|
|
|
|
function jsonErr(res, msg, status = 500) {
|
|
json(res, { error: msg }, status);
|
|
}
|
|
|
|
// ── Request router ────────────────────────────────────────────────────────────
|
|
|
|
async function handleRequest(req, res) {
|
|
const url = req.url.split('?')[0];
|
|
const method = req.method.toUpperCase();
|
|
|
|
if (method === 'OPTIONS') {
|
|
res.writeHead(204, {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE',
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
});
|
|
res.end();
|
|
return;
|
|
}
|
|
|
|
const body = await new Promise((resolve) => {
|
|
if (method !== 'POST' && method !== 'PUT') return resolve({});
|
|
let raw = '';
|
|
req.on('data', (c) => { raw += c; });
|
|
req.on('end', () => { try { resolve(JSON.parse(raw)); } catch { resolve({}); } });
|
|
});
|
|
|
|
try {
|
|
// ── Debug endpoint ─────────────────────────────────────────────────────
|
|
|
|
if (url === '/api/debug/test-device' && method === 'POST') {
|
|
const { host, port } = body;
|
|
if (!host) {
|
|
return jsonErr(res, 'Host is required', 400);
|
|
}
|
|
|
|
const devicePort = port ? parseInt(port, 10) : 49153;
|
|
|
|
console.log(`[DWM] Debug test for ${host}:${devicePort}`);
|
|
|
|
const results = {};
|
|
|
|
// Test 1: HTTP connection to setup.xml
|
|
try {
|
|
const setupUrl = `http://${host}:${devicePort}/setup.xml`;
|
|
console.log(`[DWM] Testing setup.xml URL: ${setupUrl}`);
|
|
const response = await axios.get(setupUrl, {
|
|
timeout: 5000,
|
|
httpAgent: NO_KEEPALIVE
|
|
});
|
|
results.setupXml = {
|
|
status: response.status,
|
|
dataLength: response.data.length,
|
|
success: response.status === 200
|
|
};
|
|
console.log(`[DWM] setup.xml response:`, results.setupXml);
|
|
} catch (err) {
|
|
results.setupXml = {
|
|
success: false,
|
|
error: err.message,
|
|
code: err.code
|
|
};
|
|
console.log(`[DWM] setup.xml failed:`, err.message);
|
|
}
|
|
|
|
// Test 2: Try to get device info
|
|
try {
|
|
console.log(`[DWM] Testing getDeviceInfo`);
|
|
const deviceInfo = await wemo.getDeviceInfo(host, devicePort);
|
|
results.deviceInfo = {
|
|
success: true,
|
|
data: deviceInfo
|
|
};
|
|
console.log(`[DWM] getDeviceInfo successful:`, deviceInfo);
|
|
} catch (err) {
|
|
results.deviceInfo = {
|
|
success: false,
|
|
error: err.message
|
|
};
|
|
console.log(`[DWM] getDeviceInfo failed:`, err.message);
|
|
}
|
|
|
|
// Test 3: Try to get binary state
|
|
try {
|
|
console.log(`[DWM] Testing getBinaryState`);
|
|
const state = await wemo.getBinaryState(host, devicePort);
|
|
results.binaryState = {
|
|
success: true,
|
|
state: state
|
|
};
|
|
console.log(`[DWM] getBinaryState successful:`, state);
|
|
} catch (err) {
|
|
results.binaryState = {
|
|
success: false,
|
|
error: err.message
|
|
};
|
|
console.log(`[DWM] getBinaryState failed:`, err.message);
|
|
}
|
|
|
|
return json(res, results);
|
|
}
|
|
|
|
// ── Devices ────────────────────────────────────────────────────────────
|
|
|
|
if (url === '/api/devices' && method === 'GET') {
|
|
const devices = store.getDevices();
|
|
// Add dimmer detection to each device
|
|
const devicesWithDimmerInfo = devices.map(device => ({
|
|
...device,
|
|
isDimmer: wemo.isDimmerDevice ? wemo.isDimmerDevice(device) : false
|
|
}));
|
|
return json(res, devicesWithDimmerInfo);
|
|
}
|
|
|
|
if (url === '/api/devices/discover' && method === 'POST') {
|
|
const saved = store.getDevices();
|
|
const manual = saved.map((d) => ({ host: d.host, port: d.port }));
|
|
const devices = await wemo.discoverDevices(8000, manual);
|
|
store.saveDevices(devices);
|
|
return json(res, devices);
|
|
}
|
|
|
|
if (url === '/api/devices/add' && method === 'POST') {
|
|
const { host, port } = body;
|
|
if (!host) {
|
|
return jsonErr(res, 'Host is required', 400);
|
|
}
|
|
|
|
const devicePort = port ? parseInt(port, 10) : 49153;
|
|
const manualEntry = { host, port: devicePort };
|
|
|
|
console.log(`[DWM] Attempting to add device manually: ${host}:${devicePort}`);
|
|
|
|
try {
|
|
// First try direct device info fetch
|
|
let device = null;
|
|
try {
|
|
console.log(`[DWM] Trying direct device info fetch for ${host}:${devicePort}`);
|
|
device = await wemo.getDeviceInfo(host, devicePort);
|
|
if (device) {
|
|
device.host = host;
|
|
device.port = devicePort;
|
|
device.manual = true; // Mark as manually added
|
|
console.log(`[DWM] Direct fetch successful:`, device);
|
|
}
|
|
} catch (directErr) {
|
|
console.log(`[DWM] Direct fetch failed:`, directErr.message);
|
|
}
|
|
|
|
// If direct fetch failed, try discovery with manual entry
|
|
if (!device) {
|
|
console.log(`[DWM] Trying discovery with manual entry`);
|
|
const devices = await wemo.discoverDevices(5000, [manualEntry]);
|
|
if (devices.length > 0) {
|
|
device = devices[0];
|
|
device.manual = true; // Mark as manually added
|
|
console.log(`[DWM] Discovery successful:`, device);
|
|
}
|
|
}
|
|
|
|
if (device) {
|
|
// Add to existing devices
|
|
const allDevices = [...store.getDevices(), ...[device]];
|
|
store.saveDevices(allDevices);
|
|
console.log(`[DWM] Device added successfully: ${device.friendlyName || device.host}`);
|
|
return json(res, device, 201);
|
|
} else {
|
|
console.log(`[DWM] No device found at ${host}:${devicePort}`);
|
|
return jsonErr(res, `No Wemo device found at ${host}:${devicePort}. Check IP address and port.`, 404);
|
|
}
|
|
} catch (err) {
|
|
console.log(`[DWM] Error adding device:`, err);
|
|
return jsonErr(res, `Failed to connect: ${err.message}`, 500);
|
|
}
|
|
}
|
|
|
|
const stateMatch = url.match(/^\/api\/devices\/([^/]+)\/(\d+)\/state$/);
|
|
if (stateMatch) {
|
|
const [, host, port] = stateMatch;
|
|
if (method === 'GET') {
|
|
const on = await wemo.getBinaryState(host, Number(port));
|
|
return json(res, { on });
|
|
}
|
|
if (method === 'POST') {
|
|
await wemo.setBinaryState(host, Number(port), !!body.on);
|
|
return json(res, { ok: true });
|
|
}
|
|
}
|
|
|
|
// ── Device Information ───────────────────────────────────────────────────
|
|
|
|
const deviceInfoMatch = url.match(/^\/api\/devices\/([^/]+)\/(\d+)\/info$/);
|
|
if (deviceInfoMatch && method === 'GET') {
|
|
const [, host, port] = deviceInfoMatch;
|
|
try {
|
|
const info = await wemo.getDeviceInfo(host, Number(port));
|
|
return json(res, info);
|
|
} catch (err) {
|
|
return jsonErr(res, `Failed to get device info: ${err.message}`, 500);
|
|
}
|
|
}
|
|
|
|
// ── Brightness control ───────────────────────────────────────────────────
|
|
|
|
const brightnessMatch = url.match(/^\/api\/devices\/([^/]+)\/(\d+)\/brightness$/);
|
|
if (brightnessMatch) {
|
|
const [, host, port] = brightnessMatch;
|
|
if (method === 'GET') {
|
|
const brightness = await wemo.getBrightness(host, Number(port));
|
|
return json(res, { brightness });
|
|
}
|
|
if (method === 'POST') {
|
|
await wemo.setBrightness(host, Number(port), body.brightness);
|
|
return json(res, { ok: true });
|
|
}
|
|
}
|
|
|
|
// ── DWM Rules ──────────────────────────────────────────────────────────
|
|
|
|
if (url === '/api/dwm-rules') {
|
|
if (method === 'GET') return json(res, store.getDwmRules());
|
|
if (method === 'POST') {
|
|
const rule = store.createDwmRule(body);
|
|
scheduler.reload();
|
|
return json(res, rule, 201);
|
|
}
|
|
}
|
|
|
|
const ruleMatch = url.match(/^\/api\/dwm-rules\/(.+)$/);
|
|
if (ruleMatch) {
|
|
const id = ruleMatch[1];
|
|
if (method === 'PUT') {
|
|
const updated = store.updateDwmRule(id, body);
|
|
scheduler.reload();
|
|
return json(res, updated);
|
|
}
|
|
if (method === 'DELETE') {
|
|
store.deleteDwmRule(id);
|
|
scheduler.reload();
|
|
return json(res, { ok: true });
|
|
}
|
|
}
|
|
|
|
// ── Wemo Device Rules ──────────────────────────────────────────────────
|
|
|
|
const wemoRulesMatch = url.match(/^\/api\/devices\/([^/]+)\/(\d+)\/rules$/);
|
|
if (wemoRulesMatch && method === 'GET') {
|
|
const [, host, port] = wemoRulesMatch;
|
|
return json(res, await wemo.fetchRules(host, Number(port)));
|
|
}
|
|
|
|
const wemoRuleMatch = url.match(/^\/api\/devices\/([^/]+)\/(\d+)\/rules\/(\d+)$/);
|
|
if (wemoRuleMatch && method === 'PUT') {
|
|
const [, host, port, ruleId] = wemoRuleMatch;
|
|
await wemo.updateRule(host, Number(port), Number(ruleId), body);
|
|
return json(res, { ok: true });
|
|
}
|
|
|
|
// ── Scheduler ──────────────────────────────────────────────────────────
|
|
|
|
if (url === '/api/scheduler/status' && method === 'GET') {
|
|
return json(res, scheduler.getStatus());
|
|
}
|
|
|
|
// ── Static assets ──────────────────────────────────────────────────────
|
|
|
|
if (url === '/icon.png' && method === 'GET') {
|
|
const file = path.join(__dirname, 'icon.png');
|
|
fs.readFile(file, (e, data) => {
|
|
if (e) { res.writeHead(404); res.end(); return; }
|
|
res.writeHead(200, { 'Content-Type': 'image/png' });
|
|
res.end(data);
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Fallback → serve mobile web UI
|
|
const file = path.join(WEB_DIR, 'index.html');
|
|
fs.readFile(file, (e, data) => {
|
|
if (e) { res.writeHead(404); res.end('Web UI not found'); return; }
|
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
res.end(data);
|
|
});
|
|
|
|
} catch (e) {
|
|
jsonErr(res, e.message);
|
|
}
|
|
}
|
|
|
|
// ── Start ─────────────────────────────────────────────────────────────────────
|
|
|
|
async function main() {
|
|
// Ensure data directory exists
|
|
fs.mkdirSync(DATA_DIR, { recursive: true });
|
|
|
|
await scheduler.start();
|
|
console.log(`[DWM] Scheduler started — data: ${DATA_DIR}`);
|
|
|
|
const server = http.createServer(handleRequest);
|
|
|
|
let WebSocketServer;
|
|
try { WebSocketServer = require('ws').WebSocketServer || require('ws').Server; } catch {}
|
|
|
|
if (WebSocketServer) {
|
|
_wss = new WebSocketServer({ server });
|
|
_wss.on('connection', (ws) => {
|
|
ws.send(JSON.stringify({ type: 'scheduler-status', data: scheduler.getStatus() }));
|
|
});
|
|
scheduler.onFire = (event) => broadcast('scheduler-fired', event);
|
|
scheduler.onStatus = (status) => broadcast('scheduler-status', status);
|
|
}
|
|
|
|
server.listen(PORT, '0.0.0.0', () => {
|
|
console.log(`[DWM] Web Remote: http://${getLocalIP()}:${PORT}`);
|
|
});
|
|
|
|
process.on('SIGTERM', () => { server.close(); process.exit(0); });
|
|
process.on('SIGINT', () => { server.close(); process.exit(0); });
|
|
}
|
|
|
|
main().catch((e) => { console.error('[DWM] Fatal:', e); process.exit(1); });
|