-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentManager.java
More file actions
94 lines (84 loc) · 3.56 KB
/
ComponentManager.java
File metadata and controls
94 lines (84 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package components;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.profesorfalken.jsensors.JSensors;
import com.profesorfalken.jsensors.model.components.Cpu;
import com.profesorfalken.jsensors.model.components.Disk;
import com.profesorfalken.jsensors.model.components.Gpu;
import com.profesorfalken.jsensors.model.sensors.Fan;
import com.profesorfalken.jsensors.model.sensors.Load;
import com.profesorfalken.jsensors.model.sensors.Temperature;
import oshi.SystemInfo;
import oshi.hardware.GraphicsCard;
import oshi.hardware.HWDiskStore;
public class ComponentManager
{
private List<GPU> gpus;
private List<PhysicalDisk> disks;
private CPU cpu;
private Ram ram;
public ComponentManager()
{
cpu = new CPU();
ram = new Ram();
gpus = new ArrayList<GPU>();
disks = new ArrayList<PhysicalDisk>();
try {
List<Gpu> gpuJ = JSensors.get.components().gpus;
List<GraphicsCard> graphicsCards = new SystemInfo().getHardware().getGraphicsCards();
List<HWDiskStore> hwDiskStoreList = new SystemInfo().getHardware().getDiskStores();
List<Disk> diskJ = JSensors.get.components().disks;
// Initialize GPUs - match OSHI and JSensors data safely
int gpuCount = Math.min(gpuJ != null ? gpuJ.size() : 0,
graphicsCards != null ? graphicsCards.size() : 0);
for (int i = 0; i < gpuCount; i++) {
try {
gpus.add(new GPU(graphicsCards.get(i), gpuJ.get(i)));
} catch (Exception e) {
System.err.println("Error initializing GPU " + i + ": " + e.getMessage());
}
}
// Initialize Disks - match OSHI and JSensors data safely
int diskCount = Math.min(diskJ != null ? diskJ.size() : 0,
hwDiskStoreList != null ? hwDiskStoreList.size() : 0);
for (int i = 0; i < diskCount; i++) {
try {
disks.add(new PhysicalDisk(hwDiskStoreList.get(i), diskJ.get(i)));
} catch (Exception e) {
System.err.println("Error initializing Disk " + i + ": " + e.getMessage());
}
}
// If no JSensors disks but OSHI disks exist, create them without JSensors data
if ((diskJ == null || diskJ.isEmpty()) && hwDiskStoreList != null && !hwDiskStoreList.isEmpty()) {
for (HWDiskStore hwDisk : hwDiskStoreList) {
try {
disks.add(new PhysicalDisk(hwDisk, null));
} catch (Exception e) {
System.err.println("Error initializing Disk (OSHI only): " + e.getMessage());
}
}
}
System.out.println("ComponentManager initialized: " + gpus.size() + " GPUs, " + disks.size() + " Disks");
} catch (Exception e) {
System.err.println("Error initializing ComponentManager: " + e.getMessage());
e.printStackTrace();
}
}
public void updateAll()
{
for (GPU g : gpus) {
g.update();
}
for (PhysicalDisk d : disks) {
d.update();
}
cpu.update();
ram.update();
}
public String toJson()
{
Gson g = new Gson();
return "[" + g.toJson(cpu) + ", " + g.toJson(gpus) + ", " + g.toJson(ram) + ", " + g.toJson(disks) + "]";
}
}