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

feat: use inclusion state changed event #3833

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Changes from all commits
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
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
Loading