X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fload%2FNmeaFileLoader.java;fp=src%2Ftim%2Fprune%2Fload%2FNmeaFileLoader.java;h=d62ba5fe1660a08a3398004b5b9e44b710f3d1c4;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/load/NmeaFileLoader.java b/src/tim/prune/load/NmeaFileLoader.java new file mode 100644 index 0000000..d62ba5f --- /dev/null +++ b/src/tim/prune/load/NmeaFileLoader.java @@ -0,0 +1,167 @@ +package tim.prune.load; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +import tim.prune.App; +import tim.prune.data.Field; +import tim.prune.data.SourceInfo; + +/** + * Class to handle the loading of Nmea files + */ +public class NmeaFileLoader +{ + /** App for callback of file loading */ + private App _app = null; + + /** + * Constructor + * @param inApp App object + */ + public NmeaFileLoader(App inApp) + { + _app = inApp; + } + + /** + * Open the selected file + * @param inFile File to open + */ + public void openFile(File inFile) + { + BufferedReader reader = null; + ArrayList messages = new ArrayList(); + String lastDate = null; + try + { + reader = new BufferedReader(new FileReader(inFile)); + String currLine = reader.readLine(); + boolean newSegment = true; + while (currLine != null) + { + // Try to make an NmeaMessage object for each line of file + if (currLine.trim().length() > 0) + { + NmeaMessage message = processGGA(currLine); + if (message != null) + { + if (message.hasFix()) + { + message.setSegment(newSegment); + message.setDate(lastDate); + // add message to list + messages.add(message); + } + // Start a new segment if fix lost + newSegment = !message.hasFix(); + } + else { + String date = getDateFromRMC(currLine); + if (date != null) + { + if (lastDate == null && !messages.isEmpty()) { + // Backfill first few messages received before the first date + for (int m=0; m 0) + { + _app.informDataLoaded(getFieldArray(), makeDataArray(messages), + null, new SourceInfo(inFile, SourceInfo.FILE_TYPE.NMEA), null); + } + } + + /** + * Process the given GGA sentence and return the message + * @param inLine line to process + * @return message object + */ + private static NmeaMessage processGGA(String inLine) + { + // Only consider lines which are long enough and begin with the GPS position sentence + if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPGGA")) { + return null; + } + // Assume comma delimiter, split into array + String[] splitLine = inLine.split(","); + if (splitLine != null && splitLine.length >= 10) + { + return new NmeaMessage(splitLine[2] + splitLine[3], // latitude + splitLine[4] + splitLine[5], // longitude + splitLine[9], // altitude + splitLine[1], // timestamp + splitLine[6]); // fix + } + // Couldn't parse it, return null + return null; + } + + /** + * Process the given MRC sentence and return the date + * @param inLine line to process + * @return date, if any + */ + private static String getDateFromRMC(String inLine) + { + // Only consider lines which are long enough and begin with the RMC sentence + if (inLine == null || inLine.length() < 20 || !inLine.startsWith("$GPRMC")) { + return null; + } + // Assume comma delimiter, split into array + String[] splitLine = inLine.split(","); + if (splitLine != null && splitLine.length >= 10) + { + return splitLine[9]; // date in position 9 + } + // Couldn't parse it, return null + return null; + } + + /** + * Make an object array from the data list + * @param inList list of messages + * @return object array for loading + */ + private static Object[][] makeDataArray(ArrayList inList) + { + Object[][] result = new Object[inList.size()][]; + for (int i=0; i