]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/load/NmeaFileLoader.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / load / NmeaFileLoader.java
index ad017898ba2b1b1dbbd7d1695f8952506a2f259f..2dd63dec16088fa4350b5b914c2c25a8d59ccdbc 100644 (file)
@@ -9,6 +9,7 @@ import java.util.ArrayList;
 import tim.prune.App;
 import tim.prune.data.Altitude;
 import tim.prune.data.Field;
+import tim.prune.data.SourceInfo;
 
 /**
  * Class to handle the loading of Nmea files
@@ -35,6 +36,7 @@ public class NmeaFileLoader
        {
                BufferedReader reader = null;
                ArrayList<NmeaMessage> messages = new ArrayList<NmeaMessage>();
+               String lastDate = null;
                try
                {
                        reader = new BufferedReader(new FileReader(inFile));
@@ -45,17 +47,32 @@ public class NmeaFileLoader
                                // Try to make an NmeaMessage object for each line of file
                                if (currLine.trim().length() > 0)
                                {
-                                       NmeaMessage message = processLine(currLine);
+                                       NmeaMessage message = processGGA(currLine);
                                        if (message != null)
                                        {
-                                               if (message.hasFix()) {
+                                               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<messages.size(); m++) {
+                                                                       messages.get(m).setDate(date);
+                                                               }
+                                                       }
+                                                       lastDate = date;
+                                               }
+                                       }
                                }
                                // Read next line, if any
                                currLine = reader.readLine();
@@ -67,8 +84,7 @@ public class NmeaFileLoader
                finally
                {
                        // close file ignoring errors
-                       try
-                       {
+                       try {
                                if (reader != null) reader.close();
                        }
                        catch (Exception e) {}
@@ -76,22 +92,22 @@ public class NmeaFileLoader
                if (messages.size() > 0)
                {
                        _app.informDataLoaded(getFieldArray(), makeDataArray(messages),
-                               Altitude.Format.METRES, inFile.getName());
+                               Altitude.Format.METRES, new SourceInfo(inFile, SourceInfo.FILE_TYPE.NMEA),
+                               null);
                }
        }
 
        /**
-        * Process the given NMEA line and return the message
+        * Process the given GGA sentence and return the message
         * @param inLine line to process
         * @return message object
         */
-       private static NmeaMessage processLine(String inLine)
+       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;
                }
-               // TODO: May be possible to pull date out of GPRMC messages, but then need to back-populate
                // Assume comma delimiter, split into array
                String[] splitLine = inLine.split(",");
                if (splitLine != null && splitLine.length >= 10)
@@ -106,6 +122,27 @@ public class NmeaFileLoader
                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