thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
112 lines (96 loc) • 2.89 kB
JavaScript
module.exports = {
metadata: {
plugin: "switch_F6-01-01",
label: "EnOcean IP Switch F6-01-01",
role: "actor",
family: "switch_F6-01-01",
deviceTypes: ["enocean-ip/gateway"],
services: [],
state: [{
label: 'Last Operation',
id: 'lastOperation'
},
{
label: 'Last Operation Timestamp',
id: 'lastOperationTimestamp'
}
],
events: [{
label: 'Button Pressed',
id: 'buttonPressed'
}, {
label: 'Button Released',
id: 'buttonReleased'
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new Switch();
}
};
var q = require('q');
var moment = require('moment');
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
*
*/
function Switch() {
/**
*
*/
Switch.prototype.start = function () {
var deferred = q.defer();
this.state = {};
if (this.isSimulated()) {
this.interval = setInterval(function () {
this.state.lastOperation = this.state.lastOperation == 'Button Released' ? 'Button Pressed' : 'Button Released';
this.publishStateChange();
}.bind(this), 10000);
deferred.resolve();
} else {
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 === "button") {
this.publishEvent('button' + capitalizeFirstLetter(telegram.functions[n].value), {});
this.state.lastOperation = capitalizeFirstLetter('Button ' + capitalizeFirstLetter(telegram.functions[n].value));
this.state.lastOperationTimestamp = moment().toISOString();
this.publishStateChange();
}
}
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
Switch.prototype.getState = function () {
return this.state;
};
/**
*
*/
Switch.prototype.setState = function (state) {};
/**
*
*/
Switch.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}