#!/usr/bin/perl ############################## # Calcul l'etat de la batterie ############################## use strict; use vars qw(@Batt_Data %Useful); # renvoit une liste de hash des valeurs lues (un element par batterie) sub read_proc { my @batts = find_batts(); my @Batt_Data; for (@batts) { my $batt_dir = $_; my %h1 = read_Batt_Data($batt_dir.'state'); my %h2 = read_Batt_Data($batt_dir.'info'); # TODO mieux? my %h = %h1; for my $key ( keys %h2 ) { $h{$key} = $h2{$key}; } # le tableau contient une reference au hash push @Batt_Data, {%h}; } return @Batt_Data; } # lit un fichier de donnees de la batterie dans proc sub read_Batt_Data { my ($file) = @_; my %data; #print '>>',$file,"\n"; open(DATA, "< $file") or next; while () { if (/^([^:]+):\s+(.+)/){ #print "$1 -> $2\n"; $data{$1} = $2; } } return %data; } # trouve les repertoires d'etat des batteries sub find_batts { my @batts; my $proc_path = '/proc/acpi/battery/'; # TODO verifier que c'est un repertoire opendir(BATTS, $proc_path) or die "can't open $proc_path: $!"; while (defined(my $bat_dir = readdir(BATTS))) { # TODO verifier que c'est un repertoire if ($bat_dir =~ /BAT([0-9])/) { #print $1,$bat_dir,"\n"; $batts[$1] = $proc_path.$bat_dir.'/'; } } return @batts; } # synthetise les donnees utiles sub summarize { # capacite totale des batteries #print $Batt_Data[0]{'present'},"\n"; my $total_full_cap = 0; my $total_rem = 0; my $total_rate = 0; for my $href ( @Batt_Data ) { my $cap = $href->{'last full capacity'} || 0; # toujours en mAh ? $total_full_cap += $_ for ( $cap =~ /(\d+) mAh/ ); #for ( keys %$href ) { #print $_,"\n"; #} my $remaining = $href->{'remaining capacity'} || 0; #print $remaining,"\n"; $total_rem += $_ for ( $remaining =~ /(\d+) mAh/ ); my $rate = $href->{'present rate'} || 0; $total_rate += $_ for ( $rate =~ /(\d+) mA/ ); } $Useful{'capacity'} = $total_full_cap; $Useful{'remaining'} = $total_rem; $Useful{'rate'} = $total_rate; # temps restant en heures (end of operation) $Useful{'eooTime'} = $total_rem / $total_rate; $Useful{'remainingPerc'} = 100*$total_rem/$total_full_cap; return %Useful; } # TODO adapter selon l'OS @Batt_Data = read_proc(); summarize(); print <