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

fix: use getNeighbors method #1146

Merged
merged 5 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const allowedApis = [
'_removeSceneValue',
'_activateScene',
'refreshNeighbors',
'getNodeNeighbors',
'getAssociations',
'addAssociations',
'removeAssociations',
Expand Down Expand Up @@ -370,7 +371,7 @@ async function onNodeReady (zwaveNode) {
node.ready = false
node.values = {}

dumpNode.call(this, zwaveNode)
await dumpNode.call(this, zwaveNode)

const values = zwaveNode.getDefinedValueIDs()

Expand Down Expand Up @@ -459,11 +460,11 @@ function onNodeInterviewStageCompleted (zwaveNode, stageName) {
*
* @param {import('zwave-js').ZWaveNode} zwaveNode
*/
function onNodeInterviewCompleted (zwaveNode) {
async function onNodeInterviewCompleted (zwaveNode) {
const node = this.nodes.get(zwaveNode.id)

if (node.manufacturerId === undefined) {
dumpNode.call(this, zwaveNode)
await dumpNode.call(this, zwaveNode)
}

logger.info(
Expand Down Expand Up @@ -861,7 +862,7 @@ function removeNode (nodeid) {
* @param {import('zwave-js').ZWaveNode} zwaveNode
* @returns {import('../types/index.js').Z2MNode}
*/
function addNode (zwaveNode) {
async function addNode (zwaveNode) {
const nodeId = zwaveNode.id

let node = this.nodes.get(nodeId)
Expand Down Expand Up @@ -906,7 +907,7 @@ function addNode (zwaveNode) {

this.nodes.set(nodeId, node)

dumpNode.call(this, zwaveNode)
await dumpNode.call(this, zwaveNode)

onNodeStatus.call(this, zwaveNode)
logger.debug(`Node ${nodeId} has been added to nodes array`)
Expand All @@ -921,7 +922,7 @@ function addNode (zwaveNode) {
*
* @param {import('zwave-js').ZWaveNode} zwaveNode
*/
function dumpNode (zwaveNode) {
async function dumpNode (zwaveNode) {
const nodeId = zwaveNode.id

const node = this.nodes.get(nodeId)
Expand Down Expand Up @@ -978,7 +979,7 @@ function dumpNode (zwaveNode) {
specific: deviceClass.specific.key
}

node.neighbors = zwaveNode.neighbors
node.neighbors = await this.getNodeNeighbors(nodeId, true)

const storedNode = this.storeNodes[nodeId]

Expand Down Expand Up @@ -1808,15 +1809,41 @@ ZwaveClient.prototype.removeNodeFromAllAssociations = async function (nodeId) {
* @returns {Map<number, number[]>} The nodes array where `nodeId` is the array index and the value is the array
* of neighburns of that `nodeId`
*/
ZwaveClient.prototype.refreshNeighbors = function () {
ZwaveClient.prototype.refreshNeighbors = async function () {
const toReturn = {}
for (const [nodeId, node] of this.nodes) {
node.neighbors = this.getNode(nodeId).neighbors
try {
node.neighbors = await this.getNodeNeighbors(nodeId, true)
} catch (error) {}
toReturn[nodeId] = node.neighbors
}

return toReturn
}

/**
* Get neighbors of a specific node
*
* @param {number} nodeId
* @param {boolean} dontThrow
* @returns {number[]} Node neighbors
* of neighburns of that `nodeId`
*/
ZwaveClient.prototype.getNodeNeighbors = async function (nodeId, dontThrow) {
let neighbors = []
try {
neighbors = await this.driver.controller.getNodeNeighbors(nodeId)
} catch (error) {
logger.error(
`Node ${nodeId} error while updating Neighbors: ${error.message}`
)
if (!dontThrow) {
throw error
}
}

return neighbors
}
/**
* Execute a custom function with the driver
*
Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ export interface ZwaveClient extends EventEmitter {
): Promise<void>
removeAllAssociations(nodeId: number): Promise<void>
removeNodeFromAllAssociations(nodeId: number): Promise<void>
refreshNeighbors(): void
refreshNeighbors(): Promise<Map<number, number[]>>
getNodeNeighbors(): Promise<number[]>
driverFunction(code: string): Promise<void>
connect(): Promise<void>
sendToSocket(evtName: string, data: any): void
Expand Down