Skip to content

Commit 879fe84

Browse files
authored
batterypower@joka42: Fix multiple batteries are not working (#6536)
* Closes #6480
1 parent d71a274 commit 879fe84

File tree

1 file changed

+29
-23
lines changed
  • batterypower@joka42/files/batterypower@joka42

1 file changed

+29
-23
lines changed

batterypower@joka42/files/batterypower@joka42/applet.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ BatteryPowerApplet.prototype = {
2828
this.settings.bindProperty(Settings.BindingDirection.IN, 'interval', 'interval', () => this.on_settings_changed(), null);
2929
this.UPowerRefreshed = false;
3030

31-
this.batFolder = "";
31+
this.batDirectories = [];
3232
for (let i = 0; i < 10; i++)
3333
{
3434
const statusFile = "/sys/class/power_supply/BAT" + i +"/status";
3535
if (GLib.file_test(statusFile, 1 << 4)){
36-
this.batFolder = "BAT" + i;
36+
this.batDirectories.push("BAT" + i);
3737
}
3838
}
3939

@@ -80,8 +80,8 @@ BatteryPowerApplet.prototype = {
8080
},
8181

8282
_getBatteryStatus: function () {
83-
if (this.batFolder){
84-
const statusFile = "/sys/class/power_supply/" + this.batFolder + "/status";
83+
if (this.batDirectories.length) {
84+
const statusFile = "/sys/class/power_supply/" + this.batDirectories[0] + "/status";
8585
if (GLib.file_test(statusFile, 1 << 4)) {
8686
try {
8787
return String(GLib.file_get_contents(statusFile)[1]).trim();
@@ -91,7 +91,7 @@ BatteryPowerApplet.prototype = {
9191
}
9292
}
9393

94-
if (!this.UPowerRefreshed){
94+
if (!this.UPowerRefreshed) {
9595
Main.Util.spawnCommandLine(`python3 ${__meta.path}/update_upower.py`);
9696
this.UPowerRefreshed = true;
9797
}
@@ -115,27 +115,33 @@ BatteryPowerApplet.prototype = {
115115
// If the files cannot be found, upower is used to update the power draw from the
116116
// battery.
117117

118-
if (this.batFolder) {
119-
const powerDrawFile = "/sys/class/power_supply/" + this.batFolder + "/power_now";
120-
if(GLib.file_test(powerDrawFile, 1 << 4)) {
121-
try{
122-
return parseInt(GLib.file_get_contents(powerDrawFile)[1]) / 1000000.0;
123-
} catch (error) {
124-
return 0.0;
118+
if (this.batDirectories.length) {
119+
let power = 0.0;
120+
for (let i = 0; i < this.batDirectories.length; i++) {
121+
const baseDirectory = "/sys/class/power_supply/" + this.batDirectories[i];
122+
123+
const powerDrawFile = baseDirectory + "/power_now";
124+
if(GLib.file_test(powerDrawFile, 1 << 4)) {
125+
try{
126+
power += parseInt(GLib.file_get_contents(powerDrawFile)[1]) / 1000000.0;
127+
} catch (error) {
128+
// do nothing
129+
}
125130
}
126-
}
127-
128-
const currentDrawFile = "/sys/class/power_supply/" + this.batFolder + "/current_now";
129-
const voltageDrawFile = "/sys/class/power_supply/" + this.batFolder + "/voltage_now";
130-
if (GLib.file_test(currentDrawFile, 1 << 4) && GLib.file_test(voltageDrawFile, 1 << 4)) {
131-
try {
132-
const current = parseInt(GLib.file_get_contents(currentDrawFile)[1]) / 1000000.0;
133-
const voltage = parseInt(GLib.file_get_contents(voltageDrawFile)[1]) / 1000000.0;
134-
return current * voltage;
135-
} catch (error) {
136-
return 0.0;
131+
132+
const currentDrawFile = baseDirectory + "/current_now";
133+
const voltageDrawFile = baseDirectory + "/voltage_now";
134+
if (GLib.file_test(currentDrawFile, 1 << 4) && GLib.file_test(voltageDrawFile, 1 << 4)) {
135+
try {
136+
const current = parseInt(GLib.file_get_contents(currentDrawFile)[1]) / 1000000.0;
137+
const voltage = parseInt(GLib.file_get_contents(voltageDrawFile)[1]) / 1000000.0;
138+
power += current * voltage;
139+
} catch (error) {
140+
// do nothing
141+
}
137142
}
138143
}
144+
return power
139145
}
140146

141147
// If the files could not be used, we need to update upower information and use info

0 commit comments

Comments
 (0)