mirror of
https://github.com/everoddandeven/monerod-gui.git
synced 2024-12-22 19:49:27 +00:00
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
39 lines
No EOL
1.2 KiB
TypeScript
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;
|
|
}
|
|
}
|
|
} |