This commit is contained in:
@@ -122,6 +122,70 @@ async function setBinaryState(host, port, on) {
|
||||
await soapWithFallback(host, port, BE_URL, BE_SVC, 'SetBinaryState', { BinaryState: on ? '1' : '0' });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dimmer control
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function getBrightness(host, port) {
|
||||
try {
|
||||
const res = await soapWithFallback(host, port, BE_URL, BE_SVC, 'GetBinaryState');
|
||||
const raw = String(res['BinaryState'] ?? '0');
|
||||
|
||||
// For dimmers, BinaryState contains brightness info in format: "1|brightness|..."
|
||||
// Example: "1|50|0" where 50 is the brightness level (0-100)
|
||||
if (raw.includes('|')) {
|
||||
const parts = raw.split('|');
|
||||
if (parts.length >= 2) {
|
||||
const brightness = parseInt(parts[1], 10);
|
||||
return !isNaN(brightness) ? brightness : null;
|
||||
}
|
||||
}
|
||||
|
||||
// If device is off, return 0
|
||||
return raw === '1' || raw === '8' ? 100 : 0;
|
||||
} catch (err) {
|
||||
// Fallback for non-dimmer devices
|
||||
const isOn = await getBinaryState(host, port);
|
||||
return isOn ? 100 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
async function setBrightness(host, port, brightness) {
|
||||
// Brightness should be 0-100
|
||||
const level = Math.max(0, Math.min(100, Math.round(brightness)));
|
||||
|
||||
if (level === 0) {
|
||||
// Turn off the device
|
||||
await setBinaryState(host, port, false);
|
||||
} else {
|
||||
// For dimmers, use the brightness format: "brightness|0"
|
||||
// For non-dimmers, just turn on
|
||||
try {
|
||||
await soapWithFallback(host, port, BE_URL, BE_SVC, 'SetBinaryState', {
|
||||
BinaryState: `${level}|0`
|
||||
});
|
||||
} catch (err) {
|
||||
// Fallback for non-dimmer devices - just turn on
|
||||
await setBinaryState(host, port, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isDimmerDevice(deviceInfo) {
|
||||
if (!deviceInfo) return false;
|
||||
|
||||
const { productModel, modelDescription, udn } = deviceInfo;
|
||||
|
||||
// Check various indicators that this is a dimmer
|
||||
return (
|
||||
(productModel && productModel.toLowerCase().includes('dimmer')) ||
|
||||
(modelDescription && modelDescription.toLowerCase().includes('dimmer')) ||
|
||||
(udn && udn.toLowerCase().includes('dimmer')) ||
|
||||
(productModel && productModel.includes('WDS060')) ||
|
||||
(modelDescription && modelDescription.includes('Dimmer'))
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Device info
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -478,6 +542,9 @@ function _insertNewRule(db, ruleId, ruleData) {
|
||||
module.exports = {
|
||||
getBinaryState,
|
||||
setBinaryState,
|
||||
getBrightness,
|
||||
setBrightness,
|
||||
isDimmerDevice,
|
||||
getDeviceInfo,
|
||||
discoverDevices,
|
||||
fetchRules,
|
||||
|
||||
Reference in New Issue
Block a user