Skip to content

Commit

Permalink
add more properties to "connect" event
Browse files Browse the repository at this point in the history
"connectionMode" shows which connection mode the tracker was connected by, and "port" shows what COM port the tracker was from
  • Loading branch information
JovannMC committed Jul 24, 2024
1 parent c36b5be commit bd4c431
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ let trackerModelEnabled: string;
* @event this#connect
* @type {string}
* @property {string} trackerName - The name of the tracker.
* @property {string} connectionMode - The connection mode the tracker is using - useful if multiple connection modes are used.
* @property {string} port - The COM port used the tracker is using (COM only)
**/

/**
Expand Down Expand Up @@ -956,7 +958,7 @@ function listenToDeviceEvents() {
const trackerName = peripheral.advertisement.localName;
if (trackerName && !activeDevices.includes(trackerName)) {
activeDevices.push(trackerName);
main.emit("connect", trackerName);
main.emit("connect", trackerName, "bluetooth");
}
});

Expand Down Expand Up @@ -1059,9 +1061,15 @@ function processIMUData(data: Buffer, trackerName: string, ankleValue?: number)

// If tracker isn't in activeDevices, add it and emit "connect" event
if (trackerName && !activeDevices.includes(trackerName) && (comEnabled || bluetoothEnabled)) {
log(`Tracker ${trackerName} isn't in active devices, adding and emitting connect event`);
console.log(`Tracker ${trackerName} isn't in active devices, adding and emitting connect event`);

const mode = isWirelessBT(trackerName) ? "bluetooth" : "com";
const port = isWirelessBT(trackerName) ? undefined : com.getTrackerPort(trackerName);

console.log(`Tracker ${trackerName} mode: ${mode}, port: ${port}`);

activeDevices.push(trackerName);
main.emit("connect", trackerName);
main.emit("connect", trackerName, mode, port);
}

// Decode and log the data
Expand Down
1 change: 0 additions & 1 deletion src/mode/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ let allowReconnect = true;
export default class Bluetooth extends EventEmitter {
constructor() {
super();
this.setMaxListeners(1); // Prevent memory leaks
noble.on("discover", this.onDiscover.bind(this));
main = this;
log(`Initialized Bluetooth module.`);
Expand Down
1 change: 0 additions & 1 deletion src/mode/com.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ let heartbeatInterval: number; // in milliseconds
export default class COM extends EventEmitter {
constructor(trackerModel: string, heartbeat?: number) {
super();
this.setMaxListeners(1); // Prevent memory leaks
main = this;
trackerModelEnabled = trackerModel;
heartbeatInterval = heartbeat;
Expand Down

0 comments on commit bd4c431

Please sign in to comment.