try {
// check if there is a client connected to our server
if (client != null) {
BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream()));
String s = new String();
Trackpoint trackpoint = new Trackpoint();
HashMap<String, String> nmea = new HashMap<String, String>();
// FlightGear sends a stream of $GPRMC, $GPGGA and $GPGSA NMEA data (in that order)
// this data is parsed and converted to a Trackpoint object
// after that the Trackpoint object is sent to the EventListeners
while ((s = is.readLine()) != null) {
String[] params = s.split(",");
if(params[0].equals("$GPRMC")) {
nmea = new HashMap<String, String>();
nmea.put("GPRMC", s);
trackpoint.setLatitude(new Latitude(params[3], Direction.fromValue( params[4])));
trackpoint.setLongitude(new Longitude(params[5], Direction.fromValue( params[6])));
String time = params[1];
String date = params[9];
Calendar cal = GregorianCalendar.getInstance();
cal.set(Calendar.HOUR, Integer.parseInt(time.substring(0, 2)));
cal.set(Calendar.MINUTE, Integer.parseInt(time.substring(2, 4)));
cal.set(Calendar.SECOND, Integer.parseInt(time.substring(4, 6)));
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(date.substring(0, 2)));
cal.set(Calendar.MONTH, Integer.parseInt(date.substring(2, 4)));
cal.set(Calendar.YEAR, Integer.parseInt(date.substring(4, 6)));
trackpoint.setTimestamp(cal.getTime());
trackpoint.setGroundSpeedKnots(Double.parseDouble(params[7]));
}
else if(params[0].equals("$GPGGA")) {
nmea.put("GPGGA", s);
trackpoint.setSatellites(Integer.parseInt(params[7]));
trackpoint.setAltitude(Double.parseDouble(params[9]));
}
// $GPGSA implies the end of the NMEA data sequence
else if(params[0].equals("$GPGSA")) {
nmea.put("GPGSA", s);
break;
}
}
// FlightGear sends junk data at initialization and this is filtered out here
if (trackpoint.getLatitude().getDegrees() != 0) {
latitude.setValue(trackpoint.getLatitude().getDecimal());
longitude.setValue(trackpoint.getLongitude().getDecimal());
if (pastLatitude != 0 && pastLongitude != 0) {
courseOverGround.setValue(Navigation.getCourseInDegrees(pastLatitude, pastLongitude, latitude.getValue(), longitude.getValue()));
}
speedOverGround.setValue(trackpoint.getGroundSpeed());
altitudeAbsolute.setValue(trackpoint.getAltitude());
satellites.setValue(trackpoint.getSatellites());
notifyTrackpointListeners(trackpoint);
pastLatitude = latitude.getValue();
pastLongitude = longitude.getValue();
}
}