-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathUPowerController.js
40 lines (34 loc) · 1.13 KB
/
UPowerController.js
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
38
39
40
import * as Utils from './utils.js';
import {UPOWER_DEVICES_SCRIPT_PATH} from './constants.js';
export class UPowerController {
constructor(dir) {
this.dir = dir;
this._pythonRunner = new Utils.ScriptRunner();
}
async getDevices() {
const shellLocation = this.dir.get_child(UPOWER_DEVICES_SCRIPT_PATH).get_path();
let uPowerDevices = [];
try {
const stdout = await this._pythonRunner.runScriptAsync([shellLocation]);
uPowerDevices = JSON.parse(stdout);
} catch (error) {
logError('Error in getDevices' + error);
}
return uPowerDevices
.filter((device) => device.serial)
.map((device) => ({
name: device.model,
mac: device.serial,
batteryPercentage: this._getPercentage(device.percentage),
defaultIcon: device.icon,
isConnected: true,
}));
}
_getPercentage(text) {
const match = text.match(/\d+%/);
return match ? match[0] : '';
}
destroy() {
this._pythonRunner.cancel();
}
}