From e855a3de7dc9bd7e5040bdf863b8c398b7c34e15 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Thu, 25 Jan 2024 09:43:15 -0500 Subject: [PATCH] feat(chromecast): Try to connect all discovered interfaces in the event one fails --- src/backend/sources/ChromecastSource.ts | 33 ++++++++++++++++--------- src/backend/utils/MDNSUtils.ts | 4 +-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/backend/sources/ChromecastSource.ts b/src/backend/sources/ChromecastSource.ts index aa8709bc..9b840995 100644 --- a/src/backend/sources/ChromecastSource.ts +++ b/src/backend/sources/ChromecastSource.ts @@ -230,20 +230,29 @@ export class ChromecastSource extends MemorySource { } protected initializeClientPlatform = async (device: MdnsDeviceInfo): Promise<[CastClient, PersistentClient, PlatformType]> => { - let client: PersistentClient; - let castClient = new CastClient; - castClient.on('connect', () => this.handleCastClientEvent(device.name, 'connect')); - castClient.on('error', (err) => this.handleCastClientEvent(device.name, 'error', err)); - castClient.on('close', () => this.handleCastClientEvent(device.name, 'close')); - try { - client = await connect({host: device.addresses?.[0], client: castClient}); - } catch (e) { - throw new ErrorWithCause(`Could not connect to ${device.name}`, {cause: e}); - } - const platform = createPlatform(client); + let index = 0; + for(const address of device.addresses) { + let client: PersistentClient; + let castClient = new CastClient; + castClient.on('connect', () => this.handleCastClientEvent(device.name, 'connect')); + castClient.on('error', (err) => this.handleCastClientEvent(device.name, 'error', err)); + castClient.on('close', () => this.handleCastClientEvent(device.name, 'close')); + try { + client = await connect({host: address, client: castClient}); + } catch (e) { + if(index < device.addresses.length - 1) { + this.logger.warn(new ErrorWithCause(`Could not connect to ${device.name} but more interfaces exist, will attempt next host.`, {cause: e})); + continue; + } else { + throw new ErrorWithCause(`Could not connect to ${device.name} and no additional interfaces exist`, {cause: e}); + } + } - return [castClient, client, platform]; + const platform = createPlatform(client); + + return [castClient, client, platform]; + } } protected handleCastClientEvent = (clientName: string, event: string, payload?: any) => { diff --git a/src/backend/utils/MDNSUtils.ts b/src/backend/utils/MDNSUtils.ts index f00f0d6e..053d37a0 100644 --- a/src/backend/utils/MDNSUtils.ts +++ b/src/backend/utils/MDNSUtils.ts @@ -43,7 +43,7 @@ export const discoveryAvahi = async (service: string, options?: DiscoveryOptions const triggerDiscovery = () => { for(const [k,v] of services.entries()) { - maybeLogger.debug(`Discovered device "${v.name}" with ${v.addresses.length} interfaces - first host seen: ${v.addresses[0]}`); + maybeLogger.debug(`Discovered device "${v.name}" with ${v.addresses.length} interfaces`); onDiscover(v); services.delete(k); } @@ -135,7 +135,7 @@ export const discoveryNative = async (service: string, options?: DiscoveryOption const browser = new Browser(service, {resolve: true}) .on('serviceUp', async (service) => { - maybeLogger.debug(`Discovered device "${service.name}" with ${service.addresses.length} interfaces -- first host seen: ${service.addresses?.[0]}`); + maybeLogger.debug(`Discovered device "${service.name}" with ${service.addresses.length} interfaces`); if (onDiscover) { onDiscover({name: service.name, addresses: service.addresses, type: service.service_type}); }