Kaynağa Gözat

Polling overhaul

Tankernn 8 yıl önce
ebeveyn
işleme
43c28723f2

+ 22 - 36
src/eu/tankernn/grid/Fan.java

@@ -1,5 +1,7 @@
 package eu.tankernn.grid;
 
+import java.util.function.BiFunction;
+
 import eu.tankernn.grid.model.GRID;
 
 public class Fan {
@@ -22,8 +24,8 @@ public class Fan {
 	}
 
 	public void poll() {
-		pollFanAMP();
-		pollFanRPM();
+		pollAMP();
+		pollRPM();
 		pollVoltage();
 	}
 
@@ -31,41 +33,20 @@ public class Fan {
 	 * 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
-	 * 
-	 * @param fan the index of the fan
 	 */
-	public void pollFanAMP() {
-		if (grid.getCommunicator().isConnected()) {
-			// 0x85 = -123
-			byte[] command = { -123, (byte) (index + 1) };
-
-			grid.getCommunicator().writeData(command);
-
-			current = (double) (((int) grid.getCommunicator().getSecondToLast() & 0xFF) + (((double) (grid.getCommunicator().getlast() & 0xff) / 100)));
-		} else {
-			current = 0d;
-		}
+	private void pollAMP() {
+		// 0x85 = -123
+		current = pollValue((byte) 0x85, (a, b) -> (double) a + 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
-	 * 
-	 * @param fan the index of the fan
 	 */
-	public void pollFanRPM() {
-		if (grid.getCommunicator().isConnected()) {
-			// 0x8A = -118
-			byte[] command = { -118, (byte) (index + 1) };
-
-			grid.getCommunicator().writeData(command);
-
-			rpm = (((int) (grid.getCommunicator().getSecondToLast() & 0xFF) << 8) | ((grid.getCommunicator().getlast() & 0xFF)));
-		} else {
-			rpm = 0;
-
-		}
+	private void pollRPM() {
+		// 0x8A = -118
+		rpm = pollValue((byte) 0x8A, (a, b) -> (double) ((a << 8) | b)).intValue();
 	}
 
 	/**
@@ -73,16 +54,21 @@ public class Fan {
 	 * command and reads the byte data from the buffer this byte data is then
 	 * converted to a double
 	 */
-	public void pollVoltage() {
+	private void pollVoltage() {
+		// 0x84 = -124
+		voltage = pollValue((byte) 0x84, (a, b) -> (double) a + b / 100);
+	}
+
+	private Double pollValue(byte commandByte, BiFunction<Integer, Integer, Double> resultConsumer) {
 		if (grid.getCommunicator().isConnected()) {
-			// 0x84 = -124
-			byte[] command = { -124, (byte) (index + 1) };
+			byte[] command = { commandByte, (byte) (index + 1) };
 
-			grid.getCommunicator().writeData(command);
+			byte[] response = grid.getCommunicator().writeData(command);
 
-			voltage = (double) (((int) grid.getCommunicator().getSecondToLast() & 0xFF) + (((double) (grid.getCommunicator().getlast() & 0xff) / 100)));
+			return resultConsumer.apply((response[response.length - 2] & 0xFF),
+					(response[response.length - 1] & 0xff));
 		} else {
-			voltage = 0d;
+			return 0d;
 		}
 	}
 
@@ -114,7 +100,7 @@ public class Fan {
 				firstByte = 4;
 				lastByte = 0;
 			} else {
-				wantedVoltage = (1200 * newSpeed) / 100;
+				wantedVoltage = 1200 * newSpeed / 100;
 				firstByte = wantedVoltage / 100;
 				lastByte = (wantedVoltage - (firstByte * 100));
 

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

@@ -22,13 +22,6 @@ public class Communicator {
 	private InputStream input = null;
 	private OutputStream output = null;
 
-	// Buffer
-	private int leng;
-	private byte[] buffer;
-
-	// a string for recording what goes on in the program
-	String logText = "";
-
 	/**
 	 *
 	 * This method searches for COM ports on the system and saves their
@@ -68,8 +61,7 @@ public class Communicator {
 			// logging
 			System.out.println(selectedPort + " opened successfully.");
 		} catch (Exception e) {
-			logText = "Failed to open " + selectedPort + "(" + e.toString() + ")";
-			System.out.println(logText);
+			System.out.println("Failed to open " + selectedPort + "(" + e.toString() + ")");
 			e.printStackTrace();
 		}
 	}
@@ -97,7 +89,7 @@ public class Communicator {
 	}
 
 	private boolean ping() {
-		writeData(new byte[] { (byte) 0xc0 });
+		byte[] buffer = writeData(new byte[] { (byte) 0xc0 });
 		return buffer[0] == 0x21;
 	}
 
@@ -115,8 +107,7 @@ public class Communicator {
 			serialPort.closePort();
 			System.out.println("Disconnected.");
 		} catch (IOException e) {
-			logText = "Failed to close " + serialPort.getSystemPortName() + "(" + e.toString() + ")";
-			System.out.println(logText);
+			System.err.println("Failed to close " + serialPort.getSystemPortName() + "(" + e.toString() + ")");
 		}
 	}
 
@@ -131,69 +122,40 @@ public class Communicator {
 	 * 
 	 */
 	private byte[] serialRead() throws InterruptedException {
+		// This prevents commands from being sent too soon
+		sleep(50);
 		try {
 			if (input.available() > 0) {
-				try {
-					buffer = (new byte[32]);
-					leng = input.read(buffer);
-				} catch (Exception e) {
-					System.out.println("Failed to read data. (" + e.toString() + ")");
-				}
+				byte[] buffer = new byte[input.available()];
+				input.read(buffer);
+				return buffer;
 			} else {
-				System.out.println("Failed to read data. No data to read");
+				System.err.println("Failed to read data. No data to read");
 			}
-
 		} catch (IOException e) {
-			logText = "Failed to read data. (" + e.toString() + ")";
-			System.out.println(logText);
+			System.err.println("Failed to read data. (" + e.toString() + ")");
 		}
-		// This prevents commands from being sent too soon
-		sleep(50);
-		return buffer;
+		return new byte[32];
 	}
 
 	/**
 	 * This method sends a byte array command over the serial communication
 	 * afterwards the method calls the read method
 	 * 
-	 * @param command an array of bytes as a command
+	 * @param command
+	 *            an array of bytes as a command
 	 */
 	public byte[] writeData(byte[] command) {
 		try {
 			output.write(command);
 			sleep(50);
 			return serialRead();
-		} catch (Exception e) {
-			logText = "Failed to write data. (" + e.toString() + ")";
-			System.out.println(logText);
+		} catch (InterruptedException | IOException e) {
+			System.out.println("Failed to write data. (" + e.toString() + ")");
 			return new byte[32];
 		}
 	}
 
-	/**
-	 *
-	 * @return The second to last byte of the buffer
-	 */
-	public byte getSecondToLast() {
-		if (leng >= 2) {
-			return buffer[leng - 2];
-		} else {
-			return 0;
-		}
-	}
-
-	/**
-	 *
-	 * @return The last byte of the buffer
-	 */
-	public byte getlast() {
-		if (leng >= 2) {
-			return buffer[leng - 1];
-		} else {
-			return 0;
-		}
-	}
-
 	/**
 	 * @return the portMap
 	 */
@@ -204,7 +166,7 @@ public class Communicator {
 	public boolean isConnected() {
 		return serialPort != null && serialPort.isOpen();
 	}
-	
+
 	public String getPortName() {
 		return serialPort.getSystemPortName();
 	}