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;
public class TelemetryMonitor extends Observable{
public class TelemetryMonitor extends Observable {
private static final String TAG = "TelemetryMonitor";
public static int LOGLEVEL = 0;
public static boolean WARN = LOGLEVEL > 1;
public static boolean DEBUG = LOGLEVEL > 0;
static final int STATS_UPDATE_PERIOD_MS = 4000;
static final int STATS_CONNECT_PERIOD_MS = 1000;
static final int CONNECTION_TIMEOUT_MS = 8000;
static final int STATS_UPDATE_PERIOD_MS = 4000;
static final int STATS_CONNECT_PERIOD_MS = 1000;
static final int CONNECTION_TIMEOUT_MS = 8000;
private final UAVObjectManager objMngr;
private final Telemetry tel;
// private UAVObject objPending;
// private UAVObject objPending;
private UAVObject gcsStatsObj;
private UAVObject flightStatsObj;
private Timer periodicTask;
@ -60,324 +60,333 @@ public class TelemetryMonitor extends Observable{
private boolean connected = false;
private boolean objects_updated = false;
public boolean getConnected() { return connected; };
public boolean getObjectsUpdated() { return objects_updated; };
public boolean getConnected() {
return connected;
};
public TelemetryMonitor(UAVObjectManager objMngr, Telemetry tel)
{
this.objMngr = objMngr;
this.tel = tel;
// this.objPending = null;
queue = new ArrayList<UAVObject>();
public boolean getObjectsUpdated() {
return objects_updated;
};
// Get stats objects
gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats");
public TelemetryMonitor(UAVObjectManager objMngr, Telemetry tel) {
this.objMngr = objMngr;
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
public void update(Observable observable, Object data) {
try {
flightStatsUpdated((UAVObject) data);
} catch (IOException e) {
// 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?
flightStatsObj.removeUpdatedObserver(this);
}
}
});
});
// Start update timer
setPeriod(STATS_CONNECT_PERIOD_MS);
// Start update timer
setPeriod(STATS_CONNECT_PERIOD_MS);
}
/**
* Initiate object retrieval, initialize queue with objects to be retrieved.
*
* @throws IOException
*/
public synchronized void startRetrievingObjects() throws IOException
{
if (DEBUG) Log.d(TAG, "Start retrieving objects");
public synchronized void startRetrievingObjects() throws IOException {
if (DEBUG)
Log.d(TAG, "Start retrieving objects");
// Clear object queue
queue.clear();
// Get all objects, add metaobjects, settings and data objects with OnChange update mode to the queue
List< List<UAVObject> > objs = objMngr.getObjects();
// Clear object queue
queue.clear();
// Get all objects, add metaobjects, settings and data objects with
// OnChange update mode to the queue
List<List<UAVObject>> objs = objMngr.getObjects();
ListIterator<List<UAVObject>> objListIterator = objs.listIterator();
while( objListIterator.hasNext() )
{
List <UAVObject> instList = objListIterator.next();
UAVObject obj = instList.get(0);
UAVObject.Metadata mdata = obj.getMetadata();
if ( obj.isMetadata() )
{
queue.add(obj);
}
else /* Data object */
{
UAVDataObject dobj = (UAVDataObject) obj;
if ( dobj.isSettings() )
{
queue.add(obj);
}
else
{
if ( mdata.GetFlightTelemetryUpdateMode() == UAVObject.UpdateMode.UPDATEMODE_ONCHANGE )
{
queue.add(obj);
}
}
}
}
// Start retrieving
System.out.println(TAG + "Starting to retrieve meta and settings objects from the autopilot (" + queue.size() + " objects)");
retrieveNextObject();
ListIterator<List<UAVObject>> objListIterator = objs.listIterator();
while (objListIterator.hasNext()) {
List<UAVObject> instList = objListIterator.next();
UAVObject obj = instList.get(0);
UAVObject.Metadata mdata = obj.getMetadata();
if (obj.isMetadata()) {
queue.add(obj);
} else /* Data object */
{
UAVDataObject dobj = (UAVDataObject) obj;
if (dobj.isSettings()) {
queue.add(obj);
} else {
if (mdata.GetFlightTelemetryUpdateMode() == UAVObject.UpdateMode.UPDATEMODE_ONCHANGE) {
queue.add(obj);
}
}
}
}
// Start retrieving
Log.d(TAG,
"Starting to retrieve meta and settings objects from the autopilot ("
+ queue.size() + " objects)");
retrieveNextObject();
}
/**
* Cancel the object retrieval
*/
public void stopRetrievingObjects()
{
public void stopRetrievingObjects() {
Log.d(TAG, "Stop retrieving objects");
queue.clear();
queue.clear();
}
/**
* Retrieve the next object in the queue
*
* @throws IOException
*/
public synchronized void retrieveNextObject() throws IOException
{
// If queue is empty return
if ( queue.isEmpty() )
{
if (DEBUG) Log.d(TAG, "All objects retrieved: Connected Successfully");
objects_updated = true;
setChanged();
notifyObservers();
return;
}
// Get next object from the queue
UAVObject obj = queue.remove(0);
public synchronized void retrieveNextObject() throws IOException {
// If queue is empty return
if (queue.isEmpty()) {
if (DEBUG)
Log.d(TAG, "All objects retrieved: Connected Successfully");
objects_updated = true;
setChanged();
notifyObservers();
return;
}
// Get next object from the queue
UAVObject obj = queue.remove(0);
if(obj == null) {
throw new Error("Got null object forom transaction queue");
}
if (obj == null) {
throw new Error("Got null object forom transaction queue");
}
if (DEBUG) Log.d(TAG, "Retrieving object: " + obj.getName()) ;
// Connect to object
if (DEBUG)
Log.d(TAG, "Retrieving object: " + obj.getName());
// TODO: Does this need to stay here permanently? This appears to be used for setup mainly
obj.addTransactionCompleted(new Observer() {
// TODO: Does this need to stay here permanently? This appears to be
// used for setup mainly
obj.addTransactionCompleted(new Observer() {
@Override
public void update(Observable observable, Object 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 {
transactionCompleted(result.obj, result.success);
} catch (IOException e) {
// When the telemetry stream is broken disconnect these updates
// When the telemetry stream is broken disconnect these
// updates
observable.deleteObserver(this);
}
}
});
});
// Request update
tel.updateRequested(obj);
// objPending = obj;
// Request update
tel.updateRequested(obj);
}
/**
* Called by the retrieved object when a transaction is completed.
*
* @throws IOException
*/
public synchronized void transactionCompleted(UAVObject obj, boolean success) throws IOException
{
//QMutexLocker locker(mutex);
// Disconnect from sending object
if (DEBUG) Log.d(TAG,"transactionCompleted. Status: " + success);
// TODO: Need to be able to disconnect signals
//obj->disconnect(this);
// objPending = null;
public synchronized void transactionCompleted(UAVObject obj, boolean success)
throws IOException {
if (DEBUG)
Log.d(TAG, "transactionCompleted. Status: " + success);
if(!success) {
//Log.e(TAG, "Transaction failed: " + obj.getName() + " sending again.");
return;
}
if (!success) {
// Right now success = false means received a NAK so don't
// re-attempt
Log.e(TAG, "Transaction failed.");
}
// Process next object if telemetry is still available
if ( ((String) gcsStatsObj.getField("Status").getValue()).compareTo("Connected") == 0 )
{
retrieveNextObject();
}
else
{
stopRetrievingObjects();
}
// Process next object if telemetry is still available
if (((String) gcsStatsObj.getField("Status").getValue())
.compareTo("Connected") == 0) {
retrieveNextObject();
} else {
stopRetrievingObjects();
}
}
/**
* Called each time the flight stats object is updated by the autopilot
*
* @throws IOException
*/
public synchronized void flightStatsUpdated(UAVObject obj) throws IOException
{
// Force update if not yet connected
gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats");
if (DEBUG) Log.d(TAG,"GCS Status: " + gcsStatsObj.getField("Status").getValue());
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();
}
public synchronized void flightStatsUpdated(UAVObject obj)
throws IOException {
// Force update if not yet connected
gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats");
if (DEBUG)
Log.d(TAG, "GCS Status: "
+ gcsStatsObj.getField("Status").getValue());
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();
}
}
private long lastStatsTime;
/**
* Called periodically to update the statistics and connection status.
*
* @throws IOException
*/
public synchronized void processStatsUpdates() throws IOException
{
// Get telemetry stats
if (DEBUG) Log.d(TAG, "processStatsUpdates()");
Telemetry.TelemetryStats telStats = tel.getStats();
public synchronized void processStatsUpdates() throws IOException {
// Get telemetry stats
if (DEBUG)
Log.d(TAG, "processStatsUpdates()");
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
float dT = (System.currentTimeMillis() - lastStatsTime) / 1000.0f;
lastStatsTime = System.currentTimeMillis();
// Need to compute time because this update is not regular enough
float dT = (System.currentTimeMillis() - lastStatsTime) / 1000.0f;
lastStatsTime = System.currentTimeMillis();
// Update stats object
gcsStatsObj.getField("RxDataRate").setDouble( telStats.rxBytes / dT );
gcsStatsObj.getField("TxDataRate").setDouble( telStats.txBytes / dT );
UAVObjectField field = gcsStatsObj.getField("RxFailures");
field.setDouble(field.getDouble() + telStats.rxErrors);
field = gcsStatsObj.getField("TxFailures");
field.setDouble(field.getDouble() + telStats.txErrors);
field = gcsStatsObj.getField("TxRetries");
field.setDouble(field.getDouble() + telStats.txRetries);
// Update stats object
gcsStatsObj.getField("RxDataRate").setDouble(telStats.rxBytes / dT);
gcsStatsObj.getField("TxDataRate").setDouble(telStats.txBytes / dT);
UAVObjectField field = gcsStatsObj.getField("RxFailures");
field.setDouble(field.getDouble() + telStats.rxErrors);
field = gcsStatsObj.getField("TxFailures");
field.setDouble(field.getDouble() + telStats.txErrors);
field = gcsStatsObj.getField("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
boolean connectionTimeout;
if ( telStats.rxObjects > 0 )
{
lastUpdateTime = System.currentTimeMillis();
// Check for a connection timeout
boolean connectionTimeout;
if (telStats.rxObjects > 0) {
lastUpdateTime = System.currentTimeMillis();
}
if ( (System.currentTimeMillis() - lastUpdateTime) > CONNECTION_TIMEOUT_MS )
{
connectionTimeout = true;
}
else
{
connectionTimeout = false;
}
}
if ((System.currentTimeMillis() - lastUpdateTime) > CONNECTION_TIMEOUT_MS) {
connectionTimeout = true;
} else {
connectionTimeout = false;
}
// Update connection state
gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats");
if(gcsStatsObj == null) {
System.out.println("No GCS stats yet");
return;
}
UAVObjectField statusField = gcsStatsObj.getField("Status");
String oldStatus = new String((String) statusField.getValue());
// Update connection state
gcsStatsObj = objMngr.getObject("GCSTelemetryStats");
flightStatsObj = objMngr.getObject("FlightTelemetryStats");
if (gcsStatsObj == null) {
Log.d(TAG, "No GCS stats yet");
return;
}
UAVObjectField statusField = gcsStatsObj.getField("Status");
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 )
{
// Request connection
statusField.setValue("HandshakeReq");
}
else if ( oldStatus.compareTo("HandshakeReq") == 0 )
{
// Check for connection acknowledge
if ( ((String) flightStatsObj.getField("Status").getValue()).compareTo("HandshakeAck") == 0 )
{
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
if ( ((String) flightStatsObj.getField("Status").getValue()).compareTo("Disconnected") == 0 || connectionTimeout)
{
statusField.setValue("Disconnected");
}
}
if (oldStatus.compareTo("Disconnected") == 0) {
// Request connection
statusField.setValue("HandshakeReq");
} else if (oldStatus.compareTo("HandshakeReq") == 0) {
// Check for connection acknowledge
if (((String) flightStatsObj.getField("Status").getValue())
.compareTo("HandshakeAck") == 0) {
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
if (((String) flightStatsObj.getField("Status").getValue())
.compareTo("Disconnected") == 0 || connectionTimeout) {
statusField.setValue("Disconnected");
}
}
// Force telemetry update if not yet connected
boolean gcsStatusChanged = !oldStatus.equals(statusField.getValue());
// Force telemetry update if not yet connected
boolean gcsStatusChanged = !oldStatus.equals(statusField.getValue());
boolean gcsConnected = statusField.getValue().equals("Connected");
boolean gcsDisconnected = statusField.getValue().equals("Disconnected");
boolean flightConnected = flightStatsObj.getField("Status").equals("Connected");
boolean gcsConnected = statusField.getValue().equals("Connected");
boolean gcsDisconnected = statusField.getValue().equals("Disconnected");
boolean flightConnected = flightStatsObj.getField("Status").equals(
"Connected");
if ( !gcsConnected || !flightConnected )
{
if (DEBUG) Log.d(TAG,"Sending gcs status");
gcsStatsObj.updated();
}
if (!gcsConnected || !flightConnected) {
if (DEBUG)
Log.d(TAG, "Sending gcs status");
gcsStatsObj.updated();
}
// Act on new connections or disconnections
if (gcsConnected && gcsStatusChanged)
{
if (DEBUG) Log.d(TAG,"Connection with the autopilot established");
setPeriod(STATS_UPDATE_PERIOD_MS);
connected = true;
objects_updated = false;
startRetrievingObjects();
setChanged();
}
if (gcsDisconnected && gcsStatusChanged)
{
if (DEBUG) Log.d(TAG,"Trying to connect to the autopilot");
setPeriod(STATS_CONNECT_PERIOD_MS);
connected = false;
objects_updated = false;
setChanged();
}
// Act on new connections or disconnections
if (gcsConnected && gcsStatusChanged) {
if (DEBUG)
Log.d(TAG, "Connection with the autopilot established");
setPeriod(STATS_UPDATE_PERIOD_MS);
connected = true;
objects_updated = false;
startRetrievingObjects();
setChanged();
}
if (gcsDisconnected && gcsStatusChanged) {
if (DEBUG)
Log.d(TAG, "Trying to connect to the autopilot");
setPeriod(STATS_CONNECT_PERIOD_MS);
connected = false;
objects_updated = false;
setChanged();
}
if (DEBUG) Log.d(TAG, "processStatsUpdates() - before notify");
notifyObservers();
if (DEBUG) Log.d(TAG, "processStatsUpdates() - after notify");
if (DEBUG)
Log.d(TAG, "processStatsUpdates() - before notify");
notifyObservers();
if (DEBUG)
Log.d(TAG, "processStatsUpdates() - after notify");
}
private void setPeriod(int ms) {
if(periodicTask == null)
periodicTask = new Timer();
if (periodicTask == null)
periodicTask = new Timer();
periodicTask.cancel();
currentPeriod = ms;
periodicTask = new Timer();
periodicTask.scheduleAtFixedRate(new TimerTask() {
periodicTask.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
processStatsUpdates();
} 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();
}
}
}, currentPeriod, currentPeriod);
}, currentPeriod, currentPeriod);
}
public void stopMonitor()
{
public void stopMonitor() {
periodicTask.cancel();
periodicTask = null;
}