thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
162 lines (151 loc) • 5.72 kB
JavaScript
module.exports = {
metadata: {
plugin: "tempHumCo2Sensor",
label: "EnOcean IP Temperature, Humidity and CO2 Sensor",
role: "sensor",
family: "tempHumCo2Sensor",
deviceTypes: ["enocean-ip/gateway"],
tangible: false,
services: [],
events: [{
label: "Medium CO2 Concentration",
id: "mediumCO2Concentration"
}, {
label: "High CO2 Concentration",
id: "highCO2Concentration"
},{
label: "Very High CO2 Concentration",
id: "veryHighCO2Concentration"
}
],
state: [{
label: "Humidity",
id: "humidity",
type: {
id: "decimal"
}
}, {
label: "CO2 Concentration",
id: "co2Concentration",
type: {
id: "decimal"
}
}, {
label: "Temperature",
id: "temperature",
type: {
id: "decimal"
}
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}, {
label: "Concentration Warnings",
id: "concentrationWarnings",
type: {
id: "boolean"
},
defaultValue: ""
}]
},
create: function () {
return new TempHumCO2Sensor();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function TempHumCO2Sensor() {
/**
*
*/
TempHumCO2Sensor.prototype.start = function () {
var deferred = q.defer();
this.state = {};
if (this.isSimulated()) {
this.publishStateChange();
}
else {
this.device.adapter.getDeviceState(this.configuration.deviceId).then(function(body){
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 (body.state.functions[n].key === "concentration") {
this.state.co2Concentration = body.state.functions[n].value;
if(this.configuration.concentrationWarnings) {
if (body.state.functions[n].value >= 700 && body.state.functions[n].value < 850) this.publishEvent("mediumCO2Concentration", {});
if (body.state.functions[n].value >= 850 && body.state.functions[n].value < 1050) this.publishEvent("highCO2Concentration", {});
if (body.state.functions[n].value >= 1050) this.publishEvent("veryHighCO2Concentration", {});
}
this.logDebug("CO2Concentration: " + this.state.co2Concentration);
}
if (body.state.functions[n].key === "temperature") {
this.state.temperature = body.state.functions[n].value;
this.logDebug("Temperature: " + this.state.temperature);
}
}
this.publishStateChange();
}.bind(this), function (err){
console.log(err)
}.bind(this));
// Retrieve current state
this.device.adapter.listeners.push(telegram => {
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 (telegram.functions[n].key === "concentration") {
this.state.co2Concentration = telegram.functions[n].value;
if(this.configuration.concentrationWarnings) {
if (telegram.functions[n].value >= 700 && telegram.functions[n].value < 850) this.publishEvent("mediumCO2Concentration", {});
if (telegram.functions[n].value >= 850 && telegram.functions[n].value < 1050) this.publishEvent("highCO2Concentration", {});
if (telegram.functions[n].value >= 1050) this.publishEvent("veryHighCO2Concentration", {});
}
this.logDebug("CO2 Concentration: " + this.state.co2Concentration);
}
if (telegram.functions[n].key === "temperature") {
this.state.temperature = telegram.functions[n].value;
this.logDebug("Temperature: " + this.state.temperature);
}
}
this.publishStateChange();
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
TempHumCO2Sensor.prototype.getState = function () {
return this.state;
};
/**
*
*/
TempHumCO2Sensor.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
TempHumCO2Sensor.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}