Skip to content

Commit

Permalink
feat(chromecast): Try to connect all discovered interfaces in the eve…
Browse files Browse the repository at this point in the history
…nt one fails
  • Loading branch information
FoxxMD committed Jan 25, 2024
1 parent b90e38d commit e855a3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 21 additions & 12 deletions src/backend/sources/ChromecastSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/backend/utils/MDNSUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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});
}
Expand Down

0 comments on commit e855a3d

Please sign in to comment.