UNPKG

thing-it-device-enocean-ip

Version:

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

184 lines (164 loc) 4.97 kB
module.exports = { metadata: { plugin: "plug_D2-01-08", label: "EnOcean IP Plug D2-01-08", role: "actor", family: "plug_D2-01-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: "Watt/hour", id: "energy", type: { id: "decimal" } }, { label: "Watt", id: "power", type: { id: "decimal" } }, { label: "Switch", id: "switch", type: { id: "boolean" } } ], configuration: [{ label: "Device ID", id: "deviceId", type: { id: "string" } }] }, create: function () { return new Plug(); } }; var q = require('q'); var moment = require('moment'); /** * */ function Plug() { /** * */ Plug.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 === "energy") { this.state.energy = body.state.functions[n].value; this.logDebug("Watt/hour: " + this.state.energy); } if (body.state.functions[n].key === "power") { this.state.power = body.state.functions[n].value; this.logDebug("Watt: " + this.state.power); } 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 === "energy") { this.state.power = telegram.functions[n].value / 1000; this.logDebug("kW/hour: " + this.state.power); } if (telegram.functions[n].key === "power") { this.state.energy = telegram.functions[n].value; this.logDebug("Watt: " + this.state.energy); } 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; }; /** * */ Plug.prototype.getState = function () { return this.state; }; /** * */ Plug.prototype.setState = function (state) { this.state = state; }; /** * */ Plug.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } } /** * */ Plug.prototype.on = function (params) { this.toggle(true); } /** * */ Plug.prototype.off = function (params) { this.toggle(false); } /** * */ Plug.prototype.toggle = function (power) { this.state.switch = !!power || power.constructor === Object ? !this.state.switch : power; const value = this.state.switch ? 'on' : 'off'; this.logInfo("Switch: " + value); functions = [{ "key": "switch", "value": value }]; this.device.adapter.setDeviceState(this.configuration.deviceId, functions); this.publishStateChange(); } }