documents reformatted

This commit is contained in:
cs-powell
2024-11-25 13:49:01 -05:00
parent cc17797319
commit 88967419c6
12 changed files with 490 additions and 542 deletions

View File

@@ -1,10 +1,7 @@
package ModelFiles;
public abstract class Action {
Integer priority; // Priority of the Task
Integer delay; // Potential Delay before starting the task
String targetDREF;
@@ -18,7 +15,6 @@ public Action(ActionType actionType, int delay){
this.targetDREF = null;
}
public Action(ActionType actionType, int delay, String target) {
this.actionType = actionType;
this.delay = delay;
@@ -29,7 +25,6 @@ public ActionType getType(){
return actionType;
}
public Integer getDelay() {
return delay;
}

View File

@@ -1,9 +1,7 @@
package ModelFiles;
public enum ActionType {
VISION,
MOTOR,
DELAY,
}

View File

@@ -1,7 +1,6 @@
package ModelFiles;
// package ProjectModels.CognitiveModel;
public class Delay extends Action {
public Delay() {

View File

@@ -15,7 +15,6 @@ public class MindQueue {
q.add(e);
}
public Action removeEvent(Action e) {
Action temp = e;
q.remove(e);
@@ -55,5 +54,4 @@ public class MindQueue {
return q.size();
}
}

View File

@@ -1,4 +1,5 @@
package ModelFiles;
import java.io.IOException;
// import ModelFiles.ActionType;
@@ -23,7 +24,6 @@ public class Model {
this.xpc = xpc;
}
/* Getters */
public MindQueue getQueue() {
return q;
@@ -135,6 +135,7 @@ public class Model {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
switch (motorType) {

View File

@@ -1,6 +1,6 @@
package ModelFiles;
import java.io.IOException;
import java.io.IOException;
public class Motor extends Action {
@@ -16,18 +16,15 @@ public class Motor extends Action {
// super(ActionType.MOTOR,delay);
// }
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;
}
@@ -44,5 +41,4 @@ public void sendMotorControl(XPlaneConnect xpc) {
}
}
}

View File

@@ -1,10 +1,9 @@
package ModelFiles;
public class Process {
public class Process {
public Process() {
}
}

View File

@@ -1,4 +1,5 @@
package ModelFiles;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

View File

@@ -2,7 +2,6 @@ package ModelFiles;
public class Vision extends Action {
public Vision() {
super(ActionType.VISION, 1000);
}

View File

@@ -37,14 +37,14 @@ 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
* @version 0.1
* @since 2015-03-31
*/
public class XPlaneConnect implements AutoCloseable
{
public class XPlaneConnect implements AutoCloseable {
// private static int clientNum;
private DatagramSocket socket;
private InetAddress xplaneAddr;
@@ -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 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,8 +228,7 @@ 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
{
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);
@@ -232,14 +238,14 @@ public class XPlaneConnect implements AutoCloseable
/**
* 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.
* @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].");
}
@@ -256,8 +262,7 @@ public class XPlaneConnect implements AutoCloseable
* @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
{
public float[] getDREF(String dref) throws IOException {
return getDREFs(new String[] { dref })[0];
}
@@ -265,33 +270,29 @@ public class XPlaneConnect implements AutoCloseable
* 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
{
public float[][] getDREFs(String[] drefs) throws IOException {
// Preconditions
if(drefs == null || drefs.length == 0)
{
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.
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?");
}
}
@@ -300,8 +301,7 @@ public class XPlaneConnect implements AutoCloseable
os.write("GETD".getBytes(StandardCharsets.UTF_8));
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);
}
@@ -309,20 +309,17 @@ public class XPlaneConnect implements AutoCloseable
// 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
{
@@ -333,8 +330,7 @@ public class XPlaneConnect implements AutoCloseable
return result;
}
public void sendDREF(String dref, float value) throws IOException
{
public void sendDREF(String dref, float value) throws IOException {
sendDREF(dref, new float[] { value });
}
@@ -342,11 +338,11 @@ public class XPlaneConnect implements AutoCloseable
* 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
{
public void sendDREF(String dref, float[] value) throws IOException {
sendDREFs(new String[] { dref }, new float[][] { value });
}
@@ -354,53 +350,46 @@ public class XPlaneConnect implements AutoCloseable
* 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
{
public void sendDREFs(String[] drefs, float[][] values) throws IOException {
// Preconditions
if(drefs == null || drefs.length == 0)
{
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)
{
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.
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]);
}
@@ -417,11 +406,12 @@ 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));
@@ -431,12 +421,10 @@ public class XPlaneConnect implements AutoCloseable
// 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,7 +445,11 @@ public class XPlaneConnect implements AutoCloseable
/**
* Sends command to X-Plane setting control surfaces on the player ac.
*
* @param values <p>An array containing zero to six values representing control surface data as follows:</p>
* @param values
* <p>
* An array containing zero to six values representing control
* surface data as follows:
* </p>
* <ol>
* <li>Latitudinal Stick [-1,1]</li>
* <li>Longitudinal Stick [-1,1]</li>
@@ -468,21 +460,26 @@ public class XPlaneConnect implements AutoCloseable
* <li>Speedbrakes [-0.5, 1.5]</li>
* </ol>
* <p>
* 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
* 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.
* </p>
* @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 <p>An array containing zero to six values representing control surface data as follows:</p>
* @param values
* <p>
* An array containing zero to six values representing control
* surface data as follows:
* </p>
* <ol>
* <li>Latitudinal Stick [-1,1]</li>
* <li>Longitudinal Stick [-1,1]</li>
@@ -493,26 +490,24 @@ public class XPlaneConnect implements AutoCloseable
* <li>Speedbrakes [-0.5, 1.5]</li>
* </ol>
* <p>
* 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
* 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.
* </p>
* @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
{
public void sendCTRL(float[] values, int ac) throws IOException {
// Preconditions
if(values == null)
{
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.");
}
@@ -521,27 +516,18 @@ public class XPlaneConnect implements AutoCloseable
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)
{
for (i = 0; i < 6; ++i) {
if (i == 4) {
if (i >= values.length) {
bb.put(cur, (byte) -1);
}
else
{
} 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
{
} else {
bb.putFloat(cur, values[i]);
cur += 4;
}
@@ -561,11 +547,12 @@ 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));
@@ -575,12 +562,10 @@ public class XPlaneConnect implements AutoCloseable
// 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,7 +582,10 @@ public class XPlaneConnect implements AutoCloseable
/**
* Sets the position of the player ac.
*
* @param values <p>An array containing position elements as follows:</p>
* @param values
* <p>
* An array containing position elements as follows:
* </p>
* <ol>
* <li>Latitude (deg)</li>
* <li>Longitude (deg)</li>
@@ -609,21 +596,25 @@ public class XPlaneConnect implements AutoCloseable
* <li>Gear (0=up, 1=down)</li>
* </ol>
* <p>
* 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
* 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.
* </p>
* @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 <p>An array containing position elements as follows:</p>
* @param values
* <p>
* An array containing position elements as follows:
* </p>
* <ol>
* <li>Latitude (deg)</li>
* <li>Longitude (deg)</li>
@@ -634,26 +625,24 @@ public class XPlaneConnect implements AutoCloseable
* <li>Gear (0=up, 1=down)</li>
* </ol>
* <p>
* 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
* 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.
* </p>
* @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
{
public void sendPOSI(double[] values, int ac) throws IOException {
// Preconditions
if(values == null)
{
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.");
}
@@ -661,19 +650,15 @@ public class XPlaneConnect implements AutoCloseable
int i;
ByteBuffer bb = ByteBuffer.allocate(40);
bb.order(ByteOrder.LITTLE_ENDIAN);
for(i = 0; i < values.length; ++i)
{
for (i = 0; i < values.length; ++i) {
if (i < 3) /* lat/lon/height as double */
{
bb.putDouble(values[i]);
}
else
{
} else {
bb.putFloat((float) values[i]);
}
}
for(; i < 7; ++i)
{
for (; i < 7; ++i) {
bb.putFloat(-998);
}
@@ -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,29 +698,24 @@ 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
{
public void sendDATA(float[][] data) throws IOException {
// Preconditions
if(data == null || data.length == 0)
{
if (data == null || data.length == 0) {
throw new IllegalArgumentException("data must be a non-null, non-empty array.");
}
// 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]);
}
}
@@ -757,19 +734,16 @@ 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
{
public void selectDATA(int[] rows) throws IOException {
// Preconditions
if(rows == null || rows.length == 0)
{
if (rows == null || rows.length == 0) {
throw new IllegalArgumentException("rows must be a non-null, non-empty array.");
}
// 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]);
}
@@ -782,36 +756,36 @@ public class XPlaneConnect implements AutoCloseable
}
/**
* 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
{
public void sendTEXT(String msg, int x, int y) throws IOException {
// Preconditions
if(msg == null)
{
if (msg == null) {
msg = "";
}
// 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.");
}
@@ -836,8 +810,7 @@ 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());
@@ -851,31 +824,29 @@ public class XPlaneConnect implements AutoCloseable
}
/**
* 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.
*/
public void sendWYPT(WaypointOp op, float[] points) throws IOException
{
public void sendWYPT(WaypointOp op, float[] points) throws IOException {
// Preconditions
if(points.length % 3 != 0)
{
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
ByteBuffer bb = ByteBuffer.allocate(4 * points.length);
bb.order(ByteOrder.LITTLE_ENDIAN);
for(float f : points)
{
for (float f : points) {
bb.putFloat(f);
}
@@ -895,11 +866,9 @@ 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
{
public void sendCOMM(String comm) throws IOException {
// Preconditions
if(comm == null || comm.length() == 0)
{
if (comm == null || comm.length() == 0) {
throw new IllegalArgumentException(("comm must be non-empty."));
}
@@ -909,13 +878,12 @@ public class XPlaneConnect implements AutoCloseable
// 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
@@ -924,17 +892,14 @@ public class XPlaneConnect implements AutoCloseable
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).");
}

View File

@@ -66,14 +66,11 @@ public class Main {
// 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");
}