Skip to content

Commit

Permalink
Fix freezing w/ multiple isDeviceAvailable() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
JovannMC committed Jul 22, 2024
1 parent e796f73 commit a594093
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 56 deletions.
66 changes: 41 additions & 25 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,8 @@ export default class HaritoraX extends EventEmitter {
async getAvailableDevices(): Promise<string[]> {
let availableDevices: string[] = [];

const com = new COM("wireless", 100000);
const bluetooth = new Bluetooth();
let com = new COM("wireless");
let bluetooth = new Bluetooth();

if (await com.isDeviceAvailable()) {
const devices = await com.getAvailableDevices();
Expand All @@ -792,6 +792,12 @@ export default class HaritoraX extends EventEmitter {
availableDevices.push("HaritoraX Wireless");
}

com.removeAllListeners();
bluetooth.removeAllListeners();

com = null;
bluetooth = null;

return availableDevices;
}

Expand All @@ -803,16 +809,20 @@ export default class HaritoraX extends EventEmitter {
* @returns {string[]} The available ports for the specified device.
*/
async getDevicePorts(device: string): Promise<string[]> {
const com = new COM("wireless", 100000);

if (device === "HaritoraX Wired") {
return (
(await com.getDevicePorts("HaritoraX 1.0")) ||
(await com.getDevicePorts("HaritoraX 1.1")) ||
(await com.getDevicePorts("HaritoraX 1.1b"))
);
} else {
return await com.getDevicePorts(device);
let com = new COM("wireless");
try {
if (device === "HaritoraX Wired") {
return (
(await com.getDevicePorts("HaritoraX 1.0")) ||
(await com.getDevicePorts("HaritoraX 1.1")) ||
(await com.getDevicePorts("HaritoraX 1.1b"))
);
} else {
return await com.getDevicePorts(device);
}
} finally {
com.removeAllListeners();
com = null;
}
}
}
Expand Down Expand Up @@ -1652,19 +1662,25 @@ async function removeActiveDevices(deviceTypeToRemove: string) {

function getTrackerSettingsFromMap(trackerName: string) {
const settings = trackerSettings.get(trackerName);
const settingsToLog = {
"Sensor mode": settings[0],
"FPS mode": settings[1],
"Sensor auto correction": settings[2],
"Ankle motion detection": settings[3],
};
logSettings(trackerName, settingsToLog);
return {
sensorMode: settings[0],
fpsMode: settings[1],
sensorAutoCorrection: settings[2],
ankleMotionDetection: settings[3],
};
if (settings) {
const settingsToLog = {
"Sensor mode": settings[0],
"FPS mode": settings[1],
"Sensor auto correction": settings[2],
"Ankle motion detection": settings[3],
};
logSettings(trackerName, settingsToLog);

return {
sensorMode: settings[0],
fpsMode: settings[1],
sensorAutoCorrection: settings[2],
ankleMotionDetection: settings[3],
};
} else {
error(`Tracker ${trackerName} settings not found in trackerSettings map.`);
return null;
}
}

/*
Expand Down
47 changes: 17 additions & 30 deletions src/mode/bluetooth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

import noble, { Peripheral, Service, Characteristic } from "@abandonware/noble";
import noble, { Peripheral, Service, Characteristic, _state } from "@abandonware/noble";
import { EventEmitter } from "events";

let main: Bluetooth = undefined;
Expand Down Expand Up @@ -64,40 +64,27 @@ export default class Bluetooth extends EventEmitter {
async isDeviceAvailable() {
return new Promise<Boolean>((resolve) => {
let found = false;
const discoverListener = (peripheral: Peripheral) => {

noble.on("discover", (peripheral) => {
if (peripheral.advertisement.localName && peripheral.advertisement.localName.startsWith("HaritoraXW-")) {
found = true;
noble.stopScanning();
cleanupListeners();
resolve(true);
}
};

const stateChangeListener = (state: string) => {
if (state === "poweredOn" && !found) {
noble.startScanning([], true);

setTimeout(() => {
if (!found) {
noble.stopScanning();
cleanupListeners();
resolve(false);
}
}, 3000);
} else {
cleanupListeners();
resolve(false);
}
};

const cleanupListeners = () => {
noble.removeListener("discover", discoverListener);
noble.removeListener("stateChange", stateChangeListener);
};

noble.on("discover", discoverListener);
noble.on("stateChange", stateChangeListener);
});

if (noble._state === "poweredOn" && !found) {
noble.startScanning([], true);

setTimeout(() => {
if (!found) {
noble.stopScanning();
resolve(false);
}
}, 3000);
} else if (noble._state !== "poweredOn") {
resolve(false);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/mode/com.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let trackerModelEnabled: String;
let heartbeatInterval: number; // in milliseconds

export default class COM extends EventEmitter {
constructor(trackerModel: string, heartbeat: number) {
constructor(trackerModel: string, heartbeat?: number) {
super();
this.setMaxListeners(1); // Prevent memory leaks
main = this;
Expand Down

0 comments on commit a594093

Please sign in to comment.