thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
215 lines (197 loc) • 7.6 kB
JavaScript
module.exports = {
metadata: {
plugin: "threeChannelTemperatureSensor",
label: "Enocean IP Three Channel Temperature Sensor",
role: "sensor",
family: "threeChannelTemperatureSensor",
deviceTypes: ["enocean-ip/gateway"],
tangible: false,
services: [],
state: [{
label: "Temperature 1",
id: "temperature1",
type: {
id: "decimal"
}
}, {
label: "Temperature 2",
id: "temperature2",
type: {
id: "decimal"
}
}, {
label: "Temperature 3",
id: "temperature3",
type: {
id: "decimal"
}
}, {
label: "Internal Temperature",
id: "internalTemperature",
type: {
id: "decimal"
}
}, {
label: "Battery Failing",
id: "batteryFailing",
type: {
id: "boolean"
}
}],
events: [{
label: "Battery Dying",
id: "batteryDying"
}, {
label: "Temperature Alert",
id: "temperatureAlert"
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}, {
label: "Temperature Threshold",
id: "temperatureThreshold",
type: {
id: "integer"
},
defaultValue: 100
}]
},
create: function () {
return new ThreeChannelTemperatureSensor();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function ThreeChannelTemperatureSensor() {
/**
*
*/
ThreeChannelTemperatureSensor.prototype.start = function () {
var deferred = q.defer();
this.state = {
temperature1: 0.0,
temperature2: 0.0,
temperature3: 0.0,
batteryFailing: false
};
if (this.isSimulated()) {
this.interval = setInterval(function () {
this.state.temperature1 = Math.round(15 + Math.random() * 10);
this.state.temperature2 = Math.round(15 + Math.random() * 10);
this.state.temperature3 = Math.round(15 + Math.random() * 10);
this.publishStateChange();
}.bind(this), 20000);
}
else {
// Retrieve current state
this.device.adapter.getDeviceState(this.configuration.deviceId).then(function(body){
for(let n in body.state.functions){
if (body.state.functions[n].key === 'temperature1') {
this.state.temperature1 = body.state.functions[n].value;
if(body.state.functions[n].value >= this.configuration.temperatureThreshold){
this.publishEvent('temperatureAlert', {});
}
}
if (body.state.functions[n].key === 'temperature2') {
this.state.temperature2 = body.state.functions[n].value;
if(body.state.functions[n].value >= this.configuration.temperatureThreshold){
this.publishEvent('temperatureAlert', {});
}
}
if (body.state.functions[n].key === 'temperature3') {
this.state.temperature3 = body.state.functions[n].value;
if(body.state.functions[n].value >= this.configuration.temperatureThreshold){
this.publishEvent('temperatureAlert', {});
}
}
if (body.state.functions[n].key === 'temperatureInternal') {
this.state.internalTemperature = body.state.functions[n].value;
}
if (body.state.functions[n].key === 'dyingGasp') {
if (body.state.functions[n].value === 'true') {
this.state.batteryFailing = true;
this.publishEvent('batteryDying', {});
}
else {
this.state.batteryFailing = false;
}
}
}
this.publishStateChange();
this.logDebug(this.state);
}.bind(this), function (err){
console.log(err)
}.bind(this));
//Retrieve updated sate
this.device.adapter.listeners.push(telegram => {
if (telegram.deviceId === this.configuration.deviceId) {
this.logDebug('Device ' + telegram.friendlyId + ' is processing ', telegram.functions);
for (var n in telegram.functions) {
if (telegram.functions[n].key === 'temperature1') {
this.state.temperature1 = telegram.functions[n].value;
if(telegram.functions[n].value >= this.configuration.temperatureThreshold){
this.publishEvent('temperatureAlert', {});
}
}
if (telegram.functions[n].key === 'temperature2') {
this.state.temperature2 = telegram.functions[n].value;
if(telegram.functions[n].value >= this.configuration.temperatureThreshold){
this.publishEvent('temperatureAlert', {});
}
}
if (telegram.functions[n].key === 'temperature3') {
this.state.temperature3 = telegram.functions[n].value;
if(telegram.functions[n].value >= this.configuration.temperatureThreshold){
this.publishEvent('temperatureAlert', {});
}
}
if (telegram.functions[n].key === 'temperatureInternal') {
this.state.internalTemperature = telegram.functions[n].value;
}
if (telegram.functions[n].key === 'dyingGasp') {
if (telegram.functions[n].value === 'true') {
this.state.batteryFailing = true;
this.publishEvent('batteryDying', {});
}
else {
this.state.batteryFailing = false;
}
}
}
this.publishStateChange();
}
deferred.resolve();
});
return deferred.promise;
}
};
/**
*
*/
ThreeChannelTemperatureSensor.prototype.getState = function () {
return this.state;
};
/**
*
*/
ThreeChannelTemperatureSensor.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
ThreeChannelTemperatureSensor.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}