private static void Device_MessageReceived(object sender, NmeaParser.NmeaMessageReceivedEventArgs e) { DateTime nowUTC = DateTime.Now.ToUniversalTime(); double lat = 0.00; double longitude = 0.00; bool hasValidMessage = false; string msgType = ""; DateTime fixtime = DateTime.Now.ToUniversalTime(); if (e.Message is NmeaParser.Nmea.Gps.Gprmc) { var message = e.Message as NmeaParser.Nmea.Gps.Gprmc; lat = message.Latitude; fixtime = message.FixTime.ToUniversalTime(); longitude = message.Longitude; hasValidMessage = true; msgType = "GPRMC"; if (!double.IsNaN(message.Course)) { lastBearing = message.Course; } } else if (e.Message is NmeaParser.Nmea.Gps.Gpgga) { var message = e.Message as NmeaParser.Nmea.Gps.Gpgga; fixtime = new DateTime(nowUTC.Year, nowUTC.Month, nowUTC.Day, message.FixTime.Hours, message.FixTime.Minutes, message.FixTime.Seconds, message.FixTime.Milliseconds); lat = message.Latitude; longitude = message.Longitude; hasValidMessage = true; msgType = "GPGGA"; } else if (e.Message is NmeaParser.Nmea.Gps.Gpgll) { var message = e.Message as NmeaParser.Nmea.Gps.Gpgll; lat = message.Latitude; longitude = message.Longitude; hasValidMessage = true; fixtime = new DateTime(nowUTC.Year, nowUTC.Month, nowUTC.Day, message.FixTime.Hours, message.FixTime.Minutes, message.FixTime.Seconds, message.FixTime.Milliseconds); msgType = "GPGLL"; } else if (e.Message is NmeaParser.Nmea.Gps.Gpbod) { var message = e.Message as NmeaParser.Nmea.Gps.Gpbod; lastBearing = message.TrueBearing; } if (hasValidMessage) { intialized = true; lock (instance.coordinatesQueue) { if (!OverrideLat.HasValue || !OverrideLong.HasValue) { var coords = new GpsCoords { Altitude = null, Latitude = lat, Longitude = longitude, Timestamp = nowUTC }; coords.TranslateByHeading(lastBearing, gpsOffsetFeet, inReverse); instance.coordinatesQueue.Enqueue(coords); } else { instance.coordinatesQueue.Enqueue(new GpsCoords { Altitude = null, Latitude = OverrideLat, Longitude = OverrideLong, Timestamp = nowUTC }); } } try { //Logger.Log("GPS", "Sending GPS Message. " + lat.ToString() + ", " + longitude.ToString()); if (!OverrideLat.HasValue || !OverrideLong.HasValue) { Messenger.Default.Send <GPSEventMessage>(new GPSEventMessage { Timestamp = fixtime, Latitude = lat, Longitude = longitude, MessageType = msgType }); } else { Messenger.Default.Send <GPSEventMessage>(new GPSEventMessage { Timestamp = fixtime, Latitude = OverrideLat.Value, Longitude = OverrideLong.Value, MessageType = msgType }); } } catch (Exception gExc) { Logging.Logger.Log(gExc); } } lock (instance.coordinatesQueue) { while (instance.coordinatesQueue.Count > instance.maxMessages) { instance.coordinatesQueue.Dequeue(); } } }
public static double SafeLat(GpsCoords coords) { return((coords != null) ? coords.NonNullLatitude : 0.000); }