Skip to content

Commit

Permalink
fix: dinamically update ui on value added/removed
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Nov 27, 2020
1 parent 8bb5936 commit f7c5873
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
24 changes: 16 additions & 8 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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])
}
}

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -935,7 +943,7 @@ ZwaveClient.prototype.updateDevice = function (
node.hassDevices[id] = hassDevice
}

this.emitEvent(socketEvents.nodeUpdated, node)
this.sendToSocket(socketEvents.nodeUpdated, node)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export default {
nodeRemoved: 'NODE_REMOVED',
nodeUpdated: 'NODE_UPDATED',
valueUpdated: 'VALUE_UPDATED',
valueRemoved: 'VALUE_REMOVED',
api: 'API_RETURN',
debug: 'DEBUG'
},
Expand Down
22 changes: 21 additions & 1 deletion src/components/ControlPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
})
Expand Down

0 comments on commit f7c5873

Please sign in to comment.