Skip to content

Commit 5ad4ef3

Browse files
committed
Restructure Control Hub module to export interface and implement with internal class
1 parent b4de2a4 commit 5ad4ef3

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ExpansionHub, ParentRevHub } from "@rev-robotics/expansion-hub";
2+
3+
export interface ParentControlHub extends ParentRevHub, ControlHub {
4+
readonly serialNumber: string;
5+
}
6+
7+
export interface ControlHub extends ExpansionHub {}

packages/control-hub/src/discovery.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { ControlHub } from "./internal/ControlHub.js";
1+
import { ControlHubInternal } from "./internal/ControlHub.js";
22
import { Adb, DeviceClient } from "@u4/adbkit";
33
import getPort from "get-port";
4+
import { ControlHub } from "./ControlHub.js";
45

56
export async function openConnectedControlHub(): Promise<ControlHub | undefined> {
67
try {
@@ -20,8 +21,9 @@ export async function openUsbControlHubs(): Promise<ControlHub[]> {
2021
let isHub = await isControlHub(deviceClient);
2122
if (isHub) {
2223
let port = await configureHubTcp(deviceClient);
24+
let serialNumber = device.id;
2325

24-
let hub = new ControlHub();
26+
let hub = new ControlHubInternal(serialNumber);
2527
await hub.open("127.0.0.1", (port + 1).toString());
2628
controlHubs.push(hub);
2729
}
@@ -47,7 +49,7 @@ async function isControlHub(deviceClient: DeviceClient): Promise<boolean> {
4749
}
4850

4951
async function createWiFiControlHub(): Promise<ControlHub> {
50-
let hub = new ControlHub();
52+
let hub = new ControlHubInternal();
5153

5254
if (!(await hub.isWiFiConnected())) {
5355
throw new Error("Hub is not connected via WiFi");

packages/control-hub/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { openConnectedControlHub, openUsbControlHubs } from "./discovery.js";
2+
export * from "./ControlHub.js";

packages/control-hub/src/internal/ControlHub.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,30 @@ import axios from "axios";
2323
import semver from "semver";
2424
import WebSocket from "isomorphic-ws";
2525
import adb from "@u4/adbkit";
26+
import { ControlHub, ParentControlHub } from "../ControlHub.js";
2627

27-
export class ControlHub implements ExpansionHub {
28+
export class ControlHubInternal implements ControlHub {
2829
readonly isOpen: boolean = true;
2930
moduleAddress: number = 0;
3031
responseTimeoutMs: number = 0;
3132
type: RevHubType = RevHubType.ControlHub;
3233
webSocketConnection!: WebSocket;
34+
private readonly serialNumber?: string;
3335

3436
keyGenerator = 0;
3537
currentActiveCommands = new Map<
3638
any,
3739
(response: any | undefined, error: any | undefined) => void
3840
>();
3941

42+
constructor(serialNumber?: string) {
43+
this.serialNumber = serialNumber;
44+
}
45+
46+
isParent(): this is ParentControlHub {
47+
return this.serialNumber !== undefined;
48+
}
49+
4050
async open(ip: string = "192.168.43.1", port: string = "8081"): Promise<void> {
4151
this.webSocketConnection = new WebSocket(`ws://${ip}:${port}`);
4252

@@ -209,10 +219,6 @@ export class ControlHub implements ExpansionHub {
209219
throw new Error("not implemented");
210220
}
211221

212-
isParent(): this is ParentRevHub {
213-
throw new Error("not implemented");
214-
}
215-
216222
on(eventName: "error", listener: (error: Error) => void): RevHub {
217223
throw new Error("not implemented");
218224
}

packages/sample/src/command/list.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { openConnectedControlHub, openUsbControlHubs } from "@rev-robotics/contr
55
export async function list() {
66
let usbControlHubs = await openUsbControlHubs();
77
for (const hub of usbControlHubs) {
8-
console.log(`USB Control Hub: ${hub.moduleAddress}\n\n`);
8+
if (hub.isParent()) {
9+
console.log(`USB Control Hub: ${hub.serialNumber} ${hub.moduleAddress}\n\n`);
10+
} else {
11+
console.log(`\tUSB Control Hub: ${hub.moduleAddress}\n\n`);
12+
}
913
hub.close();
1014
}
1115

0 commit comments

Comments
 (0)