<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package GLPI::Agent::Task::Inventory::Generic::Dmidecode::Battery;

use strict;
use warnings;

use parent 'GLPI::Agent::Task::Inventory::Module';

use GLPI::Agent::Tools;
use GLPI::Agent::Tools::Generic;
use GLPI::Agent::Tools::Batteries;

use constant    category    =&gt; "battery";

sub isEnabled {
    return 1;
}

sub doInventory {
    my (%params) = @_;

    my $inventory = $params{inventory};
    my $logger    = $params{logger};

    foreach my $battery (_getBatteries(logger =&gt; $logger)) {
        $inventory-&gt;addEntry(
            section =&gt; 'BATTERIES',
            entry   =&gt; $battery
        );
    }
}

sub _getBatteries {
    my $infos = getDmidecodeInfos(@_);

    return unless $infos-&gt;{22};

    my @batteries = ();
    foreach my $info (@{$infos-&gt;{22}}) {
        my $battery = _extractBatteryData($info);
        push @batteries, $battery if $battery;
    }

    return @batteries;
}

sub _extractBatteryData {
    my ($info) = @_;

    # Skip battery data without enough infos
    return unless $info-&gt;{'Name'} &amp;&amp; $info-&gt;{'Manufacturer'};
    return unless $info-&gt;{'Serial Number'} || $info-&gt;{'SBDS Serial Number'};
    return unless $info-&gt;{'Chemistry'} || $info-&gt;{'SBDS Chemistry'};

    my $battery = {
        NAME         =&gt; $info-&gt;{'Name'},
        MANUFACTURER =&gt; getCanonicalManufacturer($info-&gt;{'Manufacturer'}),
        SERIAL       =&gt; sanitizeBatterySerial(
            $info-&gt;{'Serial Number'} || $info-&gt;{'SBDS Serial Number'}
        ),
        CHEMISTRY    =&gt; $info-&gt;{'Chemistry'} ||
            $info-&gt;{'SBDS Chemistry'},
    };

    if ($info-&gt;{'Manufacture Date'}) {
        $battery-&gt;{DATE} = _parseDate($info-&gt;{'Manufacture Date'});
    } elsif ($info-&gt;{'SBDS Manufacture Date'}) {
        $battery-&gt;{DATE} = _parseDate($info-&gt;{'SBDS Manufacture Date'});
    }

    my $voltage  = getCanonicalVoltage($info-&gt;{'Design Voltage'});
    $battery-&gt;{VOLTAGE} = $voltage
        if $voltage;

    my $capacity = getCanonicalCapacity($info-&gt;{'Design Capacity'}, $voltage);
    $battery-&gt;{CAPACITY} = $capacity
        if $capacity;

    return $battery;
}

sub _parseDate {
    my ($string) = @_;

    my ($day, $month, $year);
    if ($string =~ /(\d{1,2}) [\/-] (\d{1,2}) [\/-] (\d{4})/x) {
        $month = $1;
        $day   = $2;
        $year  = $3;
        return "$day/$month/$year";
    } elsif ($string =~ /(\d{4}) [\/-] (\d{1,2}) [\/-] (\d{1,2})/x) {
        $year  = $1;
        $month = $2;
        $day   = $3;
        return "$day/$month/$year";
    } elsif ($string =~ /(\d{1,2}) [\/-] (\d{1,2}) [\/-] (\d{2})/x) {
        $month = $1;
        $day = $2;
        $year = ($3 &gt; 90 ? "19" : "20" ).$3;
        return "$day/$month/$year";
    }
    return;
}

1;
</pre></body></html>