-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGPRMCParser.cs
91 lines (77 loc) · 3.01 KB
/
GPRMCParser.cs
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace SimpleExample
{
internal static class GPRMCParser
{
public static GPRMCData Parse(string sentence)
{
if (string.IsNullOrEmpty(sentence))
throw new ArgumentNullException(nameof(sentence));
// Check sentence identifier
if (!sentence.StartsWith("$GPRMC"))
throw new ArgumentException("Invalid GPRMC sentence", nameof(sentence));
// Remove leading '$' and split the sentence into fields
string[] fields = sentence.Substring(1).Split(',');
// Check the number of fields
if (fields.Length < 12)
throw new ArgumentException("Invalid number of fields in GPRMC sentence", nameof(sentence));
fields = fields.Skip(1).ToArray();
// Extract data from fields
string utcTime = fields[0];
string status = fields[1];
double latitude = ParseLatitude(fields[2], fields[3]);
double longitude = ParseLongitude(fields[4], fields[5]);
double speed = double.Parse(fields[6]);
double course = double.Parse(fields[7]);
DateTime date = ParseDate(fields[8]);
return new GPRMCData(utcTime, status, latitude, longitude, speed, course, date);
}
private static double ParseLatitude(string value, string hemisphere)
{
double latitude = double.Parse(value, CultureInfo.InvariantCulture) / 100f;
if (hemisphere == "S")
latitude *= -1;
return latitude;
}
private static double ParseLongitude(string value, string hemisphere)
{
double longitude = double.Parse(value, CultureInfo.InvariantCulture) / 100f;
if (hemisphere == "W")
longitude *= -1;
return longitude;
}
private static DateTime ParseDate(string value)
{
int day = int.Parse(value.Substring(0, 2));
int month = int.Parse(value.Substring(2, 2));
int year = int.Parse(value.Substring(4, 2)) + 2000;
return new DateTime(year, month, day);
}
}
public class GPRMCData
{
public string UtcTime { get; }
public string Status { get; }
public double Latitude { get; }
public double Longitude { get; }
public double Speed { get; }
public double Course { get; }
public DateTime Date { get; }
public GPRMCData(string utcTime, string status, double latitude, double longitude, double speed, double course, DateTime date)
{
UtcTime = utcTime;
Status = status;
Latitude = latitude;
Longitude = longitude;
Speed = speed;
Course = course;
Date = date;
}
}
}