summaryrefslogtreecommitdiff
path: root/power.js
blob: e15e24b680e8aad6554e53b6d71022ff7667b2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const ExtensionUtils = imports.misc.extensionUtils;
const UPower = imports.gi.UPowerGlib;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;

var UPowerBatteryProvider = class {
    constructor(logger) {
        this._upower_client = UPower.Client.new();
        this._logger = logger;
    }

    locateBatteryDevice(device) {
        // upower has no field in the devices that indicate that a battery is
        // from a bluetooth device, so we must try and find by the provided mac.
        // Problem is, the native_path field has macs in all kinds of forms ...
        let _mac_addrs = [
            device.mac.toUpperCase(),
            device.mac.replace(/:/g, "_").toUpperCase(),
        ];

        let _battery_devices = this._upower_client.get_devices();
        let _bateries = _battery_devices.filter(bat => {
            let _native_path = bat.native_path.toUpperCase();
            return _mac_addrs.some(mac => _native_path.includes(mac));
        });

        if (_bateries.length > 1) {
            this._logger.warn(`device ${device.name} matched more than one UPower device by native_path`);
            _bateries = [];
        }

        let _battery_native_path = _bateries.map(bat => bat.native_path)[0] || "NOT-FOUND";
        this._logger.info(`battery: ${_battery_native_path}`);

        return _bateries;
    }
}