forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGnssDevice.cs
345 lines (295 loc) · 11.5 KB
/
GnssDevice.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Iot.Device.Common.GnssDevice
{
/// <summary>
/// Base class for all Gnss devices.
/// </summary>
public abstract class GnssDevice
{
private Fix _fix = Fix.NoFix;
private GnssOperation _mode = GnssOperation.Unknown;
private Location _location = new Location();
/// <summary>
/// Delegate type to handle the event when the Gnss module fix status changes.
/// </summary>
/// <param name="fix">The new fix status.</param>
public delegate void FixChangedHandler(Fix fix);
/// <summary>
/// Delegate type to handle the event when the Gnss module mode changes.
/// </summary>
/// <param name="mode">The new Gnss module mode.</param>
public delegate void ModeChangedHandler(GnssOperation mode);
/// <summary>
/// Delegate type to handle the event when the Gnss module location changes.
/// </summary>
/// <param name="position">The new position.</param>
public delegate void LocationChangeHandler(Location position);
/// <summary>
/// Delegate for the error handler when parsing the GPS data.
/// </summary>
/// <param name="exception">The exception that occurred during parsing.</param>
public delegate void ParsingErrorHandler(Exception exception);
/// <summary>
/// Delegate type to handle parsed message that is not processed by based class.
/// </summary>
/// <param name="data">A <see cref="NmeaData"/> element.</param>
public delegate void ParsedMessageHandler(NmeaData data);
/// <summary>
/// Delegate type to handle the event when the Gnss module receives an unparsed message.
/// </summary>
/// <param name="message">The unparsed message.</param>
public delegate void UnparsedMessageHangler(string message);
/// <summary>
/// Represents the event handler for when the fix status of the Gnss module changes.
/// </summary>
public event FixChangedHandler FixChanged;
/// <summary>
/// Event that occurs when the location changes.
/// </summary>
public event LocationChangeHandler LocationChanged;
/// <summary>
/// Represents the event that is raised when the mode of the Gnss module is changed.
/// </summary>
public event ModeChangedHandler OperationModeChanged;
/// <summary>
/// Event handler for parsing errors that occur during data processing of GNSS module.
/// </summary>
public event ParsingErrorHandler ParsingError;
/// <summary>
/// Event handler for parsed message not handled by the base class.
/// </summary>
public event ParsedMessageHandler ParsedMessage;
/// <summary>
/// Event handler for unparsed messages.
/// </summary>
public event UnparsedMessageHangler UnparsedMessage;
/// <summary>
/// Gets or sets the fix status of the Gnss module.
/// </summary>
public Fix Fix
{
get => _fix;
protected set
{
if (_fix == value)
{
return;
}
_fix = value;
FixChanged?.Invoke(value);
}
}
/// <summary>
/// Gets or sets the mode of the GNSS device.
/// </summary>
public virtual GnssMode GnssMode { get; set; }
/// <summary>
/// Gets or sets the mode of the Gnss module.
/// </summary>
/// <value>
/// The mode of the Gnss module.
/// </value>
public GnssOperation GnssOperation
{
get => _mode;
protected set
{
if (value == _mode)
{
return;
}
_mode = value;
OperationModeChanged?.Invoke(_mode);
}
}
/// <summary>
/// Gets or sets the last known location.
/// </summary>
public Location Location
{
get => _location;
protected set
{
if (_location == value)
{
return;
}
_location = value;
LocationChanged?.Invoke(_location);
}
}
/// <summary>
/// Gets or sets the satellites in view.
/// </summary>
public int[] SatellitesInView { get; protected set; }
/// <summary>
/// Starts the GNSS device.
/// </summary>
/// <returns>A value indicating whether the start was successful.</returns>
public abstract bool Start();
/// <summary>
/// Stops the GNSS device.
/// </summary>
/// <returns>A value indicating whether the stop was successful.</returns>
public abstract bool Stop();
/// <summary>
/// Gets a value indicating whether the GNSS device is running.
/// </summary>
public abstract bool IsRunning
{
get;
}
/// <summary>
/// Gets the product details.
/// </summary>
/// <returns>A string representing the product details.</returns>
public abstract string GetProductDetails();
internal void ProcessCommands(string command)
{
var data = Nmea0183Parser.Parse(command);
if (data == null)
{
// Here, we raised the unparsed event.
RaiseUnparsedMessage(command);
return;
}
if (data is GgaData ggaData)
{
// GGA data has Latitude, Longitude, Accuracy (HDOP), Altitude, and Timestamp
// So update our location accoringly and raise the event if any except timestamp changed and Accuracy.
bool changed = false;
if (Location.Latitude != ggaData.Location.Latitude)
{
Location.Latitude = ggaData.Location.Latitude;
changed = true;
}
if (Location.Longitude != ggaData.Location.Longitude)
{
Location.Longitude = ggaData.Location.Longitude;
changed = true;
}
if (Location.Altitude != ggaData.Location.Altitude)
{
Location.Altitude = ggaData.Location.Altitude;
changed = true;
}
if (Location.Accuracy != ggaData.Location.Accuracy)
{
Location.Accuracy = ggaData.Location.Accuracy;
}
if (Location.Timestamp != ggaData.Location.Timestamp)
{
Location.Timestamp = ggaData.Location.Timestamp;
}
if (changed)
{
RaiseLocationChanged(Location);
}
}
else if (data is GsaData gsaData)
{
// GSA data has OperationMode, Fix, SatellitesInUse, PositionDilutionOfPrecision, HorizontalDilutionOfPrecision, VerticalDilutionOfPrecision
GnssOperation = gsaData.OperationMode;
Fix = gsaData.Fix;
SatellitesInView = gsaData.SatellitesInUse;
if (Location.Accuracy != gsaData.PositionDilutionOfPrecision)
{
Location.Accuracy = gsaData.PositionDilutionOfPrecision;
}
if (Location.VerticalAccuracy != gsaData.VerticalDilutionOfPrecision)
{
Location.VerticalAccuracy = gsaData.VerticalDilutionOfPrecision;
}
}
else if (data is VtgData vtgData)
{
// VTG data has Course and Speed
bool changed = false;
if (Location.Course.Value != vtgData.Location.Course.Value)
{
Location.Course = vtgData.Location.Course;
changed = true;
}
if (Location.Speed.Value != vtgData.Location.Speed.Value)
{
Location.Speed = vtgData.Location.Speed;
changed = true;
}
if (changed)
{
RaiseLocationChanged(Location);
}
}
else if (data is RmcData rmcData)
{
// RMC data has Latitude, Longitude, Speed, Course, and Timestamp
bool changed = false;
if (Location.Latitude != rmcData.Location.Latitude)
{
Location.Latitude = rmcData.Location.Latitude;
changed = true;
}
if (Location.Longitude != rmcData.Location.Longitude)
{
Location.Longitude = rmcData.Location.Longitude;
changed = true;
}
if (Location.Speed.Value != rmcData.Location.Speed.Value)
{
Location.Speed = rmcData.Location.Speed;
changed = true;
}
if (Location.Course.Value != rmcData.Location.Course.Value)
{
Location.Course = rmcData.Location.Course;
changed = true;
}
if (Location.Timestamp != rmcData.Location.Timestamp)
{
Location.Timestamp = rmcData.Location.Timestamp;
}
if (changed)
{
RaiseLocationChanged(Location);
}
}
else if (data is GllData gllData)
{
// GLL data has Latitude, Longitude, and Timestamp
bool changed = false;
if (Location.Latitude != gllData.Location.Latitude)
{
Location.Latitude = gllData.Location.Latitude;
changed = true;
}
if (Location.Longitude != gllData.Location.Longitude)
{
Location.Longitude = gllData.Location.Longitude;
changed = true;
}
if (Location.Timestamp != gllData.Location.Timestamp)
{
Location.Timestamp = gllData.Location.Timestamp;
}
if (changed)
{
RaiseLocationChanged(Location);
}
}
else
{
// Pass any other processed NMEA message to the subscriber.
// Usefull for heritage of this base class.
RaiseParsedMessage(data);
}
}
internal void RaiseParsingError(Exception ex) => ParsingError?.Invoke(ex);
internal void RaiseFixChanged(Fix fix) => FixChanged?.Invoke(fix);
internal void RaiseOperationModeChanged(GnssOperation mode) => OperationModeChanged?.Invoke(mode);
internal void RaiseLocationChanged(Location position) => LocationChanged?.Invoke(position);
internal void RaiseParsedMessage(NmeaData data) => ParsedMessage?.Invoke(data);
internal void RaiseUnparsedMessage(string message) => UnparsedMessage?.Invoke(message);
}
}