Skip to content

Commit 7008ae8

Browse files
committed
feat(chrome): Handle applications with transportId context
* Add transportId shortname to logger label for better differentiation of "per session" apps * Fix handling of MS player deletion when pruning apps by checking for other valid apps before deletion
1 parent 10d9fa3 commit 7008ae8

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/backend/sources/ChromecastSource.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class ChromecastSource extends MemorySource {
192192
let storedApp = v.applications.get(a.transportId);
193193
if(!storedApp) {
194194
const appName = a.displayName;
195-
let found = `Found Application '${appName}'`;
195+
let found = `Found Application '${appName}-${a.transportId.substring(0, 4)}'`;
196196
const appLowerName = appName.toLocaleLowerCase();
197197
let filtered = false;
198198
let valid = true;
@@ -233,7 +233,7 @@ export class ChromecastSource extends MemorySource {
233233
badData: false,
234234
validAppType: valid,
235235
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)
237237
}
238238
v.applications.set(a.transportId, storedApp);
239239
} else if(storedApp.stale === true) {
@@ -280,15 +280,37 @@ export class ChromecastSource extends MemorySource {
280280
continue;
281281
}
282282

283+
const forDeletion: [string, string][] = [];
284+
283285
for(const [tId, app] of v.applications.entries()) {
284286
if(app.stale && Math.abs(app.staleAt.diff(dayjs(), 's')) > 60) {
285287
app.logger.info(`Removing due to being stale for 60 seconds`);
286-
this.deletePlayer(app.playerId, 'No updates for 60 seconds');
287288
//app.logger.close();
288289
v.applications.delete(tId);
290+
forDeletion.push([app.playerId, 'No updates for 60 seconds']);
289291
} 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+
}
292314
}
293315
}
294316
}
@@ -339,15 +361,18 @@ export class ChromecastSource extends MemorySource {
339361
}
340362

341363
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)}`);
343365
}
344366

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+
}
349374

350-
if (play.data.artists.length === 0 || play.data.track === undefined) {
375+
if (play === undefined || play.data.artists.length === 0 || play.data.track === undefined) {
351376
if (!application.badData) {
352377
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)`);
353378
application.badData = true;

0 commit comments

Comments
 (0)