View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.openbsd;
6   
7   import java.time.LocalDate;
8   import java.util.ArrayList;
9   import java.util.HashSet;
10  import java.util.List;
11  import java.util.Set;
12  
13  import oshi.annotation.concurrent.ThreadSafe;
14  import oshi.hardware.PowerSource;
15  import oshi.hardware.common.AbstractPowerSource;
16  import oshi.util.Constants;
17  import oshi.util.ExecutingCommand;
18  import oshi.util.ParseUtil;
19  
20  /**
21   * A Power Source
22   */
23  @ThreadSafe
24  public final class OpenBsdPowerSource extends AbstractPowerSource {
25  
26      public OpenBsdPowerSource(String psName, String psDeviceName, double psRemainingCapacityPercent,
27              double psTimeRemainingEstimated, double psTimeRemainingInstant, double psPowerUsageRate, double psVoltage,
28              double psAmperage, boolean psPowerOnLine, boolean psCharging, boolean psDischarging,
29              CapacityUnits psCapacityUnits, int psCurrentCapacity, int psMaxCapacity, int psDesignCapacity,
30              int psCycleCount, String psChemistry, LocalDate psManufactureDate, String psManufacturer,
31              String psSerialNumber, double psTemperature) {
32          super(psName, psDeviceName, psRemainingCapacityPercent, psTimeRemainingEstimated, psTimeRemainingInstant,
33                  psPowerUsageRate, psVoltage, psAmperage, psPowerOnLine, psCharging, psDischarging, psCapacityUnits,
34                  psCurrentCapacity, psMaxCapacity, psDesignCapacity, psCycleCount, psChemistry, psManufactureDate,
35                  psManufacturer, psSerialNumber, psTemperature);
36      }
37  
38      /**
39       * Gets Battery Information
40       *
41       * @return An array of PowerSource objects representing batteries, etc.
42       */
43      public static List<PowerSource> getPowerSources() {
44          Set<String> psNames = new HashSet<>();
45          for (String line : ExecutingCommand.runNative("systat -ab sensors")) {
46              if (line.contains(".amphour") || line.contains(".watthour")) {
47                  int dot = line.indexOf('.');
48                  psNames.add(line.substring(0, dot));
49              }
50          }
51          List<PowerSource> psList = new ArrayList<>();
52          for (String name : psNames) {
53              psList.add(getPowerSource(name));
54          }
55          return psList;
56      }
57  
58      private static OpenBsdPowerSource getPowerSource(String name) {
59          String psName = name.startsWith("acpi") ? name.substring(4) : name;
60          double psRemainingCapacityPercent = 1d;
61          double psTimeRemainingEstimated = -1d; // -1 = unknown, -2 = unlimited
62          double psPowerUsageRate = 0d;
63          double psVoltage = -1d;
64          double psAmperage = 0d;
65          boolean psPowerOnLine = false;
66          boolean psCharging = false;
67          boolean psDischarging = false;
68          CapacityUnits psCapacityUnits = CapacityUnits.RELATIVE;
69          int psCurrentCapacity = 0;
70          int psMaxCapacity = 1;
71          int psDesignCapacity = 1;
72          int psCycleCount = -1;
73          LocalDate psManufactureDate = null;
74  
75          double psTemperature = 0d;
76  
77          for (String line : ExecutingCommand.runNative("systat -ab sensors")) {
78              String[] split = ParseUtil.whitespaces.split(line);
79              if (split.length > 1 && split[0].startsWith(name)) {
80                  if (split[0].contains("volt0") || split[0].contains("volt") && line.contains("current")) {
81                      psVoltage = ParseUtil.parseDoubleOrDefault(split[1], -1d);
82                  } else if (split[0].contains("current0")) {
83                      psAmperage = ParseUtil.parseDoubleOrDefault(split[1], 0d);
84                  } else if (split[0].contains("temp0")) {
85                      psTemperature = ParseUtil.parseDoubleOrDefault(split[1], 0d);
86                  } else if (split[0].contains("watthour") || split[0].contains("amphour")) {
87                      psCapacityUnits = split[0].contains("watthour") ? CapacityUnits.MWH : CapacityUnits.MAH;
88                      if (line.contains("remaining")) {
89                          psCurrentCapacity = (int) (1000d * ParseUtil.parseDoubleOrDefault(split[1], 0d));
90                      } else if (line.contains("full")) {
91                          psMaxCapacity = (int) (1000d * ParseUtil.parseDoubleOrDefault(split[1], 0d));
92                      } else if (line.contains("new") || line.contains("design")) {
93                          psDesignCapacity = (int) (1000d * ParseUtil.parseDoubleOrDefault(split[1], 0d));
94                      }
95                  }
96              }
97          }
98  
99          int state = ParseUtil.parseIntOrDefault(ExecutingCommand.getFirstAnswer("apm -b"), 255);
100         // state 0=high, 1=low, 2=critical, 3=charging, 4=absent, 255=unknown
101         if (state < 4) {
102             psPowerOnLine = true;
103             if (state == 3) {
104                 psCharging = true;
105             } else {
106                 int time = ParseUtil.parseIntOrDefault(ExecutingCommand.getFirstAnswer("apm -m"), -1);
107                 // time is in minutes
108                 psTimeRemainingEstimated = time < 0 ? -1d : 60d * time;
109                 psDischarging = true;
110             }
111         }
112         // life is in percent
113         int life = ParseUtil.parseIntOrDefault(ExecutingCommand.getFirstAnswer("apm -l"), -1);
114         if (life > 0) {
115             psRemainingCapacityPercent = life / 100d;
116         }
117         if (psMaxCapacity < psDesignCapacity && psMaxCapacity < psCurrentCapacity) {
118             psMaxCapacity = psDesignCapacity;
119         } else if (psDesignCapacity < psMaxCapacity && psDesignCapacity < psCurrentCapacity) {
120             psDesignCapacity = psMaxCapacity;
121         }
122 
123         String psDeviceName = Constants.UNKNOWN;
124         String psSerialNumber = Constants.UNKNOWN;
125         String psChemistry = Constants.UNKNOWN;
126         String psManufacturer = Constants.UNKNOWN;
127 
128         double psTimeRemainingInstant = psTimeRemainingEstimated;
129         if (psVoltage > 0) {
130             if (psAmperage > 0 && psPowerUsageRate == 0) {
131                 psPowerUsageRate = psAmperage * psVoltage;
132             } else if (psAmperage == 0 && psPowerUsageRate > 0) {
133                 psAmperage = psPowerUsageRate / psVoltage;
134             }
135         }
136 
137         return new OpenBsdPowerSource(psName, psDeviceName, psRemainingCapacityPercent, psTimeRemainingEstimated,
138                 psTimeRemainingInstant, psPowerUsageRate, psVoltage, psAmperage, psPowerOnLine, psCharging,
139                 psDischarging, psCapacityUnits, psCurrentCapacity, psMaxCapacity, psDesignCapacity, psCycleCount,
140                 psChemistry, psManufactureDate, psManufacturer, psSerialNumber, psTemperature);
141     }
142 }