monerod-gui/app/utils/BatteryUtils.ts
argenius 9063c27cd2
Some checks are pending
MacOS - x64 DMG Build / build (20) (push) Waiting to run
Lint Test / build (20) (push) Waiting to run
Linux - AppImage Build / build (20) (push) Waiting to run
Linux - x86_64 RPM Build / build (20) (push) Waiting to run
Linux - x64 DEB Build / build (20) (push) Waiting to run
MacOS Build / build (20) (push) Waiting to run
Windows Build / build (20) (push) Waiting to run
Refactory, behavior consolidation and minor fixes
2024-11-16 01:31:33 +01:00

39 lines
No EOL
1.2 KiB
TypeScript

import * as os from 'os';
import { exec, ExecException } from "child_process";
import { powerMonitor } from "electron";
const batteryLevel = require('battery-level');
export abstract class BatteryUtils {
public static async isOnBatteryPower(): Promise<boolean> {
const onBattery = powerMonitor.isOnBatteryPower();
if (!onBattery && os.platform() == 'linux') {
return await new Promise<boolean>((resolve) => {
exec("upower -i $(upower -e | grep 'battery') | grep 'state'", (error: ExecException | null, stdout: string) => {
if (error) {
console.error(`isOnBatteryPower(): ${error.message}`);
resolve(false);
return;
}
const isOnBattery = stdout.includes("discharging");
resolve(isOnBattery);
});
});
}
return onBattery;
}
public static async getLevel(): Promise<number> {
try {
return batteryLevel();
}
catch(error: any) {
console.error(error);
return -1;
}
}
}