1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package oshi.software.os.linux.proc;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import oshi.hardware.PowerSource;
25 import oshi.util.FileUtil;
26
27
28
29
30
31
32 public class LinuxPowerSource implements PowerSource {
33
34 private static final String PS_PATH = "/sys/class/power_supply/";
35
36 private String name;
37
38 private double remainingCapacity;
39
40 private double timeRemaining;
41
42 public LinuxPowerSource(String name, double remainingCapacity,
43 double timeRemaining) {
44 this.name = name;
45 this.remainingCapacity = remainingCapacity;
46 this.timeRemaining = timeRemaining;
47 }
48
49 @Override
50 public String getName() {
51 return this.name;
52 }
53
54 @Override
55 public double getRemainingCapacity() {
56 return this.remainingCapacity;
57 }
58
59 @Override
60 public double getTimeRemaining() {
61 return this.timeRemaining;
62 }
63
64
65
66
67 public static PowerSource[] getPowerSources() {
68
69 File f = new File(PS_PATH);
70 String[] psNames = f.list();
71
72 if (psNames == null)
73 psNames = new String[0];
74 List<LinuxPowerSource> psList = new ArrayList<>(
75 psNames.length);
76
77 for (String psName : psNames) {
78
79 if (psName.startsWith("ADP"))
80 continue;
81
82 List<String> psInfo;
83 try {
84 psInfo = FileUtil.readFile(PS_PATH + psName + "/uevent");
85 } catch (IOException e) {
86 continue;
87 }
88
89 boolean isPresent = false;
90 boolean isCharging = false;
91 String name = "Unknown";
92 int energyNow = 0;
93 int energyFull = 1;
94 int powerNow = 1;
95 for (String checkLine : psInfo) {
96 if (checkLine.startsWith("POWER_SUPPLY_PRESENT")) {
97
98 String[] psSplit = checkLine.split("=");
99 if (psSplit.length > 1)
100 isPresent = Integer.parseInt(psSplit[1]) > 0;
101 if (!isPresent)
102 continue;
103 } else if (checkLine.startsWith("POWER_SUPPLY_NAME")) {
104
105 String[] psSplit = checkLine.split("=");
106 if (psSplit.length > 1)
107 name = psSplit[1];
108 } else if (checkLine.startsWith("POWER_SUPPLY_ENERGY_NOW")
109 || checkLine.startsWith("POWER_SUPPLY_CHARGE_NOW")) {
110
111 String[] psSplit = checkLine.split("=");
112 if (psSplit.length > 1)
113 energyNow = Integer.parseInt(psSplit[1]);
114 } else if (checkLine.startsWith("POWER_SUPPLY_ENERGY_FULL")
115 || checkLine.startsWith("POWER_SUPPLY_CHARGE_FULL")) {
116 String[] psSplit = checkLine.split("=");
117 if (psSplit.length > 1)
118 energyFull = Integer.parseInt(psSplit[1]);
119 } else if (checkLine.startsWith("POWER_SUPPLY_STATUS")) {
120
121 String[] psSplit = checkLine.split("=");
122 if (psSplit.length > 1 && psSplit[1].equals("Charging"))
123 isCharging = true;
124 } else if (checkLine.startsWith("POWER_SUPPLY_POWER_NOW")
125 || checkLine.startsWith("POWER_SUPPLY_CURRENT_NOW")) {
126
127 String[] psSplit = checkLine.split("=");
128 if (psSplit.length > 1)
129 powerNow = Integer.parseInt(psSplit[1]);
130 if (powerNow <= 0)
131 isCharging = true;
132 }
133 }
134 psList.add(new LinuxPowerSource(name, (double) energyNow
135 / energyFull, isCharging ? -2d : 3600d * energyNow
136 / powerNow));
137 }
138
139 return psList.toArray(new LinuxPowerSource[psList.size()]);
140 }
141 }