diff --git a/src/HaritoraX.ts b/src/HaritoraX.ts index 5becc1b..6b4a62c 100644 --- a/src/HaritoraX.ts +++ b/src/HaritoraX.ts @@ -291,19 +291,47 @@ export default class HaritoraX extends EventEmitter { * Starts the connection to the trackers with the specified mode. * * @param {string} connectionMode - Connect to the trackers with the specified mode (COM or bluetooth). - * @param {string[]} [portNames] - The port names to connect to. (COM only) + * @param {string[]} portNames - The port names to connect to. (COM only) + * @param {number} heartbeatInterval - The interval to send the heartbeat signal to the trackers. (COM only) * * @example * device.startConnection("COM"); **/ - startConnection(connectionMode: string, autoFind: boolean = false, portNames?: string[], heartbeatInterval: number = 10000) { + async startConnection(connectionMode: string, portNames?: string[], heartbeatInterval: number = 10000) { if (!isConnectionModeSupported(connectionMode)) error(`${connectionMode} connection not supported for ${trackerModelEnabled}`, true); if (connectionMode === "com") { com = new COM(trackerModelEnabled, heartbeatInterval); comEnabled = true; - com.startConnection(autoFind, portNames); + + if (!portNames) { + log("No port names provided, attempting to get device ports..."); + if (trackerModelEnabled === "wired") { + log("Wired model enabled, getting their ports..."); + //const HaritoraPorts = await com.getDevicePorts("Haritora"); + const HaritoraX10Ports = await com.getDevicePorts("HaritoraX 1.0"); + const HaritoraX11Ports = await com.getDevicePorts("HaritoraX 1.1"); + const HaritoraX11bPorts = await com.getDevicePorts("HaritoraX 1.1b"); + + //log(`Got Haritora ports: ${HaritoraPorts}`); + log(`Got HaritoraX 1.0 ports: ${HaritoraX10Ports}`); + log(`Got HaritoraX 1.1 ports: ${HaritoraX11Ports}`); + log(`Got HaritoraX 1.1b ports: ${HaritoraX11bPorts}`); + //portNames = HaritoraPorts.concat(HaritoraX10Ports, HaritoraX11Ports, HaritoraX11bPorts); + portNames = HaritoraX10Ports.concat(HaritoraX11Ports, HaritoraX11bPorts); + } else if (trackerModelEnabled === "wireless") { + log("Wireless model enabled, getting their dongles' ports..."); + const GX6Ports = await com.getDevicePorts("GX6"); + const GX2Ports = await com.getDevicePorts("GX2"); + + log(`Got GX6 ports: ${GX6Ports}`); + log(`Got GX2 ports: ${GX2Ports}`); + portNames = GX6Ports.concat(GX2Ports); + } + } + + com.startConnection(portNames); canProcessComData = true; } else if (connectionMode === "bluetooth") { bluetooth = new Bluetooth(); diff --git a/src/mode/com.ts b/src/mode/com.ts index 58e0f96..311c87e 100644 --- a/src/mode/com.ts +++ b/src/mode/com.ts @@ -39,6 +39,15 @@ const deviceInformation: Map = new Map([ ["rightElbow", ["", "", "", "", ""]], ]); +const devices = [ + { name: "GX2", vid: "1915", pid: "520F" }, + { name: "GX6", vid: "04DA", pid: "3F18" }, + { name: "HaritoraX 1.1b", vid: "", pid: "" }, + { name: "HaritoraX 1.1", vid: "", pid: "" }, + { name: "HaritoraX 1.0", vid: "", pid: "" }, + // { name: "Haritora", vid: "", pid: "" }, - these were before Shiftall (and users had to build it themselves), so they may not have the same VID/PID +]; + // Stores the ports that are currently active as objects for access later let activePorts: ActivePorts = {}; let trackersAssigned = false; @@ -54,22 +63,32 @@ export default class COM extends EventEmitter { log(`Initialized COM module with settings: ${trackerModelEnabled} ${heartbeatInterval}`); } - async startConnection(autoFind: boolean, portNames: string[]) { - const devices = [ - { name: "GX2", vid: "1915", pid: "520F" }, - { name: "GX6", vid: "04DA", pid: "3F18" }, - ]; + async getDevicePorts(device: string) { + const ports = await Binding.list(); + const availablePorts = ports + .map(port => { + const deviceMatch = devices.find(deviceItem => port.vendorId === deviceItem.vid && port.productId === deviceItem.pid); + return { + ...port, + deviceName: deviceMatch ? deviceMatch.name : undefined, + }; + }) + .filter(port_1 => port_1.deviceName !== undefined); + let foundPorts = []; + for (const port_2 of availablePorts) { + if (port_2.deviceName === device) foundPorts.push(port_2.path); + } + return foundPorts; + } - const initializeSerialPort = (port: string, deviceName: string) => { + startConnection(portNames: string[]) { + const initializeSerialPort = (port: string) => { try { const serial = new SerialPortStream({ path: port, baudRate: BAUD_RATE, binding: Binding }); const parser = serial.pipe(new ReadlineParser({ delimiter: "\n" })); activePorts[port] = serial; - serial.on("open", () => { - this.emit("connected", port); - log(`Connected to COM port for ${deviceName}: ${port}`); - }); + serial.on("open", () => this.emit("connected", port)); parser.on("data", (data) => processData(data, port)); serial.on("close", () => this.emit("disconnected", port)); serial.on("error", (err) => { @@ -82,28 +101,9 @@ export default class COM extends EventEmitter { } }; - if (autoFind) { - const ports = await Binding.list(); - const availablePorts = ports - .map((port) => { - const device = devices.find((device) => port.vendorId === device.vid && port.productId === device.pid); - return { - ...port, - deviceName: device ? device.name : undefined, - }; - }) - .filter((port) => port.deviceName !== undefined); - - for (const port of availablePorts) { - log(`Found COM port for ${port.deviceName}: ${port.path}`); - initializeSerialPort(port.path, port.deviceName); - } - } else { - for (const port of portNames) { - // For manual selection, device name is not logged since VID/PID matching is not performed - log(`Opening COM port: ${port}`); - initializeSerialPort(port, "Unknown Device"); - } + for (const port of portNames) { + log(`Opening COM port: ${port}`); + initializeSerialPort(port); } } diff --git a/test/test.js b/test/test.js index f7a2904..4cdfb0d 100644 --- a/test/test.js +++ b/test/test.js @@ -19,7 +19,7 @@ if (mode === "bt" || mode === "bluetooth") { } }, 3000);*/ } else { - device.startConnection("com", true); + device.startConnection("com"); device.on("connect", (trackerName) => { console.log(`Connected to tracker ${trackerName}`);