Skip to content

Commit

Permalink
Change some functions from "get" to "fire event"
Browse files Browse the repository at this point in the history
Renames getTrackerMag, getBatteryInfo, and getDeviceInfo to fireTrackerMag, fireTrackerBattery, and fireDeviceInfo and changes their functionality to not return the info and instead fires its events. Fixes issues with double info (and sometimes errors)
  • Loading branch information
JovannMC committed Jul 7, 2024
1 parent 72de760 commit fa7a5e1
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 162 deletions.
289 changes: 134 additions & 155 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,110 +500,6 @@ export default class HaritoraX extends EventEmitter {
}
}

/**
* Returns device info for the specified tracker or dongle.
* Supported trackers: wireless
* Supported connections: COM, Bluetooth
*
* @function getDeviceInfo
* @param trackerName - The name of the tracker.
* @returns {object} The device info (version, model, serial, comm, comm_next).
* @fires this#info
**/

async getDeviceInfo(trackerName: string) {
// Global
let serial, model, version, comm, comm_next;

if (trackerModelEnabled === "wireless" && bluetoothEnabled && isWirelessBT(trackerName)) {
const trackerObject = bluetooth
.getActiveDevices()
.find((device) => device[0] === trackerName);
if (!trackerObject) {
log(`Tracker ${trackerName} not found`);
return null;
}

const decoder = new TextDecoder("utf-8");

const readCharacteristic = async (uuid: string) => {
const buffer = await bluetooth.read(trackerName, SERVICE_UUID, uuid);
return buffer ? decoder.decode(buffer) : undefined;
};

[version, model, serial] = await Promise.all([
readCharacteristic(VERSION_UUID),
readCharacteristic(MODEL_UUID),
readCharacteristic(SERIAL_UUID),
]);
} else if (comEnabled) {
const deviceInfo = com.getDeviceInformation(trackerName);
[version, model, serial] = [
deviceInfo[VERSION_INDEX],
deviceInfo[MODEL_INDEX],
deviceInfo[SERIAL_INDEX],
];

if (trackerModelEnabled === "wired") {
[comm, comm_next] = [deviceInfo[COMM_INDEX], deviceInfo[COMM_NEXT_INDEX]];
}
} else {
log(`Tracker ${trackerName} not found or unsupported model enabled`);
return null;
}

log(`Tracker ${trackerName} info: ${version}, ${model}, ${serial}, ${comm}, ${comm_next}`);
this.emit("info", trackerName, version, model, serial, comm, comm_next);
return { version, model, serial, comm, comm_next };
}

/**
* Get battery info from the trackers.
* Supported trackers: wireless
* Supported connections: COM, Bluetooth
*
* @function getBatteryInfo
* @returns {object} The battery info (batteryRemaining, batteryVoltage, chargeStatus).
* @fires this#battery
**/

async getBatteryInfo(trackerName: string) {
log(`Getting battery info for ${trackerName}`);

// Check if battery info is already available
if (trackerBattery.has(trackerName)) {
const [batteryRemaining, batteryVoltage, chargeStatus] =
trackerBattery.get(trackerName);
logBatteryInfo(trackerName, batteryRemaining, batteryVoltage, chargeStatus);
return { batteryRemaining, batteryVoltage, chargeStatus };
}

// Attempt to read battery info for wireless BT trackers
if (isWirelessBT(trackerName)) {
log(`Reading battery info for ${trackerName}...`);
try {
const buffer = await bluetooth.read(
trackerName,
batteryService,
batteryLevelCharacteristic
);
if (!buffer) throw new Error(`Tracker ${trackerName} battery info not found`);
const batteryRemaining = new DataView(buffer).getUint8(0);
let batteryVoltage,
chargeStatus = undefined;
trackerBattery.set(trackerName, [batteryRemaining, batteryVoltage, chargeStatus]);
logBatteryInfo(trackerName, batteryRemaining, batteryVoltage, chargeStatus);
return { batteryRemaining, batteryVoltage, chargeStatus };
} catch (err) {
error(`Error getting battery info for ${trackerName}: ${err}`);
return null;
}
} else {
log(`Tracker ${trackerName} battery info not found`);
return null;
}
}

/**
* Get the active trackers.
* Supported trackers: wireless, wired
Expand Down Expand Up @@ -749,21 +645,148 @@ export default class HaritoraX extends EventEmitter {
return null;
}

/**
* Check whether the connection mode is active or not.
* Supported trackers: wireless, wired
* Supported connections: COM, Bluetooth
*
* @function getConnectionModeActive
* @param {string} connectionMode - The connection mode to check.
* @returns {boolean} Whether the connection mode is active or not.
**/
getConnectionModeActive(connectionMode: string) {
switch (connectionMode) {
case "com":
return comEnabled;
case "bluetooth":
return bluetoothEnabled;
default:
return null;
}
}

getActiveTrackerModel() {
return trackerModelEnabled;
}

// !
// TODO: test if i broke these for COM (and wired, but will need to manually data or have people test)

/**
* Returns device info for the specified tracker or dongle.
* Supported trackers: wireless
* Supported connections: COM, Bluetooth
*
* @function fireDeviceInfo
* @param trackerName - The name of the tracker.
* @fires this#info
**/

async fireDeviceInfo(trackerName: string) {
// Global
let serial, model, version, comm, comm_next;

if (trackerModelEnabled === "wireless" && bluetoothEnabled && isWirelessBT(trackerName)) {
const trackerObject = bluetooth
.getActiveDevices()
.find((device) => device[0] === trackerName);
if (!trackerObject) {
log(`Tracker ${trackerName} not found`);
return null;
}

const decoder = new TextDecoder("utf-8");

const readCharacteristic = async (uuid: string) => {
const buffer = await bluetooth.read(trackerName, SERVICE_UUID, uuid);
return buffer ? decoder.decode(buffer) : undefined;
};

[version, model, serial] = await Promise.all([
readCharacteristic(VERSION_UUID),
readCharacteristic(MODEL_UUID),
readCharacteristic(SERIAL_UUID),
]);
} else if (comEnabled) {
const deviceInfo = com.getDeviceInformation(trackerName);
[version, model, serial] = [
deviceInfo[VERSION_INDEX],
deviceInfo[MODEL_INDEX],
deviceInfo[SERIAL_INDEX],
];

if (trackerModelEnabled === "wired") {
[comm, comm_next] = [deviceInfo[COMM_INDEX], deviceInfo[COMM_NEXT_INDEX]];
}
} else {
log(`Tracker ${trackerName} not found or unsupported model enabled`);
return null;
}

log(`Tracker ${trackerName} info: ${version}, ${model}, ${serial}, ${comm}, ${comm_next}`);
this.emit("info", trackerName, version, model, serial, comm, comm_next);
return true;
}

/**
* Get battery info from the trackers.
* Supported trackers: wireless
* Supported connections: COM, Bluetooth
*
* @function fireTrackerBattery
* @fires this#battery
**/

async fireTrackerBattery(trackerName: string) {
log(`Getting battery info for ${trackerName}`);
let batteryRemaining, batteryVoltage, chargeStatus;

// Check if battery info is already available
if (trackerBattery.has(trackerName))
[batteryRemaining, batteryVoltage, chargeStatus] = trackerBattery.get(trackerName);

// Attempt to read battery info for wireless BT trackers
if (isWirelessBT(trackerName)) {
log(`Reading battery info for ${trackerName}...`);
try {
const buffer = await bluetooth.read(
trackerName,
batteryService,
batteryLevelCharacteristic
);
if (!buffer) throw new Error(`Tracker ${trackerName} battery info not found`);
batteryRemaining = new DataView(buffer).getUint8(0);
trackerBattery.set(trackerName, [batteryRemaining, batteryVoltage, chargeStatus]);
} catch (err) {
error(`Error getting battery info for ${trackerName}: ${err}`);
return null;
}
} else {
log(`Tracker ${trackerName} battery info not found`);
return null;
}

log(`Tracker ${trackerName} battery remaining: ${batteryRemaining}%`);
log(`Tracker ${trackerName} battery voltage: ${batteryVoltage}`);
log(`Tracker ${trackerName} charge status: ${chargeStatus}`);
main.emit("battery", trackerName, batteryRemaining, batteryVoltage, chargeStatus);
return true;
}

/**
* Get the tracker's magnetometer status
* Supported trackers: wireless, wired
* Supported connections: COM, Bluetooth
*
* @function getTrackerMag
* @function fireTrackerMag
* @param {string} trackerName - The name of the tracker.
* @returns {string} The tracker's magnetometer status.
* @fires this#mag
*/
async getTrackerMag(trackerName: string) {
async fireTrackerMag(trackerName: string) {
if (trackerMag.has(trackerName)) {
let magStatus = trackerMag.get(trackerName);
log(`Tracker ${trackerName} magnetometer status: ${magStatus}`);
this.emit("mag", trackerName, magStatus);
return magStatus;
main.emit("mag", trackerName, magStatus);
} else {
// Read from BLE device
if (!isWirelessBT(trackerName)) {
Expand All @@ -772,43 +795,14 @@ export default class HaritoraX extends EventEmitter {
}

try {
const magBuffer = await bluetooth.read(
trackerName,
trackerService,
magnetometerCharacteristic
);
const magStatus = magBuffer ? Buffer.from(magBuffer).toString("utf-8") : null;
this.emit("mag", trackerName, magStatus);
return processMagData(magStatus, trackerName);
await bluetooth.read(trackerName, trackerService, magnetometerCharacteristic);
} catch (err) {
error(`Error reading magnetometer status: ${err}`);
return null;
}
}
}

/**
* Check whether the connection mode is active or not.
* Supported trackers: wireless, wired
* Supported connections: COM, Bluetooth
*
* @function getConnectionModeActive
* @param {string} connectionMode - The connection mode to check.
* @returns {boolean} Whether the connection mode is active or not.
**/
getConnectionModeActive(connectionMode: string) {
switch (connectionMode) {
case "com":
return comEnabled;
case "bluetooth":
return bluetoothEnabled;
default:
return null;
}
}

getActiveTrackerModel() {
return trackerModelEnabled;
return true;
}

/**
Expand Down Expand Up @@ -1839,19 +1833,4 @@ function updateTrackerSettings(
}
}

/*
* getBatteryInfo() function helpers
*/

function logBatteryInfo(
trackerName: string,
batteryRemaining: string | number | undefined,
batteryVoltage: string | number | undefined,
chargeStatus: string | undefined
) {
log(`Tracker ${trackerName} battery remaining: ${batteryRemaining}%`);
log(`Tracker ${trackerName} battery voltage: ${batteryVoltage}`);
log(`Tracker ${trackerName} charge status: ${chargeStatus}`);
}

export { HaritoraX };
10 changes: 3 additions & 7 deletions src/mode/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,25 +340,21 @@ const importantCharacteristics = [
];

async function areAllBLEDiscovered(trackerName: string): Promise<boolean> {
// Find the device entry for the specified trackerName
const device = activeDevices.find((device: any) => device[0] === trackerName);

// If the device is not found, return false
const device = activeDevices.find((device: ActiveDevice) => device[0] === trackerName);
if (!device) return false;

// Destructure the device to get services and characteristics
const [, , services, characteristics] = device;

// Check if all important services are discovered
for (const serviceUuid of importantServices) {
if (!services.find((service: any) => service.uuid === serviceUuid)) {
if (!services.find((service: Service) => service.uuid === serviceUuid)) {
return false;
}
}

// Check if all important characteristics are discovered
for (const characteristicUuid of importantCharacteristics) {
if (!characteristics.find((characteristic: any) => characteristic.uuid === characteristicUuid)) {
if (!characteristics.find((characteristic: Characteristic) => characteristic.uuid === characteristicUuid)) {
return false;
}
}
Expand Down

0 comments on commit fa7a5e1

Please sign in to comment.