Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a startup delay and a pace control to FileFragment. #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/main/java/com/apps4av/avarehelper/FileFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;

import com.apps4av.avarehelper.connections.FileConnectionIn;
import com.apps4av.avarehelper.storage.Preferences;
Expand All @@ -37,6 +38,8 @@ public class FileFragment extends Fragment {
private Context mContext;
private Button mConnectButton;
private SavedEditText mTextFile;
private CheckBox mFileDelayCb;
private CheckBox mFileThrottleCb;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand Down Expand Up @@ -81,8 +84,14 @@ public void onClick(View v) {
/*
* List of BT devices is same
*/


mFile = FileConnectionIn.getInstance();

mFile.mFileDelayCb = (CheckBox)view.findViewById(R.id.main_cb_file_delay);
mFile.mFileThrottleCb = (CheckBox)view.findViewById(R.id.main_cb_file_throttle);


setStates();
return view;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ public void start(final Preferences pref) {
@Override
public void run() {

Logger.Logit("WiFi reading data");

BufferProcessor bp = new BufferProcessor();
Logger.Logit("BT reading data");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

package com.apps4av.avarehelper.connections;

import android.widget.CheckBox;

import com.apps4av.avarehelper.gdl90.BasicReportMessage;
import com.apps4av.avarehelper.gdl90.Constants;
import com.apps4av.avarehelper.gdl90.FisBuffer;
Expand Down Expand Up @@ -41,6 +43,7 @@
public class BufferProcessor {

private int mGeoAltitude = Integer.MIN_VALUE;
private long mLastTime = Long.MIN_VALUE; // Cache for last msg time to allow pacing. Should be instance variable, but no constructor.

com.apps4av.avarehelper.gdl90.DataBuffer dbuffer =
new com.apps4av.avarehelper.gdl90.DataBuffer(16384);
Expand All @@ -51,7 +54,23 @@ public class BufferProcessor {
com.apps4av.avarehelper.nmea.Decode ndecode =
new com.apps4av.avarehelper.nmea.Decode();
Ownship nmeaOwnship = new Ownship();
CheckBox paceOutput;

private void pace(long mTime) { // method to pace messages when used with fileplayback

int deltaT;

// Compute time since last message. If negative, assume 0.
// If set a maximum time, since this will delay updates.
deltaT = (int) java.lang.Math.min((int) java.lang.Math.max(0, (mTime - mLastTime)/2 ), 2000);
mLastTime = java.lang.Math.max(mTime, mLastTime); // Update if later.
// Wait deltaT milliseconds.
try {
Thread.sleep(deltaT);
} catch (InterruptedException e) {
return; // Not sure this is correct. REVIEW
}
}
/**
*
* @param buffer
Expand All @@ -69,14 +88,16 @@ public void put(byte [] buffer, int red) {
public LinkedList<String> decode(Preferences pref) {

LinkedList<String> objs = new LinkedList<String>();


long mTime = 0;

byte[] buf;

while(null != (buf = nbuffer.get())) {
com.apps4av.avarehelper.nmea.Message m = ndecode.decode(buf);

if(m instanceof RTMMessage) {

/*
* Make a GPS locaiton message from ADSB ownship message.
*/
Expand All @@ -91,11 +112,13 @@ public LinkedList<String> decode(Preferences pref) {
object.put("altitude", (double)((double)tm.mAltitude));
object.put("callsign", (String)"");
object.put("address", (int)tm.mIcaoAddress);
object.put("time", (long)tm.getTime());
mTime=(long) tm.getTime();
object.put("time", mTime);

} catch (JSONException e1) {
continue;
}
}

objs.add(object.toString());

}
Expand All @@ -114,7 +137,8 @@ else if(nmeaOwnship.addMessage(m)) {
object.put("speed", (double)(om.mHorizontalVelocity));
object.put("bearing", (double)om.mDirection);
object.put("altitude", (double)((double)om.mAltitude));
object.put("time", (long)om.getTime());
mTime=(long) om.getTime();
object.put("time", mTime);
} catch (JSONException e1) {
continue;
}
Expand Down Expand Up @@ -149,7 +173,8 @@ else if(nmeaOwnship.addMessage(m)) {
object.put("altitude", (double)((double)tm.mAltitude));
object.put("callsign", (String)tm.mCallSign);
object.put("address", (int)tm.mIcaoAddress);
object.put("time", (long)tm.getTime());
mTime=(long) tm.getTime();
object.put("time", mTime);
} catch (JSONException e1) {
continue;
}
Expand All @@ -159,7 +184,7 @@ else if(nmeaOwnship.addMessage(m)) {
}

else if(m instanceof BasicReportMessage) {

/*
* Make a GPS locaiton message from ADSB ownship message.
*/
Expand All @@ -174,16 +199,17 @@ else if(m instanceof BasicReportMessage) {
object.put("altitude", (double)((double)tm.mAltitude));
object.put("callsign", (String)tm.mCallSign);
object.put("address", (int)tm.mIcaoAddress);
object.put("time", (long)tm.getTime());
mTime=(long) tm.getTime();
object.put("time", mTime);
} catch (JSONException e1) {
continue;
}

objs.add(object.toString());
}

else if(m instanceof LongReportMessage) {

/*
* Make a GPS locaiton message from ADSB ownship message.
*/
Expand All @@ -198,11 +224,12 @@ else if(m instanceof LongReportMessage) {
object.put("altitude", (double)((double)tm.mAltitude));
object.put("callsign", (String)tm.mCallSign);
object.put("address", (int)tm.mIcaoAddress);
object.put("time", (long)tm.getTime());
mTime=(long) tm.getTime();
object.put("time", mTime);
} catch (JSONException e1) {
continue;
}

objs.add(object.toString());
}

Expand All @@ -223,10 +250,10 @@ else if(m instanceof UplinkMessage) {
if(p instanceof Id6364Product) {
Id6364Product pn = (Id6364Product)p;
JSONObject object = new JSONObject();

JSONArray arrayEmpty = new JSONArray();
JSONArray arrayData = new JSONArray();

int[] data = pn.getData();
if(null != data) {
for(int i = 0; i < data.length; i++) {
Expand All @@ -239,10 +266,11 @@ else if(m instanceof UplinkMessage) {
arrayEmpty.put(e);
}
}

try {
object.put("type", "nexrad");
object.put("time", (long)pn.getTime().getTimeInMillis());
mTime= (long) pn.getTime().getTimeInMillis();
object.put("time", mTime);
object.put("conus", pn.isConus());
object.put("blocknumber", (long)pn.getBlockNumber());
object.put("x", Constants.COLS_PER_BIN);
Expand All @@ -252,7 +280,7 @@ else if(m instanceof UplinkMessage) {
} catch (JSONException e1) {
continue;
}

objs.add(object.toString());
}
/*
Expand All @@ -261,33 +289,33 @@ else if(m instanceof UplinkMessage) {
else if(p instanceof Id413Product) {
Id413Product pn = (Id413Product)p;
JSONObject object = new JSONObject();

String data = pn.getData();
String type = pn.getHeader();
long time = (long)pn.getTime().getTimeInMillis();

/*
* Clear garbage spaces etc. Convert to Avare format
*/
try {
if(type.equals("WINDS")) {

String tokens[] = data.split("\n");
if(tokens.length < 2) {
/*
* Must have line like
* MSY 230000Z FT 3000 6000 F9000 C12000 G18000 C24000 C30000 D34000 39000 Y
* MSY 230000Z FT 3000 6000 F9000 C12000 G18000 C24000 C30000 D34000 39000 Y
* and second line like
* 1410 2508+10 2521+07 2620+01 3037-12 3041-26 304843 295251 29765
*/
continue;
}

tokens[0] = tokens[0].replaceAll("\\s+", " ");
tokens[1] = tokens[1].replaceAll("\\s+", " ");
String winds[] = tokens[1].split(" ");
String alts[] = tokens[0].split(" ");

/*
* Start from 3rd entry - alts
*/
Expand Down Expand Up @@ -387,22 +415,23 @@ else if(p instanceof Id413Product) {
catch (Exception e) {
continue;
}

try {
object.put("type", pn.getHeader());
mTime=time;
object.put("time", time);
object.put("location", pn.getLocation());
object.put("data", data);
} catch (JSONException e1) {
continue;
}

objs.add(object.toString());
}
}
}
else if(m instanceof OwnshipMessage) {

/*
* Make a GPS locaiton message from ADSB ownship message.
*/
Expand All @@ -414,7 +443,8 @@ else if(m instanceof OwnshipMessage) {
object.put("latitude", (double)om.mLat);
object.put("speed", (double)(om.mHorizontalVelocity));
object.put("bearing", (double)om.mDirection);
object.put("time", (long)om.getTime());
mTime=(long) om.getTime();
object.put("time", mTime);
int altitude = -1000;
if(pref.getGeoAltitude()) {
altitude = mGeoAltitude;
Expand All @@ -429,11 +459,11 @@ else if(m instanceof OwnshipMessage) {
} catch (JSONException e1) {
continue;
}

objs.add(object.toString());
}
}

if (paceOutput != null && paceOutput.isChecked()) pace(mTime);
return objs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

package com.apps4av.avarehelper.connections;

import android.widget.CheckBox;

import com.apps4av.avarehelper.storage.Preferences;
import com.apps4av.avarehelper.utils.Logger;
import com.ds.avare.IHelper;
Expand All @@ -38,6 +40,9 @@ public class FileConnectionIn {

private Thread mThread;
private String mFileName = null;
public CheckBox mFileDelayCb;
public CheckBox mFileThrottleCb;


/**
*
Expand Down Expand Up @@ -86,7 +91,8 @@ public void start(final Preferences pref) {
}

mRunning = true;



/*
* Thread that reads File
*/
Expand All @@ -98,13 +104,25 @@ public void run() {

BufferProcessor bp = new BufferProcessor();


bp.paceOutput= mFileThrottleCb;

byte[] buffer = new byte[8192];

/*
* This state machine will keep trying to connect to
* ADBS/GPS receiver
*/
// When reading from file, wait for 5 seconds to allow user to switch to Avare.
if(mFileDelayCb.isChecked()) {
Logger.Logit("Delaying 5 seconds");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
return;
}
}


while(mRunning) {

int red = 0;
Expand Down
Loading