add more settings, use dbus instead of shell commands to parse battery

stats
This commit is contained in:
Trent Palmer 2018-06-30 18:32:49 -07:00
parent 1ba4c06b9b
commit ea74627517
2 changed files with 188 additions and 125 deletions

277
applet.js
View File

@ -1,13 +1,65 @@
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Applet = imports.ui.applet; // ++ const Applet = imports.ui.applet; // ++
const Settings = imports.ui.settings; // ++ Needed if you use Settings Screen const Settings = imports.ui.settings; // ++ Needed if you use Settings Screen
const PopupMenu = imports.ui.popupMenu; // ++ Needed for menus
const Lang = imports.lang; const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Mainloop = imports.mainloop; // Needed for timer update loop const Mainloop = imports.mainloop; // Needed for timer update loop
const Util = imports.misc.util;
const Gettext = imports.gettext;
var UUID; var UUID;
// generate the following information with the following command
// dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.DBus.Introspectable.Introspect
const UPowerIface = '<node>\
<interface name="org.freedesktop.UPower">\
<method name="EnumerateDevices"> \
<arg name="devices" direction="out" type="ao"/> \
</method> \
</interface> \
</node>';
const UPowerProxy = Gio.DBusProxy.makeProxyWrapper(UPowerIface);
// generate the following information with the following command
// dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower/devices/DisplayDevice org.freedesktop.DBus.Introspectable.Introspect
// or if you prefer
// dbus-send --system --print-reply --dest=org.freedesktop.UPower /org/freedesktop/UPower/devices/battery_BAT0 org.freedesktop.DBus.Introspectable.Introspect
const UPowerDevIface = '<node>\
<interface name="org.freedesktop.UPower.Device">\
<method name="GetStatistics"> \
<arg name="data" direction="out" type="a(dd)"/> \
</method> \
<property type="s" name="NativePath" access="read"/> \
<property type="s" name="Vendor" access="read"/> \
<property type="s" name="Model" access="read"/> \
<property type="s" name="Serial" access="read"/> \
<property type="t" name="UpdateTime" access="read"/> \
<property type="u" name="Type" access="read"/> \
<property type="b" name="PowerSupply" access="read"/> \
<property type="b" name="HasHistory" access="read"/> \
<property type="b" name="HasStatistics" access="read"/> \
<property type="b" name="Online" access="read"/> \
<property type="d" name="Energy" access="read"/> \
<property type="d" name="EnergyEmpty" access="read"/> \
<property type="d" name="EnergyFull" access="read"/> \
<property type="d" name="EnergyFullDesign" access="read"/> \
<property type="d" name="EnergyRate" access="read"/> \
<property type="d" name="Voltage" access="read"/> \
<property type="d" name="Luminosity" access="read"/> \
<property type="x" name="TimeToEmpty" access="read"/> \
<property type="x" name="TimeToFull" access="read"/> \
<property type="d" name="Percentage" access="read"/> \
<property type="d" name="Temperature" access="read"/> \
<property type="b" name="IsPresent" access="read"/> \
<property type="u" name="State" access="read"/> \
<property type="b" name="IsRechargeable" access="read"/> \
<property type="d" name="Capacity" access="read"/> \
<property type="u" name="Technology" access="read"/> \
<property type="u" name="WarningLevel" access="read"/> \
<property type="u" name="BatteryLevel" access="read"/> \
<property type="s" name="IconName" access="read"/> \
</interface> \
</node>';
const UPowerDevProxy = Gio.DBusProxy.makeProxyWrapper(UPowerDevIface);
// ++ Always needed // ++ Always needed
function MyApplet(metadata, orientation, panelHeight, instance_id) { function MyApplet(metadata, orientation, panelHeight, instance_id) {
this._init(metadata, orientation, panelHeight, instance_id); this._init(metadata, orientation, panelHeight, instance_id);
@ -25,13 +77,21 @@ MyApplet.prototype = {
this.appletPath = metadata.path; this.appletPath = metadata.path;
UUID = metadata.uuid; UUID = metadata.uuid;
this.settings.bind("show-bat0-rate","show_bat0_rate");
this.settings.bind("show-bat0-percent","show_bat0_percent"); this.settings.bind("show-bat0-percent","show_bat0_percent");
this.settings.bind("show-bat0-capacity","show_bat0_capacity"); this.settings.bind("show-bat0-capacity","show_bat0_capacity");
this.settings.bind("show-bat0-estimated-time-remaining","show_bat0_estimated_time_remaining"); this.settings.bind("show-bat0-estimated-time-remaining","show_bat0_estimated_time_remaining");
this.settings.bind("show-bat1-rate","show_bat1_rate");
this.settings.bind("show-bat1-percent","show_bat1_percent"); this.settings.bind("show-bat1-percent","show_bat1_percent");
this.settings.bind("show-bat1-capacity","show_bat1_capacity"); this.settings.bind("show-bat1-capacity","show_bat1_capacity");
this.settings.bind("show-bat1-estimated-time-remaining","show_bat1_estimated_time_remaining"); this.settings.bind("show-bat1-estimated-time-remaining","show_bat1_estimated_time_remaining");
this.settings.bind("show-display-battery-rate","show_display_battery_rate");
this.settings.bind("show-display-battery-percent","show_display_battery_percent");
this.settings.bind("show-display-battery-capacity","show_display_battery_capacity");
this.settings.bind("show-display-battery-estimated-time-remaining","show_display_battery_estimated_time_remaining");
this.applet_running = true; //** New to allow applet to be fully stopped when removed from panel this.applet_running = true; //** New to allow applet to be fully stopped when removed from panel
this._main_refresh_loop(); // This starts the MainLoop timer loop this._main_refresh_loop(); // This starts the MainLoop timer loop
} }
@ -40,141 +100,108 @@ MyApplet.prototype = {
} }
}, },
_has_two_batteries: function() {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -e | grep battery | wc -l"');
if (stdout != null) {
if (stdout == 2) {
return true
}
}
return false
},
_battery_icon_to_display: function(battery) {
if (battery == "bat0") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT0 | awk \'/icon-name/ {print $2}\'"');
return stdout.toString().slice(1,-2)
} else if (battery == "bat1") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT1 | awk \'/icon-name/ {print $2}\'"');
return stdout.toString().slice(1,-2)
}
},
_get_battery_capacity: function(battery) {
if (battery == "bat0") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT0 | awk \'/energy-full:/ {print $2}\'"');
return stdout.toString().slice(0,-1).replace(/.\d*$/i,'')
} else if (battery == "bat1") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT1 | awk \'/energy-full:/ {print $2}\'"');
return stdout.toString().slice(0,-1).replace(/.\d*$/i,'')
}
},
_get_time_to_empty: function(battery) {
if (battery == "bat0") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT0 | awk \'/time\ to\ empty/ {print $4}\'"');
return stdout.toString().slice(0,-1)
} else if (battery == "bat1") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT1 | awk \'/time\ to\ empty/ {print $4}\'"');
return stdout.toString().slice(0,-1)
}
},
_get_battery_charge_percent: function(battery) {
if (battery == "bat0") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT0 | awk \'/percentage/ {gsub(\\\"%\\\",\\\"\\\");print $2}\'"');
return stdout.toString().slice(0,-1)
} else if (battery == "bat1") {
let [result, stdout, stderr] = GLib.spawn_command_line_sync('/bin/sh -c "upower -i /org/freedesktop/UPower/devices/battery_BAT1 | awk \'/percentage/ {gsub(\\\"%\\\",\\\"\\\");print $2}\'"');
return stdout.toString().slice(0,-1)
}
},
_show_bat0: function() {
if (this.show_bat0_percent || this.show_bat0_capacity || this.show_bat0_estimated_time_remaining) {
return "BAT0 - "
} else { return "" };
},
_show_bat0_percent: function(remaining_percent) {
if (this.show_bat0_percent) {
return remaining_percent + "% "
} else { return "" };
},
_show_bat0_capacity: function() {
if (this.show_bat0_capacity) {
return this._get_battery_capacity("bat0") + "Wh "
} else { return "" };
},
_show_bat0_estimated_time_remaining: function() {
if (this.show_bat0_estimated_time_remaining) {
return this._get_time_to_empty("bat0") + "HRS "
} else { return "" };
},
_show_bat1: function() {
if (this.show_bat1_percent || this.show_bat1_capacity || this.show_bat1_estimated_time_remaining) {
return "BAT1 - "
} else { return "" };
},
_show_bat1_percent: function(remaining_percent) {
if (this.show_bat1_percent) {
return remaining_percent + "% "
} else { return "" };
},
_show_bat1_capacity: function() {
if (this.show_bat1_capacity) {
return this._get_battery_capacity("bat1") + "Wh "
} else { return "" };
},
_show_bat1_estimated_time_remaining: function() {
if (this.show_bat1_estimated_time_remaining) {
return this._get_time_to_empty("bat1") + "HRS "
} else { return "" };
},
_update_applet: function() { _update_applet: function() {
if (this._has_two_batteries() === true) { if (this._has_two_batteries() === true) {
let UPBat0Proxy = new UPowerDevProxy(Gio.DBus.system,"org.freedesktop.UPower","/org/freedesktop/UPower/devices/battery_BAT0");
this._battery0_charge_percent = this._get_battery_charge_percent("bat0") let UPBat1Proxy = new UPowerDevProxy(Gio.DBus.system,"org.freedesktop.UPower","/org/freedesktop/UPower/devices/battery_BAT1");
this._battery1_charge_percent = this._get_battery_charge_percent("bat1") let UPDspDevProxy = new UPowerDevProxy(Gio.DBus.system,"org.freedesktop.UPower","/org/freedesktop/UPower/devices/DisplayDevice");
this._icon_string = "" this._icon_string = ""
this._icon_string += this._show_bat0();
this._icon_string += this._show_bat0_percent(this._battery0_charge_percent);
this._icon_string += this._show_bat0_capacity();
this._icon_string += this._show_bat0_estimated_time_remaining();
this._icon_string += this._show_bat1(); if (this.show_bat0_rate || this.show_bat0_percent || this.show_bat0_capacity || this.show_bat0_estimated_time_remaining) {
this._icon_string += this._show_bat1_percent(this._battery1_charge_percent); this._icon_string += "BAT0 - "
this._icon_string += this._show_bat1_capacity(); if (this.show_bat0_rate) {
this._icon_string += this._show_bat1_estimated_time_remaining(); this._icon_string += Math.round(UPBat0Proxy.EnergyRate*10)/10 + "W/H,"
this.set_applet_label(this._icon_string) }
if (this.show_bat0_percent) {
if (parseInt(this._battery0_charge_percent) >= parseInt(this._battery1_charge_percent)) { this._icon_string += UPBat0Proxy.Percentage + "%,"
this.set_applet_icon_symbolic_name(this._battery_icon_to_display("bat1")); }
} else { if (this.show_bat0_capacity) {
this.set_applet_icon_symbolic_name(this._battery_icon_to_display("bat0")); this._icon_string += Math.floor(UPBat0Proxy.EnergyFull) + "Wh,"
}
if (this.show_bat0_estimated_time_remaining) {
let _bat0_estimated_time_remaining = UPBat0Proxy.TimeToEmpty
this._icon_string += Math.floor(_bat0_estimated_time_remaining/3600) + ":" + Math.floor((_bat0_estimated_time_remaining%3600)/60/10) + Math.floor((_bat0_estimated_time_remaining%3600)/60%10) + "Hrs "
}
} }
if (this.show_bat1_rate || this.show_bat1_percent || this.show_bat1_capacity || this.show_bat1_estimated_time_remaining) {
this._icon_string += "BAT1 - "
if (this.show_bat1_rate) {
this._icon_string += Math.round(UPBat1Proxy.EnergyRate*10)/10 + "W/H,"
}
if (this.show_bat1_percent) {
this._icon_string += UPBat1Proxy.Percentage + "%,"
}
if (this.show_bat1_capacity) {
this._icon_string += Math.floor(UPBat1Proxy.EnergyFull) + "Wh,"
}
if (this.show_bat1_estimated_time_remaining) {
let _bat1_estimated_time_remaining = UPBat1Proxy.TimeToEmpty
this._icon_string += Math.floor(_bat1_estimated_time_remaining/3600) + ":" + Math.floor((_bat1_estimated_time_remaining%3600)/60/10) + Math.floor((_bat1_estimated_time_remaining%3600)/60%10) + "Hrs "
}
}
if (this.show_display_battery_rate || this.show_display_battery_percent || this.show_display_battery_capacity || this.show_display_battery_estimated_time_remaining) {
this._icon_string += "COMB - "
if (this.show_display_battery_rate) {
this._icon_string += Math.round(UPDspDevProxy.EnergyRate*10)/10 + "W/H,"
}
if (this.show_display_battery_percent) {
this._icon_string += Math.floor(UPDspDevProxy.Percentage) + "%,"
}
if (this.show_display_battery_capacity) {
this._icon_string += Math.floor(UPDspDevProxy.EnergyFull) + "Wh,"
}
if (this.show_display_battery_estimated_time_remaining) {
let _display_battery_estimated_time_remaining = UPDspDevProxy.TimeToEmpty
this._icon_string += Math.floor(_display_battery_estimated_time_remaining/3600) + ":" + Math.floor((_display_battery_estimated_time_remaining%3600)/60/10) + Math.floor((_display_battery_estimated_time_remaining%3600)/60%10) + "Hrs "
}
}
this.set_applet_label(this._icon_string)
this.set_applet_icon_symbolic_name(UPDspDevProxy.IconName);
} else { } else {
this._battery0_charge_percent = this._get_battery_charge_percent("bat0") let UPBat0Proxy = new UPowerDevProxy(Gio.DBus.system,"org.freedesktop.UPower","/org/freedesktop/UPower/devices/battery_BAT0");
this._icon_string = "" this._icon_string = ""
this._icon_string += this._show_bat0();
this._icon_string += this._show_bat0_percent(this._battery0_charge_percent); if (this.show_bat0_rate || this.show_bat0_percent || this.show_bat0_capacity || this.show_bat0_estimated_time_remaining) {
this._icon_string += this._show_bat0_capacity(); this._icon_string += "BAT0 - "
this._icon_string += this._show_bat0_estimated_time_remaining(); if (this.show_bat0_rate) {
this._icon_string += Math.round(UPBat0Proxy.EnergyRate*10)/10 + "W/H,"
}
if (this.show_bat0_percent) {
this._icon_string += UPBat0Proxy.Percentage + "%,"
}
if (this.show_bat0_capacity) {
this._icon_string += Math.floor(UPBat0Proxy.EnergyFull) + "Wh,"
}
if (this.show_bat0_estimated_time_remaining) {
let _bat0_estimated_time_remaining = UPBat0Proxy.TimeToEmpty
this._icon_string += Math.floor(_bat0_estimated_time_remaining/3600) + ":" + Math.floor((_bat0_estimated_time_remaining%3600)/60/10) + Math.floor((_bat0_estimated_time_remaining%3600)/60%10) + "Hrs "
}
}
this.set_applet_label(this._icon_string) this.set_applet_label(this._icon_string)
this.set_applet_icon_symbolic_name(this._battery_icon_to_display("bat0")); this.set_applet_icon_symbolic_name(UPBat0Proxy.IconName);
} }
}, },
_has_two_batteries: function() {
let UPProxy = new UPowerProxy(Gio.DBus.system,"org.freedesktop.UPower","/org/freedesktop/UPower");
let _UPowerProxy_devices = UPProxy.EnumerateDevicesSync()[0];
let _UPowerProxy_devices_length = _UPowerProxy_devices.length;
for (var i=0; i<_UPowerProxy_devices_length; i++) {
if (_UPowerProxy_devices[i] === '/org/freedesktop/UPower/devices/battery_BAT1') {
return true
};
}
return false
},
_main_refresh_loop: function() { _main_refresh_loop: function() {
this._update_applet(); this._update_applet();
if (this.applet_running === true) { if (this.applet_running === true) {

View File

@ -1,4 +1,10 @@
{ {
"show-bat0-rate": {
"type": "switch",
"default": "false",
"description": "Show BAT0 Discharge Rate",
"tooltip": "default"
},
"show-bat0-percent": { "show-bat0-percent": {
"type": "switch", "type": "switch",
"default": "false", "default": "false",
@ -17,6 +23,12 @@
"description": "Show BAT0 Estimated Time Remaining", "description": "Show BAT0 Estimated Time Remaining",
"tooltip": "default" "tooltip": "default"
}, },
"show-bat1-rate": {
"type": "switch",
"default": "false",
"description": "Show BAT1 Discharge Rate",
"tooltip": "default"
},
"show-bat1-percent": { "show-bat1-percent": {
"type": "switch", "type": "switch",
"default": "false", "default": "false",
@ -34,5 +46,29 @@
"default": "false", "default": "false",
"description": "Show BAT1 Estimated Time Remaining", "description": "Show BAT1 Estimated Time Remaining",
"tooltip": "default" "tooltip": "default"
},
"show-display-battery-rate": {
"type": "switch",
"default": "false",
"description": "Show COMBINED Discharge Rate",
"tooltip": "default"
},
"show-display-battery-percent": {
"type": "switch",
"default": "false",
"description": "Show COMBINED Percent",
"tooltip": "default"
},
"show-display-battery-capacity": {
"type": "switch",
"default": "false",
"description": "Show COMBINED Capacity",
"tooltip": "default"
},
"show-display-battery-estimated-time-remaining": {
"type": "switch",
"default": "false",
"description": "Show COMBINED Estimated Time Remaining",
"tooltip": "default"
} }
} }