show battery level

This commit is contained in:
liamcottle
2025-02-16 17:41:46 +13:00
parent e06ed0deee
commit bd9c8d49d4
4 changed files with 49 additions and 2 deletions

View File

@@ -60,6 +60,7 @@ class Connection {
// clear previous connection state
GlobalState.contacts = [];
GlobalState.batteryPercentage = null;
// update connection and listen for events
GlobalState.connection = connection;
@@ -144,14 +145,20 @@ class Connection {
// initial setup without needing database
await this.loadSelfInfo();
await this.syncDeviceTime();
await this.updateBatteryPercentage();
// wait for database to be ready
await databaseToBeReady;
// sync messages
// fetch data after database is ready
await this.loadContacts();
await this.syncMessages();
// auto update battery percentage once per minute
setInterval(async () => {
await this.updateBatteryPercentage();
}, 60000);
}
static async onDisconnected() {
@@ -166,6 +173,17 @@ class Connection {
GlobalState.contacts = await GlobalState.connection.getContacts();
}
static async updateBatteryPercentage() {
if(GlobalState.connection){
try {
const response = await GlobalState.connection.getBatteryVoltage();
GlobalState.batteryPercentage = Utils.getBatteryPercentage(response.batteryMilliVolts);
} catch(e) {
// ignore error
}
}
}
static async setAdvertName(name) {
await GlobalState.connection.setAdvertName(name);
}
@@ -270,6 +288,10 @@ class Connection {
await GlobalState.connection.reboot();
}
static async getBatteryVoltage() {
return await GlobalState.connection.getBatteryVoltage();
}
static async onContactMessageReceived(message) {
console.log("onContactMessageReceived", message);