UNPKG

thing-it-device-enocean-ip

Version:

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

189 lines (175 loc) 6.47 kB
module.exports = { metadata: { plugin: "threeChannelCurrentSensor", label: "EnOcean IP Three Channel Current Sensor", role: "sensor", family: "threeChannelCurrentSensor", deviceTypes: ["enocean-ip/gateway"], tangible: false, services: [], events:[{ label: "Power failure detected", id: "powerFailureDetected" }], state: [{ label: "Current Channel 1", id: "currentChannel1", type: { id: "decimal" } }, { label: "Current Channel 2", id: "currentChannel2", type: { id: "decimal" } }, { label: "Current Channel 3", id: "currentChannel3", type: { id: "decimal" } }, { label: "Power Failure", id: "powerFailure", type: { id: "boolean" } }], configuration: [{ label: "Device ID", id: "deviceId", type: { id: "string" } }, { label: "Current Threshold", id: "currentThreshold", type: { id: "decimal" } }] }, create: function () { return new ThreeChannelCurrentSensor(); } }; var q = require('q'); var moment = require('moment'); /** * */ function ThreeChannelCurrentSensor() { /** * */ ThreeChannelCurrentSensor.prototype.start = function () { var deferred = q.defer(); this.state = {currentChannel1: 0.0, currentChannel2: 0.0, currentChannel3: 0.0, powerFailure: false}; if(!this.configuration.currentThreshold){ this.configuration.currentThreshold = 0.2; } if (this.isSimulated()) { this.interval = setInterval(function () { this.state.currentChannel1 = Math.round(2 + Math.random() * 10); this.state.currentChannel2 = Math.round(2 + Math.random() * 10); this.state.currentChannel3 = Math.round(2 + Math.random() * 10); this.publishStateChange(); }.bind(this), 20000); } 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 === 'current1'){ if(body.state.functions[n].value > this.configuration.currentThreshold) { this.state.currentChannel1 = body.state.functions[n].value; } } if(body.state.functions[n].key === 'current2'){ if(body.state.functions[n].value > this.configuration.currentThreshold) { this.state.currentChannel2 = body.state.functions[n].value; } } if(body.state.functions[n].key === 'current3'){ if(body.state.functions[n].value > this.configuration.currentThreshold) { this.state.currentChannel3 = body.state.functions[n].value; } } if(body.state.functions[n].key === 'powerFailureDetected'){ if(body.state.functions[n].value === 'true'){ this.state.powerFailure = true; this.publishEvent(body.state.functions[n].key, {}); } else{ this.state.powerFailure = false; } } } this.publishStateChange(); this.logDebug(this.state); }.bind(this), function (err){ console.log(err) }.bind(this)); // Retrieve updated state this.device.adapter.listeners.push(telegram => { if(telegram.deviceId === this.configuration.deviceId){ for(var n in telegram.functions){ if(telegram.functions[n].key === 'current1'){ if(telegram.functions[n].value > this.configuration.currentThreshold) { this.state.currentChannel1 = telegram.functions[n].value; } } if(telegram.functions[n].key === 'current2'){ if(telegram.functions[n].value > this.configuration.currentThreshold) { this.state.currentChannel2 = telegram.functions[n].value; } } if(telegram.functions[n].key === 'current3'){ if(telegram.functions[n].value > this.configuration.currentThreshold) { this.state.currentChannel3 = telegram.functions[n].value; } } if(telegram.functions[n].key === 'powerFailureDetected'){ if(telegram.functions[n].value === 'true'){ this.state.powerFailure = true; this.publishEvent(telegram.functions[n].key, {}); } else{ this.state.powerFailure = false; } } } this.publishStateChange(); } }); } deferred.resolve(); return deferred.promise; }; /** * */ ThreeChannelCurrentSensor.prototype.getState = function () { return this.state; }; /** * */ ThreeChannelCurrentSensor.prototype.setState = function (state) { this.state = state; }; /** * */ ThreeChannelCurrentSensor.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } } }