UNPKG

thing-it-device-enocean-ip

Version:

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

129 lines (118 loc) 3.83 kB
module.exports = { metadata: { plugin: "co2Sensor", label: "EnOcean IP CO2 Sensor", role: "sensor", family: "co2Sensor", deviceTypes: ["enocean-ip/gateway"], tangible: false, services: [], events: [{ label: "High CO2 Concentration", id: "highCO2Concentration" }, { label: "Power Failure", id: "powerFailure" }], state: [{ label: "CO2 Concentration", id: "co2Concentration", type: { id: "decimal" } }, { label:"Power Failure Detected", id: "powerFailureDetected", type: { id:"boolean" } }], configuration: [{ label: "Device ID", id: "deviceId", type: { id: "string" } }] }, create: function () { return new CO2Sensor(); } }; var q = require('q'); var moment = require('moment'); /** * */ function CO2Sensor() { /** * */ CO2Sensor.prototype.start = function () { var deferred = q.defer(); this.state = {}; 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 === "co2") { this.state.co2Concentration = body.state.functions[n].value; if(telegram.functions[n].value > 1000) this.publishEvent("highCO2Concentration", {}); this.logDebug("CO2Concentration: " + this.state.co2Concentration); } } 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 (telegram.functions[n].key === "co2") { this.state.co2Concentration = telegram.functions[n].value; if(telegram.functions[n].value > 1000) this.publishEvent("highCO2Concentration", {}); this.logDebug("CO2 Concentration: " + this.state.co2Concentration); } if (telegram.functions[n].key === "powerFailureDetected") { if(telegram.functions[n].value === "false") this.state.powerFailureDetected = false; else{ this.state.powerFailureDetected = true; this.publishEvent("powerFailure", {}); } this.logDebug("Power Failure Detected: " + this.state.powerFailureDetected); } } this.publishStateChange(); } }); deferred.resolve(); } return deferred.promise; }; /** * */ CO2Sensor.prototype.getState = function () { return this.state; }; /** * */ CO2Sensor.prototype.setState = function (state) { this.state = state; }; /** * */ CO2Sensor.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } } }