homebridge-plex-intermission 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -1
- package/README.md +11 -5
- package/dist/config.js +33 -2
- package/dist/controller.js +85 -6
- package/dist/diagnostics.js +27 -1
- package/dist/platform.js +4 -0
- package/dist/players.js +163 -0
- package/dist/plex/convert.js +1 -0
- package/dist/plex/discovery.js +1 -0
- package/dist/report.js +78 -7
- package/dist/sensors/rules.js +61 -6
- package/dist/setup-service.js +16 -1
- package/dist/storage.js +8 -0
- package/homebridge-ui/public/app.css +7 -0
- package/homebridge-ui/public/app.js +332 -20
- package/homebridge-ui/public/guide.js +1 -1
- package/homebridge-ui/public/model.js +277 -10
- package/homebridge-ui/server.js +3 -1
- package/package.json +1 -1
package/dist/report.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import { commandLine, person, player, readDiagnostics, redact, scrubSecrets } from './diagnostics.js';
|
|
2
|
+
import { commandLine, person, player, readDiagnostics, redact, scrubSecrets, tailDiagnostics } from './diagnostics.js';
|
|
3
|
+
import { isPrivateAddress } from './sensors/rules.js';
|
|
3
4
|
import { hidePublicAddresses, maskAddress } from './network.js';
|
|
4
5
|
import { webhookOptionsFrom } from './setup-service.js';
|
|
5
6
|
const PORT_STATES = {
|
|
@@ -91,7 +92,22 @@ export async function setupReport(service, input) {
|
|
|
91
92
|
export async function diagnosticsReport(service, input, now) {
|
|
92
93
|
const config = input.config;
|
|
93
94
|
const selection = (config.selection ?? {});
|
|
94
|
-
const
|
|
95
|
+
const players = new Map((selection.players ?? []).map((p) => [p.id, p]));
|
|
96
|
+
// The app and the start of the ID tell apart players with the same name.
|
|
97
|
+
const describePlayer = (id) => {
|
|
98
|
+
const found = players.get(id);
|
|
99
|
+
const tell = [found?.product, id.slice(0, 8)].filter(Boolean).join(', ');
|
|
100
|
+
return found?.name ? `${player(found.name)} (${tell})` : id;
|
|
101
|
+
};
|
|
102
|
+
const devices = new Map((Array.isArray(config.devices) ? config.devices : []).map((d) => [d.id, d]));
|
|
103
|
+
const describeDevice = (id) => {
|
|
104
|
+
const device = devices.get(id);
|
|
105
|
+
if (!device) {
|
|
106
|
+
return `${id} (no longer defined, matches nothing)`;
|
|
107
|
+
}
|
|
108
|
+
const apps = device.apps?.length ? `, only ${device.apps.map((a) => a.slice(0, 8)).join(', ')}` : '';
|
|
109
|
+
return `${player(device.name ?? device.match)} (device, any app named ${player(device.match)} at home${apps})`;
|
|
110
|
+
};
|
|
95
111
|
const libraryNames = new Map((selection.libraries ?? []).map((l) => [l.key, l.title ?? l.key]));
|
|
96
112
|
const commands = new Map((config.commands?.list ?? []).map((c) => [c.id, c]));
|
|
97
113
|
const list = (values, name, all) => {
|
|
@@ -114,7 +130,12 @@ export async function diagnosticsReport(service, input, now) {
|
|
|
114
130
|
lines.push(sensor.type === 'button'
|
|
115
131
|
? ` pressed on: ${list(sensor.events, (e) => e, 'nothing')}`
|
|
116
132
|
: ` on while: ${String(sensor.state)}${Array.isArray(sensor.except) && sensor.except.length ? `, except ${sensor.except.join(' and ')}` : ''}`);
|
|
117
|
-
|
|
133
|
+
const chosen = [...(Array.isArray(f.devices) ? f.devices.map((d) => describeDevice(String(d))) : []),
|
|
134
|
+
...(Array.isArray(f.players) ? f.players.map((p) => describePlayer(String(p))) : [])];
|
|
135
|
+
lines.push(` players: ${chosen.length ? chosen.join(', ') : 'all'}`);
|
|
136
|
+
if (Array.isArray(f.exceptPlayers) && f.exceptPlayers.length) {
|
|
137
|
+
lines.push(` except apps: ${f.exceptPlayers.map((p) => describePlayer(String(p))).join(', ')}`);
|
|
138
|
+
}
|
|
118
139
|
lines.push(` people: ${list(f.users, (u) => person(u), 'all')}`);
|
|
119
140
|
lines.push(` libraries: ${list(f.libraries, (k) => libraryNames.get(k) ?? k, 'all')}`);
|
|
120
141
|
if (sensor.media && Object.keys(sensor.media).length) {
|
|
@@ -130,10 +151,60 @@ export async function diagnosticsReport(service, input, now) {
|
|
|
130
151
|
lines.push(`${command.name ?? '?'}: ${commandLine(command.command ?? '')}`);
|
|
131
152
|
}
|
|
132
153
|
}
|
|
133
|
-
const
|
|
134
|
-
|
|
154
|
+
const { seen, followed, sharedNames } = await service.players();
|
|
155
|
+
const seenIds = Object.keys(seen).sort((a, b) => Date.parse(seen[b].lastPlayedAt) - Date.parse(seen[a].lastPlayedAt));
|
|
156
|
+
if (seenIds.length > 0) {
|
|
157
|
+
lines.push('', `== Players seen playing (${seenIds.length}) ==`);
|
|
158
|
+
for (const id of seenIds) {
|
|
159
|
+
const p = seen[id];
|
|
160
|
+
const followsAs = followed[id]?.length ? `, followed as ${followed[id].map((t) => t.slice(0, 8)).join(', ')}` : '';
|
|
161
|
+
const where = [p.address ? (isPrivateAddress(p.address) ? 'private address' : 'public address') : '', p.local === undefined ? '' : `Plex says ${p.local ? 'local' : 'remote'}`];
|
|
162
|
+
lines.push(`${p.name ? player(p.name) : '?'} (${[p.product, p.platform, id.slice(0, 8)].filter(Boolean).join(', ')}): last played ${p.lastPlayedAt}`
|
|
163
|
+
+ `${where.filter(Boolean).map((w) => `, ${w}`).join('')}${players.has(id) ? ', chosen' : ''}${followsAs}`);
|
|
164
|
+
}
|
|
165
|
+
lines.push(...networkCheck(Object.values(seen), Object.keys(sharedNames)));
|
|
166
|
+
}
|
|
167
|
+
const log = (await readDiagnostics(service.storage.directory)).reverse();
|
|
168
|
+
lines.push('', `== Recent log (${log.length} lines, newest first, detailed) ==`, ...(log.length ? log : [NOTHING_LOGGED]));
|
|
169
|
+
return hidePublicAddresses(scrubSecrets(redact(lines.join('\n'), input.includeNames === true), await secretsOf(service)));
|
|
170
|
+
}
|
|
171
|
+
const NOTHING_LOGGED = 'Nothing logged yet. Restart Homebridge once, so Intermission starts keeping its log.';
|
|
172
|
+
/**
|
|
173
|
+
* The newest lines of the kept log, newest first, for watching on the settings page. Shown only on
|
|
174
|
+
* this machine, so names are kept; secrets are still removed.
|
|
175
|
+
*/
|
|
176
|
+
export async function liveLog(service, maxLines = 200) {
|
|
177
|
+
const log = (await tailDiagnostics(service.storage.directory, maxLines)).reverse();
|
|
178
|
+
if (log.length === 0) {
|
|
179
|
+
return [NOTHING_LOGGED];
|
|
180
|
+
}
|
|
181
|
+
return scrubSecrets(redact(log.join('\n'), true), await secretsOf(service)).split('\n');
|
|
182
|
+
}
|
|
183
|
+
async function secretsOf(service) {
|
|
135
184
|
const credentials = await service.storage.readCredentials();
|
|
136
185
|
const { webhookSecret } = await service.storage.state();
|
|
137
|
-
|
|
138
|
-
|
|
186
|
+
return [webhookSecret, credentials?.accountToken, ...Object.values(credentials?.serverTokens ?? {})].filter((s) => Boolean(s));
|
|
187
|
+
}
|
|
188
|
+
/** What the players' addresses say about the network, for "my Apple TV is ignored" reports. */
|
|
189
|
+
function networkCheck(seen, sharedNames) {
|
|
190
|
+
const lines = ['', '== Network check =='];
|
|
191
|
+
const byAddress = new Map();
|
|
192
|
+
for (const p of seen) {
|
|
193
|
+
if (p.address) {
|
|
194
|
+
byAddress.set(p.address, [...(byAddress.get(p.address) ?? []), p]);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const crowded = [...byAddress.values()].filter((group) => new Set(group.flatMap((p) => (p.platform ? [p.platform.toLowerCase()] : []))).size > 1);
|
|
198
|
+
lines.push(crowded.length
|
|
199
|
+
? 'Players on different platforms share an address, so Plex does not see their real addresses (Docker Desktop, rootless Docker or a proxy?). Addresses are not used to tell devices apart.'
|
|
200
|
+
: `Players' addresses look real (${byAddress.size} distinct).`);
|
|
201
|
+
const privateButRemote = seen.filter((p) => isPrivateAddress(p.address) && p.local === false).length;
|
|
202
|
+
if (privateButRemote > 0) {
|
|
203
|
+
lines.push(`${privateButRemote} player(s) with a private address are marked remote by Plex. Intermission still counts them as at home; `
|
|
204
|
+
+ 'adding your home network under LAN Networks in Plex\'s Network settings fixes it in Plex too.');
|
|
205
|
+
}
|
|
206
|
+
if (sharedNames.length) {
|
|
207
|
+
lines.push(`Names reported by two devices: ${sharedNames.map((n) => player(n)).join(', ')}.`);
|
|
208
|
+
}
|
|
209
|
+
return lines;
|
|
139
210
|
}
|
package/dist/sensors/rules.js
CHANGED
|
@@ -1,7 +1,52 @@
|
|
|
1
1
|
import { person, player as playerName } from '../diagnostics.js';
|
|
2
2
|
import { WIDE_ASPECT } from '../plex/media-facts.js';
|
|
3
|
-
export function contextOf(session, media, serverId) {
|
|
4
|
-
|
|
3
|
+
export function contextOf(session, media, serverId, extra = {}) {
|
|
4
|
+
const context = { serverId, playerId: session?.playerId, user: session?.user, media: session?.media ?? media };
|
|
5
|
+
if (extra.followedAs?.length) {
|
|
6
|
+
context.followedAs = extra.followedAs;
|
|
7
|
+
}
|
|
8
|
+
const name = extra.playerName ?? session?.playerTitle;
|
|
9
|
+
if (name !== undefined) {
|
|
10
|
+
context.playerName = name;
|
|
11
|
+
}
|
|
12
|
+
const home = extra.home ?? isHome(session);
|
|
13
|
+
if (home !== undefined) {
|
|
14
|
+
context.home = home;
|
|
15
|
+
}
|
|
16
|
+
return context;
|
|
17
|
+
}
|
|
18
|
+
const PRIVATE = /^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|127\.|fd|fc|fe80:|::1$)/i;
|
|
19
|
+
/** A private (LAN or VPN) address: a player outside the home reaches the server from a public one. */
|
|
20
|
+
export function isPrivateAddress(address) {
|
|
21
|
+
return address !== undefined && PRIVATE.test(address.replace(/^::ffff:/i, ''));
|
|
22
|
+
}
|
|
23
|
+
/** Home if Plex calls the player local or its address is private; undefined when neither is known. */
|
|
24
|
+
export function isHome(player) {
|
|
25
|
+
if (player?.local === true || isPrivateAddress(player?.address)) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return player?.local === false ? false : undefined;
|
|
29
|
+
}
|
|
30
|
+
const sameName = (a, b) => a !== undefined && b !== undefined && a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
31
|
+
function hasApp(ids, context) {
|
|
32
|
+
return (context.playerId !== undefined && ids.includes(context.playerId)) || (context.followedAs ?? []).some((id) => ids.includes(id));
|
|
33
|
+
}
|
|
34
|
+
/** Whether a playback is on a device: one of its apps, or any app reporting its name on the home network. */
|
|
35
|
+
export function onDevice(device, context) {
|
|
36
|
+
if (device.apps.length > 0) {
|
|
37
|
+
return hasApp(device.apps, context);
|
|
38
|
+
}
|
|
39
|
+
return device.match !== undefined && sameName(device.match, context.playerName) && context.home === true;
|
|
40
|
+
}
|
|
41
|
+
function playerAllowed(filters, context) {
|
|
42
|
+
if (context.playerId !== undefined && (filters.exceptPlayers ?? []).includes(context.playerId)) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const devices = filters.devices ?? [];
|
|
46
|
+
if (filters.players.length === 0 && devices.length === 0) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return hasApp(filters.players, context) || devices.some((device) => onDevice(device, context));
|
|
5
50
|
}
|
|
6
51
|
/** An empty filter allows everything; a non-empty one must contain the value. */
|
|
7
52
|
export function matchesFilters(filters, context) {
|
|
@@ -9,7 +54,7 @@ export function matchesFilters(filters, context) {
|
|
|
9
54
|
const section = context.media?.librarySectionId;
|
|
10
55
|
const library = filters.libraries.length === 0 || (section !== undefined
|
|
11
56
|
&& (filters.libraries.includes(section) || (context.serverId !== undefined && filters.libraries.includes(`${context.serverId}/${section}`))));
|
|
12
|
-
return
|
|
57
|
+
return playerAllowed(filters, context) && allows(filters.users, context.user) && library;
|
|
13
58
|
}
|
|
14
59
|
export function stateHolds(condition, snapshot, except = []) {
|
|
15
60
|
const excluded = (except.includes('credits') && snapshot.inCredits) || (except.includes('intro') && snapshot.inIntro);
|
|
@@ -75,13 +120,23 @@ export function mediaMatches(conditions, facts) {
|
|
|
75
120
|
}
|
|
76
121
|
/** Why a filter leaves out this playback, or undefined when it allows it. For detailed logging. */
|
|
77
122
|
export function filterMismatch(filters, context, names = {}) {
|
|
78
|
-
if (
|
|
79
|
-
return `player ${playerName(names.player ?? context.playerId ?? '
|
|
123
|
+
if (context.playerId !== undefined && (filters.exceptPlayers ?? []).includes(context.playerId)) {
|
|
124
|
+
return `player ${playerName(names.player ?? context.playerId)}${names.player ? ` (${names.product ?? context.playerId.slice(0, 8)})` : ''} is left out of its device`;
|
|
125
|
+
}
|
|
126
|
+
if (!playerAllowed(filters, context)) {
|
|
127
|
+
const devices = filters.devices ?? [];
|
|
128
|
+
const named = devices.find((d) => d.apps.length === 0 && sameName(d.match, context.playerName));
|
|
129
|
+
if (named && context.home !== true) {
|
|
130
|
+
return `player ${playerName(names.player ?? context.playerId ?? 'unknown')} is not on the home network (${context.home === false ? 'Plex says it is remote' : 'its address is unknown'})`;
|
|
131
|
+
}
|
|
132
|
+
// The app or ID tells apart players with the same name, such as two apps on one Apple TV.
|
|
133
|
+
const tell = names.product ?? context.playerId?.slice(0, 8);
|
|
134
|
+
return `player ${playerName(names.player ?? context.playerId ?? 'unknown')}${names.player && tell ? ` (${tell})` : ''} is not chosen`;
|
|
80
135
|
}
|
|
81
136
|
if (filters.users.length > 0 && (context.user === undefined || !filters.users.includes(context.user))) {
|
|
82
137
|
return `${context.user === undefined ? 'the person is unknown' : `${person(context.user)} is not chosen`}`;
|
|
83
138
|
}
|
|
84
|
-
if (!matchesFilters({ ...filters, players: [], users: [] }, context)) {
|
|
139
|
+
if (!matchesFilters({ ...filters, players: [], devices: [], exceptPlayers: [], users: [] }, context)) {
|
|
85
140
|
return `library ${names.library ?? context.media?.librarySectionId ?? 'unknown'} is not chosen`;
|
|
86
141
|
}
|
|
87
142
|
return undefined;
|
package/dist/setup-service.js
CHANGED
|
@@ -8,6 +8,7 @@ import { PlexHttpError, PlexServer } from './plex/server.js';
|
|
|
8
8
|
import { bridgePairing } from './child-bridge.js';
|
|
9
9
|
import { describe } from './errors.js';
|
|
10
10
|
import { lanAddress } from './network.js';
|
|
11
|
+
import { parseRecords } from './players.js';
|
|
11
12
|
import { configFingerprint } from './runtime.js';
|
|
12
13
|
import { serverToken } from './storage.js';
|
|
13
14
|
const SIGN_IN_TIMEOUT_MS = 10 * 60 * 1000;
|
|
@@ -221,7 +222,21 @@ export class SetupService {
|
|
|
221
222
|
}
|
|
222
223
|
const credentials = await this.credentials();
|
|
223
224
|
const targets = await Promise.all(servers.map(async (ref) => ({ id: ref.id, name: ref.name, server: await this.serverFor(ref, credentials) })));
|
|
224
|
-
|
|
225
|
+
const discovery = await discover(targets, await this.plexTv(), credentials.accountToken);
|
|
226
|
+
// Plex lists some apps without their name; Intermission learned it when they played.
|
|
227
|
+
const { seen } = await this.players();
|
|
228
|
+
for (const player of discovery.players) {
|
|
229
|
+
const known = seen[player.id];
|
|
230
|
+
if (known) {
|
|
231
|
+
player.product ??= known.product;
|
|
232
|
+
player.lastUsedAt = Math.max(player.lastUsedAt ?? 0, Date.parse(known.lastPlayedAt)) || player.lastUsedAt;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return discovery;
|
|
236
|
+
}
|
|
237
|
+
/** The players Intermission has seen play, and new player IDs it follows. */
|
|
238
|
+
async players() {
|
|
239
|
+
return parseRecords(await this.options.storage.readPlayers().catch(() => undefined));
|
|
225
240
|
}
|
|
226
241
|
/** The address Plex should post webhooks to: an IPv4 address on the same network as the server. */
|
|
227
242
|
async webhookUrl(options) {
|
package/dist/storage.js
CHANGED
|
@@ -10,6 +10,7 @@ const FILES = {
|
|
|
10
10
|
state: 'state.json',
|
|
11
11
|
plexPass: 'plex-pass.json',
|
|
12
12
|
running: 'running.json',
|
|
13
|
+
players: 'players.json',
|
|
13
14
|
};
|
|
14
15
|
/**
|
|
15
16
|
* Files in `<Homebridge storage>/intermission`. The folder is owner-only (0700) and every file is
|
|
@@ -79,6 +80,13 @@ export class Storage {
|
|
|
79
80
|
? { fingerprint: value.fingerprint, startedAt: value.startedAt }
|
|
80
81
|
: undefined;
|
|
81
82
|
}
|
|
83
|
+
/** The players Intermission has seen play, and new IDs it follows; see PlayerBook. */
|
|
84
|
+
readPlayers() {
|
|
85
|
+
return this.readJson(FILES.players);
|
|
86
|
+
}
|
|
87
|
+
writePlayers(value) {
|
|
88
|
+
return this.writeJson(FILES.players, value);
|
|
89
|
+
}
|
|
82
90
|
/** Removes the sign-in and Plex Pass history. Keeps the client ID and webhook key. */
|
|
83
91
|
async reset() {
|
|
84
92
|
await rm(this.path(FILES.credentials), { force: true });
|
|
@@ -76,6 +76,12 @@
|
|
|
76
76
|
/* Surfaces */
|
|
77
77
|
.im .im-card { border: 1px solid var(--im-border); border-radius: var(--im-radius); padding: 0.9rem 1rem; margin-bottom: 0.75rem; }
|
|
78
78
|
.im .im-banner { border-left: 4px solid var(--im-accent); background: var(--im-accent-soft); }
|
|
79
|
+
.im .im-pick .im-id { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.75rem; opacity: 0.8; }
|
|
80
|
+
.im .im-device { padding: 0.6rem 0.8rem; }
|
|
81
|
+
.im .im-device .im-rows { margin-top: 0.3rem; }
|
|
82
|
+
.im .im-subchips { display: flex; flex-wrap: wrap; gap: 0.3rem; align-items: center; margin: 0.4rem 0 0 0.2rem; }
|
|
83
|
+
.im .im-chip-sm { font-size: 0.8rem; padding: 0.1rem 0.55rem; }
|
|
84
|
+
.im .im-newplayer { border-top: 1px solid var(--im-border); margin-top: 0.6rem; padding-top: 0.2rem; }
|
|
79
85
|
.im .im-alert { border: 1px solid var(--im-accent-line); background: var(--im-accent-soft); border-radius: 10px; padding: 0.6rem 0.9rem; margin-bottom: 0.75rem; }
|
|
80
86
|
.im .im-alert-danger { border-color: var(--im-danger); background: transparent; color: var(--im-danger); }
|
|
81
87
|
|
|
@@ -162,6 +168,7 @@
|
|
|
162
168
|
.im .im-field small { display: block; margin-top: 0.1rem; }
|
|
163
169
|
.im .im-field:last-child { border-bottom: 0; }
|
|
164
170
|
.im .im-report { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.8rem; white-space: pre; }
|
|
171
|
+
.im .im-live-log { height: 24rem; overflow: auto; }
|
|
165
172
|
|
|
166
173
|
.im .im-recipe ol { padding-left: 1.2rem; }
|
|
167
174
|
.im .im-recipe li { margin-bottom: 0.3rem; }
|