File: NmeaMessage.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (115 lines) | stat: -rw-r--r-- 3,073 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package tim.prune.load;

import java.util.Calendar;

/**
 * Class to hold a single NMEA message
 */
public class NmeaMessage
{
	private String _latitude = null;
	private String _longitude = null;
	private String _altitude = null;
	private String _timestamp = null;
	private String _date = null;
	private boolean _fix = false;
	private boolean _segment = false;

	/**
	 * Constructor
	 * @param inLatitude latitude
	 * @param inLongitude longitude
	 * @param inAltitude altitude
	 * @param inTimestamp timestamp
	 * @param inFix fix = 0, 1 or 2
	 */
	public NmeaMessage(String inLatitude, String inLongitude,
		String inAltitude, String inTimestamp, String inFix)
	{
		_latitude = inLatitude;
		_longitude = inLongitude;
		_altitude = inAltitude;
		_timestamp = inTimestamp;
		_fix = (inFix != null && !inFix.equals("0"));
	}

	/**
	 * @return true if message has a fix
	 */
	public boolean hasFix() {
		return _fix;
	}

	/**
	 * @param inSegment segment flag
	 */
	public void setSegment(boolean inSegment)
	{
		_segment = inSegment;
	}

	/**
	 * @param inDate date from MRC sentence
	 */
	public void setDate(String inDate) {
		_date = inDate;
	}

	/**
	 * @return String array for loading
	 */
	public String[] getStrings()
	{
		String[] results = new String[] {modify(_latitude), modify(_longitude), _altitude,
			getTimestamp(), (_segment?"1":"")};
		return results;
	}

	/**
	 * Insert a separator between degrees and minutes
	 * @param inCoordinate NMEA coordinate string
	 * @return modified string or input string if format wasn't what was expected
	 */
	private static String modify(String inCoordinate)
	{
		if (inCoordinate != null && inCoordinate.length() > 6)
		{
			int dotPos = inCoordinate.indexOf('.');
			if (dotPos > 0) {
				return inCoordinate.substring(0, dotPos-2) + "d" + inCoordinate.substring(dotPos-2);
			}
		}
		return inCoordinate;
	}

	/**
	 * Use time from NMEA message, and today's date (as date isn't given in GPGGA messages)
	 * @return Timestamp in parseable format
	 */
	private String getTimestamp()
	{
		try
		{
			Calendar cal = Calendar.getInstance();
			// use date if available (today if not)
			if (_date != null && _date.length() == 6) {
				try {
					cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(_date.substring(0, 2)));
					cal.set(Calendar.MONTH, Integer.parseInt(_date.substring(2, 4))-1); // month starts at zero
					int year = Integer.parseInt(_date.substring(4, 6));
					if (year < 80) {year += 2000;} else {year += 1900;} // two-digit year hack
					cal.set(Calendar.YEAR, year);
				}
				catch (Exception e) {} // ignore exceptions for date, still take time
			}
			cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(_timestamp.substring(0, 2)));
			cal.set(Calendar.MINUTE, Integer.parseInt(_timestamp.substring(2, 4)));
			cal.set(Calendar.SECOND, Integer.parseInt(_timestamp.substring(4, 6)));
			cal.set(Calendar.MILLISECOND, 0);
			// Return time as number of milliseconds
			return "" + cal.getTimeInMillis();
		}
		catch (Exception e) {}  // ignore parsing errors, just have no timestamp
		return null;
	}
}