Skip to content

Commit

Permalink
Some fixes and adjustements
Browse files Browse the repository at this point in the history
  • Loading branch information
profesorfalken committed Jun 20, 2016
1 parent 4b004c1 commit bb21424
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
43 changes: 25 additions & 18 deletions src/main/java/com/profesorfalken/jsensors/JSensors.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public Components components() {
}

public static void main(String[] args) {
System.out.println("Scanning sensors data...");
Map<String, String> overriddenConfig = new HashMap<String, String>();
for (final String arg : args) {
if ("--debug".equals(arg)) {
Expand All @@ -84,35 +85,41 @@ public static void main(String[] args) {

Components components = JSensors.get.config(overriddenConfig).components();

Cpu cpu = components.cpu;
if (cpu != null) {
System.out.println("Found CPU component " + cpu.name);
readComponent(cpu);
List<Cpu> cpus = components.cpus;
if (cpus != null) {
for (final Cpu cpu : cpus) {
System.out.println("Found CPU component: " + cpu.name);
readComponent(cpu);
}
}

Gpu gpu = components.gpu;
if (gpu != null) {
System.out.println("Found GPU component " + gpu.name);
readComponent(gpu);

List<Gpu> gpus = components.gpus;
if (gpus != null) {
for (final Gpu gpu : gpus) {
System.out.println("Found GPU component: " + gpu.name);
readComponent(gpu);
}
}

Disk disk = components.disk;
if (disk != null) {
System.out.println("Found disk component " + disk.name);
readComponent(disk);

List<Disk> disks = components.disks;
if (disks != null) {
for (final Disk disk : disks) {
System.out.println("Found disk component: " + disk.name);
readComponent(disk);
}
}
}

private static void readComponent(Component component) {
if (component.sensors != null) {
System.out.println("Sensors: ");
List<Temperature> temps = component.sensors.temperatures;

List<Temperature> temps = component.sensors.temperatures;
for (final Temperature temp : temps) {
System.out.println(temp.name + ": " + temp.value + " C");
}
List<Fan> fans = component.sensors.fans;

List<Fan> fans = component.sensors.fans;
for (final Fan fan : fans) {
System.out.println(fan.name + ": " + fan.value + " RPM");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ public SensorsManager debugMode(boolean mode) {
protected abstract String getSensorsData();

public Components getComponents() {
Cpu cpu = null;
Gpu gpu = null;
Disk disk = null;
List<Cpu> cpus = new ArrayList<Cpu>();
List<Gpu> gpus = new ArrayList<Gpu>();
List<Disk> disks = new ArrayList<Disk>();

String normalizedSensorsData = getSensorsData();

String[] componentsData = normalizedSensorsData.split("\\[COMPONENT\\]\\r?\\n");

for (final String componentData : componentsData) {
if (componentData.startsWith("CPU")) {
cpu = getCpu(componentData);
cpus.add(getCpu(componentData));
} else if (componentData.startsWith("GPU")) {
gpu = getGpu(componentData);
gpus.add(getGpu(componentData));
} else if (componentData.startsWith("DISK")) {
disk = getDisk(componentData);
disks.add(getDisk(componentData));
}
}

return new Components(cpu, gpu, disk);
return new Components(cpus, gpus, disks);
}

private Cpu getCpu(String cpuData) {
Expand Down Expand Up @@ -103,7 +103,7 @@ private Sensors getSensors(String componentData) {
} else if (dataLine.startsWith("Fan")) {
String[] data = dataLine.split(":");
Fan fan = new Fan(data[0].trim(),
data[1].trim().length() > 0 ? Double.valueOf(data[1].trim()) : 0.0);
data[1].trim().length() > 0 ? Integer.valueOf(data[1].trim()) : 0);
fans.add(fan);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
*/
package com.profesorfalken.jsensors.model.components;

import java.util.List;

/**
*
* @author Javier Garcia Alonso
*/
public class Components {
public final Cpu cpu;
public final Gpu gpu;
public final Disk disk;
public final List<Cpu> cpus;
public final List<Gpu> gpus;
public final List<Disk> disks;

public Components(Cpu cpu, Gpu gpu, Disk disk) {
this.cpu = cpu;
this.gpu = gpu;
this.disk = disk;
public Components(List<Cpu> cpus, List<Gpu> gpus, List<Disk> disks) {
this.cpus = cpus;
this.gpus = gpus;
this.disks = disks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
*/
public class Fan {
public final String name;
public final Double value;
public final Integer value;

public Fan(String name, Double value) {
public Fan(String name, Integer value) {
this.name = name;
this.value = value;
}
Expand Down
21 changes: 18 additions & 3 deletions src/test/java/com/profesorfalken/jsensors/JSensorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -96,8 +97,12 @@ public void testCpu() throws Exception{
logger.info("Testing CPU sensors");

//Get CPU component
Cpu cpu = getJSensorsStub(TESTSET_1).components().cpu;
List<Cpu> cpus = getJSensorsStub(TESTSET_1).components().cpus;

assertNotNull("Cannot recover CPU data", cpus);
assertTrue("No CPUs found", cpus.size() > 0);

Cpu cpu = cpus.get(0);
assertNotNull("Cannot recover CPU data", cpu);

assertNotNull("No CPU name", cpu.name);
Expand Down Expand Up @@ -135,8 +140,13 @@ public void testGpu() throws Exception{
logger.info("Testing GPU sensors");

//Get GPU component
Gpu gpu = getJSensorsStub(TESTSET_1).components().gpu;
//Get CPU component
List<Gpu> gpus = getJSensorsStub(TESTSET_1).components().gpus;

assertNotNull("Cannot recover GPU data", gpus);
assertTrue("No GPUs found", gpus.size() > 0);

Gpu gpu = gpus.get(0);
assertNotNull("Cannot recover GPU data", gpu);

assertNotNull("No GPU name", gpu.name);
Expand Down Expand Up @@ -174,8 +184,13 @@ public void testDisk() throws Exception{
logger.info("Testing CPU sensors");

//Get Disk component
Disk disk = getJSensorsStub(TESTSET_1).components().disk;
//Get CPU component
List<Disk> disks = getJSensorsStub(TESTSET_1).components().disks;

assertNotNull("Cannot recover Disk data", disks);
assertTrue("No Disks found", disks.size() > 0);

Disk disk = disks.get(0);
assertNotNull("Cannot recover Disk data", disk);

assertNotNull("No Disk name", disk.name);
Expand Down

0 comments on commit bb21424

Please sign in to comment.