thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
156 lines (145 loc) • 4.85 kB
JavaScript
module.exports = {
metadata: {
plugin: "temperatureHumidity",
label: "EnOcean IP Temperature and Humidity Sensor",
role: "sensor",
family: "temperatureHumidity",
deviceTypes: ["enocean-ip/gateway"],
dataTypes: {
sensorType: {
family: "enumeration",
values: [{
id: "ENOCEANSENSOR",
label: "EnOcean Sensor"
}, {
id: "LORASENSOR",
label: "Lora Sensor"
}]
}
},
tangible: false,
services: [],
events: [],
state: [{
label: "Humidity",
id: "humidity",
type: {
id: "decimal"
}
}, {
label: "Temperature",
id: "temperature",
type: {
id: "decimal"
}
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}, {
label: "Sensor Type",
id: "sensorType",
type: {
family: "reference",
id: "sensorType"
},
defaultValue: "ENOCEANSENSOR"
}]
},
create: function () {
return new TemperatureHumidity();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function TemperatureHumidity() {
/**
*
*/
TemperatureHumidity.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 (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") {
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) {
for (var n in telegram.functions) {
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") {
this.state.temperature = telegram.functions[n].value;
this.logDebug("Temperature: " + this.state.temperature);
}
}
this.publishStateChange();
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
TemperatureHumidity.prototype.getState = function () {
return this.state;
};
/**
*
*/
TemperatureHumidity.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
TemperatureHumidity.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}