thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
114 lines (102 loc) • 2.86 kB
JavaScript
module.exports = {
metadata: {
plugin: "thermalTemperatureSensor",
label: "EnOcean IP Thermal Temperature Sensor",
role: "sensor",
family: "thermalTemperatureSensor",
deviceTypes: ["enocean-ip/gateway"],
tangible: false,
services: [],
events: [{
label: "Temperature Alert",
id: "temperatureAlert"
}],
state: [{
label: "Surface Temperature",
id: "surfaceTemperature",
type: {
id: "integer"
}
}, {
label: "Ambient Temperature",
id: "ambientTemperature",
type: {
id: "integer"
}
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}, {
label: "Temperature Threshold",
id: "temperatureThreshold",
type: {
id: "integer"
},
defaultValue: 100
}]
},
create: function () {
return new ThermalTemperatureSensor();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function ThermalTemperatureSensor() {
/**
*
*/
ThermalTemperatureSensor.prototype.start = function () {
var deferred = q.defer();
this.state = {ambientTemperature: 0,
surfaceTemperature: 0};
if (this.isSimulated()) {
this.publishStateChange();
}
else {
// Retrieve current state
var dataArray = [];
this.device.adapter.listeners.push(telegram => {
if(telegram.deviceId === this.configuration.deviceId){
dataArray = telegram.telegramInfo.data.match(/.{1,2}/g);
this.state.ambientTemperature = parseInt(dataArray[4], 16);
this.state.surfaceTemperature = parseInt(dataArray[5], 16);
this.publishStateChange();
if(this.state.surfaceTemperature >= this.configuration.temperatureThreshold){
this.publishEvent('temperatureAlert', {});
}
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
ThermalTemperatureSensor.prototype.getState = function () {
return this.state;
};
/**
*
*/
ThermalTemperatureSensor.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
ThermalTemperatureSensor.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}