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: prevent restarting driver on hard reset #3748

Closed
wants to merge 6 commits into from
Closed
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
74 changes: 40 additions & 34 deletions api/lib/ZwaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,6 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

this.closed = false
this.driverReady = false
this.hasUserCallbacks = false
this.scenes = jsonStore.get(store.scenes)

this._nodes = new Map()
Expand Down Expand Up @@ -2260,7 +2259,7 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
// this could throw so include in the try/catch
this._driver = new Driver(this.cfg.port, zwaveOptions)
this._driver.on('error', this._onDriverError.bind(this))
this._driver.once('driver ready', this._onDriverReady.bind(this))
this._driver.on('driver ready', this._onDriverReady.bind(this))
this._driver.on('all nodes ready', this._onScanComplete.bind(this))
this._driver.on(
'bootloader ready',
Expand Down Expand Up @@ -2299,9 +2298,7 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

this.server.on('hard reset', () => {
logger.info('Hard reset requested by ZwaveJS Server')
this.restart().catch((err) => {
logger.error(err)
})
this.init()
})
}

Expand Down Expand Up @@ -3928,9 +3925,7 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {
async hardReset() {
if (this.driverReady) {
await this._driver.hardReset()
this.restart().catch((err) => {
logger.error(err)
})
this.init()
} else {
throw new DriverNotReadyError()
}
Expand Down Expand Up @@ -4234,10 +4229,12 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

private async _onDriverReady() {
/*
Now the controller interview is complete. This means we know which nodes
are included in the network, but they might not be ready yet.
The node interview will continue in the background.
*/
Now the controller interview is complete. This means we know which nodes
are included in the network, but they might not be ready yet.
The node interview will continue in the background.

NOTE: This can be called also after an Hard Reset
*/

// driver ready
this.status = ZwaveClientStatus.DRIVER_READY
Expand All @@ -4248,26 +4245,32 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

this._updateControllerStatus('Driver ready')

this._inclusionStateInterval = setInterval(() => {
if (
this._driver.controller.inclusionState !== this._inclusionState
) {
this._inclusionState = this._driver.controller.inclusionState
if (this._inclusionStateInterval) {
this._inclusionStateInterval = setInterval(() => {
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)
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(() => {
/* ignore */
})

this.driver.controller.removeAllListeners()

this.driver.controller
.on('inclusion started', this._onInclusionStarted.bind(this))
.on('exclusion started', this._onExclusionStarted.bind(this))
Expand Down Expand Up @@ -4331,16 +4334,19 @@ class ZwaveClient extends TypedEventEmitter<ZwaveClientEventCallbacks> {

// start server only when driver is ready. Fixes #602
if (this.cfg.serverEnabled && this.server) {
this.server
.start(!this.hasUserCallbacks)
.then(() => {
logger.info('Z-Wave server started')
})
.catch((error) => {
logger.error(
`Failed to start zwave-js server: ${error.message}`,
)
})
// fix prevent to start server when already inited
if (!this.server['server']) {
this.server
.start(!this.hasUserCallbacks)
.then(() => {
logger.info('Z-Wave server started')
})
.catch((error) => {
logger.error(
`Failed to start zwave-js server: ${error.message}`,
)
})
}
}

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