Forráskód Böngészése

Fixed WindowsSensor and proper application shutdown

Tankernn 8 éve
szülő
commit
7684cd9604

+ 173 - 154
src/main/java/eu/tankernn/grid/Fan.java

@@ -1,154 +1,173 @@
-package eu.tankernn.grid;
-
-import java.util.function.BiFunction;
-
-import eu.tankernn.grid.model.Communicator;
-
-public class Fan {
-	private double voltage, current;
-	private int rpm, index;
-	private Communicator communicator;
-	private FanSpeedProfile profile;
-	private int speed = 0;
-
-	public Fan(Communicator communicator, int index) {
-		this.communicator = communicator;
-		this.index = index;
-		poll();
-		this.speed = (int) (100 * voltage / 12);
-	}
-
-	public void update(double temp, int minSpeed) {
-		int calcSpeed = profile.getSpeedPercentage(temp);
-		setFanSpeed(calcSpeed < minSpeed ? 0 : calcSpeed);
-	}
-
-	public void poll() {
-		pollAMP();
-		pollRPM();
-		pollVoltage();
-	}
-
-	/**
-	 * This method polls the Fan Amperage of the fan with index fan it first
-	 * send the poll command and reads the byte data from the buffer this byte
-	 * data is then converted to a double
-	 */
-	private void pollAMP() {
-		current = pollValue((byte) 0x85, (a, b) -> a + (double) b / 100);
-	}
-
-	/**
-	 * This method polls the Fan RPM of the fan with index fan it first send the
-	 * poll command and reads the byte data from the buffer this byte data is
-	 * then converted to an int
-	 */
-	private void pollRPM() {
-		rpm = pollValue((byte) 0x8A, (a, b) -> (double) ((a << 8) | b)).intValue();
-	}
-
-	/**
-	 * This method polls the voltage of all the fans it first send the poll
-	 * command and reads the byte data from the buffer this byte data is then
-	 * converted to a double
-	 */
-	private void pollVoltage() {
-		voltage = pollValue((byte) 0x84, (a, b) -> a + (double) b / 100);
-	}
-
-	private Double pollValue(byte commandByte, BiFunction<Integer, Integer, Double> resultConsumer) {
-		if (communicator.isConnected()) {
-			byte[] command = { commandByte, (byte) (index + 1) };
-
-			byte[] response = communicator.writeData(command);
-
-			return resultConsumer.apply((response[response.length - 2] & 0xFF),
-					(response[response.length - 1] & 0xff));
-		} else {
-			return 0d;
-		}
-	}
-
-	/**
-	 * this method calculates the voltage to set the fans at according to a
-	 * percentage of the maximum voltage 12V then it send the command to set
-	 * that voltage the voltages between 0 and 4 are not recognised so these are
-	 * converted to 4V the comma value of the commands are always rounded to .50
-	 * and .0
-	 * 
-	 * @param newSpeed
-	 */
-	public void setFanSpeed(int newSpeed) {
-		if (newSpeed == speed)
-			return;
-		// Spin up to 100 during first tick after being turned off
-		else if (speed == 0)
-			newSpeed = 100;
-		if (communicator.isConnected()) {
-			int firstByte, lastByte, wantedVoltage = 0;
-
-			// The voltages between 0 and 4 are not recognised by the grid so
-			// any voltage under 4 will still be 4 and from 0 it will be 0
-			if (newSpeed <= 0) {
-				firstByte = 0;
-				lastByte = 0;
-
-			} else if (newSpeed < 34) {
-				firstByte = 4;
-				lastByte = 0;
-			} else {
-				wantedVoltage = 1200 * newSpeed / 100;
-				firstByte = wantedVoltage / 100;
-				lastByte = (wantedVoltage - (firstByte * 100));
-
-				if (lastByte < 50) {
-					lastByte = 0x00;
-				} else {
-					lastByte = 0x50;
-				}
-			}
-
-			byte[] command = { 0x44, (byte) (index + 1), -64, 0x00, 0x00, (byte) firstByte, (byte) lastByte };
-
-			communicator.writeData(command);
-			speed = newSpeed;
-		}
-	}
-
-	public void setProfile(FanSpeedProfile profile) {
-		this.profile = profile;
-	}
-
-	/**
-	 * @return the voltage
-	 */
-	public double getVoltage() {
-		return voltage;
-	}
-
-	/**
-	 * @return the fanRPM
-	 */
-	public int getRPM() {
-		return rpm;
-	}
-
-	/**
-	 * @return the fanAMP
-	 */
-	public double getAMP() {
-		return current;
-	}
-
-	public double getWattage() {
-		return voltage * current;
-	}
-
-	public int getIndex() {
-		return index;
-	}
-
-	public FanSpeedProfile getProfile() {
-		return profile;
-	}
-}
+package eu.tankernn.grid;
+
+import java.io.IOException;
+import java.util.function.BiFunction;
+
+import eu.tankernn.grid.model.Communicator;
+
+public class Fan {
+	private double voltage, current;
+	private int rpm, index;
+	private Communicator communicator;
+	private FanSpeedProfile profile;
+	private int speed = 0;
+
+	public Fan(Communicator communicator, int index) {
+		this.communicator = communicator;
+		this.index = index;
+		try {
+			poll();
+		} catch (IOException e) {
+			e.printStackTrace();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+		this.speed = (int) (100 * voltage / 12);
+	}
+
+	public void update(double temp, int minSpeed) {
+		int calcSpeed = profile.getSpeedPercentage(temp);
+		try {
+			setFanSpeed(calcSpeed < minSpeed ? 0 : calcSpeed);
+		} catch (IOException | InterruptedException e) {
+			e.printStackTrace();
+		}
+	}
+
+	public void poll() throws IOException, InterruptedException {
+		pollAMP();
+		pollRPM();
+		pollVoltage();
+	}
+
+	/**
+	 * This method polls the Fan Amperage of the fan with index fan it first
+	 * send the poll command and reads the byte data from the buffer this byte
+	 * data is then converted to a double
+	 * @throws InterruptedException 
+	 * @throws IOException 
+	 */
+	private void pollAMP() throws IOException, InterruptedException {
+		current = pollValue((byte) 0x85, (a, b) -> a + (double) b / 100);
+	}
+
+	/**
+	 * This method polls the Fan RPM of the fan with index fan it first send the
+	 * poll command and reads the byte data from the buffer this byte data is
+	 * then converted to an int
+	 * @throws InterruptedException 
+	 * @throws IOException 
+	 */
+	private void pollRPM() throws IOException, InterruptedException {
+		rpm = pollValue((byte) 0x8A, (a, b) -> (double) ((a << 8) | b)).intValue();
+	}
+
+	/**
+	 * This method polls the voltage of all the fans it first send the poll
+	 * command and reads the byte data from the buffer this byte data is then
+	 * converted to a double
+	 * @throws InterruptedException 
+	 * @throws IOException 
+	 */
+	private void pollVoltage() throws IOException, InterruptedException {
+		voltage = pollValue((byte) 0x84, (a, b) -> a + (double) b / 100);
+	}
+
+	private Double pollValue(byte commandByte, BiFunction<Integer, Integer, Double> resultConsumer) throws IOException, InterruptedException {
+		if (communicator.isConnected()) {
+			byte[] command = { commandByte, (byte) (index + 1) };
+
+			byte[] response = communicator.writeData(command);
+
+			return resultConsumer.apply((response[response.length - 2] & 0xFF),
+					(response[response.length - 1] & 0xff));
+		} else {
+			return 0d;
+		}
+	}
+
+	/**
+	 * this method calculates the voltage to set the fans at according to a
+	 * percentage of the maximum voltage 12V then it send the command to set
+	 * that voltage the voltages between 0 and 4 are not recognised so these are
+	 * converted to 4V the comma value of the commands are always rounded to .50
+	 * and .0
+	 * 
+	 * @param newSpeed
+	 * @throws InterruptedException 
+	 * @throws IOException 
+	 */
+	public void setFanSpeed(int newSpeed) throws IOException, InterruptedException {
+		if (newSpeed == speed)
+			return;
+		// Spin up to 100 during first tick after being turned off
+		else if (speed == 0)
+			newSpeed = 100;
+		if (communicator.isConnected()) {
+			int firstByte, lastByte, wantedVoltage = 0;
+
+			// The voltages between 0 and 4 are not recognised by the grid so
+			// any voltage under 4 will still be 4 and from 0 it will be 0
+			if (newSpeed <= 0) {
+				firstByte = 0;
+				lastByte = 0;
+
+			} else if (newSpeed < 34) {
+				firstByte = 4;
+				lastByte = 0;
+			} else {
+				wantedVoltage = 1200 * newSpeed / 100;
+				firstByte = wantedVoltage / 100;
+				lastByte = (wantedVoltage - (firstByte * 100));
+
+				if (lastByte < 50) {
+					lastByte = 0x00;
+				} else {
+					lastByte = 0x50;
+				}
+			}
+
+			byte[] command = { 0x44, (byte) (index + 1), -64, 0x00, 0x00, (byte) firstByte, (byte) lastByte };
+
+			communicator.writeData(command);
+			speed = newSpeed;
+		}
+	}
+
+	public void setProfile(FanSpeedProfile profile) {
+		this.profile = profile;
+	}
+
+	/**
+	 * @return the voltage
+	 */
+	public double getVoltage() {
+		return voltage;
+	}
+
+	/**
+	 * @return the fanRPM
+	 */
+	public int getRPM() {
+		return rpm;
+	}
+
+	/**
+	 * @return the fanAMP
+	 */
+	public double getAMP() {
+		return current;
+	}
+
+	public double getWattage() {
+		return voltage * current;
+	}
+
+	public int getIndex() {
+		return index;
+	}
+
+	public FanSpeedProfile getProfile() {
+		return profile;
+	}
+}

+ 12 - 16
src/main/java/eu/tankernn/grid/GridControl.java

@@ -119,11 +119,7 @@ public class GridControl implements Runnable {
 		}
 		// Save misc. settings
 		try (Writer writer = new FileWriter(SETTINGS_PATH)) {
-			gson.toJson(new Settings(model.getGrid().getCommunicator().getPortName(),
-					model.getGrid().fanStream().map(f -> f.getProfile().getName()).toArray(String[]::new),
-					model.getSensor().getCpuSensors().stream().toArray(String[]::new),
-					model.getSensor().getGpuSensors().stream().toArray(String[]::new), pollingSpeed,
-					model.getMinSpeed(), this.startMinimized), writer);
+			gson.toJson(new Settings(model.getGrid().getCommunicator().getPortName(), model.getGrid().fanStream().map(f -> f.getProfile().getName()).toArray(String[]::new), model.getSensor().getCpuSensors().stream().toArray(String[]::new), model.getSensor().getGpuSensors().stream().toArray(String[]::new), pollingSpeed, model.getMinSpeed(), this.startMinimized), writer);
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
@@ -132,25 +128,25 @@ public class GridControl implements Runnable {
 	@Override
 	public void run() {
 		while (!t.isInterrupted()) {
-			model.poll();
-			model.compute();
-
-			if (frame != null)
-				frame.updateProperties();
-
 			try {
+				model.poll();
+				model.compute();
+
+				if (frame != null)
+					frame.updateProperties();
 				Thread.sleep(pollingSpeed);
 			} catch (InterruptedException ex) {
 				System.out.println("Thread was interrupted.");
 				return;
+			} catch (IOException e) {
+				e.printStackTrace();
 			}
 		}
 	}
 
 	/**
 	 * 
-	 * @param args
-	 *            the command line arguments
+	 * @param args the command line arguments
 	 */
 	public static void main(String[] args) {
 		try {
@@ -175,9 +171,9 @@ public class GridControl implements Runnable {
 		frame.dispose();
 		if (systemTray != null)
 			systemTray.shutdown();
-		for (Thread t : Thread.getAllStackTraces().keySet())
-			if (t.isAlive())
-				System.out.println(t);
+//		for (Thread t : Thread.getAllStackTraces().keySet())
+//			if (t.isAlive())
+//				System.out.println(t);
 	}
 
 	public void setPollingSpeed(int value) {

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

@@ -83,7 +83,7 @@ public class GridControlPanel extends JFrame {
 		settingsMenu.add(sensorConf);
 		sensorConf.addActionListener(e -> new SensorConfig(model.getSensor()));
 		settingsMenu.add(startMinimized);
-		startMinimized.addActionListener(e -> control.setStartMinimized(startMinimized.isEnabled()));
+		startMinimized.addActionListener(e -> control.setStartMinimized(startMinimized.isSelected()));
 		menuBar.add(profileMenu);
 		profileMenu.add(addProfile);
 		addProfile.addActionListener(e -> addProfile());

+ 16 - 14
src/main/java/eu/tankernn/grid/model/Communicator.java

@@ -24,7 +24,7 @@ public class Communicator {
 	public void connect(SerialPort selectedPort) {
 		if (selectedPort.equals(serialPort) && isConnected())
 			return; // Already connected
-		
+
 		disconnect();
 		try {
 			serialPort = selectedPort;
@@ -61,7 +61,14 @@ public class Communicator {
 	 * @return If the ping was successful (the device responded correctly)
 	 */
 	private boolean ping() {
-		byte[] buffer = writeData(new byte[] { (byte) 0xc0 });
+		byte[] buffer = null;
+		try {
+			buffer = writeData(new byte[] { (byte) 0xc0 });
+		} catch (IOException e) {
+			e.printStackTrace();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
 		return buffer[0] == 0x21;
 	}
 
@@ -109,19 +116,14 @@ public class Communicator {
 	 * 
 	 * @param command an array of bytes as a command
 	 * @return The response data from the device
+	 * @throws IOException 
+	 * @throws InterruptedException 
 	 */
-	public byte[] writeData(byte[] command) {
-		try {
-			sleep(50);
-			output.write(command);
-			sleep(50);
-			return serialRead();
-		} catch (IOException e) {
-			System.out.println("Failed to write data. (" + e.toString() + ")");
-		} catch (InterruptedException e) {
-			// Just ignore
-		}
-		return new byte[32];
+	public byte[] writeData(byte[] command) throws IOException, InterruptedException {
+		sleep(50);
+		output.write(command);
+		sleep(50);
+		return serialRead();
 	}
 
 	public boolean isConnected() {

+ 10 - 16
src/main/java/eu/tankernn/grid/model/ComputerModel.java

@@ -1,5 +1,6 @@
 package eu.tankernn.grid.model;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -40,7 +41,8 @@ public class ComputerModel {
 	 */
 	private int minSpeed = 30;
 
-	private List<FanSpeedProfile> defaultProfiles, customProfiles = new ArrayList<>();
+	private List<FanSpeedProfile> defaultProfiles,
+			customProfiles = new ArrayList<>();
 
 	/**
 	 * Populates the port map, generates the default speed profiles and
@@ -63,28 +65,21 @@ public class ComputerModel {
 	 * identifiers in the map with their name as key.
 	 */
 	public void scanPorts() {
-		portMap = Arrays.stream(SerialPort.getCommPorts())
-				.collect(Collectors.toMap(SerialPort::getSystemPortName, Function.identity()));
+		portMap = Arrays.stream(SerialPort.getCommPorts()).collect(Collectors.toMap(SerialPort::getSystemPortName, Function.identity()));
 	}
 
 	/**
 	 * Polls the GRID and the sensor.
+	 * @throws IOException 
+	 * @throws InterruptedException 
 	 */
-	public void poll() {
+	public void poll() throws IOException, InterruptedException {
 		grid.pollFans();
-
-		try {
-			sensor.poll();
-		} catch (Exception ex) {
-			System.out.println("Temperature polling failed");
-			ex.printStackTrace();
-		}
+		sensor.poll();
 	}
 
 	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));
 	}
 
 	/**
@@ -121,8 +116,7 @@ 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)) {

+ 4 - 2
src/main/java/eu/tankernn/grid/model/GRID.java

@@ -1,5 +1,6 @@
 package eu.tankernn.grid.model;
 
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
@@ -42,9 +43,10 @@ public class GRID {
 		return fanStream().mapToDouble(Fan::getWattage).sum();
 	}
 
-	public void pollFans() {
+	public void pollFans() throws IOException, InterruptedException {
 		if (communicator.isConnected())
-			fanStream().forEach(Fan::poll);
+			for (Fan f : fans)
+				f.poll();
 	}
 
 	public void updateFanSpeeds(double temp, int minSpeed) {

+ 10 - 3
src/main/java/eu/tankernn/grid/model/sensor/WindowsSensor.java

@@ -1,6 +1,7 @@
 package eu.tankernn.grid.model.sensor;
 
 import java.io.IOException;
+import java.util.Arrays;
 
 /**
  *
@@ -11,8 +12,14 @@ import java.io.IOException;
  */
 public class WindowsSensor extends Sensor {
 	public void poll() throws IOException {
-		String[] sensors = jWMI.getWMISensorList("Temperature").split(",");
-		for (String s : sensors)
-			temperatures.put(s, Double.parseDouble(jWMI.getWMIValue("Temperature", s)));
+		String sensorList = jWMI.getWMISensorList("Temperature");
+		String[] sensors = sensorList.split(", ");
+		System.out.println(Arrays.toString(sensors));
+		for (String s : sensors) {
+			String value = jWMI.getWMIValue("Temperature", s);
+			if (!value.isEmpty())
+				temperatures.put(s, Double.parseDouble(value));
+		}
+		System.out.println(temperatures.toString());
 	}
 }