thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
103 lines (86 loc) • 2.3 kB
JavaScript
module.exports = {
metadata: {
plugin: "temperatureSensor",
label: "EnOcean IP Temperature Sensor",
role: "sensor",
family: "temperatureSensor",
deviceTypes: ["enocean-ip/gateway"],
services: [],
state: [{
label: "Temperature",
id: "temperature",
type: {
id: "decimal"
}
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new TemperatureSensor();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function TemperatureSensor() {
/**
*
*/
TemperatureSensor.prototype.start = function () {
var deferred = q.defer();
this.state = {temperature: 20.0};
if (this.isSimulated()) {
this.interval = setInterval(function () {
this.state.temperature = Math.round(15 + Math.random() * 10);
this.publishStateChange();
}.bind(this), 20000);
}
else {
// Retrieve current state
this.device.adapter.getDeviceState(this.configuration.deviceId).then(state => {
this.state = state;
this.publishStateChange();
}).fail(error => {
this.logError(error);
});
this.device.adapter.registerListener((message) => {
if (message.type === 'stateChange' && message.node === this.configuration.deviceId) {
this.state = message.state;
this.publishStateChange();
}
});
}
deferred.resolve();
return deferred.promise;
};
/**
*
*/
TemperatureSensor.prototype.getState = function () {
return this.state;
};
/**
*
*/
TemperatureSensor.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
TemperatureSensor.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}