Bläddra i källkod

Sensor system work

- Added fake sensor for unsupported OS:es
- Started working on a configuration GUI for sensor groups
Tankernn 8 år sedan
förälder
incheckning
5abad1f0b4

+ 6 - 5
src/main/java/eu/tankernn/grid/frame/GridControlPanel.java

@@ -36,8 +36,10 @@ public class GridControlPanel extends JFrame {
 
 	private JMenuBar menuBar = new JMenuBar();
 	private JMenu fileMenu = new JMenu("File"),
+			settingsMenu = new JMenu("Settings"),
 			profileMenu = new JMenu("Profiles");
 	private JMenuItem saveSettings = new JMenuItem("Save settings..."),
+			sensorConf = new JMenuItem("Configure sensors..."),
 			addProfile = new JMenuItem("Add profile...");
 
 	private FanPanel[] fanPanels;
@@ -62,11 +64,7 @@ public class GridControlPanel extends JFrame {
 	}
 
 	private void setPort(ItemEvent event) {
-		String selectedPort = (String) portMap.getSelectedItem();
-
-		getModel().getGrid().disconnect();
-
-		getModel().setGrid(selectedPort);
+		model.setGrid((String) portMap.getSelectedItem());
 	}
 
 	public GridControlPanel(GridControl control, ComputerModel model) {
@@ -76,6 +74,9 @@ public class GridControlPanel extends JFrame {
 		menuBar.add(fileMenu);
 		fileMenu.add(saveSettings);
 		saveSettings.addActionListener(e -> control.saveSettings());
+		menuBar.add(settingsMenu);
+		settingsMenu.add(sensorConf);
+		sensorConf.addActionListener(e -> new SensorConfig(model.getSensor()));
 		menuBar.add(profileMenu);
 		profileMenu.add(addProfile);
 		addProfile.addActionListener(e -> {

+ 128 - 0
src/main/java/eu/tankernn/grid/frame/SensorConfig.java

@@ -0,0 +1,128 @@
+package eu.tankernn.grid.frame;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.BoxLayout;
+import javax.swing.DefaultListModel;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JList;
+import javax.swing.JPanel;
+import javax.swing.border.EmptyBorder;
+import javax.swing.border.TitledBorder;
+
+import eu.tankernn.grid.model.sensor.Sensor;
+
+public class SensorConfig extends JDialog {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	private final JPanel contentPanel = new JPanel(), buttonPanel = new JPanel(), zonePanel = new JPanel();
+	private JList<String> availableList = new JList<>(), cpuList = new JList<>(), gpuList = new JList<>();
+	private JButton cpuAdd = new JButton("->"), cpuRem = new JButton("<-"), gpuAdd = new JButton("->"),
+			gpuRem = new JButton("<-");
+
+	/**
+	 * Create the dialog.
+	 */
+	public SensorConfig(Sensor sensor) {
+		setBounds(100, 100, 450, 300);
+		getContentPane().setLayout(new BorderLayout());
+		contentPanel.setLayout(new FlowLayout());
+		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
+
+		{
+			DefaultListModel<String> model = new DefaultListModel<>();
+			for (String s : sensor.getCpuSensors())
+				model.addElement(s);
+			cpuList.setModel(model);
+		}
+		{
+			DefaultListModel<String> model = new DefaultListModel<>();
+			for (String s : sensor.getGpuSensors())
+				model.addElement(s);
+			gpuList.setModel(model);
+		}
+		{
+			DefaultListModel<String> model = new DefaultListModel<>();
+			for (String s : sensor.getSensorNames().stream().filter(s -> !sensor.getCpuSensors().contains(s))
+					.filter(s -> !sensor.getGpuSensors().contains(s)).toArray(String[]::new))
+				model.addElement(s);
+			availableList.setModel(model);
+		}
+
+		availableList.setBorder(new TitledBorder("Available sensors:"));
+		cpuList.setBorder(new TitledBorder("CPU sensors:"));
+		gpuList.setBorder(new TitledBorder("GPU sensors:"));
+		
+		availableList.setPreferredSize(new Dimension(150, 400));
+		cpuList.setPreferredSize(new Dimension(150, 200));
+		gpuList.setPreferredSize(new Dimension(150, 200));
+		
+		zonePanel.setLayout(new BorderLayout());
+		zonePanel.add(cpuList, BorderLayout.NORTH);
+		zonePanel.add(gpuList, BorderLayout.SOUTH);
+		
+		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
+		buttonPanel.add(cpuAdd);
+		buttonPanel.add(cpuRem);
+		buttonPanel.add(gpuAdd);
+		buttonPanel.add(gpuRem);
+		
+		cpuAdd.addActionListener(a -> move(availableList, cpuList));
+		cpuRem.addActionListener(a -> move(cpuList, availableList));
+		gpuAdd.addActionListener(a -> move(availableList, gpuList));
+		gpuRem.addActionListener(a -> move(gpuList, availableList));
+		
+		contentPanel.add(availableList, BorderLayout.WEST);
+		contentPanel.add(buttonPanel, BorderLayout.CENTER);
+		contentPanel.add(zonePanel, BorderLayout.EAST);
+
+		getContentPane().add(contentPanel, BorderLayout.CENTER);
+		{
+			JPanel buttonPane = new JPanel();
+			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
+			getContentPane().add(buttonPane, BorderLayout.SOUTH);
+			{
+				JButton okButton = new JButton("OK");
+				okButton.addActionListener(a -> {
+					sensor.setCpuSensors(toList(cpuList));
+					sensor.setGpuSensors(toList(gpuList));
+					dispose();
+				});
+				buttonPane.add(okButton);
+				getRootPane().setDefaultButton(okButton);
+			}
+			{
+				JButton cancelButton = new JButton("Cancel");
+				cancelButton.addActionListener(a -> dispose());
+				buttonPane.add(cancelButton);
+			}
+		}
+		pack();
+		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+		setVisible(true);
+	}
+	
+	private List<String> toList(JList<String> jlist) {
+		List<String> list = new ArrayList<>();
+		for (int i = 0; i < jlist.getModel().getSize(); i++)
+			list.add(jlist.getModel().getElementAt(i));
+		return list;
+	}
+
+	private void move(JList<String> origin, JList<String> target) {
+		if (origin.getSelectedIndex() < 0)
+			return;
+		String name = origin.getSelectedValue();
+		((DefaultListModel<String>) origin.getModel()).remove(origin.getSelectedIndex());
+		((DefaultListModel<String>) target.getModel()).addElement(name);
+	}
+}

+ 2 - 0
src/main/java/eu/tankernn/grid/model/Communicator.java

@@ -22,6 +22,7 @@ public class Communicator {
 	 * @param selectedPort
 	 */
 	public void connect(SerialPort selectedPort) {
+		disconnect();
 		try {
 			serialPort = selectedPort;
 
@@ -44,6 +45,7 @@ public class Communicator {
 			}
 
 			System.err.println("Device did not respond correctly to ping.");
+			serialPort.closePort();
 		} catch (Exception e) {
 			System.out.println("Failed to open " + selectedPort.getSystemPortName() + ".");
 			e.printStackTrace();

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

@@ -40,8 +40,7 @@ public class ComputerModel {
 	 */
 	private int minSpeed = 30;
 
-	private List<FanSpeedProfile> defaultProfiles,
-			customProfiles = new ArrayList<>();
+	private List<FanSpeedProfile> defaultProfiles, customProfiles = new ArrayList<>();
 
 	/**
 	 *
@@ -89,7 +88,9 @@ public class ComputerModel {
 	}
 
 	private List<FanSpeedProfile> generateProfiles() {
-		return IntStream.range(30 / 5, 100 / 5 + 1).map(i -> i * 5).mapToObj(i -> new FanSpeedProfile(i + "%", new int[] { i })).collect(Collectors.toCollection(ArrayList::new));
+		return IntStream.range(30 / 5, 100 / 5 + 1).map(i -> i * 5)
+				.mapToObj(i -> new FanSpeedProfile(i + "%", new int[] { i }))
+				.collect(Collectors.toCollection(ArrayList::new));
 	}
 
 	/**
@@ -126,7 +127,8 @@ public class ComputerModel {
 	/**
 	 * Connects to the GRID on the port specified.
 	 *
-	 * @param selectedPort The COM port the GRID controller is located at
+	 * @param selectedPort
+	 *            The COM port the GRID controller is located at
 	 */
 	public void setGrid(String selectedPort) {
 		if (!portMap.containsKey(selectedPort)) {

+ 6 - 3
src/main/java/eu/tankernn/grid/model/GRID.java

@@ -22,10 +22,12 @@ public class GRID {
 	public void disconnect() {
 		communicator.disconnect();
 	}
-	
+
 	/**
 	 * Gets the fan at the specified index.
-	 * @param index The fan index (0-5)
+	 * 
+	 * @param index
+	 *            The fan index (0-5)
 	 * @return The fan object
 	 */
 	public Fan getFan(int index) {
@@ -41,7 +43,8 @@ public class GRID {
 	}
 
 	public void pollFans() {
-		fanStream().forEach(Fan::poll);
+		if (communicator.isConnected())
+			fanStream().forEach(Fan::poll);
 	}
 
 	public void updateFanSpeeds(double temp, int minSpeed) {

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

@@ -3,16 +3,12 @@ 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 {
@@ -37,16 +33,4 @@ public class LMSensor extends Sensor {
 		}
 	}
 
-	@Override
-	public double getCPUTemp() {
-		//TODO Make configurable
-		return temperatures.get("Core 0");
-	}
-
-	@Override
-	public double getGPUTemp() {
-		// TODO Make configurable
-		return temperatures.get("SYSTIN");
-	}
-
 }

+ 37 - 2
src/main/java/eu/tankernn/grid/model/sensor/Sensor.java

@@ -3,10 +3,17 @@ package eu.tankernn.grid.model.sensor;
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
 import java.lang.management.OperatingSystemMXBean;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public abstract class Sensor {
 	OperatingSystemMXBean bean;
 	public final int cpuCoreNumber;
+	protected Map<String, Double> temperatures = new HashMap<>();
+	private List<String> cpuSensors = new ArrayList<>(), gpuSensors = new ArrayList<>();
 	
 	public Sensor() {
 		bean = ManagementFactory.getOperatingSystemMXBean();
@@ -18,8 +25,36 @@ public abstract class Sensor {
 	public double getCpuLoad() {
 		return bean.getSystemLoadAverage();
 	}
+	
+	public Set<String> getSensorNames() {
+		return temperatures.keySet();
+	}
+
+	public double getCPUTemp() {
+		return getTemp(cpuSensors);
+	}
+
+	public double getGPUTemp() {
+		return getTemp(gpuSensors);
+	}
+	
+	private double getTemp(List<String> sensorNames) {
+		return sensorNames.stream().mapToDouble(temperatures::get).sum() / sensorNames.size();
+	}
+
+	public List<String> getCpuSensors() {
+		return cpuSensors;
+	}
 
-	public abstract double getCPUTemp();
+	public void setCpuSensors(List<String> cpuSensors) {
+		this.cpuSensors = cpuSensors;
+	}
 
-	public abstract double getGPUTemp();
+	public List<String> getGpuSensors() {
+		return gpuSensors;
+	}
+
+	public void setGpuSensors(List<String> gpuSensors) {
+		this.gpuSensors = gpuSensors;
+	}
 }

+ 11 - 1
src/main/java/eu/tankernn/grid/model/sensor/SensorFactory.java

@@ -1,5 +1,7 @@
 package eu.tankernn.grid.model.sensor;
 
+import java.io.IOException;
+
 import org.apache.commons.lang3.SystemUtils;
 
 public class SensorFactory {
@@ -9,7 +11,15 @@ public class SensorFactory {
 		} else if (SystemUtils.IS_OS_LINUX) {
 			return new LMSensor();
 		} else {
-			throw new UnsupportedOperationException(SystemUtils.OS_NAME + " is not a supported operating system.");
+			System.err.println(SystemUtils.OS_NAME + " is not a supported operating system.");
+			return new Sensor() {
+				
+				@Override
+				public void poll() throws IOException {
+					temperatures.put("Fake 0", 0d);
+					temperatures.put("Fake 100", 100d);
+				}
+			};
 		}
 	}
 }