Skip to content

Commit

Permalink
Add autofind for GX6 and GX2
Browse files Browse the repository at this point in the history
find the com ports automatically for the dongles if wanted, no need to specify manually!

need to see if this can be applied to haritorax wired too (gonna ask someone for data)
  • Loading branch information
JovannMC committed Jul 21, 2024
1 parent c80c971 commit c34cf1f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ export default class HaritoraX extends EventEmitter {
* @example
* device.startConnection("COM");
**/
startConnection(connectionMode: string, portNames?: string[], heartbeatInterval: number = 10000) {
startConnection(connectionMode: string, autoFind: boolean = false, 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(portNames);
com.startConnection(autoFind, portNames);
canProcessComData = true;
} else if (connectionMode === "bluetooth") {
bluetooth = new Bluetooth();
Expand Down
45 changes: 36 additions & 9 deletions src/mode/com.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { EventEmitter } from "events";
const Binding = autoDetect();

let main: COM = undefined;
let heartbeatInterval: number; // in milliseconds

const BAUD_RATE = 500000; // from the haritora_setting.json in the HaritoraConfigurator

Expand Down Expand Up @@ -44,24 +43,33 @@ const deviceInformation: Map<string, string[]> = new Map([
let activePorts: ActivePorts = {};
let trackersAssigned = false;
let trackerModelEnabled: String;
let heartbeatInterval: number; // in milliseconds

export default class COM extends EventEmitter {
constructor(trackerModel: string, heartbeat = 10000) {
constructor(trackerModel: string, heartbeat: number) {
super();
heartbeatInterval = heartbeat;
main = this;
trackerModelEnabled = trackerModel;
heartbeatInterval = heartbeat;
log(`Initialized COM module with settings: ${trackerModelEnabled} ${heartbeatInterval}`);
}

startConnection(portNames: string[]) {
const initializeSerialPort = (port: string) => {
async startConnection(autoFind: boolean, portNames: string[]) {
const devices = [
{ name: "GX2", vid: "1915", pid: "520F" },
{ name: "GX6", vid: "04DA", pid: "3F18" },
];

const initializeSerialPort = (port: string, deviceName: 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));
serial.on("open", () => {
this.emit("connected", port);
log(`Connected to COM port for ${deviceName}: ${port}`);
});
parser.on("data", (data) => processData(data, port));
serial.on("close", () => this.emit("disconnected", port));
serial.on("error", (err) => {
Expand All @@ -74,9 +82,28 @@ export default class COM extends EventEmitter {
}
};

for (const port of portNames) {
log(`Opening COM port: ${port}`);
initializeSerialPort(port);
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");
}
}
}

Expand Down
13 changes: 2 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,18 @@ if (mode === "bt" || mode === "bluetooth") {

setInterval(async () => {
console.log("Active trackers for BT:", device.getActiveTrackers());
console.log("Info: ", await device.getDeviceInfo("HaritoraXW-(SERIAL)"));
console.log("Mag: ", await device.getTrackerMag("HaritoraXW-(SERIAL)"));
console.log("Battery: ", await device.getBatteryInfo("HaritoraXW-(SERIAL)"));
}, 5000);

/*setInterval(async () => {
try {
console.log("Active trackers for BT:", device.getActiveTrackers());
console.log("Device info:", await device.getDeviceInfo("HaritoraXW-(SERIAL)"));
console.log("Device battery:", await device.getBatteryInfo("HaritoraXW-(SERIAL)"));
console.log("Device settings:", await device.getTrackerSettings("HaritoraXW-(SERIAL)"));
} catch (error) {
console.error("Error getting device data:", error);
}
}, 3000);*/
} else {
device.startConnection("com", ["COM3", "COM4", "COM5"]);
device.startConnection("com", true);

device.on("connect", (trackerName) => {
console.log(`Connected to tracker ${trackerName}`);
Expand All @@ -34,9 +29,6 @@ if (mode === "bt" || mode === "bluetooth") {
setInterval(async () => {
try {
console.log("Active trackers for COM:", device.getActiveTrackers());
console.log("Device info:", await device.getDeviceInfo("rightAnkle"));
console.log("Device battery:", await device.getBatteryInfo("rightAnkle"));
console.log("Device magnetometer:", await device.getTrackerMag("rightAnkle"));
} catch (error) {
console.error("Error getting device data:", error);
}
Expand All @@ -47,12 +39,11 @@ if (mode === "bt" || mode === "bluetooth") {
console.log(`Tracker settings map:`, device.getTrackerSettings("rightAnkle"));
console.log(`Tracker raw hex settings map:`, device.getTrackerSettingsRaw("rightAnkle"));
console.log(`Tracker buttons map:`, device.getTrackerButtons("rightAnkle"));
console.log(`Tracker battery map:`, device.getBatteryInfo("rightAnkle"));
}, 5000);

/*setTimeout(() => {
console.log("Stopping connection");
device.stopConnection("gx");
device.stopConnection("com");
}, 5000);
setTimeout(() => {
Expand Down

0 comments on commit c34cf1f

Please sign in to comment.