@@ -192,7 +192,7 @@ export class ChromecastSource extends MemorySource {
192
192
let storedApp = v . applications . get ( a . transportId ) ;
193
193
if ( ! storedApp ) {
194
194
const appName = a . displayName ;
195
- let found = `Found Application '${ appName } '` ;
195
+ let found = `Found Application '${ appName } - ${ a . transportId . substring ( 0 , 4 ) } '` ;
196
196
const appLowerName = appName . toLocaleLowerCase ( ) ;
197
197
let filtered = false ;
198
198
let valid = true ;
@@ -233,7 +233,7 @@ export class ChromecastSource extends MemorySource {
233
233
badData : false ,
234
234
validAppType : valid ,
235
235
playerId : genGroupIdStr ( [ genDeviceId ( k , a . displayName ) , NO_USER ] ) ,
236
- logger : v . logger . child ( { labels : [ `App ${ a . displayName . substring ( 0 , 25 ) } ` ] } , mergeArr )
236
+ logger : v . logger . child ( { labels : [ `App ${ a . displayName . substring ( 0 , 25 ) } - ${ a . transportId . substring ( 0 , 4 ) } ` ] } , mergeArr )
237
237
}
238
238
v . applications . set ( a . transportId , storedApp ) ;
239
239
} else if ( storedApp . stale === true ) {
@@ -280,15 +280,37 @@ export class ChromecastSource extends MemorySource {
280
280
continue ;
281
281
}
282
282
283
+ const forDeletion : [ string , string ] [ ] = [ ] ;
284
+
283
285
for ( const [ tId , app ] of v . applications . entries ( ) ) {
284
286
if ( app . stale && Math . abs ( app . staleAt . diff ( dayjs ( ) , 's' ) ) > 60 ) {
285
287
app . logger . info ( `Removing due to being stale for 60 seconds` ) ;
286
- this . deletePlayer ( app . playerId , 'No updates for 60 seconds' ) ;
287
288
//app.logger.close();
288
289
v . applications . delete ( tId ) ;
290
+ forDeletion . push ( [ app . playerId , 'No updates for 60 seconds' ] ) ;
289
291
} else if ( app . badData && Math . abs ( app . badDataAt . diff ( dayjs ( ) , 's' ) ) > 60 && this . players . has ( app . playerId ) ) {
290
- app . logger . info ( `Removing player due to bad data for 60 seconds` ) ;
291
- this . deletePlayer ( app . playerId , 'Bad data for 60 seconds' ) ;
292
+ forDeletion . push ( [ app . playerId , 'Bad data for 60 seconds' ] ) ;
293
+ }
294
+ }
295
+ if ( forDeletion . length > 0 ) {
296
+ // if the cast device disconnected and reconnected (for some reason)
297
+ // or a user disconnected and then reconnected manually
298
+ // -- for the same *app*
299
+ // then the same playerId will exist for two applications that have different destination/session ids
300
+ // and we don't want to delete the player if another exists that isn't also being deleted
301
+ for ( const [ playerId , reason ] of forDeletion ) {
302
+ if ( ! this . players . has ( playerId ) ) {
303
+ // already deleted
304
+ continue ;
305
+ }
306
+ const apps = Array . from ( v . applications . values ( ) ) ;
307
+ // check that either all apps with this player id are gone
308
+ if ( apps . every ( ( x => x . playerId !== playerId ) ) ) {
309
+ this . deletePlayer ( playerId , reason ) ;
310
+ } // or that all actually have bad data
311
+ else if ( ! apps . some ( x => x . playerId === x . playerId && ! x . badData ) ) {
312
+ this . deletePlayer ( playerId , reason ) ;
313
+ }
292
314
}
293
315
}
294
316
}
@@ -339,15 +361,18 @@ export class ChromecastSource extends MemorySource {
339
361
}
340
362
341
363
if ( this . config . options . logPayload ) {
342
- application . logger . debug ( `Media Status Payload:\n ${ JSON . stringify ( mediaStatus ) } ` ) ;
364
+ application . logger . debug ( `Media Status Payload:\n ${ mediaStatus === undefined || mediaStatus === null ? 'undefined' : JSON . stringify ( mediaStatus ) } ` ) ;
343
365
}
344
366
345
- const play = ChromecastSource . formatPlayObj ( mediaStatus , {
346
- deviceId : genDeviceId ( k , application . displayName ) ,
347
- source : application . displayName
348
- } ) ;
367
+ let play : PlayObject | undefined ;
368
+ if ( mediaStatus !== undefined && mediaStatus !== null ) {
369
+ play = ChromecastSource . formatPlayObj ( mediaStatus , {
370
+ deviceId : genDeviceId ( k , application . displayName ) ,
371
+ source : application . displayName
372
+ } ) ;
373
+ }
349
374
350
- if ( play . data . artists . length === 0 || play . data . track === undefined ) {
375
+ if ( play === undefined || play . data . artists . length === 0 || play . data . track === undefined ) {
351
376
if ( ! application . badData ) {
352
377
application . logger . warn ( `Media information either did not return artists or track. This isn't scrollable! Skipping this update and marking App as having bad data (to be removed after 60 seconds)` ) ;
353
378
application . badData = true ;
0 commit comments