UNPKG

thing-it-device-enocean-ip

Version:

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

197 lines (186 loc) 6.35 kB
module.exports = { metadata: { plugin: "radiatorValve", label: "Micropelt Radiator Valve", role: "actor", family: "radiatorValve", deviceTypes: ["enocean-ip/gateway"], tangible: false, services: [{ label: "Set Temperature", id: "setTemperature" }], events: [{ label:"Low Battery", id: "lowBattery" }], state: [{ label: "Valve", id: "valve", type: { id: "decimal" } }, { label: "Battery Low", id: "batteryLow", type: { id: "boolean" } }, { label: "Actuator Obstructed", id: "actuatorObstructed", type: { id: "boolean" } }, { label: "Temperature", id: "temperature", type: { id: "decimal" } }, { label: "Temperature Setpoint", id: "setpoint", type: { id: "decimal" } }], configuration: [{ label: "Device ID", id: "deviceId", type: { id: "string" } }] }, create: function () { return new RadiatorValve(); } }; var q = require('q'); var moment = require('moment'); /** * */ function RadiatorValve() { /** * */ RadiatorValve.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){ console.log(body.state.functions[n]); if(body.state.functions[n].key === "valve"){ this.state.valve = body.state.functions[n].value; } if(body.state.functions[n].key === "batteryLow"){ if(body.state.functions[n].value === "true"){ this.state.batteryLow = true; this.publishEvent("lowBattery", {}); } else this.state.batteryLow = false; } if(body.state.functions[n].key === "actuatorObstructed"){ if(body.state.functions[n].value === "true"){ this.state.actuatorObstructed = true; } else this.state.actuatorObstructed = false; } if(body.state.functions[n].key === "temperature"){ this.state.temperature = body.state.functions[n].value; } if(body.state.functions[n].key === "temperatureSetpoint"){ this.state.setpoint = body.state.functions[n].value; } } this.publishStateChange(); console.log(this.state); }.bind(this), function (err){ console.log(err) }.bind(this)); this.device.adapter.listeners.push(telegram => { if (telegram.deviceId === this.configuration.deviceId) { console.log(telegram.functions); for(let n in telegram.functions){ if(telegram.functions[n].key === "valve"){ this.state.valve = telegram.functions[n].value; } if(telegram.functions[n].key === "batteryLow"){ if(telegram.functions[n].value === "true"){ this.state.batteryLow = true; this.publishEvent("lowBattery", {}); } else this.state.batteryLow = false; } if(telegram.functions[n].key === "temperature"){ this.state.temperature = telegram.functions[n].value; } if(telegram.functions[n].key === "temperatureSetpoint"){ this.state.setpoint = telegram.functions[n].value; } } this.publishStateChange(); } }); deferred.resolve(); } this.logInfo("Actor Started"); return deferred.promise; }; /** * */ RadiatorValve.prototype.getState = function () { return this.state; }; /** * */ RadiatorValve.prototype.setState = function (state) { let updatedState = this.updateState(state); this.state = updatedState; this.publishStateChange(); this.publishEvent('set', {}); }; /** * */ RadiatorValve.prototype.stop = function () { if (this.isSimulated()) { if (this.interval) { clearInterval(this.interval); } } }; /** * */ RadiatorValve.prototype.setTemperature = function (setPointTemperature, currentTemperature) { let deltaTemperature = currentTemperature - setPointTemperature; let functions = []; if(deltaTemperature !== 0){ this.logInfo("Setting Temperature to: ", setPointTemperature); functions = [{"key": "temperature", "value": currentTemperature},{"key": "temperatureSetpoint", "value": setPointTemperature}]; this.device.adapter.setDeviceState(this.configuration.deviceId, functions); } } /** * */ RadiatorValve.prototype.updateState = function(state){ if (_.isObjectLike(state)) { if (_.isNumber(state.setpoint) && state.setpoint !== this.state.setpoint) { this.state.setpoint = state.setpoint; this.setTemperature(this.state.setpoint, this.state.temperature); } } return this.state; } }