diff --git a/index.js b/index.js index 954aab58f..02cda7206 100644 --- a/index.js +++ b/index.js @@ -105,7 +105,11 @@ function handleMessage(msg) { const modelID = msg.endpoints[0].device.modelId; const deviceID = msg.endpoints[0].devId; const cid = msg.data.cid; - const parser = parsers.find((p) => p.supportedDevices.includes((deviceID, modelID)) && p.cid === cid); + const parser = parsers.find((p) => { + const device = p.supportedDevices.find((device) => device[0] === deviceID && device[1] === modelID); + return device && p.cid === cid; + }); + if (!parser) { console.log(` WARNING: No parser available for (${deviceID}, '${modelID}') for cid: ${cid} diff --git a/parsers.js b/parsers.js index d67d79907..2434ceb98 100644 --- a/parsers.js +++ b/parsers.js @@ -7,19 +7,19 @@ const clickLookup = { } // Used as reference: (DeviceID, ModelID, Description) -const sensorMapping = [ - (260, 'lumi.sensor_switch', 'WXKG01LM - button switch'), - (24321, 'lumi.sens', 'WSDCGQ01LM - temprature/humidity sensor'), - (260, 'lumi.sensor_motion', 'RTCGQ11LM - Human body sensor'), - (260, 'lumi.sensor_magnet', 'YTC4005CN - Magnet door/window sensor'), +const documentation = [ + [260, 'lumi.sensor_switch', 'WXKG01LM - button switch'], + [24321, 'lumi.sens', 'WSDCGQ01LM - temprature/humidity sensor'], + [260, 'lumi.sensor_motion', 'RTCGQ11LM - Human body sensor'], + [260, 'lumi.sensor_magnet', 'YTC4005CN - Magnet door/window sensor'], ] // Global variable store that can be used by devices. const store = {} -module.exports = [ +const parsers = [ { - supportedDevices: [(260, 'lumi.sensor_switch')], + supportedDevices: [[260, 'lumi.sensor_switch']], cid: 'genOnOff', topic: 'switch', parse: (msg, publish) => { @@ -53,19 +53,19 @@ module.exports = [ } }, { - supportedDevices: [(24321, 'lumi.sens')], + supportedDevices: [[24321, 'lumi.sens']], cid: 'msTemperatureMeasurement', topic: 'temperature', parse: (msg) => parseFloat(msg.data.data['measuredValue']) / 100.0 }, { - supportedDevices: [(24321, 'lumi.sens')], + supportedDevices: [[24321, 'lumi.sens']], cid: 'msRelativeHumidity', topic: 'humidity', parse: (msg) => parseFloat(msg.data.data['measuredValue']) / 100.0 }, { - supportedDevices: [(260, 'lumi.sensor_motion')], + supportedDevices: [[260, 'lumi.sensor_motion']], cid: 'msOccupancySensing', topic: 'occupancy', parse: (msg, publish) => { @@ -89,9 +89,26 @@ module.exports = [ } }, { - supportedDevices: [(260, 'lumi.sensor_magnet')], + supportedDevices: [[260, 'lumi.sensor_magnet']], cid: 'genOnOff', topic: 'state', parse: (msg) => msg.data.data['onOff'] ? 'open' : 'closed' } -]; \ No newline at end of file +]; + +// Ensure consistency of documentation. +parsers.forEach((parser) => { + parser.supportedDevices.forEach(device => { + const docs = documentation.filter(doc => { + return device[0] === doc[0] && device[1] === doc[1]; + }); + + if (docs.length === 0) { + console.log("ERROR: Incosistent documentation"); + console.log(`Please add [${device[0]}, '${device[1]}', 'Add description here'], to documentation`); + process.exit() + } + }); +}); + +module.exports = parsers;