@cldmv/node-android-tv-remote 1.0.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.
@@ -0,0 +1,102 @@
1
+ const adb = require("adbkit");
2
+
3
+ /**
4
+ * Test script to connect to an ADB device and read settings.
5
+ * Usage: node test/connect-readall.js <ip> [port]
6
+ */
7
+
8
+ /**
9
+ * Get or set Android settings via ADB.
10
+ * @param {string} ip
11
+ * @param {number} port
12
+ * @param {string} mode - 'get' or 'set'
13
+ * @returns {Promise<void>}
14
+ */
15
+
16
+ function logWithTime(...args) {
17
+ const now = new Date().toISOString();
18
+ console.log(`[${now}]`, ...args);
19
+ }
20
+ function warnWithTime(...args) {
21
+ const now = new Date().toISOString();
22
+ console.warn(`[${now}]`, ...args);
23
+ }
24
+ function errorWithTime(...args) {
25
+ const now = new Date().toISOString();
26
+ console.error(`[${now}]`, ...args);
27
+ }
28
+
29
+ function handleSettings(ip, port, mode, quiet = true) {
30
+ const client = adb.createClient();
31
+ const host = `${ip}:${port}`;
32
+ const keys = [
33
+ { ns: "system", key: "screen_off_timeout", value: 2147483647 },
34
+ { ns: "secure", key: "sleep_timeout", value: 0 },
35
+ { ns: "global", key: "stay_on_while_plugged_in", value: 3 }
36
+ ];
37
+
38
+ return client
39
+ .connect(ip, port)
40
+ .then(() => {
41
+ if (!quiet) logWithTime(`Connected to ${host}`);
42
+ let chain = Promise.resolve();
43
+ keys.forEach((item) => {
44
+ chain = chain.then(() => {
45
+ let cmd;
46
+ if (mode === "set") {
47
+ cmd = `settings put ${item.ns} ${item.key} ${item.value}`;
48
+ } else {
49
+ cmd = `settings get ${item.ns} ${item.key}`;
50
+ }
51
+ return client
52
+ .shell(host, cmd)
53
+ .then(adb.util.readAll)
54
+ .then((result) => {
55
+ if (!quiet) {
56
+ if (mode === "set") {
57
+ logWithTime(`Set ${item.ns} ${item.key} to ${item.value}`);
58
+ } else {
59
+ logWithTime(`${item.ns} ${item.key}: ${result.toString().trim()}`);
60
+ }
61
+ }
62
+ });
63
+ });
64
+ });
65
+ return chain;
66
+ })
67
+ .then(() => client.disconnect(ip, port))
68
+ .then(() => {
69
+ if (!quiet) logWithTime("Disconnected cleanly");
70
+ })
71
+ .catch((err) => {
72
+ if (err.message && err.message.includes("disconnected")) {
73
+ if (!quiet) warnWithTime("Warning: Device already disconnected before explicit disconnect call.");
74
+ } else {
75
+ errorWithTime("Error:", err.message || err);
76
+ if (err.message && err.message.includes("device unauthorized")) {
77
+ errorWithTime(
78
+ "Your device is unauthorized. Please check your TV and accept the authorization dialog to allow this computer to connect via ADB."
79
+ );
80
+ }
81
+ process.exit(1);
82
+ }
83
+ });
84
+ }
85
+
86
+ // CLI usage
87
+
88
+ const ip = process.argv[2];
89
+ const port = process.argv[3] ? parseInt(process.argv[3], 10) : 5555;
90
+ const mode = process.argv[4] === "set" ? "set" : "get";
91
+ const quiet = process.argv.includes("--quiet");
92
+
93
+ if (!ip) {
94
+ errorWithTime("Usage: node test/connect-readall.js <ip> [port] [get|set] [--quiet]");
95
+ process.exit(1);
96
+ }
97
+
98
+ if (!quiet) logWithTime(`Running in ${mode} mode on ${ip}:${port}`);
99
+
100
+ handleSettings(ip, port, mode, quiet);
101
+
102
+ if (!quiet) logWithTime(`Ran in ${mode} mode on ${ip}:${port}`);