Repository navigation
Expand file tree
/
Copy pathruntime.ts
More file actions
57 lines (50 loc) · 2.18 KB
/
Copy pathruntime.ts
File metadata and controls
57 lines (50 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { createTvRuntime, type TvConnection, type TvRuntimeHost } from '@agent-device/tv-cdp';
import { TIZEN_REMOTE_KEYS } from './keys.ts';
import { PROVIDER, type TizenOptions } from './provider.ts';
import { createSdb, sdbSerial } from './sdb.ts';
import { locateSdb, type HostEnv } from './tools.ts';
type TizenRuntimeHost = TvRuntimeHost & Readonly<{ env: HostEnv }>;
async function readySerials(sdb: ReturnType<typeof createSdb>) {
return (await sdb.listDevices())
.filter((device) => device.state === 'device')
.map((device) => device.serial);
}
export function createTizenRuntime(options: TizenOptions, host: TizenRuntimeHost) {
const sdb = createSdb(() => locateSdb(options.sdbPath, host.env), host.env);
async function resolveSerial(requested: string | undefined) {
const connected = await readySerials(sdb);
if (!requested) {
if (connected.length === 1) return connected[0]!;
throw host.createError(
'DEVICE_NOT_FOUND',
connected.length === 0
? 'No Tizen TV is connected to sdb'
: 'More than one Tizen TV is connected to sdb',
{ available: connected, hint: 'Pass --device <TV IP or sdb serial>.' },
);
}
const serial = sdbSerial(requested);
if (connected.includes(serial)) return serial;
await sdb.connect(serial);
if ((await readySerials(sdb)).includes(serial)) return serial;
throw host.createError('DEVICE_NOT_FOUND', `sdb could not connect to the Tizen TV ${serial}`, {
available: connected,
hint: 'Check that the TV is on and in Developer Mode with this host IP set as the developer IP.',
});
}
async function connectTv(flagDevice: string | undefined): Promise<TvConnection> {
const requested = flagDevice ?? options.device;
const tv = sdb.forDevice(await resolveSerial(requested));
return {
// The daemon selects the leased device by the name --device gave, so the TV keeps it.
name: requested ?? tv.serial,
launch: async (appId) => await tv.launch(appId),
close: tv.kill,
listApps: tv.listApps,
};
}
return createTvRuntime(
{ provider: PROVIDER, label: 'Tizen', remoteKeys: TIZEN_REMOTE_KEYS, connectTv },
host,
);
}