UNPKG

thing-it-device-enocean-ip

Version:

[thing-it-node] Device Plugin for EnOcean IP products.

158 lines (138 loc) 3.79 kB
module.exports = { metadata: { plugin: "eltako_A5-38-08", label: "EnOcean IP Eltako A5-38-08", role: "actor", family: "eltako_A5-38-08", 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 Eltako(); } }; var q = require('q'); var moment = require('moment'); /** * */ function Eltako() { /** * */ Eltako.prototype.start = function () { var deferred = q.defer(); this.state = { switch: false }; 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' ? true : false; 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' ? true : false; this.logDebug("Switch: " + this.state.switch); } } this.publishStateChange(); } }); deferred.resolve(); } return deferred.promise; }; /** * */ Eltako.prototype.getState = function () { return this.state; }; /** * */ Eltako.prototype.setState = function (state) { this.state = state; }; /** * */ Eltako.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } } /** * */ Eltako.prototype.on = function (params) { this.toggle(true); } /** * */ Eltako.prototype.off = function (params) { this.toggle(false); } /** * */ Eltako.prototype.toggle = function (power) { this.state.switch = !!power || power.constructor === Object ? (this.state.switch ? false : true) : power; const value = this.state.switch ? 'on' : 'off'; this.logInfo("Switch: " + value); functions = [{ "key": "switch", "value": value }, { "key": "switchType", "value": "duration" }]; this.device.adapter.setDeviceState(this.configuration.deviceId, functions); this.publishStateChange(); } }