This commit is contained in:
+51
-9
@@ -88,19 +88,46 @@ async function handleRequest(req, res) {
|
||||
// ── Devices ────────────────────────────────────────────────────────────
|
||||
|
||||
if (url === '/api/devices' && method === 'GET') {
|
||||
return json(res, store.getDevices());
|
||||
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 }));
|
||||
// Add any manual entries from the request body
|
||||
if (body.manualEntries && Array.isArray(body.manualEntries)) {
|
||||
manual.push(...body.manualEntries);
|
||||
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 };
|
||||
|
||||
try {
|
||||
// Try to discover this specific device
|
||||
const devices = await wemo.discoverDevices(5000, [manualEntry]);
|
||||
if (devices.length > 0) {
|
||||
// Add to existing devices
|
||||
const allDevices = [...store.getDevices(), ...devices];
|
||||
store.saveDevices(allDevices);
|
||||
return json(res, devices[0], 201);
|
||||
} else {
|
||||
return jsonErr(res, 'No Wemo device found at this address', 404);
|
||||
}
|
||||
} catch (err) {
|
||||
return jsonErr(res, `Failed to connect: ${err.message}`, 500);
|
||||
}
|
||||
const devs = await wemo.discoverDevices(8000, manual);
|
||||
store.saveDevices(devs);
|
||||
return json(res, devs);
|
||||
}
|
||||
|
||||
const stateMatch = url.match(/^\/api\/devices\/([^/]+)\/(\d+)\/state$/);
|
||||
@@ -116,6 +143,21 @@ async function handleRequest(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── 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') {
|
||||
|
||||
Reference in New Issue
Block a user