thing-it-device-enocean-ip
Version:
[thing-it-node] Device Plugin for EnOcean IP products.
319 lines (291 loc) • 8.94 kB
JavaScript
module.exports = {
metadata: {
plugin: "opus_D2-05-02",
label: "Opus Bridge D2-05-02",
role: "actor",
family: "opus_D2-05-02",
deviceTypes: ["enocean-ip/gateway"],
tangible: false,
services: [
{
id: 'raisePosition',
label: 'Raise Position'
},
{
id: 'lowerPosition',
label: 'Lower Position'
},
{
id: 'positionUp',
label: 'Position Up'
},
{
id: 'positionDown',
label: 'Position Down'
},
{
id: 'incrementRotation',
label: 'Increment Rotation'
},
{
id: 'decrementRotation',
label: 'Decrement Rotation'
},
{
id: 'stopMotion',
label: 'Stop Motion'
},
{
id: 'setPosition',
label: 'Set position',
parameters: [{
id: "position",
label: "Position",
type: {
id: "integer"
}
}]
},
{
id: 'setRotation',
label: 'Set rotation',
parameters: [{
id: "rotation",
label: "Rotation",
type: {
id: "integer"
}
}]
}
],
events: [],
state: [
{
id: "position", label: "position",
type: {
id: "integer"
}
},
{
id: "rotation", label: "rotation",
type: {
id: "integer"
}
},
{
id: "locked", label: "Locked",
type: {
id: "boolean"
}
}
],
configuration: [
{
label: "Device ID",
id: "deviceId",
type: {
id: "string"
}
},
{
label: "Position Step Size",
id: "positionStepSize",
type: {
id: "integer"
},
defaultValue: "5"
},
{
label: "Position Up Value",
id: "positionUpValue",
type: {
id: "integer"
},
defaultValue: "0"
},
{
label: "Position Down Value",
id: "positionDownValue",
type: {
id: "integer"
},
defaultValue: "100"
},
{
label: "Rotation Step Size",
id: "rotationStepSize",
type: {
id: "integer"
},
defaultValue: "10"
}
]
},
create: function () {
return new OpusBridge();
}
};
var q = require('q');
var moment = require('moment');
/**
*
*/
function OpusBridge() {
/**
*
*/
OpusBridge.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 === "angle") {
this.state.angle = body.state.functions[n].value;
this.logDebug("Angle: " + this.state.angle);
}
if (body.state.functions[n].key === "position") {
this.state.position = body.state.functions[n].value;
this.logDebug("Position: " + this.state.switch);
}
if (body.state.functions[n].key === "lockingMode") {
this.state.locked = body.state.functions[n].value === 'block';
this.logDebug("Locked: " + this.state.locked);
}
}
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 === "angle") {
this.state.angle = telegram.functions[n].value;
this.logDebug("Angle: " + this.state.angle);
}
if (telegram.functions[n].key === "position") {
this.state.position = telegram.functions[n].value;
this.logDebug("Position: " + this.state.position);
}
if (telegram.functions[n].key === "lockingMode") {
this.state.locked = telegram.functions[n].value === 'block';
this.logDebug("Locked: " + this.state.locked);
}
}
this.publishStateChange();
}
});
deferred.resolve();
}
return deferred.promise;
};
/**
*
*/
OpusBridge.prototype.getState = function () {
return this.state;
};
/**
*
*/
OpusBridge.prototype.setState = function (state) {
if (this.state.position !== state.position) this.setPosition(state.position);
if(this.state.rotation !== state.rotation) this.setRotation(state.rotation);
this.state = state;
};
/**
*
*/
OpusBridge.prototype.stop = function () {
if (this.isSimulated()) {
if (this.interval) {
clearInterval(this.interval);
}
}
};
/**
*
*/
OpusBridge.prototype.setPosition = function (targetPosition) {
this.state.position = targetPosition;
this.logInfo("Position: " + value);
functions = [{
"key": "position",
"value": targetPosition
}];
this.device.adapter.setDeviceState(this.configuration.deviceId, functions);
this.publishStateChange();
};
/**
*
*/
OpusBridge.prototype.raisePosition = function () {
const targetPosition = this.state.position - this.configuration.positionStepSize;
this.setPosition(targetPosition);
};
/**
*
*/
OpusBridge.prototype.lowerPosition = function () {
const targetPosition = this.state.position + this.configuration.positionStepSize;
this.setPosition(targetPosition);
};
/**
*
*/
OpusBridge.prototype.positionUp = function () {
const targetPosition = this.configuration.positionUpValue;
this.setPosition(targetPosition);
};
/**
*
*/
OpusBridge.prototype.positionDown = function () {
const targetPosition = this.configuration.positionDownValue;
this.setPosition(targetPosition);
};
/**
*
*/
OpusBridge.prototype.setRotation = function (targetRotation) {
this.state.rotation = targetRotation;
this.logInfo("Rotation: " + value);
functions = [{
"key": "angle",
"value": targetRotation
}];
this.device.adapter.setDeviceState(this.configuration.deviceId, functions);
this.publishStateChange();
};
/**
*
*/
OpusBridge.prototype.incrementRotation = function () {
const targetRotation = this.state.position + this.configuration.rotationStepSize;
this.setRotation(targetRotation);
};
/**
*
*/
OpusBridge.prototype.decrementRotation = function () {
const targetRotation = this.state.position - this.configuration.rotationStepSize;
this.setRotation(targetRotation);
};
/**
*
*/
OpusBridge.prototype.stopMotion = function () {
this.logInfo("Stopped jalousie");
functions = [{
"key": "stop",
"value": "true"
}];
this.device.adapter.setDeviceState(this.configuration.deviceId, functions);
this.publishStateChange();
};
}