From f7c5873eca999e1555bf07776bf0f8b9df1f29c3 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Fri, 27 Nov 2020 10:36:14 +0100 Subject: [PATCH] fix: dinamically update ui on value added/removed --- lib/Gateway.js | 8 ++++---- lib/ZwaveClient.js | 24 ++++++++++++++++-------- src/App.vue | 1 + src/components/ControlPanel.vue | 22 +++++++++++++++++++++- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/Gateway.js b/lib/Gateway.js index 7aa51dc69e8..006f3112878 100755 --- a/lib/Gateway.js +++ b/lib/Gateway.js @@ -225,7 +225,7 @@ function onValueChanged (valueId, node, changed) { // emit event to socket if (this.zwave) { - this.zwave.emitEvent(this.zwave.socketEvents.valueUpdated, valueId) + this.zwave.sendToSocket(this.zwave.socketEvents.valueUpdated, valueId) } const result = this.valueTopic(node, valueId, true) @@ -382,7 +382,7 @@ function onNodeStatus (node) { // } if (this.zwave) { - this.zwave.emitEvent(this.zwave.socketEvents.nodeUpdated, node) + this.zwave.sendToSocket(this.zwave.socketEvents.nodeUpdated, node) } if (!this.config.ignoreStatus) { @@ -809,7 +809,7 @@ Gateway.prototype.rediscoverNode = function (nodeID) { this.discoverValue(node, node.values[id]) } - this.zwave.emitEvent(this.zwave.socketEvents.nodeUpdated, node) + this.zwave.sendToSocket(this.zwave.socketEvents.nodeUpdated, node) } } @@ -820,7 +820,7 @@ Gateway.prototype.disableDiscovery = function (nodeID) { node.hassDevices[id].ignoreDiscovery = true } - this.zwave.emitEvent(this.zwave.socketEvents.nodeUpdated, node) + this.zwave.sendToSocket(this.zwave.socketEvents.nodeUpdated, node) } } diff --git a/lib/ZwaveClient.js b/lib/ZwaveClient.js index c4bb33f02c8..d34ae039090 100644 --- a/lib/ZwaveClient.js +++ b/lib/ZwaveClient.js @@ -35,6 +35,7 @@ const socketEvents = { nodeRemoved: 'NODE_REMOVED', nodeUpdated: 'NODE_UPDATED', valueUpdated: 'VALUE_UPDATED', + valueRemoved: 'VALUE_REMOVED', api: 'API_RETURN', // api results debug: 'DEBUG' } @@ -165,7 +166,7 @@ function scanComplete () { function updateControllerStatus (status) { debug(status) this.cntStatus = status - this.emitEvent(socketEvents.controller, status) + this.sendToSocket(socketEvents.controller, status) } function onInclusionStarted (secure) { @@ -385,6 +386,11 @@ function onNodeValueAdded (zwaveNode, args) { args.newValue ) + // handle node values added 'on fly' + if (zwaveNode.ready) { + addValue.call(this, zwaveNode, args) + } + this.emit( 'event', eventEmitter.node, @@ -522,7 +528,7 @@ function removeNode (nodeid) { this.emit('nodeRemoved', node) this.addEmptyNodes() - this.emitEvent(socketEvents.nodeRemoved, this.nodes[nodeid]) + this.sendToSocket(socketEvents.nodeRemoved, this.nodes[nodeid]) } } @@ -738,9 +744,11 @@ function updateValue (zwaveNode, args) { function removeValue (zwaveNode, args) { const node = this.nodes[zwaveNode.id] const idString = getValueID(args) + const toRemove = node ? node.values[idString] : null - if (node.values[idString]) { + if (toRemove) { delete node.values[idString] + this.sendToSocket(socketEvents.valueRemoved, toRemove) debug('ValueRemoved: %s from node %d', idString, zwaveNode.id) } else { debug('ValueRemoved: no such node: ' + zwaveNode.id, 'error') @@ -935,7 +943,7 @@ ZwaveClient.prototype.updateDevice = function ( node.hassDevices[id] = hassDevice } - this.emitEvent(socketEvents.nodeUpdated, node) + this.sendToSocket(socketEvents.nodeUpdated, node) } } @@ -955,7 +963,7 @@ ZwaveClient.prototype.addDevice = function (hassDevice, nodeId) { hassDevice.persistent = false node.hassDevices[id] = hassDevice - this.emitEvent(socketEvents.nodeUpdated, node) + this.sendToSocket(socketEvents.nodeUpdated, node) } } @@ -982,7 +990,7 @@ ZwaveClient.prototype.storeDevices = async function (devices, nodeId, remove) { node.hassDevices = copy(devices) await jsonStore.put(store.nodes, this.storeNodes) - this.emitEvent(socketEvents.nodeUpdated, node) + this.sendToSocket(socketEvents.nodeUpdated, node) } } @@ -1295,10 +1303,10 @@ ZwaveClient.prototype.connect = async function () { /** * * - * @param {String} evtName Event name + * @param {String} evtName Socket event name * @param {Object} data Event data object */ -ZwaveClient.prototype.emitEvent = function (evtName, data) { +ZwaveClient.prototype.sendToSocket = function (evtName, data) { if (this.socket) { this.socket.emit(evtName, data) } diff --git a/src/App.vue b/src/App.vue index fc858dc8511..728e050ef10 100644 --- a/src/App.vue +++ b/src/App.vue @@ -229,6 +229,7 @@ export default { nodeRemoved: 'NODE_REMOVED', nodeUpdated: 'NODE_UPDATED', valueUpdated: 'VALUE_UPDATED', + valueRemoved: 'VALUE_REMOVED', api: 'API_RETURN', debug: 'DEBUG' }, diff --git a/src/components/ControlPanel.vue b/src/components/ControlPanel.vue index d814f2b5241..7d2f9a2b567 100644 --- a/src/components/ControlPanel.vue +++ b/src/components/ControlPanel.vue @@ -1469,8 +1469,21 @@ export default { } }) + this.socket.on(this.socketEvents.valueRemoved, data => { + const valueId = self.getValue(data) + + if (valueId) { + const node = self.nodes[data.nodeId] + const index = node.values.indexOf(valueId) + + if (index >= 0) { + node.values.splice(index, 1) + } + } + }) + this.socket.on(this.socketEvents.valueUpdated, data => { - var valueId = self.getValue(data) + const valueId = self.getValue(data) if (valueId) { // this value is waiting for an update @@ -1480,6 +1493,13 @@ export default { } valueId.newValue = data.value valueId.value = data.value + } else { + // means that this value has been added + const node = self.nodes[data.nodeId] + if (node) { + data.newValue = data.value + node.values.push(data) + } } })