diff --git a/maxcube-commandparser.js b/maxcube-commandparser.js index 08c4cb9..bf6ac58 100644 --- a/maxcube-commandparser.js +++ b/maxcube-commandparser.js @@ -62,6 +62,11 @@ function parseCommandHello (payload) { function parseCommandMetadata (payload) { var payloadArr = payload.split(","); + if (payloadArr.length < 3) { + console.error('Invalid Metadata received'); + return { rooms: {}, devices: {} }; + } + var decodedPayload = new Buffer(payloadArr[2], 'base64'); var room_count = decodedPayload[2]; var currentIndex = 3; @@ -205,9 +210,20 @@ function decodeDevice (payload) { var deviceStatus = {}; var deviceType = undefined; switch (payload[0]) { - case 8: deviceType = EQ3MAX_DEV_TYPE_PUSH_BUTTON; break; + case 6: + if (payload[1] === 9) { + deviceType = EQ3MAX_DEV_TYPE_PUSH_BUTTON; + deviceStatus = decodeDeviceButton (payload); + } else { + deviceType = EQ3MAX_DEV_TYPE_SHUTTER_CONTACT; + deviceStatus = decodeDeviceContact (payload); + } + + break; + case 8: deviceType = EQ3MAX_DEV_TYPE_PUSH_BUTTON; deviceStatus = decodeDeviceButton (payload); break; case 11: deviceType = EQ3MAX_DEV_TYPE_THERMOSTAT; deviceStatus = decodeDeviceThermostat (payload); break; case 12: deviceType = EQ3MAX_DEV_TYPE_WALLTHERMOSTAT; deviceStatus = decodeDeviceThermostat (payload); break; + case 13: deviceType = EQ3MAX_DEV_TYPE_THERMOSTAT_PLUS; deviceStatus = decodeDeviceThermostat (payload); break; // ??? if 13 is correct default: deviceType = EQ3MAX_DEV_TYPE_UNKNOWN; break; } @@ -216,6 +232,40 @@ function decodeDevice (payload) { return deviceStatus; } +function decodeDeviceContact (payload) { + var deviceStatus = { + rf_address: payload.slice(1, 4).toString('hex'), + opened: (payload[5] & 0x2) && !(payload[5] & 0x1), + initialized: !!(payload[5] & (1 << 1)), + fromCmd: !!(payload[5] & (1 << 2)), + error: !!(payload[5] & (1 << 3)), + valid: !!(payload[5] & (1 << 4)), + dst_active: !!(payload[6] & (1 << 3)), + gateway_known: !!(payload[6] & (1 << 4)), + link_error: !!(payload[6] & (1 << 6)), + battery_low: !!(payload[6] & (1 << 7)), + opened: !!(payload[6] & (1 << 1)) + }; + return deviceStatus; +} + +function decodeDeviceButton (payload) { + var deviceStatus = { + rf_address: payload.slice(1, 4).toString('hex'), + opened: (payload[5] & 0x2) && !(payload[5] & 0x1), + initialized: !!(payload[5] & (1 << 1)), + fromCmd: !!(payload[5] & (1 << 2)), + error: !!(payload[5] & (1 << 3)), + valid: !!(payload[5] & (1 << 4)), + dst_active: !!(payload[6] & (1 << 3)), + gateway_known: !!(payload[6] & (1 << 4)), + link_error: !!(payload[6] & (1 << 6)), + battery_low: !!(payload[6] & (1 << 7)), + eco_mode: !!(payload[6] & (1 << 1)) + }; + return deviceStatus; +} + function decodeDeviceThermostat (payload) { /* source: http://www.domoticaforum.eu/viewtopic.php?f=66&t=6654 diff --git a/maxcube-lowlevel.js b/maxcube-lowlevel.js index 09af07f..eff8001 100644 --- a/maxcube-lowlevel.js +++ b/maxcube-lowlevel.js @@ -48,7 +48,7 @@ function initSocket () { this.socket.on('error', function(err) { console.error(err); - self.emit('error'); + self.emit('error', err); }); } diff --git a/maxcube.js b/maxcube.js index a4f64d3..a8e015b 100644 --- a/maxcube.js +++ b/maxcube.js @@ -49,6 +49,7 @@ function MaxCube(ip, port) { case 'H': { self.commStatus.duty_cycle = parsedCommand.duty_cycle; self.commStatus.free_memory_slots = parsedCommand.free_memory_slots; + self.emit('hello', parsedCommand); break; } case 'M': {