Skip to content

Commit

Permalink
remove isWirelessBT
Browse files Browse the repository at this point in the history
unnecessarily checking more parameters than needed lol
  • Loading branch information
JovannMC committed Aug 8, 2024
1 parent 97de0b6 commit 7cc8567
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export default class HaritoraX extends EventEmitter {
"Ankle motion detection": ankleMotionDetection,
};

if (isWirelessBT(trackerName)) {
if (trackerName.startsWith("HaritoraXW")) {
writeToBluetooth(trackerName, sensorModeCharacteristic, sensorMode === 1 ? 5 : 8);
writeToBluetooth(trackerName, fpsModeCharacteristic, fpsMode === 50 ? 1 : 2);
writeToBluetooth(trackerName, correctionCharacteristic, sensorAutoCorrectionBit);
Expand Down Expand Up @@ -518,7 +518,7 @@ export default class HaritoraX extends EventEmitter {

if (trackerModelEnabled === "wired") {
return await getTrackerSettingsFromMap("HaritoraXWired");
} else if (trackerModelEnabled === "wireless" && bluetoothEnabled && isWirelessBT(trackerName)) {
} else if (trackerModelEnabled === "wireless" && bluetoothEnabled && trackerName.startsWith("HaritoraXW")) {
if (forceBluetoothRead || !trackerSettings.has(trackerName)) {
try {
const readCharacteristic = async (characteristic: string) => {
Expand All @@ -545,7 +545,7 @@ export default class HaritoraX extends EventEmitter {
} else {
return await getTrackerSettingsFromMap(trackerName);
}
} else if (trackerModelEnabled === "wireless" && comEnabled && !isWirelessBT(trackerName)) {
} else if (trackerModelEnabled === "wireless" && comEnabled && !trackerName.startsWith("HaritoraXW")) {
return getTrackerSettingsFromMap(trackerName);
} else {
error(`Cannot get settings for ${trackerName} settings.`);
Expand Down Expand Up @@ -630,7 +630,7 @@ export default class HaritoraX extends EventEmitter {
// Global
let serial, model, version, comm, comm_next;

if (trackerModelEnabled === "wireless" && bluetoothEnabled && isWirelessBT(trackerName)) {
if (trackerModelEnabled === "wireless" && bluetoothEnabled && trackerName.startsWith("HaritoraXW")) {
const trackerObject = bluetooth.getActiveDevices().find((device) => device[0] === trackerName);
if (!trackerObject) {
log(`Tracker ${trackerName} not found`);
Expand Down Expand Up @@ -682,7 +682,7 @@ export default class HaritoraX extends EventEmitter {
// Check if battery info is already available
if (trackerBattery.has(trackerName)) {
[batteryRemaining, batteryVoltage, chargeStatus] = trackerBattery.get(trackerName);
} else if (isWirelessBT(trackerName)) {
} else if (trackerName.startsWith("HaritoraXW")) {
// Attempt to read battery info for wireless BT trackers
log(`Reading battery info for ${trackerName}...`);
try {
Expand Down Expand Up @@ -745,7 +745,7 @@ export default class HaritoraX extends EventEmitter {
main.emit("mag", trackerName, magStatus);
} else {
// Read from BLE device
if (!isWirelessBT(trackerName)) {
if (!trackerName.startsWith("HaritoraXW")) {
log(`Tracker ${trackerName} magnetometer status not found`);
return null;
}
Expand Down Expand Up @@ -1115,8 +1115,8 @@ function processIMUData(data: Buffer, trackerName: string, ankleValue?: number)
if (trackerName && !activeDevices.includes(trackerName) && (comEnabled || bluetoothEnabled)) {
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);
const mode = trackerName.startsWith("HaritoraXW") ? "bluetooth" : "com";
const port = trackerName.startsWith("HaritoraXW") ? undefined : com.getTrackerPort(trackerName);

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

Expand All @@ -1141,7 +1141,7 @@ function processIMUData(data: Buffer, trackerName: string, ankleValue?: number)
}

main.emit("imu", trackerName, rotation, gravity, ankle ? ankle : ankleValue);
if (!isWirelessBT(trackerName)) main.emit("mag", trackerName, magStatus);
if (!trackerName.startsWith("HaritoraXW")) main.emit("mag", trackerName, magStatus);
} catch (err) {
error(`Error decoding tracker ${trackerName} IMU packet data: ${err}`, false);
}
Expand Down Expand Up @@ -1181,7 +1181,7 @@ function decodeIMUPacket(data: Buffer, trackerName: string) {
let bufferData = data.toString("base64");
ankle = bufferData.slice(-2) !== "==" ? data.readUint16LE(data.length - 2) : undefined;

if (!isWirelessBT(trackerName)) {
if (!trackerName.startsWith("HaritoraXW")) {
const magnetometerData = bufferData.charAt(bufferData.length - 5);

switch (magnetometerData) {
Expand Down Expand Up @@ -1373,7 +1373,7 @@ function processSettingsData(data: string, trackerName: string) {
const ANKLE_MOTION_DETECTION_INDEX = 13;

try {
if (trackerModelEnabled === "wireless" && !isWirelessBT(trackerName)) {
if (trackerModelEnabled === "wireless" && !trackerName.startsWith("HaritoraXW")) {
const sensorMode = parseInt(data[SENSOR_MODE_INDEX]);
const fpsMode = parseInt(data[POSTURE_DATA_RATE_INDEX]);
const sensorAutoCorrection = parseInt(data[SENSOR_AUTO_CORRECTION_INDEX]);
Expand Down Expand Up @@ -1478,7 +1478,7 @@ function processButtonData(data: string, trackerName: string, characteristic?: s
let isOn = true;

try {
if (isWirelessBT(trackerName)) {
if (trackerName.startsWith("HaritoraXW")) {
buttonPressed = processWirelessBTTrackerData(characteristic, currentButtons);
} else if (comEnabled && trackerModelEnabled === "wireless") {
const { buttonPressed: newButtonPressed, isOn: newIsOn } = processWirelessTrackerData(
Expand Down Expand Up @@ -1599,7 +1599,7 @@ function processBatteryData(data: string, trackerName: string, characteristic?:
if (status !== undefined) log(`Tracker ${trackerName} Status: ${status}`);
};

if (comEnabled && !isWirelessBT(trackerName)) {
if (comEnabled && !trackerName.startsWith("HaritoraXW")) {
try {
const batteryInfo = JSON.parse(data);
batteryData[0] = batteryInfo["battery remaining"];
Expand All @@ -1610,7 +1610,7 @@ function processBatteryData(data: string, trackerName: string, characteristic?:
error(`Error parsing battery data JSON for ${trackerName}: ${err}`);
log(`Raw battery data: ${data}`);
}
} else if (trackerModelEnabled === "wireless" && isWirelessBT(trackerName) && bluetoothEnabled && characteristic) {
} else if (trackerModelEnabled === "wireless" && trackerName.startsWith("HaritoraXW") && bluetoothEnabled && characteristic) {
try {
if (characteristic === "BatteryLevel") {
const batteryRemaining = parseInt(Buffer.from(data, "base64").toString("hex"), 16);
Expand Down Expand Up @@ -1692,10 +1692,6 @@ function error(message: string, exceptional = false) {
})();
}

function isWirelessBT(trackerName: string): boolean {
return trackerName.startsWith("HaritoraX") && trackerName !== "HaritoraXWired";
}

function calculateSensorAutoCorrectionBits(sensorAutoCorrection: SensorCorrectionKey[]): number {
return sensorAutoCorrection.reduce((acc, curr) => acc | SENSOR_CORRECTION_BITS[curr], 0);
}
Expand Down

0 comments on commit 7cc8567

Please sign in to comment.