Преглед изворни кода

Linux sensor support

Temperature readings are starting to come along in Linux. The sensor
names are currently hard-coded, but a configuration GUI is coming.
frans пре 8 година
родитељ
комит
8a6b80e6db

+ 7 - 2
pom.xml

@@ -19,7 +19,12 @@
 			<artifactId>gson</artifactId>
 			<version>2.8.0</version>
 		</dependency>
-
+		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
+		<dependency>
+			<groupId>org.apache.commons</groupId>
+			<artifactId>commons-lang3</artifactId>
+			<version>3.0</version>
+		</dependency>
 	</dependencies>
 
 	<build>
@@ -62,7 +67,7 @@
 			</plugin>
 		</plugins>
 	</build>
-	
+
 	<properties>
 		<build.number>SNAPSHOT</build.number>
 	</properties>

+ 3 - 7
src/main/java/eu/tankernn/grid/frame/GridControlPanel.java

@@ -51,12 +51,10 @@ public class GridControlPanel extends JFrame {
 
 	private JLabel CPULabel = new JLabel("CPU:");
 
-	private JLabel CPULabelMax = new JLabel("CPU max");
+	private JLabel CPULoadLabel = new JLabel("CPU load");
 
 	private JLabel GPULabel = new JLabel("GPU:");
 
-	private JLabel GPULabelMax = new JLabel("GPU max");
-
 	private JLabel PowerLabel = new JLabel("Power");
 
 	private void setMinRPM(ChangeEvent event) {
@@ -116,8 +114,7 @@ public class GridControlPanel extends JFrame {
 		infoPanel.setLayout(new GridLayout(3, 2));
 		infoPanel.add(CPULabel);
 		infoPanel.add(GPULabel);
-		infoPanel.add(CPULabelMax);
-		infoPanel.add(GPULabelMax);
+		infoPanel.add(CPULoadLabel);
 		infoPanel.add(PowerLabel);
 		infoPanel.add(labelledComponent("Minimum speed (%): ", minSpeed));
 		this.add(infoPanel, BorderLayout.SOUTH);
@@ -167,9 +164,8 @@ public class GridControlPanel extends JFrame {
 		// \u00B0 = "Degree sign"
 		CPULabel.setText("CPU: " + df.format(getModel().getSensor().getCPUTemp()) + " \u00B0C");
 		PowerLabel.setText("Total power: " + df.format(getModel().getGrid().getTotalWattage()) + " W");
-		CPULabelMax.setText("CPU: " + df.format(getModel().getSensor().getCpuMax()) + " \u00B0C Max");
+		CPULoadLabel.setText("CPU load: " + df.format(getModel().getSensor().getCpuLoad()) + " %");
 		GPULabel.setText("GPU: " + df.format(getModel().getSensor().getGPUTemp()) + " \u00B0C");
-		GPULabelMax.setText("GPU: " + df.format(getModel().getSensor().getGpuMax()) + " \u00B0C Max");
 
 		for (FanPanel p : fanPanels)
 			p.update();

+ 4 - 5
src/main/java/eu/tankernn/grid/model/ComputerModel.java

@@ -12,6 +12,8 @@ import java.util.stream.Stream;
 import com.fazecast.jSerialComm.SerialPort;
 
 import eu.tankernn.grid.FanSpeedProfile;
+import eu.tankernn.grid.model.sensor.Sensor;
+import eu.tankernn.grid.model.sensor.SensorFactory;
 
 /**
  * This model contains two main data members as well as some data members used
@@ -51,7 +53,7 @@ public class ComputerModel {
 		defaultProfiles = generateProfiles();
 
 		try {
-			sensor = new Sensor();
+			sensor = SensorFactory.getSensor();
 		} catch (Exception ex) {
 			Logger.getLogger(ComputerModel.class.getName()).log(Level.SEVERE, "Failed to init sensor.", ex);
 		}
@@ -79,10 +81,7 @@ public class ComputerModel {
 		grid.pollFans();
 
 		try {
-			getSensor().pollCPUTemp();
-			getSensor().pollCPUMax();
-			getSensor().pollGPUTemp();
-			getSensor().pollGPUMax();
+			sensor.poll();
 		} catch (Exception ex) {
 			System.out.println("Temperature polling failed");
 			ex.printStackTrace();

+ 0 - 152
src/main/java/eu/tankernn/grid/model/Sensor.java

@@ -1,152 +0,0 @@
-package eu.tankernn.grid.model;
-
-import java.io.IOException;
-
-/**
- *
- * This class is a model for a collection of sensors
- * It contains a datamembers for some specific sensors as well as members for lists of sensors of a specific type.
- * each member has a getter but no setter.
- * Instead of the setter the members (except for the lists) have a poll function which polls the value of the member using the jWMI class 
- * 
- * @author Roel
- */
-public class Sensor {
-    private String[] temperatureSensorList;
-    private String[] loadSensorList;
-    private int cpuCoreNumber;
-    private double cpuLoad;
-    private double cpuPackageTemp;
-    private double cpuMax;
-    private double gpuTemp;
-    private double gpuMax;
-
-    /**
-     * The constructor for this class
-     * At the start the lists of sensors are made with the help of the jWMI class
-     * Then the number of cores of the system is calculated
-     * @throws IOException when the WMI value can't be obtained
-     */
-    public Sensor() throws IOException {
-        temperatureSensorList = jWMI.getWMISensorList("Temperature").split(", ");
-        loadSensorList = jWMI.getWMISensorList("Load").split(", ");
-        //System.out.println(Arrays.toString(temperatureSensorList));
-        //System.out.println(Arrays.toString(loadSensorList));
-
-        //Init for cpuCoreNumber
-        cpuCoreNumber = 0;
-        for (String temperatureSensorList1 : loadSensorList) {
-            if (temperatureSensorList1.contains("CPU Core #")) {
-                int tempNumber;
-                // gets the int value of the number after #
-                tempNumber = Integer.parseInt(temperatureSensorList1.substring(10, 11).trim());
-                if (tempNumber > cpuCoreNumber) {
-                    cpuCoreNumber = tempNumber;
-                }
-            }
-        }
-    }
-
-    /**
-     *  This method polls the value of the CPU Load sensor
-     * @throws Exception
-     */
-    public void pollCPULoad() throws Exception {
-        cpuLoad = Double.parseDouble(jWMI.getWMIValue("Load", "CPU Total"));
-
-    }
-    
-    /**
-     *  This method polls the value of the CPU Load sensor
-     * @throws NumberFormatException 
-     * @throws Exception
-     */
-    public void pollCPUMax() throws NumberFormatException, Exception {
-        cpuMax = Double.parseDouble(jWMI.getWMIValue("Temperature", "CPU Package", "Max"));
-
-    }
-    
-    /**
-     *  This method polls the value of the CPU Load sensor
-     * @throws Exception
-     */
-    public void pollGPUMax() throws Exception {
-        gpuMax = Double.parseDouble(jWMI.getWMIValue("Temperature", "GPU Core", "Max"));
-
-    }
-    
-    /**
-     * This method polls the value of the GPU Temperature sensor
-     * @throws Exception
-     */
-    public void pollGPUTemp() throws Exception {
-        gpuTemp = Double.parseDouble(jWMI.getWMIValue("Temperature", "GPU Core"));
-
-    }
-
-    /**
-     * This method polls the value of the CPU Temperature sensor
-     * @throws Exception
-     */
-    public void pollCPUTemp() throws Exception {
-        cpuPackageTemp = Double.parseDouble(jWMI.getWMIValue("Temperature", "CPU Package"));
-    }
-
-    /**
-     * @return the temperatureSensorList
-     */
-    public String[] getTemperatureSensorList() {
-        return temperatureSensorList;
-    }
-
-    /**
-     * @return the loadSensorList
-     */
-    public String[] getLoadSensorList() {
-        return loadSensorList;
-    }
-
-    /**
-     * @return the cpuCoreNumber
-     */
-    public int getCpuCoreNumber() {
-        return cpuCoreNumber;
-    }
-
-   
-    /**
-     * @return the cpuLoad
-     */
-    public double getCpuLoad() {
-        return cpuLoad;
-    }
-
-    /**
-     * @return the cpuPackageTemp
-     */
-    public double getCPUTemp() {
-        return cpuPackageTemp;
-    }
-
-    /**
-     * @return the gpuTemp
-     */
-    public double getGPUTemp() {
-        return gpuTemp;
-    }
-
-    /**
-     * @return the cpuMax
-     */
-    public double getCpuMax() {
-        return cpuMax;
-    }
-    
-     /**
-     * @return the cpuMax
-     */
-    public double getGpuMax() {
-        return gpuMax;
-    }
-
-}

+ 52 - 0
src/main/java/eu/tankernn/grid/model/sensor/LMSensor.java

@@ -0,0 +1,52 @@
+package eu.tankernn.grid.model.sensor;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class LMSensor extends Sensor {
+	
+	public static final String REGEX = "(.*):\\s*([\\+\\-][\\d\\.]*).C";
+	
+	Map<String, Double> temperatures = new HashMap<>();
+
+	@Override
+	public void poll() throws IOException {
+		Process proc = Runtime.getRuntime().exec("sensors -A");
+		try {
+			proc.waitFor();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+			return;
+		}
+
+		BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+		Pattern p = Pattern.compile(REGEX, Pattern.MULTILINE);
+		StringBuilder sb = new StringBuilder();
+		String line;
+		while ((line = stdoutReader.readLine()) != null) {
+			sb.append(line + '\n');
+		}
+		Matcher m = p.matcher(sb);
+		while (m.find()) {
+			temperatures.put(m.group(1), Double.valueOf(m.group(2)));
+		}
+	}
+
+	@Override
+	public double getCPUTemp() {
+		//TODO Make configurable
+		return temperatures.get("Core 0");
+	}
+
+	@Override
+	public double getGPUTemp() {
+		// TODO Make configurable
+		return temperatures.get("SYSTIN");
+	}
+
+}

+ 25 - 0
src/main/java/eu/tankernn/grid/model/sensor/Sensor.java

@@ -0,0 +1,25 @@
+package eu.tankernn.grid.model.sensor;
+
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
+
+public abstract class Sensor {
+	OperatingSystemMXBean bean;
+	public final int cpuCoreNumber;
+	
+	public Sensor() {
+		bean = ManagementFactory.getOperatingSystemMXBean();
+		cpuCoreNumber = bean.getAvailableProcessors();
+	}
+	
+	public abstract void poll() throws IOException;
+	
+	public double getCpuLoad() {
+		return bean.getSystemLoadAverage();
+	}
+
+	public abstract double getCPUTemp();
+
+	public abstract double getGPUTemp();
+}

+ 15 - 0
src/main/java/eu/tankernn/grid/model/sensor/SensorFactory.java

@@ -0,0 +1,15 @@
+package eu.tankernn.grid.model.sensor;
+
+import org.apache.commons.lang3.SystemUtils;
+
+public class SensorFactory {
+	public static Sensor getSensor() {
+		if (SystemUtils.IS_OS_WINDOWS) {
+			return new WindowsSensor();
+		} else if (SystemUtils.IS_OS_LINUX) {
+			return new LMSensor();
+		} else {
+			throw new UnsupportedOperationException(SystemUtils.OS_NAME + " is not a supported operating system.");
+		}
+	}
+}

+ 51 - 0
src/main/java/eu/tankernn/grid/model/sensor/WindowsSensor.java

@@ -0,0 +1,51 @@
+package eu.tankernn.grid.model.sensor;
+
+import java.io.IOException;
+
+/**
+ *
+ * This class is a model for a collection of sensors It contains a datamembers
+ * for some specific sensors as well as members for lists of sensors of a
+ * specific type. each member has a getter but no setter. Instead of the setter
+ * the members (except for the lists) have a poll function which polls the value
+ * of the member using the jWMI class
+ * 
+ * @author Roel
+ */
+public class WindowsSensor extends Sensor {
+
+	private double cpuPackageTemp;
+	private double gpuTemp;
+
+	public void poll() throws IOException {
+		pollCPUTemp();
+		pollGPUTemp();
+	}
+
+	/**
+	 * This method polls the value of the GPU Temperature sensor
+	 * 
+	 * @throws Exception
+	 */
+	public void pollGPUTemp() throws IOException {
+		gpuTemp = Double.parseDouble(jWMI.getWMIValue("Temperature", "GPU Core"));
+	}
+
+	/**
+	 * This method polls the value of the CPU Temperature sensor
+	 * 
+	 * @throws Exception
+	 */
+	public void pollCPUTemp() throws IOException {
+		cpuPackageTemp = Double.parseDouble(jWMI.getWMIValue("Temperature", "CPU Package"));
+	}
+
+	public double getCPUTemp() {
+		return cpuPackageTemp;
+	}
+
+	public double getGPUTemp() {
+		return gpuTemp;
+	}
+
+}

+ 1 - 1
src/main/java/eu/tankernn/grid/model/jWMI.java → src/main/java/eu/tankernn/grid/model/sensor/jWMI.java

@@ -1,4 +1,4 @@
-package eu.tankernn.grid.model;
+package eu.tankernn.grid.model.sensor;
 
 import java.io.BufferedReader;
 import java.io.File;