Skip to content

Commit

Permalink
fix: retry adding devices if the add fails (note: needs newer compani…
Browse files Browse the repository at this point in the history
…on version to work)
  • Loading branch information
Julusian committed Apr 20, 2022
1 parent d41695e commit 447e3e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type CompanionSatelliteClientEvents = {
brightness: [{ deviceId: string; percent: number }]
newDevice: [{ deviceId: string }]
clearDeck: [{ deviceId: string }]
deviceErrored: [{ deviceId: string; message: string }]
}

export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatelliteClientEvents> {
Expand Down Expand Up @@ -241,6 +242,9 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit
case 'ADD-DEVICE':
this.handleAddedDevice(params)
break
case 'REMOVE-DEVICE':
console.log('Removed device: ${body}')
break
case 'BEGIN':
console.log(`Connected to Companion: ${body}`)
break
Expand Down Expand Up @@ -304,6 +308,12 @@ export class CompanionSatelliteClient extends EE3.EventEmitter<CompanionSatellit
private handleAddedDevice(params: Record<string, string | boolean>): void {
if (!params.OK || params.ERROR) {
console.log(`Add device failed: ${JSON.stringify(params)}`)
if (typeof params.DEVICEID === 'string') {
this.emit('deviceErrored', {
deviceId: params.DEVICEID,
message: `${params.MESSAGE || 'Unknown Error'}`,
})
}
return
}
if (typeof params.DEVICEID !== 'string') {
Expand Down
24 changes: 24 additions & 0 deletions src/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@ export class DeviceManager {
console.error(`Setup device: ${e}`)
}
})
client.on('deviceErrored', async (d) => {
try {
const dev = this.devices.get(d.deviceId)
if (dev) {
await dev.showStatus(this.client.host, d.message)

// Try again to add the device, in case we can recover
this.delayRetryAddOfDevice(d.deviceId)
} else {
throw new Error(`Device missing: ${d.deviceId}`)
}
} catch (e) {
console.error(`Failed device: ${e}`)
}
})
}

private delayRetryAddOfDevice(deviceId: string) {
setTimeout(() => {
const dev = this.devices.get(deviceId)
if (dev) {
this.client.addDevice(deviceId, dev.productName, dev.getRegisterProps())
}
}, 1000)
}

public async close(): Promise<void> {
Expand Down

0 comments on commit 447e3e6

Please sign in to comment.