forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nmea0183Parser.cs
276 lines (246 loc) · 8.51 KB
/
Nmea0183Parser.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Iot.Device.Common.GnssDevice
{
/// <summary>
/// Provides methods for parsing NMEA0183 data from a Gnss device.
/// </summary>
public static class Nmea0183Parser
{
/// <summary>
/// Gets an array of parsable <see cref="NmeaData"/> parsers.
/// </summary>
private static ArrayList MneaDatas { get; } = new ()
{
new GllData(),
new GsaData(),
new GgaData(),
new RmcData(),
new VtgData(),
new ZdaData(),
};
/// <summary>
/// Adds a parser to the list of available parsers.
/// </summary>
/// <param name="parser">The parser class.</param>
public static void AddParser(NmeaData parser)
{
MneaDatas.Add(parser);
}
/// <summary>
/// Removes a parser from the list of available parsers.
/// </summary>
/// <param name="dataId">The data type to parse eg $GPGLL.</param>
public static void RemoveParser(string dataId)
{
var toRemove = default(NmeaData);
foreach (NmeaData item in MneaDatas)
{
if (item.MessageId == dataId)
{
toRemove = item;
break;
}
}
if (toRemove != null)
{
MneaDatas.Remove(toRemove);
}
}
/// <summary>
/// Parses a string and return the parsed NmeaData object.
/// </summary>
/// <param name="inputData">A valid MNEA string.</param>
/// <returns>Parsed NmeaData object if any or null.</returns>
public static NmeaData Parse(string inputData)
{
foreach (NmeaData parser in MneaDatas)
{
if (parser.IsMatch(inputData))
{
return parser.Parse(inputData);
}
}
Debug.WriteLine($"No parser found for: {inputData}");
return null;
}
/// <summary>
/// Converts a string to a geographic location.
/// </summary>
/// <param name="data">Valid input data.</param>
/// <param name="direction">The direction.</param>
/// <param name="degreesLength">Number of degrees digits.</param>
/// <returns>A double representing an coordinate elements.</returns>
public static double ConvertToGeoLocation(string data, string direction, int degreesLength)
{
if (string.IsNullOrEmpty(data))
{
return 0;
}
if (string.IsNullOrEmpty(direction))
{
return 0;
}
var degrees = double.Parse(data.Substring(0, degreesLength));
var minutes = double.Parse(data.Substring(degreesLength));
var result = degrees + (minutes / 60);
if (direction == "S" || direction == "W")
{
return -result;
}
return result;
}
/// <summary>
/// Converts a string to a GnssOperation.
/// </summary>
/// <param name="data">A valid string.</param>
/// <returns>A <see cref="GnssOperation"/>.</returns>
public static GnssOperation ConvertToMode(string data)
{
switch (data)
{
case "M":
return GnssOperation.Manual;
case "A":
return GnssOperation.Auto;
default:
return GnssOperation.Unknown;
}
}
/// <summary>
/// Converts a string to a Fix.
/// </summary>
/// <param name="data">A valid string.</param>
/// <returns>A <see cref="Fix"/>.</returns>
public static Fix ConvertToFix(string data)
{
switch (data)
{
default:
case "1":
return Fix.NoFix;
case "2":
return Fix.Fix2D;
case "3":
return Fix.Fix3D;
}
}
/// <summary>
/// Converts a string to a PositioningIndicator.
/// </summary>
/// <param name="data">A valid string.</param>
/// <returns>The proper <see cref="PositioningIndicator"/> mode.</returns>
public static PositioningIndicator ConvertToPositioningIndicator(string data)
{
switch (data)
{
case "A":
return PositioningIndicator.Autonomous;
case "D":
return PositioningIndicator.Differential;
case "E":
return PositioningIndicator.Estimated;
case "M":
return PositioningIndicator.Manual;
default:
case "N":
return PositioningIndicator.NotValid;
}
}
/// <summary>
/// Converts a string to a Status.
/// </summary>
/// <param name="data">A valid string.</param>
/// <returns>The proper <see cref="Status"/>.</returns>
public static Status ConvertToStatus(string data)
{
switch (data)
{
case "A":
return Status.Valid;
default:
case "V":
return Status.NotValid;
}
}
/// <summary>
/// Converts a string date and time to a DateTime.
/// </summary>
/// <param name="date">The date string as YYMMDD.</param>
/// <param name="time">The time as HHMMSS.ss.</param>
/// <returns>A <see cref="DateTime"/> object.</returns>
public static DateTime ConvertToUtcDateTime(string date, string time)
{
if (string.IsNullOrEmpty(date) || string.IsNullOrEmpty(time))
{
return default;
}
var timespan = ConvertToTimeSpan(time);
var day = int.Parse(date.Substring(0, 2));
var month = int.Parse(date.Substring(2, 2));
var year = int.Parse(date.Substring(4, 2)) + 2000;
return new DateTime(year, month, day).Add(timespan);
}
/// <summary>
/// Converts a string time to a TimeSpan.
/// </summary>
/// <param name="time">The time as HHMMSS.ss.</param>
/// <returns>A <see cref="TimeSpan"/> object.</returns>
public static TimeSpan ConvertToTimeSpan(string time)
{
if (string.IsNullOrEmpty(time))
{
return default;
}
var hour = int.Parse(time.Substring(0, 2));
var minute = int.Parse(time.Substring(2, 2));
var second = int.Parse(time.Substring(4, 2));
var millec = int.Parse(time.Substring(7));
return new TimeSpan(0, hour, minute, second, millec);
}
/// <summary>
/// Computes the checksum of an NMEA1083 message.
/// </summary>
/// <param name="data">The string to compute.</param>
/// <returns>A byte array with the checksum.</returns>
public static byte ComputeChecksum(string data)
{
var checksum = 0;
foreach (char c in data)
{
checksum ^= c;
}
return (byte)checksum;
}
/// <summary>
/// Converts a string to a double. Default to 0 if the string is empty.
/// </summary>
/// <param name="data">A string to convernt.</param>
/// <returns>A double.</returns>
public static double ConvertToDouble(string data)
{
if (string.IsNullOrEmpty(data))
{
return 0;
}
return double.Parse(data);
}
/// <summary>
/// Converts a string to a int. Default to 0 if the string is empty.
/// </summary>
/// <param name="data">A string to convernt.</param>
/// <returns>An int.</returns>
public static int ConvertToInt(string data)
{
if (string.IsNullOrEmpty(data))
{
return 0;
}
return int.Parse(data);
}
}
}