UNPKG

thing-it-device-enocean-ip

Version:

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

163 lines (137 loc) 3.87 kB
module.exports = { metadata: { plugin: "twoChannelSwitch", label: "EnOcean IP Two Channel Switch", role: "actor", family: "light", deviceTypes: ["enocean-ip/gateway"], services: [ {id: "toggle", label: "Toggle"}, ], state: [{ label: "Light", id: "light", type: { id: "boolean" } }], configuration: [{ label: "Device ID", id: "deviceId", type: { id: "string" } }, { label: "Channel", id: "channel", type: { id: "integer" } }] }, create: function () { return new TwoChannelSwitch(); } }; var q = require('q'); var moment = require('moment'); /** * */ function TwoChannelSwitch() { /** * */ TwoChannelSwitch.prototype.start = function () { var deferred = q.defer(); this.logLevel = 'debug'; //TODO GET ACTUAL STATE FROM REAL DEVICE this.state = {light: false}; if (this.isSimulated()) { this.interval = setInterval(function () { this.state.light = !this.state.light; }.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); } }); // this.interval = setInterval(function () { // this.toggle(); // console.log("TestInterval"); // }.bind(this), Math.floor(Math.random() * 5000) + 500); deferred.resolve(); } return deferred.promise; }; /** * */ TwoChannelSwitch.prototype.getState = function () { // Obtain from Device return this.state; }; /** * */ TwoChannelSwitch.prototype.setState = function (state) { if (this.isSimulated()) { this.state = state; this.publishStateChange(); } else { this.state = state; this.device.adapter.setDeviceState(this.configuration.deviceId, [{ key: "switch", value: this.state.light, }, { key: "channel", value: this.configuration.channel, },]).then(function () { this.publishStateChange(); }.bind(this)).fail(function (error) { this.logError(error); }.bind(this)); } }; /** * */ /** * */ TwoChannelSwitch.prototype.toggle = function () { if (this.isSimulated()) { this.publishStateChange(); } else { if (this.state.light === "off") { var output = "on"; } else { this.state.light = "off"; } this.device.adapter.setDeviceState(this.configuration.deviceId, [{ key: "switch", value: output, }, { key: "channel", value: this.configuration.channel, },]).then(function () { this.publishStateChange(); }.bind(this)).fail(function (error) { this.logError(error); }.bind(this)); } }; /** * */ TwoChannelSwitch.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } } }