From ca1277f57e10cd50f310be43a2b2588818d2efb6 Mon Sep 17 00:00:00 2001 From: JovannMC Date: Sat, 13 Apr 2024 12:47:52 +0300 Subject: [PATCH] Improve connection error handling Add some form of error handling when starting connections and improve on some existing error handling --- src/devices/haritorax-wireless.ts | 46 +++++++++++++++++++++++-------- src/mode/bluetooth.ts | 35 +++++++++++++++++------ src/mode/gx.ts | 18 +++++++++--- 3 files changed, 74 insertions(+), 25 deletions(-) diff --git a/src/devices/haritorax-wireless.ts b/src/devices/haritorax-wireless.ts index d44663e..f2ac000 100644 --- a/src/devices/haritorax-wireless.ts +++ b/src/devices/haritorax-wireless.ts @@ -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; } /** @@ -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; + } } } @@ -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}...`); @@ -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; } @@ -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; } @@ -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); } diff --git a/src/mode/bluetooth.ts b/src/mode/bluetooth.ts index d4637ae..dcaa687 100644 --- a/src/mode/bluetooth.ts +++ b/src/mode/bluetooth.ts @@ -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; } } @@ -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() { diff --git a/src/mode/gx.ts b/src/mode/gx.ts index ab5bb7e..52ad9e6 100644 --- a/src/mode/gx.ts +++ b/src/mode/gx.ts @@ -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() {