UNPKG

node-red-dashboard-2-t86

Version:

Set of Node-RED nodes to controll home automation based on Unipi Patron and DALI.

158 lines (157 loc) 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = main; const editor_prase_helper_1 = require("../editor-prase-helper"); var Dimming; (function (Dimming) { Dimming["Off"] = "off"; Dimming["Up"] = "up"; Dimming["Down"] = "down"; })(Dimming || (Dimming = {})); var Event; (function (Event) { Event["Start"] = "start"; Event["Stop"] = "stop"; Event["Toggle"] = "toggle"; })(Event || (Event = {})); function parseEditorConfig(config) { return { name: config.name, dimmImpKp: config.dimmimpkp, dimmImpStart: (0, editor_prase_helper_1.typedStrToValue)(config.dimmimpstart, config.dimmimpstartType), dimmImpStop: (0, editor_prase_helper_1.typedStrToValue)(config.dimmimpstop, config.dimmimpstopType), toggle: (0, editor_prase_helper_1.typedStrToValue)(config.toggle, config.toggleType) }; } class PushDimmer { constructor(RED, node, config) { this.dimming = Dimming.Off; this.RED = RED; this.node = node; RED.nodes.createNode(node, config); this.pConf = parseEditorConfig(config); this.node.on('input', this.onInput.bind(this)); this.node.on('close', this.onClose.bind(this)); this.updateStatus(); } onInput(msg, send, done) { // Check if all conditions to be it ballast state message are met if (msg && msg.payload && typeof msg.payload.level === 'number' && typeof msg.payload.state === 'boolean' && msg.payload.config && msg.payload.config.levelBounds && typeof msg.payload.config.levelBounds.min === 'number' && typeof msg.payload.config.levelBounds.max === 'number') { this.ballastState = { level: msg.payload.level, state: msg.payload.state, min: msg.payload.config.levelBounds.min, max: msg.payload.config.levelBounds.max }; this.updateStatus(); return; } const evt = this.parseDimmEvent(msg); if (evt) { this.handleEvent(evt, msg, send); this.updateStatus(); } else { // TODO: Should we warn? } if (done) done(); } onClose() { } handleEvent(evt, msg, send) { if (!this.ballastState) return; if (this.dimming === Dimming.Off && evt === Event.Start) { if (this.ballastState.level > this.ballastState.min + Math.floor((this.ballastState.max - this.ballastState.min) / 10)) { this.dimming = Dimming.Down; } else { this.dimming = Dimming.Up; } this.sendDimmingMsg(msg, send); return; } if (evt === Event.Stop) { this.dimming = Dimming.Off; this.sendDimmingMsg(msg, send); return; } if (this.dimming === Dimming.Off) { this.sendToggleMsg(msg, send); } } sendDimmingMsg(msg, send) { if (!msg) msg = {}; msg.payload = { event: this.dimming === Dimming.Off ? 'stop' : this.dimming }; send(msg); } sendToggleMsg(msg, send) { if (!msg) msg = {}; msg.payload = { event: 'toggle' }; send(msg); } parseDimmEvent(msg) { const keyList = this.pConf.dimmImpKp.split('.'); let obj = msg; for (const key of keyList) { if (obj === undefined) return undefined; obj = obj[key]; } if (obj === this.pConf.dimmImpStart) return Event.Start; if (obj === this.pConf.dimmImpStop) return Event.Stop; if (obj === this.pConf.toggle) return Event.Toggle; return undefined; } updateStatus() { if (!this.ballastState) { this.node.status({ fill: "yellow", shape: "ring", text: "Initialization" }); return; } if (this.dimming === Dimming.Off) { this.node.status({ fill: "blue", shape: "dot", text: 'Ready' }); return; } let onTheEdge; let size = ''; let dir = ''; if (this.dimming === Dimming.Down) { dir = 'down'; if (this.ballastState.level <= this.ballastState.min) { onTheEdge = this.ballastState.min; size = 'min'; } } if (this.dimming === Dimming.Up) { dir = 'up'; if (this.ballastState.level >= this.ballastState.max) { onTheEdge = this.ballastState.max; size = 'max'; } } let msg = 'Dimming ' + dir; msg += (onTheEdge !== undefined) ? `, level on ${size} (${onTheEdge})` : ''; this.node.status({ fill: "green", shape: "dot", text: msg }); } } // Register constructor function with Node-RED function main(RED) { RED.nodes.registerType('push-dimmer', function (config) { new PushDimmer(RED, this, config); }); }