Class Win32BatteryService

java.lang.Object
io.github.eggy03.ferrumx.windows.service.peripheral.Win32BatteryService
All Implemented Interfaces:
CommonServiceInterface<Win32Battery>

public class Win32BatteryService extends Object implements CommonServiceInterface<Win32Battery>
Service class for fetching battery information from the system.

This class executes the Cimv2.WIN32_BATTERY PowerShell command and maps the resulting JSON into an immutable list of Win32Battery objects.

Usage examples

// Convenience API (creates its own short-lived session)
Win32BatteryService service = new Win32BatteryService();
List<Win32Battery> batteries = service.get();

// API with re-usable session (caller manages session lifecycle)
try (PowerShell session = PowerShell.openSession()) {
    List<Win32Battery> batteries = service.get(session);
}

// API with execution timeout (auto-created session is terminated if the timeout is exceeded)
Win32BatteryService service = new Win32BatteryService();
List<Win32Battery> batteries = service.get(10);

Execution models and concurrency

This service supports multiple PowerShell execution strategies:

  • jPowerShell-based execution via get() and get(PowerShell):
    These methods rely on jPowerShell sessions. Due to internal global configuration of jPowerShell, the PowerShell sessions launched by it is not safe to use concurrently across multiple threads or executors. Running these methods in parallel may result in runtime exceptions.
  • Isolated PowerShell execution via get(long timeout):
    This method doesn't rely on jPowerShell and instead, launches a standalone PowerShell process per invocation using TerminalUtility. Each call is fully isolated and safe to use in multithreaded and executor-based environments.

For concurrent or executor-based workloads, prefer get(long timeout).

Since:
3.0.0
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    @NotNull @Unmodifiable List<Win32Battery>
    get()
    Retrieves an immutable list of batteries present on the system.
    @NotNull @Unmodifiable List<Win32Battery>
    get(long timeout)
    Retrieves an immutable list of batteries present on the system using an isolated PowerShell process with a configurable timeout.
    @NotNull @Unmodifiable List<Win32Battery>
    get(@NonNull com.profesorfalken.jpowershell.PowerShell powerShell)
    Retrieves an immutable list of batteries present on the system using the caller's PowerShell session.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Win32BatteryService

      public Win32BatteryService()
  • Method Details

    • get

      @UsesJPowerShell @NotNull public @NotNull @Unmodifiable List<Win32Battery> get()
      Retrieves an immutable list of batteries present on the system.

      Each invocation creates and uses a short-lived PowerShell session internally.

      Specified by:
      get in interface CommonServiceInterface<Win32Battery>
      Returns:
      an immutable list of Win32Battery objects representing the system's batteries. If no batteries are present, returns an empty list.
      Since:
      3.0.0
    • get

      @UsesJPowerShell @NotNull public @NotNull @Unmodifiable List<Win32Battery> get(@NonNull @NonNull com.profesorfalken.jpowershell.PowerShell powerShell)
      Retrieves an immutable list of batteries present on the system using the caller's PowerShell session.
      Specified by:
      get in interface CommonServiceInterface<Win32Battery>
      Parameters:
      powerShell - an existing PowerShell session managed by the caller
      Returns:
      an immutable list of Win32Battery objects representing the system's batteries. If no batteries are present, returns an empty list.
      Since:
      3.0.0
    • get

      @IsolatedPowerShell @NotNull public @NotNull @Unmodifiable List<Win32Battery> get(long timeout)
      Retrieves an immutable list of batteries present on the system using an isolated PowerShell process with a configurable timeout.

      Each invocation creates an isolated PowerShell process, which is pre-maturely terminated if execution exceeds the specified timeout.

      Specified by:
      get in interface CommonServiceInterface<Win32Battery>
      Parameters:
      timeout - the maximum time (in seconds) to wait for the PowerShell command to complete before terminating the process
      Returns:
      an immutable list of Win32Battery objects representing the system's batteries. If no batteries are present, returns an empty list.
      Since:
      3.1.0