Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make nodes a map instead of an array #710

Merged
merged 14 commits into from
Feb 24, 2021
Merged
45 changes: 28 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function setupSocket (server) {
socketManager.on(inboundEvents.init, function (socket) {
if (gw.zwave) {
socket.emit(socketEvents.init, {
nodes: gw.zwave.nodes,
nodes: gw.zwave.getNodes(),
info: gw.zwave.getInfo(),
error: gw.zwave.error,
cntStatus: gw.zwave.cntStatus
Expand All @@ -347,7 +347,7 @@ function setupSocket (server) {
})

socketManager.on(inboundEvents.mqtt, async function (socket, data) {
logger.info(`Mqtt api call: ${data.apiName}`)
logger.info(`Mqtt api call: ${data.api}`)

let res, err

Expand Down Expand Up @@ -669,27 +669,38 @@ app.post('/api/importConfig', apisLimiter, isAuthenticated, async function (
req,
res
) {
const config = req.body.data
let config = req.body.data
try {
if (!gw.zwave) throw Error('Zwave client not inited')

if (!Array.isArray(config)) throw Error('Configuration not valid')
else {
// try convert to node object
if (Array.isArray(config)) {
const parsed = {}

for (let i = 0; i < config.length; i++) {
const e = config[i]
if (
e &&
(!utils.hasProperty(e, 'name') || !utils.hasProperty(e, 'loc'))
) {
continue
} else if (e) {
await gw.zwave.callApi('_setNodeName', i, e.name || '')
await gw.zwave.callApi('_setNodeLocation', i, e.loc || '')
if (e.hassDevices) {
await gw.zwave.storeDevices(e.hassDevices, i, false)
}
if (config[i]) {
parsed[i] = config[i]
}
}

config = parsed
}

for (const nodeId in config) {
const node = config[nodeId]
if (!node || typeof node !== 'object') continue

if (utils.hasProperty(node, 'name')) {
await gw.zwave.callApi('setNodeName', nodeId, node.name || '')
}

if (utils.hasProperty(node, 'loc')) {
await gw.zwave.callApi('setNodeLocation', nodeId, node.loc || '')
}

if (node.hassDevices) {
await gw.zwave.storeDevices(node.hassDevices, nodeId, false)
}
}

res.json({ success: true, message: 'Configuration imported successfully' })
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/mqtt.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This are the available apis:
- `_addSceneValue(sceneId, valueId, value, timeout)`: Add a value to a specific scene
- `_removeSceneValue(sceneId, valueId)`: remove a valueId from a scene
- `_activateScene(sceneId)`: activate a scene
- `_setNodeName(name)` and `_setNodeLocation(location)` will use internal nodes store to save nodes names/locations in a json file
- `setNodeName(name)` and `setNodeLocation(location)` will use internal nodes store to save nodes names/locations in a json file and will also try to store this info on the controller
- `refreshNeighborns()`: Returns an Array where the Array index is the `nodeId`, array value is an Array with all the ids of the node neighborns
- `getNodes()`: Returns an array with all nodes in the network (and their info/valueids)
- `getInfo()`: Returns an object with:
Expand Down
18 changes: 9 additions & 9 deletions lib/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ Gateway.prototype.parsePayload = function (payload, valueId, valueConf) {
}

if (valueConf.parseReceive) {
const node = this.zwave.nodes[valueId.nodeId]
const node = this.zwave.nodes.get(valueId.nodeId)
const parsedVal = evalFunction(
valueConf.receiveFunction,
valueId,
Expand Down Expand Up @@ -1091,7 +1091,7 @@ Gateway.prototype.valueTopic = function (node, valueId, returnObject = false) {
* @param {number} nodeID
*/
Gateway.prototype.rediscoverNode = function (nodeID) {
const node = this.zwave.nodes[nodeID]
const node = this.zwave.nodes.get(nodeID)
if (node) {
robertsLando marked this conversation as resolved.
Show resolved Hide resolved
// delete all discovered values
onNodeRemoved.call(this, node)
Expand All @@ -1117,7 +1117,7 @@ Gateway.prototype.rediscoverNode = function (nodeID) {
* @param {number} nodeID
*/
Gateway.prototype.disableDiscovery = function (nodeID) {
const node = this.zwave.nodes[nodeID]
const node = this.zwave.nodes.get(nodeID)
if (node && node.hassDevices) {
for (const id in node.hassDevices) {
node.hassDevices[id].ignoreDiscovery = true
Expand Down Expand Up @@ -1219,15 +1219,15 @@ Gateway.prototype.rediscoverAll = function () {
if (!this.config.hassDiscovery) return

const nodes = this.zwave ? this.zwave.nodes : []
for (let i = 0; i < nodes.length; i++) {
const devices = nodes[i] && nodes[i].hassDevices ? nodes[i].hassDevices : {}
for (const [nodeId, node] of nodes) {
const devices = node.hassDevices || {}
for (const id in devices) {
const d = devices[id]
if (d && d.discoveryTopic && d.discovery_payload) {
this.publishDiscovery(d, i)
this.publishDiscovery(d, nodeId)
}
} // end foreach hassdevice
} // end foreach node
}
}

/**
Expand Down Expand Up @@ -2126,7 +2126,7 @@ Gateway.prototype.discoverValue = function (node, vId) {
* @param {number} nodeId
*/
Gateway.prototype.updateNodeTopics = function (nodeId) {
const node = this.zwave.nodes[nodeId]
const node = this.zwave.nodes.get(nodeId)
if (node) {
const topics = Object.keys(this.topicValues).filter(
k => this.topicValues[k].nodeId === node.id
Expand All @@ -2147,7 +2147,7 @@ Gateway.prototype.updateNodeTopics = function (nodeId) {
* @param {number} nodeId
*/
Gateway.prototype.removeNodeRetained = function (nodeId) {
const node = this.zwave.nodes[nodeId]
const node = this.zwave.nodes.get(nodeId)
if (node) {
const topics = Object.keys(node.values).map(v =>
this.valueTopic(node, node.values[v])
Expand Down
Loading