@cldmv/node-android-tv-remote 1.4.0 → 1.5.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cldmv/node-android-tv-remote",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "description": "A Node.js library for controlling Android TV devices via ADB keycodes and text input.",
5
5
  "main": "src/lib/android-tv-remote.js",
6
6
  "scripts": {
@@ -266,5 +266,10 @@
266
266
  "systemNavigationUp": 280,
267
267
  "systemNavigationDown": 281,
268
268
  "systemNavigationLeft": 282,
269
- "systemNavigationRight": 283
269
+ "systemNavigationRight": 283,
270
+ "azmNetflix": 290,
271
+ "azmPrime": 291,
272
+ "azmDirecttv": 292,
273
+ "azmPeacock": 296,
274
+ "azmGuide": 297
270
275
  }
@@ -1,6 +1,8 @@
1
1
  [
2
2
  "home",
3
+ "wakeup",
3
4
  "back",
5
+ "recall",
4
6
  "menu",
5
7
  "ok",
6
8
  "select",
@@ -58,6 +60,7 @@
58
60
  "numpadAdd",
59
61
  "numpadDot",
60
62
  "numpadComma",
63
+ "enter",
61
64
  "numpadEnter",
62
65
  "numpadEquals",
63
66
  "numpadLeftParen",
@@ -74,5 +77,10 @@
74
77
  "mediaStepForward",
75
78
  "mediaStepBackward",
76
79
  "mediaAudioTrack",
77
- "mediaTopMenu"
80
+ "mediaTopMenu",
81
+ "azmNetflix",
82
+ "azmPrime",
83
+ "azmDirecttv",
84
+ "azmPeacock",
85
+ "azmGuide"
78
86
  ]
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  /**
2
3
  * Android TV Remote module.
3
4
  * @module android-tv-remote
@@ -137,36 +138,13 @@ const remoteKeys = require("../data/remote-keys.json");
137
138
  * @function
138
139
  * @public
139
140
  * @param {RemoteConfig} config - Configuration for the remote.
140
- * @param {boolean} [config.maintainConnection=true] - Whether to maintain the ADB connection with a heartbeat.
141
- * @param {number} [config.heartbeatInterval=30000] - Heartbeat interval in ms (default 30s).
142
141
  * @returns {Remote}
143
142
  */
144
143
  module.exports = function (config) {
145
- /**
146
- * On initialization, check if already connected to the device and set internal state.
147
- * This ensures the internal state is correct if the device is already connected.
148
- */
149
- (async () => {
150
- try {
151
- const status = await getConnectionStatus(true);
152
- if (status === "connected") {
153
- connected = true;
154
- if (!quiet) logWithTime("Already connected to " + host + " (on init)");
155
- startHeartbeat();
156
- }
157
- } catch (e) {
158
- // Ignore errors on init
159
- }
160
- })();
161
- /**
162
- * @type {RemoteConfig}
163
- */
164
- /**
165
- * Initializes and configures the Android TV Remote instance.
166
- * Handles connection, persistent heartbeat, and auto-reconnect logic.
167
- * @returns {Remote}
168
- */
169
- config = config || {};
144
+ // Ensure config is an object and has the required 'ip' property
145
+ if (!config || typeof config !== "object" || !config.ip) {
146
+ throw new Error("Missing required 'ip' property in RemoteConfig.");
147
+ }
170
148
  const ip = config.ip;
171
149
  const port = config.port || 5555;
172
150
  const inputDevice = config.inputDevice || "/dev/input/event0";
@@ -184,6 +162,23 @@ module.exports = function (config) {
184
162
  const heartbeatInterval = typeof config.heartbeatInterval === "number" ? config.heartbeatInterval : 30000;
185
163
  let heartbeatTimer = null;
186
164
 
165
+ /**
166
+ * On initialization, check if already connected to the device and set internal state.
167
+ * This ensures the internal state is correct if the device is already connected.
168
+ */
169
+ (async () => {
170
+ try {
171
+ const status = await getConnectionStatus(true);
172
+ if (status === "connected") {
173
+ connected = true;
174
+ if (!quiet) logWithTime("Already connected to " + host + " (on init)");
175
+ startHeartbeat();
176
+ }
177
+ } catch (e) {
178
+ // Ignore errors on init
179
+ }
180
+ })();
181
+
187
182
  /**
188
183
  * Resets the disconnect timer if autoDisconnect is enabled.
189
184
  * Disconnects after inactivity if enabled.
@@ -441,7 +436,33 @@ module.exports = function (config) {
441
436
  return !!connected;
442
437
  }
443
438
 
444
- return {
439
+ /**
440
+ * Returns a list of all available keyboard key function names on the live API (keyboard.key).
441
+ * This inspects the actual keyboard.key object, so it always matches the real API surface, including all aliases and dynamic keys.
442
+ * @returns {string[]}
443
+ * @example
444
+ * remote.getKeyboardKeys();
445
+ */
446
+ function getKeyboardKeys() {
447
+ // 'this' is not bound, so we must access the constructed keyboard.key object
448
+ // Only return the top-level key names (not .keycode or .shift)
449
+ if (!this || !this.keyboard || !this.keyboard.key) return [];
450
+ return Object.keys(this.keyboard.key).filter((k) => typeof this.keyboard.key[k] === "function");
451
+ }
452
+
453
+ /**
454
+ * Returns a list of all available press command names on the live API (press object), including aliases and dynamic keys.
455
+ * This inspects the actual press object, so it always matches the real API surface.
456
+ * @returns {string[]}
457
+ * @example
458
+ * remote.getPressCommands();
459
+ */
460
+ function getPressCommands() {
461
+ if (!this || !this.press) return [];
462
+ return Object.keys(this.press).filter((k) => k !== "long" && typeof this.press[k] === "function");
463
+ }
464
+
465
+ const remoteApi = {
445
466
  /**
446
467
  * Returns the current connection status as tracked by the module, or does a live check if requested.
447
468
  * @function
@@ -452,6 +473,24 @@ module.exports = function (config) {
452
473
  * await remote.getConnectionStatus(true); // live check
453
474
  */
454
475
  getConnectionStatus,
476
+ /**
477
+ * Returns a list of all available keyboard key function names on the live API (keyboard.key).
478
+ * @returns {string[]}
479
+ * @example
480
+ * remote.getKeyboardKeys();
481
+ */
482
+ getKeyboardKeys: function () {
483
+ return getKeyboardKeys.call(remoteApi);
484
+ },
485
+ /**
486
+ * Returns a list of all available press command names on the live API (press object), including aliases and dynamic keys.
487
+ * @returns {string[]}
488
+ * @example
489
+ * remote.getPressCommands();
490
+ */
491
+ getPressCommands: function () {
492
+ return getPressCommands.call(remoteApi);
493
+ },
455
494
  /**
456
495
  * Returns true if the module believes it is connected to the device.
457
496
  * @readonly
@@ -459,7 +498,7 @@ module.exports = function (config) {
459
498
  * @example
460
499
  * if (remote.isConnected) { ... }
461
500
  */
462
- isConnected,
501
+ isConnected: isConnected(),
463
502
  /**
464
503
  * Get or set Android settings via ADB.
465
504
  * @public
@@ -523,16 +562,26 @@ module.exports = function (config) {
523
562
  * @public
524
563
  * @param {function(Error=, any=)=} [cb]
525
564
  * @returns {Promise|undefined}
565
+ * @example
566
+ * await remote.connect();
567
+ * remote.connect((err) => { ... });
526
568
  */
527
- connect: connectWrapped,
569
+ connect: /**
570
+ * @type {(cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
571
+ */ (connectWrapped),
528
572
 
529
573
  /**
530
574
  * Disconnect from the device. Supports both promise and callback styles.
531
575
  * @public
532
576
  * @param {function(Error=, any=)=} [cb]
533
577
  * @returns {Promise|undefined}
578
+ * @example
579
+ * await remote.disconnect();
580
+ * remote.disconnect((err) => { ... });
534
581
  */
535
- disconnect: disconnectWrapped,
582
+ disconnect: /**
583
+ * @type {(cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
584
+ */ (disconnectWrapped),
536
585
 
537
586
  /**
538
587
  * Send a keycode. Supports both promise and callback styles.
@@ -540,8 +589,13 @@ module.exports = function (config) {
540
589
  * @param {number} code
541
590
  * @param {function(Error=, any=)=} [cb]
542
591
  * @returns {Promise|undefined}
592
+ * @example
593
+ * await remote.inputKeycode(23);
594
+ * remote.inputKeycode(23, (err) => { ... });
543
595
  */
544
- inputKeycode: inputKeycodeWrapped,
596
+ inputKeycode: /**
597
+ * @type {(code: number, cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
598
+ */ (inputKeycodeWrapped),
545
599
 
546
600
  /**
547
601
  * All remote key functions support both promise and callback styles.
@@ -761,4 +815,5 @@ module.exports = function (config) {
761
815
  )
762
816
  }
763
817
  };
818
+ return remoteApi;
764
819
  };
@@ -4,6 +4,7 @@ const Remote = require("../src/lib/android-tv-remote");
4
4
  const TEST_IP = process.env.ANDROID_TV_IP || "192.168.1.100";
5
5
  const TEST_PORT = process.env.ANDROID_TV_PORT ? parseInt(process.env.ANDROID_TV_PORT, 10) : 5555;
6
6
 
7
+ /** @type {import('../src/lib/android-tv-remote').RemoteConfig} */
7
8
  const config = {
8
9
  ip: TEST_IP,
9
10
  port: TEST_PORT,
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Test for getKeyboardKeys and getPressCommands methods of the android-tv-remote module.
3
+ *
4
+ * Usage: Run with `node test/api-surface.test.js`
5
+ */
6
+
7
+ const assert = require("assert");
8
+ const createRemote = require("../src/lib/android-tv-remote");
9
+
10
+ // Minimal config for instantiation (no real device needed for API surface test)
11
+ const remote = createRemote({ ip: "127.0.0.1", autoConnect: false, maintainConnection: false, quiet: true });
12
+
13
+ /**
14
+ * Test getKeyboardKeys returns all top-level function keys from keyboard.key
15
+ */
16
+ function testGetKeyboardKeys() {
17
+ const keys = remote.getKeyboardKeys();
18
+ console.log("getKeyboardKeys:", keys);
19
+ assert(Array.isArray(keys), "getKeyboardKeys should return an array");
20
+ assert(keys.length > 0, "getKeyboardKeys should return at least one key");
21
+ // Spot check for some expected keys
22
+ ["a", "enter", "space"].forEach((k) => {
23
+ assert(keys.includes(k), `getKeyboardKeys should include '${k}'`);
24
+ });
25
+ // Should not include 'shift' or 'keycode' (sub-objects)
26
+ assert(!keys.includes("shift"), "getKeyboardKeys should not include shift");
27
+ assert(!keys.includes("keycode"), "getKeyboardKeys should not include keycode");
28
+ }
29
+
30
+ /**
31
+ * Test getPressCommands returns all top-level function keys from press
32
+ */
33
+ function testGetPressCommands() {
34
+ const keys = remote.getPressCommands();
35
+ console.log("getPressCommands:", keys);
36
+ assert(Array.isArray(keys), "getPressCommands should return an array");
37
+ assert(keys.length > 0, "getPressCommands should return at least one key");
38
+ // Spot check for some expected keys (may vary by remote-keys.json)
39
+ ["home", "back", "ok", "up", "down", "left", "right"].forEach((k) => {
40
+ assert(keys.includes(k), `getPressCommands should include '${k}'`);
41
+ });
42
+ // Should not include 'long' (sub-object)
43
+ assert(!keys.includes("long"), "getPressCommands should not include long");
44
+ }
45
+
46
+ console.log("Testing getKeyboardKeys...");
47
+ testGetKeyboardKeys();
48
+ console.log("Testing getPressCommands...");
49
+ testGetPressCommands();
50
+ console.log("All API surface tests passed.");
@@ -16,27 +16,18 @@ async function main() {
16
16
 
17
17
  if (!quiet) console.log(`Running in ${mode} mode on ${ip}:${port}`);
18
18
 
19
- const setup = new AndroidTVSetup({ ip, port, quiet });
20
- try {
21
- /**
22
- * Check if already connected to the device. If not, connect.
23
- * @returns {Promise<void>}
24
- */
25
- const isConnected = typeof setup.isConnected === 'function' ? await setup.isConnected() : false;
26
- if (!isConnected) {
27
- await setup.connect();
28
- } else if (!quiet) {
29
- console.log(`Already connected to ${ip}:${port}`);
30
- }
31
- if (mode === "set") {
32
- await setup.setSettings();
33
- }
34
- await setup.ensureAwake();
35
- await setup.disconnect();
36
- } catch (err) {
37
- setup.errorWithTime("Error:", err.message || err);
38
- process.exit(1);
39
- }
19
+ const setup = new AndroidTVSetup({ ip, port, quiet });
20
+ try {
21
+ await setup.connect();
22
+ if (mode === "set") {
23
+ await setup.setSettings();
24
+ }
25
+ await setup.ensureAwake();
26
+ await setup.disconnect();
27
+ } catch (err) {
28
+ setup.errorWithTime("Error:", err.message || err);
29
+ process.exit(1);
30
+ }
40
31
 
41
32
  if (!quiet) console.log(`Ran in ${mode} mode on ${ip}:${port}`);
42
33
  }