@@ -28,7 +28,6 @@ import {config, Logger} from "@foxxmd/winston";
28
28
import { ContextualValidationError } from "@foxxmd/chromecast-client/dist/cjs/src/utils.js" ;
29
29
import { buildTrackString } from "../../core/StringUtils.js" ;
30
30
import { discoveryAvahi , discoveryNative } from "../utils/MDNSUtils.js" ;
31
- import { options } from "superagent" ;
32
31
33
32
interface ChromecastDeviceInfo {
34
33
mdns : MdnsDeviceInfo
@@ -114,10 +113,6 @@ export class ChromecastSource extends MemorySource {
114
113
} = { } ,
115
114
} = this . config ;
116
115
117
- for ( const device of devices ) {
118
- await this . initializeDevice ( { name : device . name , addresses : [ device . address ] , type : 'googlecast' } ) ;
119
- }
120
-
121
116
this . discoverDevices ( logPayload ) ;
122
117
if ( useAutoDiscovery ) {
123
118
this . logger . debug ( 'Will run mDNS discovery on subsequent heartbeats.' )
@@ -131,9 +126,16 @@ export class ChromecastSource extends MemorySource {
131
126
data : {
132
127
useAvahi,
133
128
useAutoDiscovery,
129
+ devices = [ ] ,
134
130
} = { }
135
131
} = this . config ;
136
132
133
+ for ( const device of devices ) {
134
+ this . initializeDevice ( { name : device . name , addresses : [ device . address ] , type : 'googlecast' } ) . catch ( ( err ) => {
135
+ this . logger . error ( new ErrorWithCause ( 'Uncaught error occurred while connecting to manually configured device' , { cause : err } ) ) ;
136
+ } ) ;
137
+ }
138
+
137
139
if ( useAutoDiscovery ) {
138
140
if ( useAvahi ) {
139
141
this . discoverAvahi ( initial ) . catch ( ( err ) => {
@@ -273,6 +275,7 @@ export class ChromecastSource extends MemorySource {
273
275
if ( info === undefined ) {
274
276
this . logger . error ( new ErrorWithCause ( `(${ clientName } ) Encountered error in castv2 lib` , { cause : payload as Error } ) ) ;
275
277
} else {
278
+ info . connected = false ;
276
279
info . logger . error ( new ErrorWithCause ( `Encountered error in castv2 lib` , { cause : payload as Error } ) ) ;
277
280
}
278
281
break ;
@@ -290,14 +293,14 @@ export class ChromecastSource extends MemorySource {
290
293
apps = await getCurrentPlatformApplications ( v . platform ) ;
291
294
v . retries = 0 ;
292
295
} catch ( e ) {
293
- v . logger . warn ( new ErrorWithCause ( ' Could not refresh applications' , { cause : e } ) ) ;
296
+ v . logger . warn ( new ErrorWithCause ( ` Could not refresh applications. Will after ${ 5 - v . retries } retries if error does not resolve itself.` , { cause : e } ) ) ;
294
297
const validationError = findCauseByReference ( e , ContextualValidationError ) ;
295
298
if ( validationError && validationError . data !== undefined ) {
296
299
v . logger . warn ( JSON . stringify ( validationError . data ) ) ;
297
300
}
298
301
v . retries ++ ;
299
- if ( v . retries >= 6 ) {
300
- this . removeApplications ( k , 'Unable to refresh application more than 6 times consecutively! If this device comes back online it will be re-added on next heartbeat.' ) ;
302
+ if ( v . retries >= 5 ) {
303
+ this . removeDevice ( k , 'Unable to refresh application more than 6 times consecutively! If this device comes back online it will be re-added on next heartbeat.' ) ;
301
304
}
302
305
continue ;
303
306
}
@@ -375,13 +378,25 @@ export class ChromecastSource extends MemorySource {
375
378
}
376
379
}
377
380
381
+ protected removeDevice = ( deviceName : string , reason ?: string ) => {
382
+ this . removeApplications ( deviceName , reason ) ;
383
+ if ( reason !== undefined ) {
384
+ this . logger . warn ( reason ) ;
385
+ }
386
+ const device = this . devices . get ( deviceName ) ;
387
+ device . platform . close ( ) ;
388
+ device . client . close ( ) ;
389
+ this . devices . delete ( deviceName ) ;
390
+ }
391
+
378
392
protected removeApplications = ( deviceName : string , reason ?: string ) => {
379
393
const deviceInfo = this . devices . get ( deviceName ) ;
380
394
if ( deviceInfo === undefined ) {
381
395
this . logger . warn ( `No device with ${ deviceName } exists, no applications to remove.` ) ;
382
396
return ;
383
397
}
384
398
for ( const [ tId , app ] of deviceInfo . applications ) {
399
+ app . controller . dispose ( ) ;
385
400
this . deletePlayer ( app . playerId , reason )
386
401
//app.logger.close();
387
402
deviceInfo . applications . delete ( tId ) ;
@@ -390,7 +405,7 @@ export class ChromecastSource extends MemorySource {
390
405
391
406
protected pruneApplications = ( force : boolean = false ) => {
392
407
for ( const [ k , v ] of this . devices . entries ( ) ) {
393
- if ( ! force && ! v . connected ) {
408
+ if ( ! force && ( ! v . connected || v . retries > 0 ) ) {
394
409
continue ;
395
410
}
396
411
@@ -400,6 +415,7 @@ export class ChromecastSource extends MemorySource {
400
415
if ( app . stale && Math . abs ( app . staleAt . diff ( dayjs ( ) , 's' ) ) > 60 ) {
401
416
app . logger . info ( `Removing due to being stale for 60 seconds` ) ;
402
417
//app.logger.close();
418
+ app . controller . dispose ( ) ;
403
419
v . applications . delete ( tId ) ;
404
420
forDeletion . push ( [ app . playerId , 'No updates for 60 seconds' ] ) ;
405
421
} else if ( app . badData && Math . abs ( app . badDataAt . diff ( dayjs ( ) , 's' ) ) > 60 && this . players . has ( app . playerId ) ) {
0 commit comments