Added support for sending multiple datarefs to the Java client.
This commit is contained in:
@@ -331,8 +331,37 @@ public class XPlaneConnect implements AutoCloseable
|
||||
* @throws IOException If the command cannot be sent.
|
||||
*/
|
||||
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.
|
||||
* @throws IOException If the command cannot be sent.
|
||||
*/
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
String dref = drefs[i];
|
||||
float[] value = values[i];
|
||||
|
||||
if (dref == null)
|
||||
{
|
||||
throw new IllegalArgumentException("dref must be a valid string.");
|
||||
@@ -355,19 +384,17 @@ public class XPlaneConnect implements AutoCloseable
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(4 * value.length);
|
||||
bb.order(ByteOrder.LITTLE_ENDIAN);
|
||||
for(int i = 0; i < value.length; ++i)
|
||||
for (int j = 0; j < value.length; ++j)
|
||||
{
|
||||
bb.putFloat(i * 4, value[i]);
|
||||
bb.putFloat(j * 4, value[j]);
|
||||
}
|
||||
|
||||
//Build and send message
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
os.write("DREF".getBytes(StandardCharsets.UTF_8));
|
||||
os.write(0xFF); //Placeholder for message length
|
||||
os.write(drefBytes.length);
|
||||
os.write(drefBytes, 0, drefBytes.length);
|
||||
os.write(value.length);
|
||||
os.write(bb.array());
|
||||
}
|
||||
sendUDP(os.toByteArray());
|
||||
}
|
||||
|
||||
|
||||
@@ -333,6 +333,33 @@ public class XPlaneConnectTest
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendDREFs() throws IOException
|
||||
{
|
||||
String[] drefs =
|
||||
{
|
||||
"sim/cockpit/switches/gear_handle_status",
|
||||
"sim/cockpit/autopilot/altitude"
|
||||
};
|
||||
try(XPlaneConnect xpc = new XPlaneConnect())
|
||||
{
|
||||
float[][] values = {{1}, {2000}};
|
||||
xpc.sendDREFs(drefs, values);
|
||||
|
||||
float[][] result = xpc.getDREFs(drefs);
|
||||
assertEquals(values[0][0], result[0][0], 1e-4);
|
||||
assertEquals(values[1][0], result[1][0], 1e-4);
|
||||
|
||||
values[0][0] = 0;
|
||||
values[1][0] = 4000;
|
||||
xpc.sendDREFs(drefs, values);
|
||||
|
||||
result = xpc.getDREFs(drefs);
|
||||
assertEquals(values[0][0], result[0][0], 1e-4);
|
||||
assertEquals(values[1][0], result[1][0], 1e-4);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSendDREF_NullDREF() throws IOException
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user