thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
155 lines (135 loc) • 3.71 kB
JavaScript
module.exports = {
metadata: {
plugin: "opus_D2-01-01",
label: "Opus Gn Bridge D2-01-01",
role: "actor",
family: "opus_D2-01-01",
deviceTypes: ["enocean-ip/gateway"],
tangible: false,
services: [{
label: "Turn on",
id: "on"
},
{
label: "Turn off",
id: "off"
},
{
label: "Toggle",
id: "toggle"
}
],
events: [],
state: [
{
label: "Switch",
id: "switch",
type: {
id: "boolean"
}
}
],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new OpusGnBridge();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function OpusGnBridge() {
/**
*
*/
OpusGnBridge.prototype.start = function () {
var deferred = q.defer();
this.state = {};
if (this.isSimulated()) {
this.publishStateChange();
} 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 === "switch") {
this.state.switch = body.state.functions[n].value === 'on';
this.logDebug("Switch: " + this.state.switch);
}
}
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 (telegram.functions[n].key === "switch") {
this.state.switch = telegram.functions[n].value === 'on';
this.logDebug("Switch: " + this.state.switch);
}
}
this.publishStateChange();
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
OpusGnBridge.prototype.getState = function () {
return this.state;
};
/**
*
*/
OpusGnBridge.prototype.setState = function (state) {
this.state = state;
};
/**
*
*/
OpusGnBridge.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
/**
*
*/
OpusGnBridge.prototype.on = function (params) {
this.toggle(true);
};
/**
*
*/
OpusGnBridge.prototype.off = function (params) {
this.toggle(false);
};
/**
*
*/
OpusGnBridge.prototype.toggle = function (power) {
this.state.switch = !!power || power.constructor === Object ? !this.state.switch : power;
const value = this.state.switch ? 'on' : 'off';
this.logInfo("Switch: " + value);
functions = [{
"key": "switch",
"value": value
}];
this.device.adapter.setDeviceState(this.configuration.deviceId, functions);
this.publishStateChange();
}
}