thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
135 lines (119 loc) • 3.12 kB
JavaScript
module.exports = {
metadata: {
plugin: "outlet",
label: "EnOcean IP Smart Power Outlet",
role: "actor",
family: "outlet",
deviceTypes: ["enocean-ip/gateway"],
services: [{label: "Toggle", id: "toggle"}],
state: [{
label: "Switch",
id: "switch",
type: {
id: "boolean"
}
}, {
label: "Brightness",
id: "brightness",
type: {
id: "integer"
}
}, {
label: "Power Consumption",
id: "power",
type: {
id: "decimal"
}
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new Outlet();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function Outlet() {
/**
*
*/
Outlet.prototype.start = function () {
var deferred = q.defer();
this.state = {switch: false, power: 0};
if (this.isSimulated()) {
this.interval = setInterval(function () {
if (this.state.switch) {
this.state.power += 0.1;
this.publishStateChange();
}
}.bind(this), 10000);
deferred.resolve();
}
else {
// State changes are solely published via streaming
this.device.adapter.listeners.push(telegram => {
if (telegram.deviceId === this.configuration.deviceId) {
console.log('Device ' + telegram.friendlyId + ' is interested in ', telegram);
}
});
}
deferred.resolve();
return deferred.promise;
};
/**
*
*/
Outlet.prototype.getState = function () {
// Obtain from Device
return this.state;
};
/**
*
*/
Outlet.prototype.setState = function (state) {
if (this.isSimulated()) {
this.state = state;
this.publishStateChange();
} else {
this.device.adapter.setState(this.configuration.deviceId, [{
key: "switch",
channel: 0,
value: this.state.switch ? 'on' : 'off'
}, {
key: "switch",
channel: 0,
value: this.state.switch ? 'on' : 'off'
}]).then(function () {
this.state = state;
this.publishStateChange();
}.bind(this)).fail(function (error) {
this.logError(error);
}.bind(this));
}
};
/**
*
*/
Outlet.prototype.toggle = function () {
this.setState({switch: !this.switch});
};
/**
*
*/
Outlet.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}