2011-03-07 01:22:11 +01:00
|
|
|
package org.openpilot.uavtalk;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
2011-03-10 20:44:40 +01:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2011-03-07 01:22:11 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.Socket;
|
2011-03-07 11:54:43 +01:00
|
|
|
import java.util.Observable;
|
|
|
|
import java.util.Observer;
|
2011-03-07 01:22:11 +01:00
|
|
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.openpilot.uavtalk.uavobjects.UAVObjectsInitialize;
|
|
|
|
import org.openpilot.uavtalk.UAVTalk;
|
|
|
|
|
|
|
|
|
|
|
|
public class TalkTest {
|
|
|
|
|
|
|
|
static UAVObjectManager objMngr;
|
|
|
|
static final String IP_ADDRDESS = new String("127.0.0.1");
|
2011-03-07 11:54:43 +01:00
|
|
|
static final int PORT_NUM = 7777;
|
|
|
|
boolean succeed = false;
|
|
|
|
|
2011-03-10 21:36:37 +01:00
|
|
|
byte[] flightStatsConnected =
|
|
|
|
{0x3c,0x20,0x1d,0x00,
|
|
|
|
(byte) 0x5e,(byte) 0x26,(byte) 0x0c,(byte) 0x66,
|
|
|
|
0x03,0x00,0x00,0x00,
|
|
|
|
0x00,0x00,0x00,0x00,
|
|
|
|
0x00,0x00,0x00,0x00,
|
|
|
|
0x00,0x00,0x00,0x00,
|
|
|
|
0x00,0x00,0x00,0x00,
|
|
|
|
0x00,(byte) 0xAE};
|
|
|
|
|
2011-03-07 01:22:11 +01:00
|
|
|
@BeforeClass
|
|
|
|
public static void setUpBeforeClass() throws Exception {
|
|
|
|
objMngr = new UAVObjectManager();
|
|
|
|
UAVObjectsInitialize.register(objMngr);
|
|
|
|
}
|
|
|
|
|
2011-03-10 20:44:40 +01:00
|
|
|
//@Test
|
2011-03-07 11:54:43 +01:00
|
|
|
public void testGetFlightStatus() {
|
2011-03-07 01:22:11 +01:00
|
|
|
Socket connection = null;
|
|
|
|
UAVTalk talk = null;
|
|
|
|
try{
|
|
|
|
InetAddress ip = InetAddress.getByName(IP_ADDRDESS);
|
|
|
|
connection = new Socket(ip, PORT_NUM);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Couldn't connect to test platform");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
talk = new UAVTalk(connection.getInputStream(), connection.getOutputStream(), objMngr);
|
|
|
|
} catch (IOException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
fail("Couldn't construct UAVTalk object");
|
|
|
|
}
|
|
|
|
|
2011-03-07 11:54:43 +01:00
|
|
|
Thread inputStream = talk.getInputProcessThread();
|
|
|
|
inputStream.start();
|
|
|
|
|
|
|
|
succeed = false;
|
|
|
|
|
|
|
|
UAVObject obj = objMngr.getObject("FlightTelemetryStats");
|
|
|
|
|
|
|
|
obj.addUpdatedObserver( new Observer() {
|
|
|
|
public void update(Observable observable, Object data) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
System.out.println("Updated: " + data.toString());
|
|
|
|
succeed = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
} catch (InterruptedException e1) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!succeed)
|
|
|
|
fail("Never received a FlightTelemetryStats update");
|
|
|
|
|
2011-03-07 01:22:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2012-08-06 04:52:09 +02:00
|
|
|
public void testSendObjectRequest() throws IOException {
|
2011-03-10 20:44:40 +01:00
|
|
|
ByteArrayInputStream is = new ByteArrayInputStream(new byte[0], 0, 0);
|
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream(100);
|
|
|
|
|
|
|
|
UAVTalk talk = new UAVTalk(is,os,objMngr);
|
|
|
|
UAVObject obj = objMngr.getObject("FlightTelemetryStats");
|
|
|
|
obj.getField("Status").setValue("Connected");
|
|
|
|
|
|
|
|
talk.sendObject(obj, false, false);
|
2011-03-10 21:36:37 +01:00
|
|
|
|
2011-03-10 20:44:40 +01:00
|
|
|
System.out.println("Size: " + os.size());
|
|
|
|
byte [] array = os.toByteArray();
|
|
|
|
for(int i = 0; i < array.length; i++) {
|
|
|
|
System.out.print("0x" + Integer.toHexString((int) array[i] & 0xff));
|
2011-03-10 21:36:37 +01:00
|
|
|
System.out.print("/0x" + Integer.toHexString((int) flightStatsConnected[i] & 0xff));
|
2011-03-10 20:44:40 +01:00
|
|
|
if(i != array.length-1)
|
2011-03-10 21:36:37 +01:00
|
|
|
System.out.print("\n");
|
2011-03-10 20:44:40 +01:00
|
|
|
}
|
|
|
|
System.out.print("\n");
|
2011-03-10 21:36:37 +01:00
|
|
|
for(int i = 0; i < array.length; i++)
|
|
|
|
assertEquals(os.toByteArray()[i], flightStatsConnected[i]);
|
2011-03-07 01:22:11 +01:00
|
|
|
}
|
2011-03-10 20:44:40 +01:00
|
|
|
|
2011-03-07 01:22:11 +01:00
|
|
|
@Test
|
2011-03-10 21:36:37 +01:00
|
|
|
public void testReceiveObject() throws InterruptedException {
|
|
|
|
ByteArrayInputStream is = new ByteArrayInputStream(flightStatsConnected, 0, flightStatsConnected.length);
|
2011-03-10 20:44:40 +01:00
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream(100);
|
|
|
|
|
2011-03-10 21:36:37 +01:00
|
|
|
// Make the Status wrong initially
|
2011-03-10 20:44:40 +01:00
|
|
|
UAVObject obj = objMngr.getObject("FlightTelemetryStats");
|
|
|
|
obj.getField("Status").setValue("Disconnected");
|
|
|
|
|
|
|
|
// Test receiving from that stream
|
2011-03-10 21:36:37 +01:00
|
|
|
UAVTalk talk = new UAVTalk(is,os,objMngr);
|
2011-03-10 20:44:40 +01:00
|
|
|
Thread inputStream = talk.getInputProcessThread();
|
|
|
|
inputStream.start();
|
|
|
|
|
2011-03-10 21:36:37 +01:00
|
|
|
Thread.sleep(1000);
|
|
|
|
|
2011-03-10 20:44:40 +01:00
|
|
|
System.out.println("Should be FlightTelemetry Stats:");
|
|
|
|
System.out.println(objMngr.getObject("FlightTelemetryStats").toString());
|
2011-03-07 01:22:11 +01:00
|
|
|
|
2011-03-10 21:36:37 +01:00
|
|
|
assertEquals(obj.getField("Status").getValue(), new String("Connected"));
|
2011-03-07 01:22:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-10 20:44:40 +01:00
|
|
|
|
2011-03-07 01:22:11 +01:00
|
|
|
}
|