1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-30 15:52:12 +01:00

AndroidGCS: Whitespace fixing for TelemetryMonitor

This commit is contained in:
James Cotton 2012-08-10 01:11:04 -05:00
parent 78469aa6a4
commit 5eabb1777d

View File

@ -60,11 +60,15 @@ public class TelemetryMonitor extends Observable{
private boolean connected = false; private boolean connected = false;
private boolean objects_updated = false; private boolean objects_updated = false;
public boolean getConnected() { return connected; }; public boolean getConnected() {
public boolean getObjectsUpdated() { return objects_updated; }; return connected;
};
public TelemetryMonitor(UAVObjectManager objMngr, Telemetry tel) public boolean getObjectsUpdated() {
{ return objects_updated;
};
public TelemetryMonitor(UAVObjectManager objMngr, Telemetry tel) {
this.objMngr = objMngr; this.objMngr = objMngr;
this.tel = tel; this.tel = tel;
// this.objPending = null; // this.objPending = null;
@ -81,7 +85,8 @@ public class TelemetryMonitor extends Observable{
flightStatsUpdated((UAVObject) data); flightStatsUpdated((UAVObject) data);
} catch (IOException e) { } catch (IOException e) {
// The UAVTalk stream was broken, disconnect this signal // The UAVTalk stream was broken, disconnect this signal
// TODO: Should this actually be disconnected. Do we create a new TelemetryMonitor for this // TODO: Should this actually be disconnected. Do we create
// a new TelemetryMonitor for this
// or fix the stream? // or fix the stream?
flightStatsObj.removeUpdatedObserver(this); flightStatsObj.removeUpdatedObserver(this);
} }
@ -94,67 +99,63 @@ public class TelemetryMonitor extends Observable{
/** /**
* Initiate object retrieval, initialize queue with objects to be retrieved. * Initiate object retrieval, initialize queue with objects to be retrieved.
*
* @throws IOException * @throws IOException
*/ */
public synchronized void startRetrievingObjects() throws IOException public synchronized void startRetrievingObjects() throws IOException {
{ if (DEBUG)
if (DEBUG) Log.d(TAG, "Start retrieving objects"); Log.d(TAG, "Start retrieving objects");
// Clear object queue // Clear object queue
queue.clear(); queue.clear();
// Get all objects, add metaobjects, settings and data objects with OnChange update mode to the queue // Get all objects, add metaobjects, settings and data objects with
// OnChange update mode to the queue
List<List<UAVObject>> objs = objMngr.getObjects(); List<List<UAVObject>> objs = objMngr.getObjects();
ListIterator<List<UAVObject>> objListIterator = objs.listIterator(); ListIterator<List<UAVObject>> objListIterator = objs.listIterator();
while( objListIterator.hasNext() ) while (objListIterator.hasNext()) {
{
List<UAVObject> instList = objListIterator.next(); List<UAVObject> instList = objListIterator.next();
UAVObject obj = instList.get(0); UAVObject obj = instList.get(0);
UAVObject.Metadata mdata = obj.getMetadata(); UAVObject.Metadata mdata = obj.getMetadata();
if ( obj.isMetadata() ) if (obj.isMetadata()) {
{
queue.add(obj); queue.add(obj);
} } else /* Data object */
else /* Data object */
{ {
UAVDataObject dobj = (UAVDataObject) obj; UAVDataObject dobj = (UAVDataObject) obj;
if ( dobj.isSettings() ) if (dobj.isSettings()) {
{
queue.add(obj); queue.add(obj);
} } else {
else if (mdata.GetFlightTelemetryUpdateMode() == UAVObject.UpdateMode.UPDATEMODE_ONCHANGE) {
{
if ( mdata.GetFlightTelemetryUpdateMode() == UAVObject.UpdateMode.UPDATEMODE_ONCHANGE )
{
queue.add(obj); queue.add(obj);
} }
} }
} }
} }
// Start retrieving // Start retrieving
System.out.println(TAG + "Starting to retrieve meta and settings objects from the autopilot (" + queue.size() + " objects)"); Log.d(TAG,
"Starting to retrieve meta and settings objects from the autopilot ("
+ queue.size() + " objects)");
retrieveNextObject(); retrieveNextObject();
} }
/** /**
* Cancel the object retrieval * Cancel the object retrieval
*/ */
public void stopRetrievingObjects() public void stopRetrievingObjects() {
{
Log.d(TAG, "Stop retrieving objects"); Log.d(TAG, "Stop retrieving objects");
queue.clear(); queue.clear();
} }
/** /**
* Retrieve the next object in the queue * Retrieve the next object in the queue
*
* @throws IOException * @throws IOException
*/ */
public synchronized void retrieveNextObject() throws IOException public synchronized void retrieveNextObject() throws IOException {
{
// If queue is empty return // If queue is empty return
if ( queue.isEmpty() ) if (queue.isEmpty()) {
{ if (DEBUG)
if (DEBUG) Log.d(TAG, "All objects retrieved: Connected Successfully"); Log.d(TAG, "All objects retrieved: Connected Successfully");
objects_updated = true; objects_updated = true;
setChanged(); setChanged();
notifyObservers(); notifyObservers();
@ -167,19 +168,24 @@ public class TelemetryMonitor extends Observable{
throw new Error("Got null object forom transaction queue"); throw new Error("Got null object forom transaction queue");
} }
if (DEBUG) Log.d(TAG, "Retrieving object: " + obj.getName()) ; if (DEBUG)
// Connect to object Log.d(TAG, "Retrieving object: " + obj.getName());
// TODO: Does this need to stay here permanently? This appears to be used for setup mainly // TODO: Does this need to stay here permanently? This appears to be
// used for setup mainly
obj.addTransactionCompleted(new Observer() { obj.addTransactionCompleted(new Observer() {
@Override @Override
public void update(Observable observable, Object data) { public void update(Observable observable, Object data) {
UAVObject.TransactionResult result = (UAVObject.TransactionResult) data; UAVObject.TransactionResult result = (UAVObject.TransactionResult) data;
if (DEBUG) Log.d(TAG,"Got transaction completed event from " + result.obj.getName() + " status: " + result.success); if (DEBUG)
Log.d(TAG, "Got transaction completed event from "
+ result.obj.getName() + " status: "
+ result.success);
try { try {
transactionCompleted(result.obj, result.success); transactionCompleted(result.obj, result.success);
} catch (IOException e) { } catch (IOException e) {
// When the telemetry stream is broken disconnect these updates // When the telemetry stream is broken disconnect these
// updates
observable.deleteObserver(this); observable.deleteObserver(this);
} }
} }
@ -187,68 +193,72 @@ public class TelemetryMonitor extends Observable{
// Request update // Request update
tel.updateRequested(obj); tel.updateRequested(obj);
// objPending = obj;
} }
/** /**
* Called by the retrieved object when a transaction is completed. * Called by the retrieved object when a transaction is completed.
*
* @throws IOException * @throws IOException
*/ */
public synchronized void transactionCompleted(UAVObject obj, boolean success) throws IOException public synchronized void transactionCompleted(UAVObject obj, boolean success)
{ throws IOException {
//QMutexLocker locker(mutex); if (DEBUG)
// Disconnect from sending object Log.d(TAG, "transactionCompleted. Status: " + success);
if (DEBUG) Log.d(TAG,"transactionCompleted. Status: " + success);
// TODO: Need to be able to disconnect signals
//obj->disconnect(this);
// objPending = null;
if (!success) { if (!success) {
//Log.e(TAG, "Transaction failed: " + obj.getName() + " sending again."); // Right now success = false means received a NAK so don't
return; // re-attempt
Log.e(TAG, "Transaction failed.");
} }
// Process next object if telemetry is still available // Process next object if telemetry is still available
if ( ((String) gcsStatsObj.getField("Status").getValue()).compareTo("Connected") == 0 ) if (((String) gcsStatsObj.getField("Status").getValue())
{ .compareTo("Connected") == 0) {
retrieveNextObject(); retrieveNextObject();
} } else {
else
{
stopRetrievingObjects(); stopRetrievingObjects();
} }
} }
/** /**
* Called each time the flight stats object is updated by the autopilot * Called each time the flight stats object is updated by the autopilot
*
* @throws IOException * @throws IOException
*/ */
public synchronized void flightStatsUpdated(UAVObject obj) throws IOException public synchronized void flightStatsUpdated(UAVObject obj)
{ throws IOException {
// Force update if not yet connected // Force update if not yet connected
gcsStatsObj = objMngr.getObject("GCSTelemetryStats"); gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats"); flightStatsObj = objMngr.getObject("FlightTelemetryStats");
if (DEBUG) Log.d(TAG,"GCS Status: " + gcsStatsObj.getField("Status").getValue()); if (DEBUG)
if (DEBUG) Log.d(TAG,"Flight Status: " + flightStatsObj.getField("Status").getValue()); Log.d(TAG, "GCS Status: "
if ( ((String) gcsStatsObj.getField("Status").getValue()).compareTo("Connected") != 0 || + gcsStatsObj.getField("Status").getValue());
((String) flightStatsObj.getField("Status").getValue()).compareTo("Connected") == 0 ) if (DEBUG)
{ Log.d(TAG, "Flight Status: "
+ flightStatsObj.getField("Status").getValue());
if (((String) gcsStatsObj.getField("Status").getValue())
.compareTo("Connected") != 0
|| ((String) flightStatsObj.getField("Status").getValue())
.compareTo("Connected") == 0) {
processStatsUpdates(); processStatsUpdates();
} }
} }
private long lastStatsTime; private long lastStatsTime;
/** /**
* Called periodically to update the statistics and connection status. * Called periodically to update the statistics and connection status.
*
* @throws IOException * @throws IOException
*/ */
public synchronized void processStatsUpdates() throws IOException public synchronized void processStatsUpdates() throws IOException {
{
// Get telemetry stats // Get telemetry stats
if (DEBUG) Log.d(TAG, "processStatsUpdates()"); if (DEBUG)
Log.d(TAG, "processStatsUpdates()");
Telemetry.TelemetryStats telStats = tel.getStats(); Telemetry.TelemetryStats telStats = tel.getStats();
if (DEBUG) Log.d(TAG, "processStatsUpdates() - stats reset"); if (DEBUG)
Log.d(TAG, "processStatsUpdates() - stats reset");
// Need to compute time because this update is not regular enough // Need to compute time because this update is not regular enough
float dT = (System.currentTimeMillis() - lastStatsTime) / 1000.0f; float dT = (System.currentTimeMillis() - lastStatsTime) / 1000.0f;
@ -266,21 +276,18 @@ public class TelemetryMonitor extends Observable{
tel.resetStats(); tel.resetStats();
if (DEBUG) Log.d(TAG, "processStatsUpdates() - stats updated"); if (DEBUG)
Log.d(TAG, "processStatsUpdates() - stats updated");
// Check for a connection timeout // Check for a connection timeout
boolean connectionTimeout; boolean connectionTimeout;
if ( telStats.rxObjects > 0 ) if (telStats.rxObjects > 0) {
{
lastUpdateTime = System.currentTimeMillis(); lastUpdateTime = System.currentTimeMillis();
} }
if ( (System.currentTimeMillis() - lastUpdateTime) > CONNECTION_TIMEOUT_MS ) if ((System.currentTimeMillis() - lastUpdateTime) > CONNECTION_TIMEOUT_MS) {
{
connectionTimeout = true; connectionTimeout = true;
} } else {
else
{
connectionTimeout = false; connectionTimeout = false;
} }
@ -288,33 +295,32 @@ public class TelemetryMonitor extends Observable{
gcsStatsObj = objMngr.getObject("GCSTelemetryStats"); gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats"); flightStatsObj = objMngr.getObject("FlightTelemetryStats");
if (gcsStatsObj == null) { if (gcsStatsObj == null) {
System.out.println("No GCS stats yet"); Log.d(TAG, "No GCS stats yet");
return; return;
} }
UAVObjectField statusField = gcsStatsObj.getField("Status"); UAVObjectField statusField = gcsStatsObj.getField("Status");
String oldStatus = new String((String) statusField.getValue()); String oldStatus = new String((String) statusField.getValue());
if (DEBUG) Log.d(TAG,"GCS: " + statusField.getValue() + " Flight: " + flightStatsObj.getField("Status").getValue()); if (DEBUG)
Log.d(TAG, "GCS: " + statusField.getValue() + " Flight: "
+ flightStatsObj.getField("Status").getValue());
if ( oldStatus.compareTo("Disconnected") == 0 ) if (oldStatus.compareTo("Disconnected") == 0) {
{
// Request connection // Request connection
statusField.setValue("HandshakeReq"); statusField.setValue("HandshakeReq");
} } else if (oldStatus.compareTo("HandshakeReq") == 0) {
else if ( oldStatus.compareTo("HandshakeReq") == 0 )
{
// Check for connection acknowledge // Check for connection acknowledge
if ( ((String) flightStatsObj.getField("Status").getValue()).compareTo("HandshakeAck") == 0 ) if (((String) flightStatsObj.getField("Status").getValue())
{ .compareTo("HandshakeAck") == 0) {
statusField.setValue("Connected"); statusField.setValue("Connected");
if (DEBUG) Log.d(TAG,"Connected" + statusField.toString()); if (DEBUG)
Log.d(TAG, "Connected" + statusField.toString());
} }
} } else if (oldStatus.compareTo("Connected") == 0) {
else if ( oldStatus.compareTo("Connected") == 0 ) // Check if the connection is still active and the the autopilot is
{ // still connected
// Check if the connection is still active and the the autopilot is still connected if (((String) flightStatsObj.getField("Status").getValue())
if ( ((String) flightStatsObj.getField("Status").getValue()).compareTo("Disconnected") == 0 || connectionTimeout) .compareTo("Disconnected") == 0 || connectionTimeout) {
{
statusField.setValue("Disconnected"); statusField.setValue("Disconnected");
} }
} }
@ -324,36 +330,39 @@ public class TelemetryMonitor extends Observable{
boolean gcsConnected = statusField.getValue().equals("Connected"); boolean gcsConnected = statusField.getValue().equals("Connected");
boolean gcsDisconnected = statusField.getValue().equals("Disconnected"); boolean gcsDisconnected = statusField.getValue().equals("Disconnected");
boolean flightConnected = flightStatsObj.getField("Status").equals("Connected"); boolean flightConnected = flightStatsObj.getField("Status").equals(
"Connected");
if ( !gcsConnected || !flightConnected ) if (!gcsConnected || !flightConnected) {
{ if (DEBUG)
if (DEBUG) Log.d(TAG,"Sending gcs status"); Log.d(TAG, "Sending gcs status");
gcsStatsObj.updated(); gcsStatsObj.updated();
} }
// Act on new connections or disconnections // Act on new connections or disconnections
if (gcsConnected && gcsStatusChanged) if (gcsConnected && gcsStatusChanged) {
{ if (DEBUG)
if (DEBUG) Log.d(TAG,"Connection with the autopilot established"); Log.d(TAG, "Connection with the autopilot established");
setPeriod(STATS_UPDATE_PERIOD_MS); setPeriod(STATS_UPDATE_PERIOD_MS);
connected = true; connected = true;
objects_updated = false; objects_updated = false;
startRetrievingObjects(); startRetrievingObjects();
setChanged(); setChanged();
} }
if (gcsDisconnected && gcsStatusChanged) if (gcsDisconnected && gcsStatusChanged) {
{ if (DEBUG)
if (DEBUG) Log.d(TAG,"Trying to connect to the autopilot"); Log.d(TAG, "Trying to connect to the autopilot");
setPeriod(STATS_CONNECT_PERIOD_MS); setPeriod(STATS_CONNECT_PERIOD_MS);
connected = false; connected = false;
objects_updated = false; objects_updated = false;
setChanged(); setChanged();
} }
if (DEBUG) Log.d(TAG, "processStatsUpdates() - before notify"); if (DEBUG)
Log.d(TAG, "processStatsUpdates() - before notify");
notifyObservers(); notifyObservers();
if (DEBUG) Log.d(TAG, "processStatsUpdates() - after notify"); if (DEBUG)
Log.d(TAG, "processStatsUpdates() - after notify");
} }
private void setPeriod(int ms) { private void setPeriod(int ms) {
@ -369,15 +378,15 @@ public class TelemetryMonitor extends Observable{
try { try {
processStatsUpdates(); processStatsUpdates();
} catch (IOException e) { } catch (IOException e) {
// Once the stream has died stop trying to process these updates // Once the stream has died stop trying to process these
// updates
periodicTask.cancel(); periodicTask.cancel();
} }
} }
}, currentPeriod, currentPeriod); }, currentPeriod, currentPeriod);
} }
public void stopMonitor() public void stopMonitor() {
{
periodicTask.cancel(); periodicTask.cancel();
periodicTask = null; periodicTask = null;
} }