larsgk/web-thermometer

My humble feedback

Opened this issue · 4 comments

Hey @larsgk,
Thanks for writing https://dev.to/denladeside/creating-a-web-thermometer-2137 and sharing this demo code!

I had a look at the code and thought you may appreciate feedback ;)

In the code below, device will always be non null when requestDevice() resolves. So you can simply remove the if condition and write await this.#openDevice(device); only.

if (device) {
await this.#openDevice(device);
}


There, you don't have to await for sensor notifications to start the battery notifications. You can use Promise.all() for instance or something else (see below).

// Initialize sensor notifications
await this.#startThermometerNotifications(server);
// On Linux with earlier versions of BlueZ, there is an issue with 16bit IDs
try {
await this.#startBatteryNotifications(server);
} catch(err) {
console.log("Error with battery service: ", err);
}

// Start sensor and battery notifications
const startThermometerNotificationsPromise = this.#startThermometerNotifications(server);
const startBatteryNotificationsPromise = this.#startBatteryNotifications(server);

await startThermometerNotificationsPromise;

try {
  await startBatteryNotificationsPromise;
} catch (error) {
  // On Linux with earlier versions of BlueZ, there is an issue with 16bit IDs
  console.log("Error with battery service: ", err);
}

It seems deviceId can be removed below as it's not used

const deviceId = target.service.device.id;


Once again, thanks for your work on this!

larsgk commented

Thanks a lot François :) - I'll fix in the weekend (or maybe you wanna do a PR? :))

(gentle ping)

Any news on that front @larsgk?

larsgk commented

Any news on that front @larsgk?

Aaa sorry, I didn't know you were waiting :) family and work happened .. but I'll see if I can try to get it fixed this week together with a new Fugu & LEGO post (if you have a quick PR, I can also merge)