Skip to content

Commit

Permalink
Improve connection error handling
Browse files Browse the repository at this point in the history
Add some form of error handling when starting connections and improve on some existing error handling
  • Loading branch information
JovannMC committed Apr 13, 2024
1 parent 7b1fbfb commit ca1277f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
46 changes: 34 additions & 12 deletions src/devices/haritorax-wireless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,29 @@ export default class HaritoraXWireless extends EventEmitter {
* Starts the connection to the trackers with the specified mode.
*
* @param {string} connectionMode - Connect to the trackers with the specified mode (GX6 or bluetooth).
* @param {array} portNames - The port names to connect to. (GX6 only)
* @param {string[]} [portNames] - The port names to connect to. (GX6 only)
* @returns {boolean} - Whether the connection was successfully started. (Bluetooth only)
*
* @example
* device.startConnection("gx");
**/
startConnection(connectionMode: string, portNames?: string[]) {
if (connectionMode === "gx") {
// gotta assume the user will actually provide valid ports, since I couldn't figure out how to return false if there's errors.
gx.startConnection(portNames);
gxEnabled = true;
return true;
} else if (connectionMode === "bluetooth") {
bluetooth.startConnection();
bluetoothEnabled = true;
try {
bluetooth.startConnection();
bluetoothEnabled = true;
} catch (err) {
console.error(`Error starting bluetooth connection: ${err}`);
return false;
}
return true;
}
return false;
}

/**
Expand All @@ -213,13 +223,25 @@ export default class HaritoraXWireless extends EventEmitter {
* @example
* device.stopConnection("gx");
**/
stopConnection(connectionMode: string) {
async stopConnection(connectionMode: string) {
if (connectionMode === "gx") {
gx.stopConnection();
gxEnabled = false;
let connectionStopped = await gx.stopConnection();
if (connectionStopped) {
gxEnabled = false;
return true;
} else {
console.error("Error stopping GX6 connection")
return false;
}
} else if (connectionMode === "bluetooth") {
bluetooth.stopConnection();
bluetoothEnabled = false;
let connectionStopped = await bluetooth.stopConnection();
if (connectionStopped) {
bluetoothEnabled = false;
return true;
} else {
console.error("Error stopping Bluetooth connection")
return false;
}
}
}

Expand Down Expand Up @@ -262,7 +284,7 @@ export default class HaritoraXWireless extends EventEmitter {
];

if (trackerName.startsWith("HaritoraX")) {
log("Setting tracker settings for bluetooth is not supported yet.");
console.error("Setting tracker settings for bluetooth is not supported yet.");
return false;
} else {
log(`Setting tracker settings for ${trackerName}...`);
Expand Down Expand Up @@ -452,7 +474,7 @@ Raw hex data calculated to be sent: ${hexValue}`);
return false;
}
} else {
log("No connection mode is enabled");
console.error("No connection mode is enabled");
return false;
}

Expand Down Expand Up @@ -939,7 +961,7 @@ function processIMUData(data: string, trackerName: string) {

// Check if the data is valid
if (!data || data.length !== 24) {
log(`Invalid IMU packet for tracker ${trackerName}: ${data}`);
console.error(`Invalid IMU packet for tracker ${trackerName}: ${data}`);
return false;
}

Expand Down Expand Up @@ -1210,7 +1232,7 @@ function processTrackerData(data: string, trackerName: string) {
log(`Tracker ${trackerName} other data processed: ${data}`);
}

// TODO - Find out what the other data represents, then add to emitted event
// TODO - Find out what the other data represents, then add to emitted event. Seems to be likely mag data?
haritora.emit("tracker", trackerName, data);
}

Expand Down
35 changes: 26 additions & 9 deletions src/mode/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,19 @@ export default class Bluetooth extends EventEmitter {

startConnection() {
console.log("Connected to bluetooth");
this.emit("connected");

try {
noble.startScanning([], true);
} catch (error) {
console.error(`Error starting scanning:\n${error}`);
if (noble.state === "poweredOn") {
try {
noble.startScanning([], true);
this.emit("connected");
return true;
} catch (error) {
console.error(`Error starting scanning:\n${error}`);
return false;
}
} else {
console.error(`Error occurred while trying to start scanning: Bluetooth state is ${noble.state}`);
return false;
}
}

Expand Down Expand Up @@ -198,15 +205,25 @@ export default class Bluetooth extends EventEmitter {
}

stopConnection() {
console.log("(haritorax-interpreter) - Disconnected from bluetooth");
noble.stopScanning();
for (let device of activeDevices) {
device.disconnect();
try {
noble.stopScanning();
for (let device of activeDevices) {
device.disconnect();
}
} catch (err) {
console.error(
"(haritorax-interpreter) - Error while closing bluetooth connection: ",
err
);
return false;
}

activeDevices = [];
allowReconnect = false;

this.emit("disconnected");
console.log("(haritorax-interpreter) - Disconnected from bluetooth");
return true;
}

getServices() {
Expand Down
18 changes: 14 additions & 4 deletions src/mode/gx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,23 @@ export default class GX6 extends EventEmitter {
}

stopConnection() {
for (let port in activePorts) {
if (activePorts[port].isOpen) {
activePorts[port].close();
activePorts[port].destroy();
try {
for (let port in activePorts) {
if (activePorts[port].isOpen) {
activePorts[port].close();
activePorts[port].destroy();
}
}
} catch (err) {
console.error(
"(haritorax-interpreter) - Error while closing serial ports: ",
err
);
return false;
}

this.emit("disconnected");
return true;
}

getTrackerAssignment() {
Expand Down

0 comments on commit ca1277f

Please sign in to comment.