thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
157 lines (137 loc) • 4.04 kB
JavaScript
module.exports = {
metadata: {
plugin: "dimmer",
label: "EnOcean IP Smart Dimmer",
role: "actor",
family: "dimmer",
deviceTypes: ["enocean-ip/gateway"],
services: [{
label: "Set Brightness",
id: "setBrightness"
}],
state: [{
label: "Brightness",
id: "brightness",
type: {
id: "integer"
}
}],
configuration: [{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
}]
},
create: function () {
return new Dimmer();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function Dimmer() {
/**
*
*/
Dimmer.prototype.start = function () {
var deferred = q.defer();
this.logLevel = 'debug';
this.state = {brightness: 1};
if (this.isSimulated()) {
this.interval = setInterval(function () {
if (this.state.brightness) {
this.state.brightness = Math.floor(Math.random() * 100) + 1;
console.log("1 interval");
this.publishStateChange();
}
}.bind(this), 10000);
deferred.resolve();
} else {
// State changes are solely published via streaming
console.log("bevor interval");
this.device.adapter.listeners.push(telegram => {
if (telegram.deviceId === this.configuration.deviceId) {
console.log('Device ' + telegram.friendlyId + ' is interested in ', telegram);
}
});
console.log("after interval");
this.interval = setInterval(function () {
if (this.state.brightness) {
this.state.brightness = Math.floor(Math.random() * 100) + 1;
console.log("2 interval" + this.state.brightness);
this.setBrightness(this.state.brightness);
this.publishStateChange();
}
}.bind(this), 3000);
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
Dimmer.prototype.getState = function () {
// Obtain from Device
return this.state;
};
/**
*
*/
Dimmer.prototype.setState = function (state) {
if (this.isSimulated()) {
this.state = state;
this.publishStateChange();
} else {
this.device.adapter.setDeviceState(this.configuration.deviceId, [{
key: "dimValue",
channel: 0,
value: '1'
},]).then(function () {
this.state = state;
console.log("seems to work");
this.publishStateChange();
}.bind(this)).fail(function (error) {
this.logError(error);
}.bind(this));
}
};
/**
*
*/
/**
*
*/
Dimmer.prototype.setBrightness = function (brightness) {
if (this.isSimulated()) {
this.state.brightness = brightness;
this.publishStateChange();
} else {
console.log("bright interval");
this.device.adapter.setDeviceState(this.configuration.deviceId, [{
key: "dimValue",
channel: "1",
value: 100
}]).then(function () {
this.state.brightness = brightness;
console.log("here too");
this.publishStateChange();
}.bind(this)).fail(function (error) {
this.logError(error);
}.bind(this));
}
};
/**
*
*/
Dimmer.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}