2021-07-27 05:11:42 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
my $output;
|
|
|
|
my ($bar, $pretty) = (0, 0);
|
|
|
|
|
|
|
|
if ( defined($ARGV[0]) && $ARGV[0] ne '-b' && $ARGV[0] ne '--bar' && $ARGV[0] ne '-p' && $ARGV[0] ne '--pretty' ) {
|
2023-01-06 19:41:31 +00:00
|
|
|
print'
|
2021-07-27 05:11:42 +00:00
|
|
|
pow.pl - Power Status Script
|
|
|
|
|
|
|
|
Usage: pow.pl
|
|
|
|
|
|
|
|
Prints information about all power devices as JSON.
|
|
|
|
|
2023-01-06 19:41:31 +00:00
|
|
|
-p --pretty Display as human-readable
|
|
|
|
-b --bar Waybar simplified output
|
|
|
|
-h --help Display help. This is the only option.
|
2021-07-27 05:11:42 +00:00
|
|
|
|
|
|
|
';
|
|
|
|
exit();
|
|
|
|
} elsif ($ARGV[0] eq '-b' || $ARGV[0] eq '--bar') {
|
2023-01-06 19:41:31 +00:00
|
|
|
$bar = 1;
|
2021-07-27 05:11:42 +00:00
|
|
|
} elsif ($ARGV[0] eq '-p' || $ARGV[0] eq '--pretty') {
|
2023-01-06 19:41:31 +00:00
|
|
|
$pretty = 1;
|
2021-07-27 05:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my %battery_total = (
|
2023-01-06 19:41:31 +00:00
|
|
|
'current' => 0,
|
|
|
|
'max' => 0,
|
|
|
|
'percentage' => 0
|
2021-07-27 05:11:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
my @devices = <"/sys/class/power_supply/*">;
|
2023-01-06 19:41:31 +00:00
|
|
|
unless (scalar(@devices)) {
|
|
|
|
print('{"AC":{"Type":"AC","Status":"Plugged-In"}}');
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
my $nobat = 1;
|
2021-07-27 05:11:42 +00:00
|
|
|
$output .= "{";
|
|
|
|
foreach (@devices) {
|
2023-01-06 19:41:31 +00:00
|
|
|
my $path = $_;
|
|
|
|
my $name = $_;
|
|
|
|
$name =~ s/.*\///;
|
|
|
|
$output .= '"' . $name . '":{';
|
|
|
|
open(my $t,'<',"$_/type");
|
|
|
|
my $type = <$t>;
|
|
|
|
chomp $type;
|
|
|
|
close $t;
|
|
|
|
$output .= '"Type":"' . $type . '"';
|
|
|
|
if ($name =~ /BAT[0-9]+/) {
|
|
|
|
open(my $s,'<',"$_/status");
|
|
|
|
my $status = <$s>;
|
|
|
|
chomp $status;
|
|
|
|
$output .= ',"Status":"' . $status . '"';
|
|
|
|
} else {
|
|
|
|
open(my $s,'<',"$_/online");
|
|
|
|
my $status = <$s>;
|
|
|
|
chomp $status;
|
|
|
|
if ($status) {
|
|
|
|
$status = "Plugged-In";
|
2021-07-27 05:11:42 +00:00
|
|
|
} else {
|
2023-01-06 19:41:31 +00:00
|
|
|
$status = "Unplugged";
|
2021-07-27 05:11:42 +00:00
|
|
|
}
|
2023-01-06 19:41:31 +00:00
|
|
|
$output .= ',"Status":"' . $status . '"';
|
|
|
|
}
|
|
|
|
close $s;
|
|
|
|
if ($name =~ /BAT[0-9]+/) {
|
|
|
|
open(my $m,'<',"$_/energy_full");
|
|
|
|
my $max = <$m>;
|
|
|
|
close $m;
|
|
|
|
chomp $max;
|
|
|
|
$battery_total{'max'} += $max;
|
|
|
|
open(my $c,'<',"$_/energy_now");
|
|
|
|
my $current = <$c>;
|
|
|
|
chomp $current;
|
|
|
|
#$current =~ s/\n//;
|
|
|
|
$battery_total{'current'} += $current;
|
|
|
|
close $c;
|
|
|
|
$output .= ',"Current":"'
|
|
|
|
. $current
|
|
|
|
. '","Max":"'
|
|
|
|
. $max
|
|
|
|
. '","Percentage":"'
|
|
|
|
. int($current/$max*100)
|
|
|
|
. '"';
|
|
|
|
}
|
|
|
|
$output .= "},";
|
2021-07-27 05:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$battery_total{'percentage'} = sprintf("%0d",
|
2023-01-06 19:41:31 +00:00
|
|
|
$battery_total{'current'} / $battery_total{'max'} * 100
|
2021-07-27 05:11:42 +00:00
|
|
|
);
|
|
|
|
$output .= '"Total":{"Current":"'
|
2023-01-06 19:41:31 +00:00
|
|
|
. $battery_total{'current'}
|
|
|
|
. '","Max":"'
|
|
|
|
. $battery_total{'max'}
|
|
|
|
. '","Percentage":"'
|
|
|
|
. $battery_total{'percentage'}
|
|
|
|
. '"}}';
|
2021-07-27 05:11:42 +00:00
|
|
|
|
|
|
|
if ($bar) {
|
2023-01-06 19:41:31 +00:00
|
|
|
use JSON::XS;
|
|
|
|
my $json = JSON::XS->new();
|
|
|
|
my $powref = $json->decode($output);
|
|
|
|
my $class = 'discharging';
|
|
|
|
$output = '';
|
|
|
|
if ($powref->{AC}->{Status} eq "Plugged-In") {
|
2023-01-07 13:16:47 +00:00
|
|
|
$class = 'charging';
|
2023-04-17 19:38:15 +00:00
|
|
|
$output .= " ";
|
2023-01-07 13:16:47 +00:00
|
|
|
} elsif ($powref->{Total}->{Percentage} <= 10) {
|
|
|
|
$class = 'critical';
|
2023-04-17 19:38:15 +00:00
|
|
|
$output .= " ";
|
2023-01-07 13:16:47 +00:00
|
|
|
} elsif ($powref->{Total}->{Percentage} <= 35) {
|
|
|
|
$class = 'low';
|
2023-04-17 19:38:15 +00:00
|
|
|
$output .= " ";
|
2023-01-06 19:41:31 +00:00
|
|
|
} elsif ($powref->{Total}->{Percentage} <= 60) {
|
2023-04-17 19:38:15 +00:00
|
|
|
$output .= " ";
|
2023-01-06 19:41:31 +00:00
|
|
|
} elsif ($powref->{Total}->{Percentage} <= 85) {
|
2023-04-17 19:38:15 +00:00
|
|
|
$output .= " ";
|
2023-01-06 19:41:31 +00:00
|
|
|
} else {
|
2023-04-17 19:38:15 +00:00
|
|
|
$output .= " ";
|
2023-01-06 19:41:31 +00:00
|
|
|
}
|
|
|
|
$output = '{'
|
2023-04-17 19:38:15 +00:00
|
|
|
. '"text":"' . $output . $powref->{Total}->{Percentage} . '%",'
|
2023-01-06 19:41:31 +00:00
|
|
|
. '"icon":"' . $output . '",'
|
|
|
|
. '"percentage":"' . $powref->{Total}->{Percentage} . '",'
|
|
|
|
. '"tooltip":"' . $class . '",'
|
|
|
|
. '"class":"' . $class . '"'
|
|
|
|
. '}';
|
|
|
|
} elsif ($pretty) {
|
|
|
|
use JSON::XS;
|
|
|
|
my $json = JSON::XS->new();
|
|
|
|
my $powref = $json->decode($output);
|
|
|
|
my $total = '';
|
|
|
|
$output = "Device Status Percentage\n";
|
|
|
|
foreach my $device (keys %{$powref}) {
|
2023-01-07 13:16:47 +00:00
|
|
|
my $status;
|
|
|
|
if ($powref->{$device}->{Status} eq "Plugged-In") {
|
|
|
|
$status = "";
|
|
|
|
} elsif (!defined($powref->{$device}->{Percentage}) || $powref->{$device}->{Percentage} == 0) {
|
|
|
|
$status = " ";
|
2023-01-06 19:41:31 +00:00
|
|
|
} elsif ($powref->{$device}->{Percentage} <= 10) {
|
|
|
|
$status = " " . $powref->{$device}->{'Percentage'} . "%";
|
|
|
|
} elsif ($powref->{$device}->{Percentage} <= 35) {
|
|
|
|
$status = " " . $powref->{$device}->{'Percentage'} . "%";
|
|
|
|
} elsif ($powref->{$device}->{Percentage} <= 60) {
|
|
|
|
$status = " " . $powref->{$device}->{'Percentage'} . "%";
|
|
|
|
} elsif ($powref->{$device}->{Percentage} <= 85) {
|
|
|
|
$status = " " . $powref->{$device}->{'Percentage'} . "%";
|
2023-01-07 13:16:47 +00:00
|
|
|
} else {
|
|
|
|
$status = " " . $powref->{$device}->{'Percentage'} . "%";
|
|
|
|
}
|
|
|
|
if ($device eq 'Total') {
|
|
|
|
$total .= sprintf("%-24s %-12s", $device, $status);
|
|
|
|
} else {
|
|
|
|
#$output .= sprintf("%".length($powref->{$device->{'Type'}})."s (%".length($powref->{$device->{'Type'}})."s): %3d\%\n", $device, $device->{'Type'}, $device->{'Percentage'});
|
|
|
|
my $name = ' ('.$powref->{$device}->{'Type'}.')';
|
|
|
|
if ((length($device)+length($name)) >= 24) {
|
|
|
|
$name = substr($device, 0, (21-length($name))) . '...' . $name;
|
|
|
|
} else {
|
|
|
|
$name = substr(($device.$name), 0, 24);
|
|
|
|
}
|
|
|
|
$output .= sprintf("%-24s %-12s %-12s\n", $name, $powref->{$device}->{Status}, $status);
|
|
|
|
}
|
2023-01-06 19:41:31 +00:00
|
|
|
}
|
|
|
|
$output .= $total;
|
2021-07-27 05:11:42 +00:00
|
|
|
}
|
|
|
|
print $output . "\n";
|
|
|
|
|
|
|
|
exit();
|