From 88967419c67b76d6111381855cd03070fea5ad70 Mon Sep 17 00:00:00 2001 From: cs-powell <142438185+cs-powell@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:49:01 -0500 Subject: [PATCH] documents reformatted --- .../CognitiveModel/ModelFiles/Action.java | 61 +- .../CognitiveModel/ModelFiles/ActionType.java | 4 +- .../CognitiveModel/ModelFiles/Delay.java | 9 +- .../CognitiveModel/ModelFiles/MindQueue.java | 30 +- .../CognitiveModel/ModelFiles/Model.java | 137 ++-- .../CognitiveModel/ModelFiles/Motor.java | 50 +- .../CognitiveModel/ModelFiles/MotorType.java | 4 +- .../CognitiveModel/ModelFiles/Process.java | 5 +- .../CognitiveModel/ModelFiles/Tests.java | 3 +- .../CognitiveModel/ModelFiles/Vision.java | 17 +- .../ModelFiles/XPlaneConnect.java | 665 +++++++++--------- Java/ProjectModels/Main.java | 47 +- 12 files changed, 490 insertions(+), 542 deletions(-) diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/Action.java b/Java/ProjectModels/CognitiveModel/ModelFiles/Action.java index 079c656..b96df3b 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/Action.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/Action.java @@ -1,41 +1,36 @@ package ModelFiles; - public abstract class Action { + Integer priority; // Priority of the Task + Integer delay; // Potential Delay before starting the task + String targetDREF; + Integer taskTime; // How long should the task take + boolean taskComplete; // Is the task completed? + ActionType actionType; + public Action(ActionType actionType, int delay) { + this.actionType = actionType; + this.delay = delay; + this.targetDREF = null; + } -Integer priority; //Priority of the Task -Integer delay; //Potential Delay before starting the task -String targetDREF; -Integer taskTime; //How long should the task take -boolean taskComplete; //Is the task completed? -ActionType actionType; + public Action(ActionType actionType, int delay, String target) { + this.actionType = actionType; + this.delay = delay; + targetDREF = target; + } + + public ActionType getType() { + return actionType; + } + + public Integer getDelay() { + return delay; + } + + public String getTarget() { + return targetDREF; + } -public Action(ActionType actionType, int delay){ - this.actionType = actionType; - this.delay = delay; - this.targetDREF = null; -} - - -public Action(ActionType actionType, int delay, String target){ - this.actionType = actionType; - this.delay = delay; - targetDREF = target; - } - -public ActionType getType(){ - return actionType; -} - - -public Integer getDelay(){ - return delay; -} - -public String getTarget(){ - return targetDREF; -} - } diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/ActionType.java b/Java/ProjectModels/CognitiveModel/ModelFiles/ActionType.java index dfba7d2..24bb9c3 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/ActionType.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/ActionType.java @@ -1,9 +1,7 @@ package ModelFiles; - -public enum ActionType{ +public enum ActionType { VISION, MOTOR, DELAY, } - diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/Delay.java b/Java/ProjectModels/CognitiveModel/ModelFiles/Delay.java index 37c5abc..f0cf276 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/Delay.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/Delay.java @@ -1,15 +1,14 @@ package ModelFiles; // package ProjectModels.CognitiveModel; - public class Delay extends Action { - public Delay(){ - super(ActionType.DELAY,1000); + public Delay() { + super(ActionType.DELAY, 1000); } - public Delay(int delay){ - super(ActionType.DELAY,delay); + public Delay(int delay) { + super(ActionType.DELAY, delay); } } \ No newline at end of file diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/MindQueue.java b/Java/ProjectModels/CognitiveModel/ModelFiles/MindQueue.java index daf4f46..7e45543 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/MindQueue.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/MindQueue.java @@ -7,16 +7,15 @@ public class MindQueue { LinkedList q; - public MindQueue () { + public MindQueue() { q = new LinkedList(); } - public void push(Action e){ + public void push(Action e) { q.add(e); } - - public Action removeEvent(Action e){ + public Action removeEvent(Action e) { Action temp = e; q.remove(e); return temp; @@ -24,36 +23,35 @@ public class MindQueue { public Action pop() { Vision v = new Vision(); - if(q.isEmpty()) { + if (q.isEmpty()) { return v; } return q.remove(); } - public boolean isEmpty(){ + public boolean isEmpty() { return q.isEmpty(); } - public String printQueue(){ + public String printQueue() { String queueTrace = "Next to Execute ==> "; for (Action action : q) { - if(action.getType() == ActionType.VISION){ + if (action.getType() == ActionType.VISION) { queueTrace += "[V]"; - } - if(action.getType() == ActionType.MOTOR){ + } + if (action.getType() == ActionType.MOTOR) { queueTrace += "[M]"; - } - if(action.getType() == ActionType.DELAY){ + } + if (action.getType() == ActionType.DELAY) { queueTrace += "[D]"; - } + } } - return queueTrace; + return queueTrace; } - public int queueLength(){ + public int queueLength() { return q.size(); } - } diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/Model.java b/Java/ProjectModels/CognitiveModel/ModelFiles/Model.java index 9ea733c..58b5513 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/Model.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/Model.java @@ -1,4 +1,5 @@ package ModelFiles; + import java.io.IOException; // import ModelFiles.ActionType; @@ -6,12 +7,12 @@ import java.io.IOException; public class Model { - private MindQueue q; //Queue for actions + private MindQueue q; // Queue for actions boolean modelActive; // On/Off Switch for the model execution - XPlaneConnect xpc; //Allows connection to the simulator + XPlaneConnect xpc; // Allows connection to the simulator float[] storedVision; - //Constructors + // Constructors public Model() { q = new MindQueue(); modelActive = false; @@ -20,62 +21,61 @@ public class Model { public Model(XPlaneConnect xpc) { q = new MindQueue(); modelActive = false; - this.xpc = xpc; + this.xpc = xpc; } - - /*Getters*/ + /* Getters */ public MindQueue getQueue() { return q; } - /*Setters*/ + /* Setters */ /* * Setup Methods * Printing, Empty check, Activation/Deactivation of Model, etc. - */ + */ public void activateModel() { modelActive = true; } - public void establishConnection(XPlaneConnect newXPC){ + public void establishConnection(XPlaneConnect newXPC) { this.xpc = newXPC; } - public void deactivateModel(){ + public void deactivateModel() { modelActive = false; } - public boolean isEmpty(){ + public boolean isEmpty() { return q.isEmpty(); } - public void push(Action a){ + public void push(Action a) { q.push(a); } - public boolean isActive(){ + public boolean isActive() { return modelActive; } - public void printModelQueue(){ + public void printModelQueue() { System.out.println(q.printQueue()); } - public int getModelQueueLength(){ + public int getModelQueueLength() { return q.queueLength(); } - - public void createAction(ActionType actionType, MotorType motorType, int delay,String target) { + + public void createAction(ActionType actionType, MotorType motorType, int delay, String target) { Action newAction = null; - switch(actionType) { + switch (actionType) { case VISION: - newAction = new Vision(delay,target); + newAction = new Vision(delay, target); this.push(newAction); break; case MOTOR: - newAction = new Motor(motorType,delay,target); + newAction = new Motor(motorType, delay, target); this.push(newAction); break; @@ -83,13 +83,13 @@ public class Model { newAction = new Delay(delay); this.push(newAction); break; - } + } } /* * Processing Methods * - */ + */ /* * Process the next event in the model queue @@ -99,30 +99,30 @@ public class Model { Action temp = q.pop(); // System.out.println("Type of Action: " + temp.getType()); - if(temp.getType() == ActionType.VISION){ // Vision Action (Get Data) - handelVisionAction(temp, returnArray); - } else if (temp.getType() == ActionType.MOTOR){ //Motor Action (Act Upon Data) + if (temp.getType() == ActionType.VISION) { // Vision Action (Get Data) + handelVisionAction(temp, returnArray); + } else if (temp.getType() == ActionType.MOTOR) { // Motor Action (Act Upon Data) handleMotorAction(temp); - } else if (temp.getType() == ActionType.DELAY){//Pure Delays (Do nothing) - handleDelayAction(temp); + } else if (temp.getType() == ActionType.DELAY) {// Pure Delays (Do nothing) + handleDelayAction(temp); } // q.push(temp); return returnArray; - + } - + /* Next Helpers */ private void handelVisionAction(Action temp, float[] returnArray) { - Vision tempV = (Vision) temp; - initiateDelay(tempV.getDelay()); - String dref = tempV.getTarget(); - try { - returnArray = xpc.getDREF(dref); - storedVision = returnArray.clone(); - // System.out.println(returnArray[0]); - } catch (IOException e) { - - } + Vision tempV = (Vision) temp; + initiateDelay(tempV.getDelay()); + String dref = tempV.getTarget(); + try { + returnArray = xpc.getDREF(dref); + storedVision = returnArray.clone(); + // System.out.println(returnArray[0]); + } catch (IOException e) { + + } } private void handleMotorAction(Action temp) { @@ -135,50 +135,51 @@ public class Model { // TODO Auto-generated catch block e.printStackTrace(); } + try { - - switch(motorType){ - case PITCHUP: - float[] pitchUp = {currentControls[0] + 0.01f}; - if(storedVision[0] > 80) { - if(currentControls[0] < 0.2f) { - System.out.println("Pitching Up"); - xpc.sendCTRL(pitchUp); - } + + switch (motorType) { + case PITCHUP: + float[] pitchUp = { currentControls[0] + 0.01f }; + if (storedVision[0] > 80) { + if (currentControls[0] < 0.2f) { + System.out.println("Pitching Up"); + xpc.sendCTRL(pitchUp); + } } - + break; - case PITCHDOWN: - float[] pitchDown = {currentControls[0] - 0.01f}; - if(storedVision[0] < 80) { - if(currentControls[0] > -0.2f) { - // System.out.println("Sending Pitch Down"); - System.out.println("Pitching Down"); - xpc.sendCTRL(pitchDown); - } + case PITCHDOWN: + float[] pitchDown = { currentControls[0] - 0.01f }; + if (storedVision[0] < 80) { + if (currentControls[0] > -0.2f) { + // System.out.println("Sending Pitch Down"); + System.out.println("Pitching Down"); + xpc.sendCTRL(pitchDown); + } } - + break; + } + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } } - private void handleDelayAction(Action temp){ + private void handleDelayAction(Action temp) { Delay tempD = (Delay) temp; initiateDelay(tempD.getDelay()); } - public static void initiateDelay(int delay){ + public static void initiateDelay(int delay) { try { - Thread.sleep(delay); // Using the action's Built in Delay + Thread.sleep(delay); // Using the action's Built in Delay } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } - } - + } + } diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/Motor.java b/Java/ProjectModels/CognitiveModel/ModelFiles/Motor.java index 9a2cdaa..f2cb776 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/Motor.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/Motor.java @@ -1,6 +1,6 @@ package ModelFiles; -import java.io.IOException; +import java.io.IOException; public class Motor extends Action { @@ -8,41 +8,37 @@ public class Motor extends Action { MotorType motorType; public Motor(MotorType motorType) { - super(ActionType.MOTOR,1000); + super(ActionType.MOTOR, 1000); this.motorType = motorType; } -// public Motor(int delay) { -// super(ActionType.MOTOR,delay); -// } + // public Motor(int delay) { + // super(ActionType.MOTOR,delay); + // } + public Motor(MotorType motorType, int delay, String target) { + super(ActionType.MOTOR, delay, target); + this.motorType = motorType; + } -public Motor(MotorType motorType,int delay, String target) { - super(ActionType.MOTOR,delay,target); - this.motorType = motorType; -} + public void createMotorControl() { // TODO: Maybe takes in a formatted set of vision inputs? + control = null; + } + public MotorType getMotorType() { + return motorType; + } -public void createMotorControl(){ // TODO: Maybe takes in a formatted set of vision inputs? - control = null; -} + public void sendMotorControl(XPlaneConnect xpc) { - -public MotorType getMotorType(){ - return motorType; -} - -public void sendMotorControl(XPlaneConnect xpc) { - - if(control != null) { - try { - xpc.sendCTRL(control); - } catch (IOException e) { - // TODO Auto-generated catch block - System.out.println("Control failed to send"); + if (control != null) { + try { + xpc.sendCTRL(control); + } catch (IOException e) { + // TODO Auto-generated catch block + System.out.println("Control failed to send"); + } } } -} - } \ No newline at end of file diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/MotorType.java b/Java/ProjectModels/CognitiveModel/ModelFiles/MotorType.java index 7ec0604..ff46913 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/MotorType.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/MotorType.java @@ -1,6 +1,6 @@ package ModelFiles; -public enum MotorType{ - PITCHUP, +public enum MotorType { + PITCHUP, PITCHDOWN } diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/Process.java b/Java/ProjectModels/CognitiveModel/ModelFiles/Process.java index 0b1fc77..502f0ba 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/Process.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/Process.java @@ -1,10 +1,9 @@ package ModelFiles; + public class Process { - - public Process(){ + public Process() { } - } diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/Tests.java b/Java/ProjectModels/CognitiveModel/ModelFiles/Tests.java index 6fd99ee..16de4e3 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/Tests.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/Tests.java @@ -1,4 +1,5 @@ package ModelFiles; + import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -14,5 +15,5 @@ public class Tests { m.push(a); assertEquals(i, m.pop()); } - + } diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/Vision.java b/Java/ProjectModels/CognitiveModel/ModelFiles/Vision.java index 88e40c2..48971da 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/Vision.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/Vision.java @@ -2,21 +2,20 @@ package ModelFiles; public class Vision extends Action { - - public Vision(){ - super(ActionType.VISION,1000); + public Vision() { + super(ActionType.VISION, 1000); } - public Vision(int delay){ - super(ActionType.VISION,delay); + public Vision(int delay) { + super(ActionType.VISION, delay); } - public Vision(String dref){ - super(ActionType.VISION,1000,dref); + public Vision(String dref) { + super(ActionType.VISION, 1000, dref); } - public Vision(int delay, String dref){ - super(ActionType.VISION,delay, dref); + public Vision(int delay, String dref) { + super(ActionType.VISION, delay, dref); } } \ No newline at end of file diff --git a/Java/ProjectModels/CognitiveModel/ModelFiles/XPlaneConnect.java b/Java/ProjectModels/CognitiveModel/ModelFiles/XPlaneConnect.java index c888332..7e202ef 100644 --- a/Java/ProjectModels/CognitiveModel/ModelFiles/XPlaneConnect.java +++ b/Java/ProjectModels/CognitiveModel/ModelFiles/XPlaneConnect.java @@ -37,15 +37,15 @@ import java.util.Arrays; import ModelFiles.XPCdependencies.*; /** - * Represents a client that can connect to and interact with the X-Plane Connect plugin. + * Represents a client that can connect to and interact with the X-Plane Connect + * plugin. * - * @author Jason Watkins + * @author Jason Watkins * @version 0.1 - * @since 2015-03-31 + * @since 2015-03-31 */ -public class XPlaneConnect implements AutoCloseable -{ - //private static int clientNum; +public class XPlaneConnect implements AutoCloseable { + // private static int clientNum; private DatagramSocket socket; private InetAddress xplaneAddr; private int xplanePort; @@ -55,14 +55,18 @@ public class XPlaneConnect implements AutoCloseable * * @return The incoming port number. */ - public int getRecvPort() { return socket.getLocalPort(); } + public int getRecvPort() { + return socket.getLocalPort(); + } /** * Gets the port on which the client sends data to X-Plane. * * @return The outgoing port number. */ - public int getXPlanePort() { return xplanePort; } + public int getXPlanePort() { + return xplanePort; + } /** * Sets the port on which the client sends data to X-Plane @@ -70,10 +74,8 @@ public class XPlaneConnect implements AutoCloseable * @param port The new outgoing port number. * @throws IllegalArgumentException If {@code port} is not a valid port number. */ - public void setXPlanePort(int port) - { - if(port < 0 || port >= 0xFFFF) - { + public void setXPlanePort(int port) { + if (port < 0 || port >= 0xFFFF) { throw new IllegalArgumentException("Invalid port (must be non-negative and less than 65536)."); } xplanePort = port; @@ -84,7 +86,9 @@ public class XPlaneConnect implements AutoCloseable * * @return The hostname of the X-Plane host. */ - public String getXPlaneAddr() { return xplaneAddr.getHostAddress(); } + public String getXPlaneAddr() { + return xplaneAddr.getHostAddress(); + } /** * Sets the hostname of the X-Plane host. @@ -92,31 +96,33 @@ public class XPlaneConnect implements AutoCloseable * @param host The new hostname of the X-Plane host machine. * @throws UnknownHostException {@code host} is not valid. */ - public void setXplaneAddr(String host) throws UnknownHostException - { + public void setXplaneAddr(String host) throws UnknownHostException { xplaneAddr = InetAddress.getByName(host); } /** - * Initializes a new instance of the {@code XPlaneConnect} class using default ports and assuming X-Plane is running on the + * Initializes a new instance of the {@code XPlaneConnect} class using default + * ports and assuming X-Plane is running on the * local machine. * - * @throws SocketException If this instance is unable to bind to the default receive port. + * @throws SocketException If this instance is unable to bind to the default + * receive port. */ - public XPlaneConnect() throws SocketException - { + public XPlaneConnect() throws SocketException { this(100); } /** - * Initializes a new instance of the {@code XPlaneConnect} class with the specified timeout using default ports and + * Initializes a new instance of the {@code XPlaneConnect} class with the + * specified timeout using default ports and * assuming X-Plane is running on the local machine. * - * @param timeout The time, in milliseconds, after which read attempts will timeout. - * @throws SocketException If this instance is unable to bind to the default receive port. + * @param timeout The time, in milliseconds, after which read attempts will + * timeout. + * @throws SocketException If this instance is unable to bind to the default + * receive port. */ - public XPlaneConnect(int timeout) throws SocketException - { + public XPlaneConnect(int timeout) throws SocketException { this.socket = new DatagramSocket(0); this.xplaneAddr = InetAddress.getLoopbackAddress(); this.xplanePort = 49009; @@ -124,25 +130,29 @@ public class XPlaneConnect implements AutoCloseable } /** - * Initializes a new instance of the {@code XPlaneConnect} class using the specified ports and X-Plane host. + * Initializes a new instance of the {@code XPlaneConnect} class using the + * specified ports and X-Plane host. * * @param xpHost The network host on which X-Plane is running. * @param xpPort The port on which the X-Plane Connect plugin is listening. * @param port The local port to use when sending and receiving data from XPC. - * @throws java.net.SocketException If this instance is unable to bind to the specified port. - * @throws java.net.UnknownHostException If the specified hostname can not be resolved. + * @throws java.net.SocketException If this instance is unable to bind to + * the specified port. + * @throws java.net.UnknownHostException If the specified hostname can not be + * resolved. */ public XPlaneConnect(String xpHost, int xpPort, int port) - throws java.net.SocketException, java.net.UnknownHostException - { + throws java.net.SocketException, java.net.UnknownHostException { this(xpHost, xpPort, port, 100); } - /** - * Initializes a new instance of the {@code XPlaneConnect} class from a received discovery Beacon + * Initializes a new instance of the {@code XPlaneConnect} class from a received + * discovery Beacon + * * @param beacon The beacon received from {@code XPlaneConnectDiscovery} - * @throws SocketException If this instance is unable to bind to the specified port. + * @throws SocketException If this instance is unable to bind to the specified + * port. */ public XPlaneConnect(Beacon beacon) throws SocketException { this.socket = new DatagramSocket(0); @@ -152,18 +162,21 @@ public class XPlaneConnect implements AutoCloseable } /** - * Initializes a new instance of the {@code XPlaneConnect} class using the specified ports, hostname, and timeout. + * Initializes a new instance of the {@code XPlaneConnect} class using the + * specified ports, hostname, and timeout. * - * @param xpHost The network host on which X-Plane is running. - * @param xpPort The port on which the X-Plane Connect plugin is listening. - * @param port The port on which the X-Plane Connect plugin is sending data. - * @param timeout The time, in milliseconds, after which read attempts will timeout. - * @throws java.net.SocketException If this instance is unable to bind to the specified port. - * @throws java.net.UnknownHostException If the specified hostname can not be resolved. + * @param xpHost The network host on which X-Plane is running. + * @param xpPort The port on which the X-Plane Connect plugin is listening. + * @param port The port on which the X-Plane Connect plugin is sending data. + * @param timeout The time, in milliseconds, after which read attempts will + * timeout. + * @throws java.net.SocketException If this instance is unable to bind to + * the specified port. + * @throws java.net.UnknownHostException If the specified hostname can not be + * resolved. */ public XPlaneConnect(String xpHost, int xpPort, int port, int timeout) - throws java.net.SocketException, java.net.UnknownHostException - { + throws java.net.SocketException, java.net.UnknownHostException { this.socket = new DatagramSocket(port); this.xplaneAddr = InetAddress.getByName(xpHost); this.xplanePort = xpPort; @@ -173,32 +186,27 @@ public class XPlaneConnect implements AutoCloseable /** * Closes the underlying socket. */ - public void close() - { - if(socket != null) - { + public void close() { + if (socket != null) { socket.close(); socket = null; } } /** - * Read data from the X-Plane plugin. This method will read whatever data is available and return it. + * Read data from the X-Plane plugin. This method will read whatever data is + * available and return it. * * @return The data send from X-Plane. * @throws IOException If the read operation fails */ - private byte[] readUDP() throws IOException - { + private byte[] readUDP() throws IOException { byte[] buffer = new byte[65536]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); - try - { + try { socket.receive(packet); return Arrays.copyOf(buffer, packet.getLength()); - } - catch (SocketTimeoutException ex) - { + } catch (SocketTimeoutException ex) { return new byte[0]; } } @@ -209,8 +217,7 @@ public class XPlaneConnect implements AutoCloseable * @param buffer The data to send. * @throws IOException If the send operation fails. */ - private void sendUDP(byte[] buffer) throws IOException - { + private void sendUDP(byte[] buffer) throws IOException { DatagramPacket packet = new DatagramPacket(buffer, buffer.length, xplaneAddr, xplanePort); socket.send(packet); } @@ -221,31 +228,30 @@ public class XPlaneConnect implements AutoCloseable * @param pause {@code true} to pause the simulator; {@code false} to un-pause. * @throws IOException If the command cannot be sent. */ - public void pauseSim(boolean pause) throws IOException - { - // S I M U LEN VAL - byte[] msg = {0x53, 0x49, 0x4D, 0x55, 0x00, 0x00}; - msg[5] = (byte)(pause ? 0x01 : 0x00); + public void pauseSim(boolean pause) throws IOException { + // S I M U LEN VAL + byte[] msg = { 0x53, 0x49, 0x4D, 0x55, 0x00, 0x00 }; + msg[5] = (byte) (pause ? 0x01 : 0x00); sendUDP(msg); } /** * Pauses, unpauses, or switches the pause state of X-Plane. * - * @param pause {@code 1} to pause the simulator, {@code 0} to unpause, or {@code 2} to switch. - * @throws IllegalArgumentException If the values of {@code pause} is not a valid command. - * @throws IOException If the command cannot be sent. + * @param pause {@code 1} to pause the simulator, {@code 0} to unpause, or + * {@code 2} to switch. + * @throws IllegalArgumentException If the values of {@code pause} is not a + * valid command. + * @throws IOException If the command cannot be sent. */ - public void pauseSim(int pause) throws IOException - { - if(pause < 0 || (pause > 2 && pause < 100) || (pause > 119 && pause < 200) || pause > 219) - { + public void pauseSim(int pause) throws IOException { + if (pause < 0 || (pause > 2 && pause < 100) || (pause > 119 && pause < 200) || pause > 219) { throw new IllegalArgumentException("pause must be a value in the range [0, 2], [100, 119], or [200, 219]."); } - // S I M U LEN VAL - byte[] msg = {0x53, 0x49, 0x4D, 0x55, 0x00, 0x00}; - msg[5] = (byte)pause; + // S I M U LEN VAL + byte[] msg = { 0x53, 0x49, 0x4D, 0x55, 0x00, 0x00 }; + msg[5] = (byte) pause; sendUDP(msg); } @@ -253,78 +259,69 @@ public class XPlaneConnect implements AutoCloseable * Requests a single dref value from X-Plane. * * @param dref The name of the dref requested. - * @return A byte array representing data dependent on the dref requested. + * @return A byte array representing data dependent on the dref requested. * @throws IOException If either the request or the response fails. */ - public float[] getDREF(String dref) throws IOException - { - return getDREFs(new String[] {dref})[0]; + public float[] getDREF(String dref) throws IOException { + return getDREFs(new String[] { dref })[0]; } /** * Requests several dref values from X-Plane. * * @param drefs An array of dref names to request. - * @return A multidimensional array representing the data for each requested dref. + * @return A multidimensional array representing the data for each requested + * dref. * @throws IOException If either the request or the response fails. */ - public float[][] getDREFs(String[] drefs) throws IOException - { - //Preconditions - if(drefs == null || drefs.length == 0) - { + public float[][] getDREFs(String[] drefs) throws IOException { + // Preconditions + if (drefs == null || drefs.length == 0) { throw new IllegalArgumentException("drefs must be a valid array with at least one dref."); } - if(drefs.length > 255) - { + if (drefs.length > 255) { throw new IllegalArgumentException("Can not request more than 255 DREFs at once."); } - //Convert drefs to bytes. + // Convert drefs to bytes. byte[][] drefBytes = new byte[drefs.length][]; - for(int i = 0; i < drefs.length; ++i) - { + for (int i = 0; i < drefs.length; ++i) { drefBytes[i] = drefs[i].getBytes(StandardCharsets.UTF_8); - if(drefBytes[i].length == 0) - { + if (drefBytes[i].length == 0) { throw new IllegalArgumentException("DREF " + i + " is an empty string!"); } - if(drefBytes[i].length > 255) - { - throw new IllegalArgumentException("DREF " + i + " is too long (must be less than 255 bytes in UTF-8). Are you sure this is a valid DREF?"); + if (drefBytes[i].length > 255) { + throw new IllegalArgumentException("DREF " + i + + " is too long (must be less than 255 bytes in UTF-8). Are you sure this is a valid DREF?"); } } - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("GETD".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(drefs.length); - for(byte[] dref : drefBytes) - { + for (byte[] dref : drefBytes) { os.write(dref.length); os.write(dref, 0, dref.length); } sendUDP(os.toByteArray()); - //Read response + // Read response byte[] data = readUDP(); - if(data.length == 0) - { + if (data.length == 0) { throw new IOException("No response received."); } - if(data.length < 6) - { + if (data.length < 6) { throw new IOException("Response too short"); } float[][] result = new float[drefs.length][]; ByteBuffer bb = ByteBuffer.wrap(data); bb.order(ByteOrder.LITTLE_ENDIAN); int cur = 6; - for(int j = 0; j < result.length; ++j) - { + for (int j = 0; j < result.length; ++j) { result[j] = new float[data[cur++]]; - for(int k = 0; k < result[j].length; ++k) //TODO: There must be a better way to do this + for (int k = 0; k < result[j].length; ++k) // TODO: There must be a better way to do this { result[j][k] = bb.getFloat(cur); cur += 4; @@ -333,78 +330,70 @@ public class XPlaneConnect implements AutoCloseable return result; } - public void sendDREF(String dref, float value) throws IOException - { - sendDREF(dref, new float[] {value}); + public void sendDREF(String dref, float value) throws IOException { + sendDREF(dref, new float[] { value }); } /** * Sends a command to X-Plane that sets the given DREF. * * @param dref The name of the X-Plane dataref to set. - * @param value An array of floating point values whose structure depends on the dref specified. + * @param value An array of floating point values whose structure depends on the + * dref specified. * @throws IOException If the command cannot be sent. */ - public void sendDREF(String dref, float[] value) throws IOException - { - sendDREFs(new String[] {dref}, new float[][] {value}); + public void sendDREF(String dref, float[] value) throws IOException { + sendDREFs(new String[] { dref }, new float[][] { value }); } /** * Sends a command to X-Plane that sets the given DREF. * * @param drefs The names of the X-Plane datarefs to set. - * @param values A sequence of arrays of floating point values whose structure depends on the drefs specified. + * @param values A sequence of arrays of floating point values whose structure + * depends on the drefs specified. * @throws IOException If the command cannot be sent. */ - public void sendDREFs(String[] drefs, float[][] values) throws IOException - { - //Preconditions - if(drefs == null || drefs.length == 0) - { + public void sendDREFs(String[] drefs, float[][] values) throws IOException { + // Preconditions + if (drefs == null || drefs.length == 0) { throw new IllegalArgumentException(("drefs must be non-empty.")); } - if(values == null || values.length != drefs.length) - { + if (values == null || values.length != drefs.length) { throw new IllegalArgumentException("values must be of the same size as drefs."); } ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("DREF".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length - for(int i = 0; i < drefs.length; ++i) - { + os.write(0xFF); // Placeholder for message length + for (int i = 0; i < drefs.length; ++i) { String dref = drefs[i]; float[] value = values[i]; - if (dref == null) - { + if (dref == null) { throw new IllegalArgumentException("dref must be a valid string."); } - if (value == null || value.length == 0) - { + if (value == null || value.length == 0) { throw new IllegalArgumentException("value must be non-null and should contain at least one value."); } - //Convert drefs to bytes. + // Convert drefs to bytes. byte[] drefBytes = dref.getBytes(StandardCharsets.UTF_8); - if (drefBytes.length == 0) - { + if (drefBytes.length == 0) { throw new IllegalArgumentException("DREF is an empty string!"); } - if (drefBytes.length > 255) - { - throw new IllegalArgumentException("dref must be less than 255 bytes in UTF-8. Are you sure this is a valid dref?"); + if (drefBytes.length > 255) { + throw new IllegalArgumentException( + "dref must be less than 255 bytes in UTF-8. Are you sure this is a valid dref?"); } ByteBuffer bb = ByteBuffer.allocate(4 * value.length); bb.order(ByteOrder.LITTLE_ENDIAN); - for (int j = 0; j < value.length; ++j) - { + for (int j = 0; j < value.length; ++j) { bb.putFloat(j * 4, value[j]); } - //Build and send message + // Build and send message os.write(drefBytes.length); os.write(drefBytes, 0, drefBytes.length); os.write(value.length); @@ -417,26 +406,25 @@ public class XPlaneConnect implements AutoCloseable * Gets the control surface information for the specified airplane. * * @param ac The aircraft to get control surface information for. - * @return An array containing control surface data in the same format as {@code sendCTRL}. - * @throws IOException If the command cannot be sent or a response cannot be read. + * @return An array containing control surface data in the same format as + * {@code sendCTRL}. + * @throws IOException If the command cannot be sent or a response cannot be + * read. */ - public float[] getCTRL(int ac) throws IOException - { + public float[] getCTRL(int ac) throws IOException { // Send request ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("GETC".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(ac); sendUDP(os.toByteArray()); // Read response byte[] data = readUDP(); - if(data.length == 0) - { + if (data.length == 0) { throw new IOException("No response received."); } - if(data.length < 31) - { + if (data.length < 31) { throw new IOException("Response too short"); } @@ -457,91 +445,89 @@ public class XPlaneConnect implements AutoCloseable /** * Sends command to X-Plane setting control surfaces on the player ac. * - * @param values

An array containing zero to six values representing control surface data as follows:

+ * @param values + *

+ * An array containing zero to six values representing control + * surface data as follows: + *

*
    - *
  1. Latitudinal Stick [-1,1]
  2. - *
  3. Longitudinal Stick [-1,1]
  4. - *
  5. Rudder Pedals [-1, 1]
  6. - *
  7. Throttle [-1, 1]
  8. - *
  9. Gear (0=up, 1=down)
  10. - *
  11. Flaps [0, 1]
  12. - *
  13. Speedbrakes [-0.5, 1.5]
  14. + *
  15. Latitudinal Stick [-1,1]
  16. + *
  17. Longitudinal Stick [-1,1]
  18. + *
  19. Rudder Pedals [-1, 1]
  20. + *
  21. Throttle [-1, 1]
  22. + *
  23. Gear (0=up, 1=down)
  24. + *
  25. Flaps [0, 1]
  26. + *
  27. Speedbrakes [-0.5, 1.5]
  28. *
*

- * If @{code ctrl} is less than 6 elements long, The missing elements will not be changed. To - * change values in the middle of the array without affecting the preceding values, set the - * preceding values to -998. + * If @{code ctrl} is less than 6 elements long, The missing + * elements will not be changed. To + * change values in the middle of the array without affecting the + * preceding values, set the + * preceding values to -998. *

* @throws IOException If the command cannot be sent. */ - public void sendCTRL(float[] values) throws IOException - { + public void sendCTRL(float[] values) throws IOException { sendCTRL(values, 0); } /** * Sends command to X-Plane setting control surfaces on the specified ac. * - * @param values

An array containing zero to six values representing control surface data as follows:

- *
    - *
  1. Latitudinal Stick [-1,1]
  2. - *
  3. Longitudinal Stick [-1,1]
  4. - *
  5. Rudder Pedals [-1, 1]
  6. - *
  7. Throttle [-1, 1]
  8. - *
  9. Gear (0=up, 1=down)
  10. - *
  11. Flaps [0, 1]
  12. - *
  13. Speedbrakes [-0.5, 1.5]
  14. - *
- *

- * If @{code ctrl} is less than 6 elements long, The missing elements will not be changed. To - * change values in the middle of the array without affecting the preceding values, set the - * preceding values to -998. - *

- * @param ac The ac to set. 0 for the player's ac. + * @param values + *

+ * An array containing zero to six values representing control + * surface data as follows: + *

+ *
    + *
  1. Latitudinal Stick [-1,1]
  2. + *
  3. Longitudinal Stick [-1,1]
  4. + *
  5. Rudder Pedals [-1, 1]
  6. + *
  7. Throttle [-1, 1]
  8. + *
  9. Gear (0=up, 1=down)
  10. + *
  11. Flaps [0, 1]
  12. + *
  13. Speedbrakes [-0.5, 1.5]
  14. + *
+ *

+ * If @{code ctrl} is less than 6 elements long, The missing + * elements will not be changed. To + * change values in the middle of the array without affecting the + * preceding values, set the + * preceding values to -998. + *

+ * @param ac The ac to set. 0 for the player's ac. * @throws IOException If the command cannot be sent. */ - public void sendCTRL(float[] values, int ac) throws IOException - { - //Preconditions - if(values == null) - { + public void sendCTRL(float[] values, int ac) throws IOException { + // Preconditions + if (values == null) { throw new IllegalArgumentException("ctrl must no be null."); } - if(values.length > 7) - { + if (values.length > 7) { throw new IllegalArgumentException("ctrl must have 7 or fewer elements."); } - if(ac < 0 || ac > 9) - { + if (ac < 0 || ac > 9) { throw new IllegalArgumentException("ac must be non-negative and less than 9."); } - //Pad command values and convert to bytes + // Pad command values and convert to bytes int i; int cur = 0; ByteBuffer bb = ByteBuffer.allocate(26); bb.order(ByteOrder.LITTLE_ENDIAN); - for(i = 0; i < 6; ++i) - { - if(i == 4) - { - if(i >= values.length) - { - bb.put(cur, (byte)-1); - } - else - { - bb.put(cur, (byte)values[i]); + for (i = 0; i < 6; ++i) { + if (i == 4) { + if (i >= values.length) { + bb.put(cur, (byte) -1); + } else { + bb.put(cur, (byte) values[i]); } cur += 1; - } - else if (i >= values.length) - { + } else if (i >= values.length) { bb.putFloat(cur, -998); - cur+= 4; - } - else - { + cur += 4; + } else { bb.putFloat(cur, values[i]); cur += 4; } @@ -549,10 +535,10 @@ public class XPlaneConnect implements AutoCloseable bb.put(cur++, (byte) ac); bb.putFloat(cur, values.length == 7 ? values[6] : -998); - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("CTRL".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(bb.array()); sendUDP(os.toByteArray()); } @@ -561,26 +547,25 @@ public class XPlaneConnect implements AutoCloseable * Gets position information for the specified airplane. * * @param ac The aircraft to get position information for. - * @return An array containing control surface data in the same format as {@code sendPOSI}. - * @throws IOException If the command cannot be sent or a response cannot be read. + * @return An array containing control surface data in the same format as + * {@code sendPOSI}. + * @throws IOException If the command cannot be sent or a response cannot be + * read. */ - public double[] getPOSI(int ac) throws IOException - { + public double[] getPOSI(int ac) throws IOException { // Send request ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("GETP".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(ac); sendUDP(os.toByteArray()); // Read response byte[] data = readUDP(); - if(data.length == 0) - { + if (data.length == 0) { throw new IOException("No response received."); } - if(data.length < 34) - { + if (data.length < 34) { throw new IOException("Response too short"); } @@ -588,8 +573,7 @@ public class XPlaneConnect implements AutoCloseable double[] result = new double[7]; ByteBuffer bb = ByteBuffer.wrap(data); bb.order(ByteOrder.LITTLE_ENDIAN); - for(int i = 0; i < 7; ++i) - { + for (int i = 0; i < 7; ++i) { result[i] = bb.getFloat(6 + 4 * i); } return result; @@ -598,89 +582,90 @@ public class XPlaneConnect implements AutoCloseable /** * Sets the position of the player ac. * - * @param values

An array containing position elements as follows:

- *
    - *
  1. Latitude (deg)
  2. - *
  3. Longitude (deg)
  4. - *
  5. Altitude (m above MSL)
  6. - *
  7. Roll (deg)
  8. - *
  9. Pitch (deg)
  10. - *
  11. True Heading (deg)
  12. - *
  13. Gear (0=up, 1=down)
  14. - *
- *

- * If @{code ctrl} is less than 6 elements long, The missing elements will not be changed. To - * change values in the middle of the array without affecting the preceding values, set the - * preceding values to -998. - *

+ * @param values + *

+ * An array containing position elements as follows: + *

+ *
    + *
  1. Latitude (deg)
  2. + *
  3. Longitude (deg)
  4. + *
  5. Altitude (m above MSL)
  6. + *
  7. Roll (deg)
  8. + *
  9. Pitch (deg)
  10. + *
  11. True Heading (deg)
  12. + *
  13. Gear (0=up, 1=down)
  14. + *
+ *

+ * If @{code ctrl} is less than 6 elements long, The missing + * elements will not be changed. To + * change values in the middle of the array without affecting the + * preceding values, set the + * preceding values to -998. + *

* @throws IOException If the command can not be sent. */ - public void sendPOSI(double[] values) throws IOException - { + public void sendPOSI(double[] values) throws IOException { sendPOSI(values, 0); } /** * Sets the position of the specified ac with double precision coordinates. * - * @param values

An array containing position elements as follows:

- *
    - *
  1. Latitude (deg)
  2. - *
  3. Longitude (deg)
  4. - *
  5. Altitude (m above MSL)
  6. - *
  7. Roll (deg)
  8. - *
  9. Pitch (deg)
  10. - *
  11. True Heading (deg)
  12. - *
  13. Gear (0=up, 1=down)
  14. - *
- *

- * If @{code ctrl} is less than 6 elements long, The missing elements will not be changed. To - * change values in the middle of the array without affecting the preceding values, set the - * preceding values to -998. - *

- * @param ac The ac to set. 0 for the player ac. + * @param values + *

+ * An array containing position elements as follows: + *

+ *
    + *
  1. Latitude (deg)
  2. + *
  3. Longitude (deg)
  4. + *
  5. Altitude (m above MSL)
  6. + *
  7. Roll (deg)
  8. + *
  9. Pitch (deg)
  10. + *
  11. True Heading (deg)
  12. + *
  13. Gear (0=up, 1=down)
  14. + *
+ *

+ * If @{code ctrl} is less than 6 elements long, The missing + * elements will not be changed. To + * change values in the middle of the array without affecting the + * preceding values, set the + * preceding values to -998. + *

+ * @param ac The ac to set. 0 for the player ac. * @throws IOException If the command can not be sent. */ - public void sendPOSI(double[] values, int ac) throws IOException - { - //Preconditions - if(values == null) - { + public void sendPOSI(double[] values, int ac) throws IOException { + // Preconditions + if (values == null) { throw new IllegalArgumentException("posi must no be null."); } - if(values.length > 7) - { + if (values.length > 7) { throw new IllegalArgumentException("posi must have 7 or fewer elements."); } - if(ac < 0 || ac > 255) - { + if (ac < 0 || ac > 255) { throw new IllegalArgumentException("ac must be between 0 and 255."); } - //Pad command values and convert to bytes + // Pad command values and convert to bytes int i; ByteBuffer bb = ByteBuffer.allocate(40); bb.order(ByteOrder.LITTLE_ENDIAN); - for(i = 0; i < values.length; ++i) - { - if(i<3) /* lat/lon/height as double */ + for (i = 0; i < values.length; ++i) { + if (i < 3) /* lat/lon/height as double */ { bb.putDouble(values[i]); - } - else - { - bb.putFloat((float)values[i]); + } else { + bb.putFloat((float) values[i]); } } - for(; i < 7; ++i) - { + for (; i < 7; ++i) { bb.putFloat(-998); } - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("POSI".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(ac); os.write(bb.array()); sendUDP(os.toByteArray()); @@ -692,17 +677,14 @@ public class XPlaneConnect implements AutoCloseable * @return The data read. * @throws IOException If the read operation fails. */ - public float[][] readData() throws IOException - { + public float[][] readData() throws IOException { byte[] buffer = readUDP(); ByteBuffer bb = ByteBuffer.wrap(buffer); int cur = 5; int len = bb.get(cur++); float[][] result = new float[bb.get(len)][9]; - for(int i = 0; i < len; ++i) - { - for(int j = 0; j < 9; ++j) - { + for (int i = 0; i < len; ++i) { + for (int j = 0; j < 9; ++j) { result[i][j] = bb.getFloat(cur); cur += 4; } @@ -716,37 +698,32 @@ public class XPlaneConnect implements AutoCloseable * @param data The data to send. * @throws IOException If the command cannot be sent. */ - public void sendDATA(float[][] data) throws IOException - { - //Preconditions - if(data == null || data.length == 0) - { + public void sendDATA(float[][] data) throws IOException { + // Preconditions + if (data == null || data.length == 0) { throw new IllegalArgumentException("data must be a non-null, non-empty array."); } - //Convert data to bytes + // Convert data to bytes ByteBuffer bb = ByteBuffer.allocate(4 * 9 * data.length); bb.order(ByteOrder.LITTLE_ENDIAN); - for(int i = 0; i < data.length; ++i) - { + for (int i = 0; i < data.length; ++i) { int rowStart = 9 * 4 * i; float[] row = data[i]; - if(row.length != 9) - { + if (row.length != 9) { throw new IllegalArgumentException("Rows must contain exactly 9 items. (Row " + i + ")"); } bb.putInt(rowStart, (int) row[0]); - for(int j = 1; j < row.length; ++j) - { + for (int j = 1; j < row.length; ++j) { bb.putFloat(rowStart + 4 * j, row[j]); } } - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("DATA".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(bb.array()); sendUDP(os.toByteArray()); } @@ -757,61 +734,58 @@ public class XPlaneConnect implements AutoCloseable * @param rows The row numbers to select. * @throws IOException If the command cannot be sent. */ - public void selectDATA(int[] rows) throws IOException - { - //Preconditions - if(rows == null || rows.length == 0) - { + public void selectDATA(int[] rows) throws IOException { + // Preconditions + if (rows == null || rows.length == 0) { throw new IllegalArgumentException("rows must be a non-null, non-empty array."); } - //Convert data to bytes + // Convert data to bytes ByteBuffer bb = ByteBuffer.allocate(4 * rows.length); bb.order(ByteOrder.LITTLE_ENDIAN); - for(int i = 0; i < rows.length; ++i) - { + for (int i = 0; i < rows.length; ++i) { bb.putInt(i * 4, rows[i]); } - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("DSEL".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(bb.array()); sendUDP(os.toByteArray()); } /** - * Sets a message to be displayed on the screen in X-Plane at the default screen location. + * Sets a message to be displayed on the screen in X-Plane at the default screen + * location. * * @param msg The message to display. Should not contain any newline characters. * @throws IOException If the command cannot be sent. */ - public void sendTEXT(String msg) throws IOException - { + public void sendTEXT(String msg) throws IOException { sendTEXT(msg, -1, -1); } /** - * Sets a message to be displayed on the screen in X-Plane at the specified coordinates. + * Sets a message to be displayed on the screen in X-Plane at the specified + * coordinates. * * @param msg The message to display. Should not contain any newline characters. - * @param x The number of pixels from the right edge of the screen to display the text. - * @param y The number of pixels from the bottom edge of the screen to display the text. + * @param x The number of pixels from the right edge of the screen to display + * the text. + * @param y The number of pixels from the bottom edge of the screen to display + * the text. * @throws IOException If the command cannot be sent. */ - public void sendTEXT(String msg, int x, int y) throws IOException - { - //Preconditions - if(msg == null) - { + public void sendTEXT(String msg, int x, int y) throws IOException { + // Preconditions + if (msg == null) { msg = ""; } - //Convert drefs to bytes. + // Convert drefs to bytes. byte[] msgBytes = msg.getBytes(StandardCharsets.UTF_8); - if(msgBytes.length > 255) - { + if (msgBytes.length > 255) { throw new IllegalArgumentException("msg must be less than 255 bytes in UTF-8."); } @@ -820,10 +794,10 @@ public class XPlaneConnect implements AutoCloseable bb.putInt(0, x); bb.putInt(4, y); - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("TEXT".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(bb.array()); os.write(msgBytes.length); os.write(msgBytes); @@ -836,53 +810,50 @@ public class XPlaneConnect implements AutoCloseable * @param view The view to use. * @throws IOException If the command cannot be sent. */ - public void sendVIEW(ViewType view) throws IOException - { + public void sendVIEW(ViewType view) throws IOException { ByteBuffer bb = ByteBuffer.allocate(4); bb.order(ByteOrder.LITTLE_ENDIAN); bb.putInt(view.getValue()); - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("VIEW".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(bb.array()); sendUDP(os.toByteArray()); } /** - * Adds, removes, or clears a set of waypoints. If the command is clear, the points are ignored + * Adds, removes, or clears a set of waypoints. If the command is clear, the + * points are ignored * and all points are removed. * * @param op The operation to perform. - * @param points An array of values representing points. Each triplet in the array will be + * @param points An array of values representing points. Each triplet in the + * array will be * interpreted as a (Lat, Lon, Alt) point. - * @throws IOException If the command cannot be sent. + * @throws IOException If the command cannot be sent. */ - public void sendWYPT(WaypointOp op, float[] points) throws IOException - { - //Preconditions - if(points.length % 3 != 0) - { + public void sendWYPT(WaypointOp op, float[] points) throws IOException { + // Preconditions + if (points.length % 3 != 0) { throw new IllegalArgumentException("points.length should be divisible by 3."); } - if(points.length / 3 > 255) - { + if (points.length / 3 > 255) { throw new IllegalArgumentException("Too many points. Must be less than 256."); } - //Convert points to bytes + // Convert points to bytes ByteBuffer bb = ByteBuffer.allocate(4 * points.length); bb.order(ByteOrder.LITTLE_ENDIAN); - for(float f : points) - { + for (float f : points) { bb.putFloat(f); } - //Build and send message + // Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("WYPT".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write(op.getValue()); os.write(points.length / 3); os.write(bb.array()); @@ -895,52 +866,46 @@ public class XPlaneConnect implements AutoCloseable * @param comm The name of the X-Plane command to send. * @throws IOException If the command cannot be sent. */ - public void sendCOMM(String comm) throws IOException - { - //Preconditions - if(comm == null || comm.length() == 0) - { + public void sendCOMM(String comm) throws IOException { + // Preconditions + if (comm == null || comm.length() == 0) { throw new IllegalArgumentException(("comm must be non-empty.")); } ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("COMM".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length - //Convert comm to bytes. + // Convert comm to bytes. byte[] commBytes = comm.getBytes(StandardCharsets.UTF_8); - if (commBytes.length == 0) - { + if (commBytes.length == 0) { throw new IllegalArgumentException("COMM is an empty string!"); } - if (commBytes.length > 255) - { - throw new IllegalArgumentException("comm must be less than 255 bytes in UTF-8. Are you sure this is a valid comm?"); + if (commBytes.length > 255) { + throw new IllegalArgumentException( + "comm must be less than 255 bytes in UTF-8. Are you sure this is a valid comm?"); } - //Build and send message + // Build and send message os.write(commBytes.length); os.write(commBytes); sendUDP(os.toByteArray()); } - /** * Sets the port on which the client will receive data from X-Plane. * * @param port The new incoming port number. * @throws IOException If the command cannot be sent. */ - public void setCONN(int port) throws IOException - { - if(port < 0 || port >= 0xFFFF) - { + public void setCONN(int port) throws IOException { + if (port < 0 || port >= 0xFFFF) { throw new IllegalArgumentException("Invalid port (must be non-negative and less than 65536)."); } ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write("CONN".getBytes(StandardCharsets.UTF_8)); - os.write(0xFF); //Placeholder for message length + os.write(0xFF); // Placeholder for message length os.write((byte) port); os.write((byte) (port >> 8)); sendUDP(os.toByteArray()); diff --git a/Java/ProjectModels/Main.java b/Java/ProjectModels/Main.java index e9e7fb3..1a78a46 100644 --- a/Java/ProjectModels/Main.java +++ b/Java/ProjectModels/Main.java @@ -25,7 +25,7 @@ import ModelFiles.XPlaneConnect; public class Main { public static void main(String[] args) { - //Encapsulation (or lack thereof) Test + // Encapsulation (or lack thereof) Test Model m = new Model(); MindQueue q = m.getQueue(); Action a = new Vision(); @@ -34,46 +34,43 @@ public class Main { MindQueue q2 = m.getQueue(); System.out.println(q2.pop()); - //Using actions - try(XPlaneConnect xpc = new XPlaneConnect()) { - Action b = new Vision(10,"sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); - Action d = new Delay(20); + // Using actions + try (XPlaneConnect xpc = new XPlaneConnect()) { + Action b = new Vision(10, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); + Action d = new Delay(20); // Ensure connection established. - m = new Model(xpc); - m.activateModel(); - m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); - m.createAction(ActionType.MOTOR, MotorType.PITCHUP, 0, null); - // m.push(b); - // m.push(d); - m.printModelQueue(); - // m.deactivateModel(); - while(m.isActive() && !m.isEmpty()) { + m = new Model(xpc); + m.activateModel(); + m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); + m.createAction(ActionType.MOTOR, MotorType.PITCHUP, 0, null); + // m.push(b); + // m.push(d); + m.printModelQueue(); + // m.deactivateModel(); + while (m.isActive() && !m.isEmpty()) { float[] array = m.next(); - if(array != null) { + if (array != null) { // System.out.println(array[0]); // if(array[0] > 80.0f) { - // m.push(d); + // m.push(d); // } } else { // System.out.println("no dice"); // System.out.println(xpc.getDREF(null)"); } - if(m.getModelQueueLength() < 5){ + if (m.getModelQueueLength() < 5) { m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); m.createAction(ActionType.MOTOR, MotorType.PITCHUP, 0, null); m.createAction(ActionType.MOTOR, MotorType.PITCHDOWN, 0, null); } - + // m.printModelQueue(); } - } - catch (SocketException ex) - { + } catch (SocketException ex) { System.out.println("Unable to set up the connection. (Error message was '" + ex.getMessage() + "'.)"); - } - catch (IOException ex) - { - System.out.println("Something went wrong with one of the commands. (Error message was '" + ex.getMessage() + "'.)"); + } catch (IOException ex) { + System.out.println( + "Something went wrong with one of the commands. (Error message was '" + ex.getMessage() + "'.)"); } System.out.println("Exiting"); }