1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-29 14: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

@ -36,20 +36,20 @@ import java.util.TimerTask;
import android.util.Log; import android.util.Log;
public class TelemetryMonitor extends Observable{ public class TelemetryMonitor extends Observable {
private static final String TAG = "TelemetryMonitor"; private static final String TAG = "TelemetryMonitor";
public static int LOGLEVEL = 0; public static int LOGLEVEL = 0;
public static boolean WARN = LOGLEVEL > 1; public static boolean WARN = LOGLEVEL > 1;
public static boolean DEBUG = LOGLEVEL > 0; public static boolean DEBUG = LOGLEVEL > 0;
static final int STATS_UPDATE_PERIOD_MS = 4000; static final int STATS_UPDATE_PERIOD_MS = 4000;
static final int STATS_CONNECT_PERIOD_MS = 1000; static final int STATS_CONNECT_PERIOD_MS = 1000;
static final int CONNECTION_TIMEOUT_MS = 8000; static final int CONNECTION_TIMEOUT_MS = 8000;
private final UAVObjectManager objMngr; private final UAVObjectManager objMngr;
private final Telemetry tel; private final Telemetry tel;
// private UAVObject objPending; // private UAVObject objPending;
private UAVObject gcsStatsObj; private UAVObject gcsStatsObj;
private UAVObject flightStatsObj; private UAVObject flightStatsObj;
private Timer periodicTask; private Timer periodicTask;
@ -60,324 +60,333 @@ 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;
this.objMngr = objMngr; };
this.tel = tel;
// this.objPending = null;
queue = new ArrayList<UAVObject>();
// Get stats objects public TelemetryMonitor(UAVObjectManager objMngr, Telemetry tel) {
gcsStatsObj = objMngr.getObject("GCSTelemetryStats"); this.objMngr = objMngr;
flightStatsObj = objMngr.getObject("FlightTelemetryStats"); this.tel = tel;
// this.objPending = null;
queue = new ArrayList<UAVObject>();
flightStatsObj.addUpdatedObserver(new Observer() { // Get stats objects
gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats");
flightStatsObj.addUpdatedObserver(new Observer() {
@Override @Override
public void update(Observable observable, Object data) { public void update(Observable observable, Object data) {
try { try {
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);
} }
} }
}); });
// Start update timer // Start update timer
setPeriod(STATS_CONNECT_PERIOD_MS); setPeriod(STATS_CONNECT_PERIOD_MS);
} }
/** /**
* 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
List< List<UAVObject> > objs = objMngr.getObjects(); // OnChange update mode to the queue
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);
{ } else /* Data object */
queue.add(obj); {
} UAVDataObject dobj = (UAVDataObject) obj;
else /* Data object */ if (dobj.isSettings()) {
{ queue.add(obj);
UAVDataObject dobj = (UAVDataObject) obj; } else {
if ( dobj.isSettings() ) if (mdata.GetFlightTelemetryUpdateMode() == UAVObject.UpdateMode.UPDATEMODE_ONCHANGE) {
{ queue.add(obj);
queue.add(obj); }
} }
else }
{ }
if ( mdata.GetFlightTelemetryUpdateMode() == UAVObject.UpdateMode.UPDATEMODE_ONCHANGE ) // Start retrieving
{ Log.d(TAG,
queue.add(obj); "Starting to retrieve meta and settings objects from the autopilot ("
} + queue.size() + " objects)");
} retrieveNextObject();
}
}
// Start retrieving
System.out.println(TAG + "Starting to retrieve meta and settings objects from the autopilot (" + queue.size() + " objects)");
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)
{ Log.d(TAG, "All objects retrieved: Connected Successfully");
if (DEBUG) Log.d(TAG, "All objects retrieved: Connected Successfully"); objects_updated = true;
objects_updated = true; setChanged();
setChanged(); notifyObservers();
notifyObservers(); return;
return; }
} // Get next object from the queue
// Get next object from the queue UAVObject obj = queue.remove(0);
UAVObject obj = queue.remove(0);
if(obj == null) { if (obj == null) {
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
obj.addTransactionCompleted(new Observer() { // used for setup mainly
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);
} }
} }
}); });
// 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: "
processStatsUpdates(); + flightStatsObj.getField("Status").getValue());
} if (((String) gcsStatsObj.getField("Status").getValue())
.compareTo("Connected") != 0
|| ((String) flightStatsObj.getField("Status").getValue())
.compareTo("Connected") == 0) {
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)
if (DEBUG) Log.d(TAG, "processStatsUpdates()"); 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;
lastStatsTime = System.currentTimeMillis(); lastStatsTime = System.currentTimeMillis();
// Update stats object // Update stats object
gcsStatsObj.getField("RxDataRate").setDouble( telStats.rxBytes / dT ); gcsStatsObj.getField("RxDataRate").setDouble(telStats.rxBytes / dT);
gcsStatsObj.getField("TxDataRate").setDouble( telStats.txBytes / dT ); gcsStatsObj.getField("TxDataRate").setDouble(telStats.txBytes / dT);
UAVObjectField field = gcsStatsObj.getField("RxFailures"); UAVObjectField field = gcsStatsObj.getField("RxFailures");
field.setDouble(field.getDouble() + telStats.rxErrors); field.setDouble(field.getDouble() + telStats.rxErrors);
field = gcsStatsObj.getField("TxFailures"); field = gcsStatsObj.getField("TxFailures");
field.setDouble(field.getDouble() + telStats.txErrors); field.setDouble(field.getDouble() + telStats.txErrors);
field = gcsStatsObj.getField("TxRetries"); field = gcsStatsObj.getField("TxRetries");
field.setDouble(field.getDouble() + telStats.txRetries); field.setDouble(field.getDouble() + telStats.txRetries);
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 {
} connectionTimeout = false;
else }
{
connectionTimeout = false;
}
// Update connection state // Update connection state
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) {
} // Check for connection acknowledge
else if ( oldStatus.compareTo("HandshakeReq") == 0 ) if (((String) flightStatsObj.getField("Status").getValue())
{ .compareTo("HandshakeAck") == 0) {
// Check for connection acknowledge statusField.setValue("Connected");
if ( ((String) flightStatsObj.getField("Status").getValue()).compareTo("HandshakeAck") == 0 ) if (DEBUG)
{ Log.d(TAG, "Connected" + statusField.toString());
statusField.setValue("Connected"); }
if (DEBUG) Log.d(TAG,"Connected" + statusField.toString()); } else if (oldStatus.compareTo("Connected") == 0) {
} // Check if the connection is still active and the the autopilot is
} // still connected
else if ( oldStatus.compareTo("Connected") == 0 ) if (((String) flightStatsObj.getField("Status").getValue())
{ .compareTo("Disconnected") == 0 || connectionTimeout) {
// Check if the connection is still active and the the autopilot is still connected statusField.setValue("Disconnected");
if ( ((String) flightStatsObj.getField("Status").getValue()).compareTo("Disconnected") == 0 || connectionTimeout) }
{ }
statusField.setValue("Disconnected");
}
}
// Force telemetry update if not yet connected // Force telemetry update if not yet connected
boolean gcsStatusChanged = !oldStatus.equals(statusField.getValue()); boolean gcsStatusChanged = !oldStatus.equals(statusField.getValue());
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)
notifyObservers(); Log.d(TAG, "processStatsUpdates() - before notify");
if (DEBUG) Log.d(TAG, "processStatsUpdates() - after notify"); notifyObservers();
if (DEBUG)
Log.d(TAG, "processStatsUpdates() - after notify");
} }
private void setPeriod(int ms) { private void setPeriod(int ms) {
if(periodicTask == null) if (periodicTask == null)
periodicTask = new Timer(); periodicTask = new Timer();
periodicTask.cancel(); periodicTask.cancel();
currentPeriod = ms; currentPeriod = ms;
periodicTask = new Timer(); periodicTask = new Timer();
periodicTask.scheduleAtFixedRate(new TimerTask() { periodicTask.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run() { public void run() {
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;
} }