UNPKG

thing-it-device-enocean-ip

Version:

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

126 lines (111 loc) 3.4 kB
module.exports = { metadata: { plugin: "illumination_A5-06-01", label: "Illumination A5-06-01", role: "sensor", family: "illumination_A5-06-01", deviceTypes: ["enocean-ip/gateway"], tangible: false, services: [], events: [], state: [{ label: "Illumination", id: "illumination", type: { id: "decimal" } }], configuration: [{ label: "Device ID", id: "deviceId", type: { id: "string" } }, { label: "Release Time", id: "releaseTime", type: { id: "integer" }, defaultValue: 120, unit: "s" }] }, create: function () { return new Illumination(); } }; var q = require('q'); var moment = require('moment'); /** * */ function Illumination() { /** * */ Illumination.prototype.start = function () { var deferred = q.defer(); this.state = {}; if(!this.configuration.releaseTime) this.configuration.releaseTime = 120; if (this.isSimulated()) { this.publishStateChange(); } else { this.device.adapter.getDeviceState(this.configuration.deviceId).then(function(body){ for(var n in body.state.functions){ if (body.state.functions[n].key === "illumination") { if(body.state.functions[n].value === 300) this.state.illumination = 0; else this.state.illumination = body.state.functions[n].value; } } 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) { console.log(telegram); for (var n in telegram.functions) { if(this.zeroTimeout) clearTimeout(this.zeroTimeout); if (telegram.functions[n].key === "illumination") { this.state.illumination = telegram.functions[n].value; } if(this.state.illumination === 300) { this.zeroTimeout = setTimeout(() => { this.state.illumination = 0; this.publishStateChange(); }, this.configuration.releaseTime * 1000); } } this.publishStateChange(); } }); deferred.resolve(); } return deferred.promise; }; /** * */ Illumination.prototype.getState = function () { return this.state; }; /** * */ Illumination.prototype.setState = function (state) { this.state = state; }; /** * */ Illumination.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } } }