thing-it-device-enocean-ip
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -114,2 +114,4 @@ module.exports = { | ||
this.ticCountArray = []; | ||
this.lastProcessedTS = Date.now(); | ||
this.lastReceivedTS = Date.now(); | ||
@@ -137,8 +139,10 @@ if (!this.configuration.ghostTickInterval) { | ||
//if (this.configuration.sensorType === "BATTERYSENSOR") { | ||
//TODO this should be only started if the device is initialized and connected | ||
//TODO Interval only if battery sensor | ||
if (this.configuration.sensorType === "BATTERYSENSOR") { | ||
this.ticksUpdateInterval = setInterval(function () { | ||
this.updateTicksOverTime(); | ||
this.updateTicsOverTime(); | ||
this.publishStateChange(); | ||
}.bind(this), 60000); | ||
// } | ||
} | ||
@@ -158,3 +162,3 @@ this.device.adapter.listeners.push(telegram => { | ||
this.ticksTimestampsArray.unshift(Date.now()); | ||
this.updateTicksOverTime(); | ||
this.updateTicsOverTime(); | ||
this.publishStateChange(); | ||
@@ -181,3 +185,3 @@ | ||
this.ticksTimestampsArray.unshift(Date.now()); | ||
this.updateTicksOverTime(); | ||
this.updateTicsOverTime(); | ||
@@ -211,47 +215,60 @@ this.publishStateChange(); | ||
dataArray = telegram.telegramInfo.data.match(/.{1,2}/g); | ||
let timeStamp = Date.now(); | ||
if (dataArray[3] === "0D") { | ||
this.state.lastMotionTimestamp = moment().toISOString(); | ||
this.ticksTimestampsArray.unshift(Date.now()); | ||
// "OD" means motion detected? Means if we do not get "0D" we can assume that there is also no motion data? | ||
switch (dataArray[3]) { | ||
if (this.occupancyTimeout) { | ||
clearTimeout(this.occupancyTimeout); | ||
} | ||
case "0D": //MOTION DETECTED | ||
this.state.lastMotionTimestamp = moment().toISOString(); | ||
if (!this.state.occupied) { | ||
this.state.occupied = true; | ||
this.publishEvent('motionDetected', {}); | ||
this.publishStateChange({occupied: this.state.occupied}); | ||
} | ||
} else { | ||
if (this.tickRepeatInterval) { | ||
clearInterval(this.tickRepeatInterval); | ||
} | ||
} | ||
if (!this.state.occupied) { | ||
this.state.occupied = true; | ||
this.publishEvent('motionDetected', {}); | ||
this.publishStateChange({occupied: this.state.occupied}); //Only publish the changed state TODO Not really needed here? | ||
} | ||
ticCounter = dataArray[1] + dataArray[2]; | ||
this.ticCountArray.unshift(parseInt(ticCounter, 16)); | ||
//RESET OCCUPANCY TIMOUT | ||
if (this.occupancyTimeout) { | ||
clearTimeout(this.occupancyTimeout); | ||
} | ||
if (this.ticCountArray[0] < this.ticCountArray[1]) tic = this.ticCountArray[0] + (65535 - this.ticCountArray[1]); | ||
else tic = this.ticCountArray[0] - this.ticCountArray[1]; | ||
//SET NEW OCCUPANCY TIMEOUT | ||
this.occupancyTimeout = setTimeout(() => { | ||
this.state.occupied = false; | ||
this.publishEvent('noMoreMotion', {}); | ||
this.publishStateChange({occupied: this.state.occupied}); //Only publish the changed state | ||
}, this.configuration.releaseTime * 1000); | ||
duration = (this.ticksTimestampsArray[0] - this.ticksTimestampsArray[1]) / 60000; | ||
//EXTRACT TICS FROM THE LATEST TELEGRAM | ||
ticCounter = dataArray[1] + dataArray[2]; | ||
this.ticCountArray.unshift(parseInt(ticCounter, 16)); | ||
if (this.ticCountArray[0] < this.ticCountArray[1]) { | ||
tic = this.ticCountArray[0] + (65535 - this.ticCountArray[1]); | ||
} else { | ||
tic = this.ticCountArray[0] - this.ticCountArray[1]; | ||
} | ||
if (this.ticksTimestampsArray.length > 1) { | ||
this.state.ticksPerMinute = Math.round(tic / duration); | ||
} else { | ||
this.state.ticksPerMinute = 0; | ||
} | ||
//SPREAD "tic" TIMESTAMPS TO tickTimestampArray | ||
let timestampDifference = timeStamp - this.lastReceivedTS; | ||
if (timestampDifference > 120000) timestampDifference = 120000; | ||
for (let i = tic; i >= 0; i--) { | ||
this.ticksTimestampsArray.push(Math.round(timeStamp - (timestampDifference / tic * i))); | ||
} | ||
this.lastReceivedTS = timeStamp; | ||
break; | ||
this.publishStateChange({ | ||
ticksPerMinute: this.state.ticksPerMinute, | ||
lastMotionTimestamp: this.state.lastMotionTimestamp | ||
}); | ||
case "09": //NO MOTION DETECTED. Normally after 10 minutes | ||
this.lastReceivedTS = timeStamp; | ||
break; | ||
this.occupancyTimeout = setTimeout(() => { | ||
this.state.occupied = false; | ||
this.publishEvent('noMoreMotion', {}); | ||
this.publishStateChange({occupied: this.state.occupied}); | ||
}, this.configuration.releaseTime * 1000); | ||
default: | ||
this.logDebug("Unknown telegram type..."); | ||
break; | ||
} | ||
let ticHistory = this.processHistoricalTics(); | ||
if (ticHistory !== null) { | ||
this.publishStateChangeHistory(ticHistory); | ||
} | ||
} | ||
@@ -267,3 +284,37 @@ } | ||
OccupancySensor.prototype.updateTicksOverTime = function () { | ||
/** | ||
* | ||
*/ | ||
OccupancySensor.prototype.processHistoricalTics = function () { | ||
const minute = 60000; | ||
if (this.lastReceivedTS - this.lastProcessedTS >= minute) { | ||
let stateHistory = []; | ||
while ((this.lastReceivedTS - this.lastProcessedTS) >= minute) { | ||
if ((this.ticksTimestampsArray.length !== 0) && (this.ticksTimestampsArray[0] <= (this.lastProcessedTS + minute))) { | ||
let ticsInSelectedMinute = []; | ||
while (this.ticksTimestampsArray[0] <= (this.lastProcessedTS + minute)) { | ||
ticsInSelectedMinute.push(this.ticksTimestampsArray.shift()); | ||
} | ||
stateHistory.push({ | ||
timestamp: new Date(this.lastProcessedTS + minute).toISOString(), | ||
state: {ticksPerMinute: ticsInSelectedMinute.length}, | ||
}); | ||
} else { | ||
stateHistory.push({ | ||
timestamp: new Date(this.lastProcessedTS + minute).toISOString(), | ||
state: {ticksPerMinute: 0}, | ||
}); | ||
} | ||
this.lastProcessedTS += 60000; | ||
} | ||
return stateHistory; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
/** | ||
* | ||
*/ | ||
OccupancySensor.prototype.updateTicsOverTime = function () { | ||
let timestamp = Date.now(); | ||
@@ -270,0 +321,0 @@ let tickCountTimeMinutes = this.configuration.tickCountTimeMinutes || 5; |
@@ -8,2 +8,14 @@ module.exports = { | ||
deviceTypes: ["enocean-ip/gateway"], | ||
dataTypes: { | ||
sensorType: { | ||
family: "enumeration", | ||
values: [{ | ||
id: "ENOCEANSENSOR", | ||
label: "EnOcean Sensor" | ||
}, { | ||
id: "LORASENSOR", | ||
label: "Lora Sensor" | ||
}] | ||
} | ||
}, | ||
tangible: false, | ||
@@ -31,2 +43,10 @@ services: [], | ||
} | ||
}, { | ||
label: "Sensor Type", | ||
id: "sensorType", | ||
type: { | ||
family: "reference", | ||
id: "sensorType" | ||
}, | ||
defaultValue: "ENOCEANSENSOR" | ||
}] | ||
@@ -61,6 +81,14 @@ }, | ||
for(var n in body.state.functions){ | ||
if(body.state.functions[n].key === "humidity"){ | ||
this.state.humidity = body.state.functions[n].value; | ||
this.logDebug("Humidity: "+ this.state.humidity); | ||
if (this.configuration.sensorType === "LORASENSOR") { | ||
if (body.state.functions[n].key === "relativeHumidity") { | ||
this.state.humidity = body.state.functions[n].value; | ||
this.logDebug("Humidity: " + this.state.humidity); | ||
} | ||
} | ||
else{ | ||
if (body.state.functions[n].key === "humidity") { | ||
this.state.humidity = body.state.functions[n].value; | ||
this.logDebug("Humidity: " + this.state.humidity); | ||
} | ||
} | ||
if (body.state.functions[n].key === "temperature") { | ||
@@ -80,8 +108,15 @@ this.state.temperature = body.state.functions[n].value; | ||
if (telegram.deviceId === this.configuration.deviceId) { | ||
//console.log(telegram); | ||
for (var n in telegram.functions) { | ||
if (telegram.functions[n].key === "humidity") { | ||
this.state.humidity = telegram.functions[n].value; | ||
this.logDebug("Humidity: " + this.state.humidity) | ||
if (this.configuration.sensorType === 'LORASENSOR') { | ||
if (telegram.functions[n].key === "relativeHumidity") { | ||
this.state.humidity = telegram.functions[n].value; | ||
this.logDebug("Humidity: " + this.state.humidity); | ||
} | ||
} | ||
else{ | ||
if (telegram.functions[n].key === "humidity") { | ||
this.state.humidity = telegram.functions[n].value; | ||
this.logDebug("Humidity: " + this.state.humidity); | ||
} | ||
} | ||
if (telegram.functions[n].key === "temperature") { | ||
@@ -88,0 +123,0 @@ this.state.temperature = telegram.functions[n].value; |
@@ -153,4 +153,4 @@ module.exports = { | ||
this.request = require('request-json'); | ||
this.client = this.request.createClient('http://' + account + ':' + password + '@' + host + ':' + port + '/'); | ||
this.listeners = []; | ||
@@ -228,3 +228,3 @@ let message = ''; | ||
if (message.telegram) { | ||
console.log('##################################### Telegram recieved. ', message.telegram); | ||
//console.log('##################################### Telegram recieved. ', message.telegram); | ||
for (var n in this.listeners) { | ||
@@ -231,0 +231,0 @@ this.listeners[n](message.telegram); |
{ | ||
"name": "thing-it-device-enocean-ip", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "[thing-it-node] Device Plugin for EnOcean IP products.", | ||
@@ -5,0 +5,0 @@ "authors": "Marc Gille", |
137711
3306