Skip to content

Commit

Permalink
feat: use inclusion state changed event (#3833)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Aug 5, 2024
1 parent e590d88 commit a452b02
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions api/lib/ZwaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,6 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
validateDSKAndEnterPIN: this._onValidateDSK.bind(this),
abort: this._onAbortInclusion.bind(this),
}
private _inclusionStateInterval: NodeJS.Timeout

private _inclusionState: InclusionState = undefined

private _controllerListenersAdded: boolean = false
Expand Down Expand Up @@ -1135,11 +1133,6 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
this.closed = true
this.driverReady = false

if (this._inclusionStateInterval) {
clearInterval(this._inclusionStateInterval)
this._inclusionStateInterval = null
}

if (this.commandsTimeout) {
clearTimeout(this.commandsTimeout)
this.commandsTimeout = null
Expand Down Expand Up @@ -2387,6 +2380,15 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
}
}

private async sendInitToSockets() {
const sockets = await this.socket.fetchSockets()

for (const socket of sockets) {
// force send init to all connected sockets
socket.emit(socketEvents.init, this.getState())
}
}

public emitValueChanged(
valueId: ZUIValueId,
node: ZUINode,
Expand Down Expand Up @@ -4293,26 +4295,6 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

this._updateControllerStatus('Driver ready')

if (!this._inclusionStateInterval) {
this._inclusionStateInterval = setInterval(() => {
if (!this.driverReady) return

if (
this._driver.controller.inclusionState !==
this._inclusionState
) {
this._inclusionState =
this._driver.controller.inclusionState

this.sendToSocket(socketEvents.controller, {
status: this._cntStatus,
error: this._error,
inclusionState: this._inclusionState,
})
}
}, 2000)
}

try {
// this must be done only after driver is ready
this._scheduledConfigCheck().catch(() => {
Expand All @@ -4338,6 +4320,10 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
'exclusion stopped',
this._onExclusionStopped.bind(this),
)
.on(
'inclusion state changed',
this._onInclusionStateChanged.bind(this),
)
.on('inclusion failed', this._onInclusionFailed.bind(this))
.on('exclusion failed', this._onExclusionFailed.bind(this))
.on('node found', this._onNodeFound.bind(this))
Expand Down Expand Up @@ -4417,12 +4403,7 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

logger.info(`Scanning network with homeid: ${homeHex}`)

const sockets = await this.socket.fetchSockets()

for (const socket of sockets) {
// force send init to all connected sockets
socket.emit(socketEvents.init, this.getState())
}
await this.sendInitToSockets()

this.loadFakeNodes().catch((e) => {
logger.error(`Error while loading fake nodes: ${e.message}`)
Expand Down Expand Up @@ -4657,6 +4638,18 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
this.emit('event', EventSource.CONTROLLER, 'exclusion stopped')
}

private _onInclusionStateChanged(state: InclusionState) {
if (state !== this._inclusionState) {
this._inclusionState = state

this.sendToSocket(socketEvents.controller, {
status: this._cntStatus,
error: this._error,
inclusionState: this._inclusionState,
})
}
}

private _onInclusionFailed() {
const message = 'Inclusion failed'
this.isReplacing = false
Expand Down

0 comments on commit a452b02

Please sign in to comment.