//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Software to access vehicle information via the OBD-II connector.
*
* Copyright © 2012 Nils Hammar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Alternative licensing is possible, see the licensing document.
*
* The above text may not be removed or modified.
*/
namespace SharedObjects.DataLogging.Objects
{
using System;
using System.Globalization;
///
/// Parsed data from GPS
///
public class GpsData
{
///
/// Gets Timestamp for GPS data.
///
public DateTime dateTime { get; private set; }
///
/// Gets Latitude, positive values for northern hemisphere, negative for southern.
///
public double lat { get; private set; }
///
/// Gets Longitude, positive values for east of Greenwich, negative for west.
///
public double lon { get; private set; }
///
/// Gets Speed in km/h.
///
public double speedKph { get; private set; }
///
/// Gets Speed in mph.
///
public double speedMph { get; private set; }
///
/// Gets Current heading.
///
public double heading { get; private set; }
///
/// Gets Horizontal Dilution (precision).
///
public double dilution { get; private set; }
///
/// Gets Altitude value, error is about +/- 10m.
///
public double altitude { get; private set; }
///
/// Initializes a new instance of the class.
///
/// Time string
/// Date string
/// Latitude string
/// North/South string
/// Longitude string
/// West/East string
/// Speed string
/// Heading string
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "2", Justification = "Reviewed.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "4", Justification = "Reviewed.")]
public GpsData(string time, string date, string lat, string latNS, string lon, string lonWE, string speedKnots, string heading)
{
string dateTimeStr = date + " " + time;
this.dateTime = parseDateTime(dateTimeStr);
// Translate from degrees and minutes to decimal location.
double lat1 = safeParse(lat.Substring(0, 2)) + (safeParse(lat.Substring(2)) / 60.0);
double lon1 = safeParse(lon.Substring(0, 3)) + (safeParse(lon.Substring(3)) / 60.0);
if (latNS == "N")
{
this.lat = lat1;
}
else
{
this.lat = -lat1;
}
if (lonWE == "E")
{
this.lon = lon1;
}
else
{
this.lon = -lon1;
}
this.speedKph = 1.85200 * safeParse(speedKnots);
this.speedMph = this.speedKph / 1.609344;
this.heading = safeParse(heading);
this.dilution = 0;
this.altitude = 0;
}
///
/// Add information from the NMEA GGA entry.
///
/// Horizontal dilution string
/// Altitude string
/// Altitude unit.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "altitudeUnit", Justification = "Reviewed.")]
public void addGGA(string dilution, string altitude, string altitudeUnit)
{
this.dilution = safeParse(dilution);
this.altitude = safeParse(altitude);
}
///
/// Parse the DateTime string.
///
/// String to parse.
/// Parsed value or DateTime.MinValue on failure to parse.
private static DateTime parseDateTime(string dateTimeStr)
{
// DateTime dt = DateTime.Now;
DateTime dt = DateTime.MinValue;
try
{
dateTimeStr = dateTimeStr.Substring(0, 4) + "20" + dateTimeStr.Substring(4, 9);
dt = DateTime.ParseExact(dateTimeStr, "ddMMyyyy HHmmss", CultureInfo.InvariantCulture);
}
catch
{
}
return dt;
}
///
/// Parse a string to a double value.
///
/// String to parse.
/// Parsed value or zero on failure.
private static double safeParse(string value)
{
double v1 = 0;
try
{
v1 = Convert.ToDouble(value, CultureInfo.InvariantCulture);
}
catch
{
}
return v1;
}
}
}