Skip to content

Commit

Permalink
Remove flawed BLE logic
Browse files Browse the repository at this point in the history
ok so my dumb code is fucking stupid, and this was very much flawed for the entire time this has been here. im stupid. there was no need for "initialization".
  • Loading branch information
JovannMC committed Jul 20, 2024
1 parent 3cfed37 commit eecc94c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 57 deletions.
8 changes: 0 additions & 8 deletions src/HaritoraX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,6 @@ function listenToDeviceEvents() {
main.emit("disconnect", trackerName);
});

bluetooth.on("ready", (trackerName) => {
main.emit("initialized", trackerName);
});

bluetooth.on("log", (message: string) => {
log(message);
});
Expand Down Expand Up @@ -1002,10 +998,6 @@ function processIMUData(data: Buffer, trackerName: string, ankleValue?: number)
log(`Tracker ${trackerName} isn't in active devices, adding and emitting connect event`);
activeDevices.push(trackerName);
main.emit("connect", trackerName);
// there is no waiting for the tracker to be initialized for COM wireless trackers - emitting it here after a second for consistency with BLE
setTimeout(() => {
main.emit("initialized", trackerName);
}, 1000);
}

// Decode and log the data
Expand Down
54 changes: 5 additions & 49 deletions src/mode/bluetooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const characteristics = new Map([
["0c900df6a85e11edafa10242ac120002", "Response"],
]);

type ActiveDevice = [string, Peripheral, Service[], Characteristic[], boolean];
type ActiveDevice = [string, Peripheral, Service[], Characteristic[]];
let activeDevices: ActiveDevice[] = [];

let allowReconnect = true;
Expand Down Expand Up @@ -99,20 +99,12 @@ export default class Bluetooth extends EventEmitter {

try {
await connectPeripheral(peripheral);
log(`(bluetooth) Connected to ${localName}`);
this.emit("connect", peripheral);

const { services, characteristics } = await discoverServicesAndCharacteristics(peripheral);

updateActiveDevices(localName, peripheral, services, characteristics);

while (!(await areAllBLEDiscovered(localName))) {
if (activeDevices.find((device) => device[0] === localName)[4]) break;
log(`Waiting for all services and characteristics to be discovered for ${localName}...`);
await new Promise((resolve) => setTimeout(resolve, 1000));
}

this.emit("ready", localName);
log(`(bluetooth) Connected to ${localName}`);
this.emit("connect", peripheral);
} catch (err) {
error(`Error during discovery/connection process: ${err}`, true);
}
Expand Down Expand Up @@ -255,9 +247,9 @@ async function discoverCharacteristics(localName: string, service: Service) {
});
}

function updateActiveDevices(localName: string, peripheral: Peripheral, services: Service[], characteristics: Characteristic[], isReady = false) {
function updateActiveDevices(localName: string, peripheral: Peripheral, services: Service[], characteristics: Characteristic[]) {
const deviceIndex = activeDevices.findIndex((device) => device[0] === localName);
const deviceData: ActiveDevice = [localName, peripheral, services, characteristics, isReady];
const deviceData: ActiveDevice = [localName, peripheral, services, characteristics];
if (deviceIndex !== -1) activeDevices[deviceIndex] = deviceData;
else activeDevices.push(deviceData);
}
Expand Down Expand Up @@ -296,7 +288,6 @@ async function writeCharacteristic(characteristicInstance: Characteristic, data:
async function getDevice(localName: string): Promise<ActiveDevice> {
const device = activeDevices.find((device: ActiveDevice) => device[0] === localName);
if (!device) error(`Device ${localName} not found, list: ${activeDevices}`, true);
if (!device[4]) error(`Device ${localName} not ready yet`, true);

return device;
}
Expand All @@ -321,41 +312,6 @@ function getCharacteristic(service: Service, characteristic: string): Characteri
* General helper functions
*/

const importantServices = ["ef84369a90a911eda1eb0242ac120002", "180f"];
const importantCharacteristics = [
"2a19",
"ef84420290a911eda1eb0242ac120002",
"ef8443f690a911eda1eb0242ac120002",
"ef8445c290a911eda1eb0242ac120002",
"ef84c30090a911eda1eb0242ac120002",
"ef84c30590a911eda1eb0242ac120002",
];

async function areAllBLEDiscovered(trackerName: string): Promise<boolean> {
const device = activeDevices.find((device: ActiveDevice) => device[0] === trackerName);
if (!device) return false;

const [, , services, characteristics] = device;

// Check if all important services are discovered
for (const serviceUuid of importantServices) {
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: Characteristic) => characteristic.uuid === characteristicUuid)) {
return false;
}
}

log(`All services and characteristics discovered for ${trackerName}`);
activeDevices.find((device) => device[0] === trackerName)[4] = true;
return true;
}

function emitData(localName: string, service: string, characteristic: string, data: any) {
main.emit(
"data",
Expand Down

0 comments on commit eecc94c

Please sign in to comment.