-
Notifications
You must be signed in to change notification settings - Fork 9
/
Interpreter.cs
2051 lines (1813 loc) · 75.9 KB
/
Interpreter.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using GeoFramework.Gps.Filters;
using GeoFramework.Gps.IO;
#if !PocketPC
using System.Runtime.ConstrainedExecution;
using System.Runtime.CompilerServices;
using System.Drawing;
#endif
namespace GeoFramework.Gps
{
/// <summary>Represents a base class for designing a GPS data interpreter.</summary>
/// <remarks>
/// <para>This class serves as the base class for all GPS data interpreters, regardless
/// of the protocol being used. For example, the <strong>NmeaInterpreter</strong> class
/// inherits from this class to process NMEA-0183 data from any data source. This class
/// provides basic functionality to start, pause, resume and stop the processing of GPS
/// data, and provides management of a thread used to process the next set of incoming
/// data.</para>
/// <para>Inheritors should override the <strong>OnReadPacket</strong> event and
/// provide functionality to read the next packet of data from the underlying stream.
/// All raw GPS data must be provided in the form of a <strong>Stream</strong> object,
/// and the method should read and process only a single packet of data.</para>
/// </remarks>
/// <seealso cref="OnReadPacket">OnReadPacket Method</seealso>
#if !PocketPC
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
#endif
public abstract class Interpreter : Component
{
#region Private variables
// Real-time GPS information
private DateTime _utcDateTime;
private DateTime _dateTime;
private Distance _GeoidalSeparation;
private Distance _Altitude;
private Distance _AltitudeAboveEllipsoid;
private Speed _Speed;
private Azimuth _Bearing;
private Position _Position;
private Longitude _MagneticVariation;
private FixStatus _FixStatus;
private FixMode _FixMode;
private FixMethod _FixMethod;
private FixQuality _FixQuality;
private int _FixedSatelliteCount;
private bool _IsFixRequired;
private DilutionOfPrecision _MaximumHorizontalDOP = DilutionOfPrecision.Maximum;
private DilutionOfPrecision _MaximumVerticalDOP = DilutionOfPrecision.Maximum;
private DilutionOfPrecision _HorizontalDOP;
private DilutionOfPrecision _VerticalDOP;
private DilutionOfPrecision _MeanDOP;
private List<Satellite> _Satellites = new List<Satellite>(16);
// Filtering
private PrecisionFilter _Filter = KalmanFilter.Default;
private bool _IsFilterEnabled = true;
// Engine management
private bool _IsRunning;
private Device _Device;
#if !PocketPC
private ThreadPriority _ThreadPriority = ThreadPriority.Normal;
#else
private ThreadPriority _ThreadPriority = ThreadPriority.BelowNormal;
#endif
private Thread _ParsingThread;
private ManualResetEvent _PausedWaitHandle = new ManualResetEvent(true);
private int _MaximumReconnectionAttempts = -1;
private int _ReconnectionAttemptCount;
private Stream _RecordingStream;
private bool _IsDisposed;
private bool _AllowAutomaticReconnection = true;
#if PocketPC
private bool _IsParsingThreadAlive;
#endif
#if !PocketPC
#region Event stacking
/* Event Stacking
*
* Synchronous invokes cause problems if consumers take too much time to
* process the event. Raising events asynchronously and tracking their
* completion prevents events from stacking up. Just about every event
* in an interpreter could benefit from this.
*/
private IAsyncResult _positionChangedAsyncResult;
#endregion
#endif
#endregion
private static TimeSpan _CommandTimeout = TimeSpan.FromSeconds(5);
private static TimeSpan _ReadTimeout = TimeSpan.FromSeconds(5);
/// <summary>Represents a synchronization object which is locked during state changes.</summary>
/// <value>An <strong>Object</strong>.</value>
/// <remarks>
/// <para>Since the <strong>Interpreter</strong> class is
/// multithreaded, this object is used to prevent changes in state from all happening
/// at once. For example, if two threads attempt to start and stop the interpreter,
/// this object is locked so that only one action can occur at a time. This approach
/// greatly improves the stability of the class.</para>
/// <para>By default, this object is locked during calls to <strong>Start</strong>,
/// <strong>Stop</strong>, <strong>Pause</strong>, <strong>Resume</strong> and
/// <strong>OnReadPacket</strong>.</para>
/// </remarks>
protected readonly object SyncRoot = new object();
/// <summary>
/// Represents a synchronization object which is locked during state changes to recording.
/// </summary>
protected readonly object RecordingSyncRoot = new object();
#region Events
/// <summary>
/// Occurs when the current distance above sea level has changed.
/// </summary>
public event EventHandler<DistanceEventArgs> AltitudeChanged;
/// <summary>
/// Occurs when a new altitude report has been received, even if the value has not changed.
/// </summary>
public event EventHandler<DistanceEventArgs> AltitudeReceived;
/// <summary>
/// Occurs when the current distance above the ellipsoid has changed.
/// </summary>
public event EventHandler<DistanceEventArgs> AltitudeAboveEllipsoidChanged;
/// <summary>
/// Occurs when a new altitude-above-ellipsoid report has been received, even if the value has not changed.
/// </summary>
public event EventHandler<DistanceEventArgs> AltitudeAboveEllipsoidReceived;
/// <summary>
/// Occurs when the current direction of travel has changed.
/// </summary>
public event EventHandler<AzimuthEventArgs> BearingChanged;
/// <summary>
/// Occurs when a new bearing report has been received, even if the value has not changed.
/// </summary>
public event EventHandler<AzimuthEventArgs> BearingReceived;
/// <summary>
/// Occurs when the fix quality has changed.
/// </summary>
public event EventHandler<FixQualityEventArgs> FixQualityChanged;
/// <summary>
/// Occurs when the fix mode has changed.
/// </summary>
public event EventHandler<FixModeEventArgs> FixModeChanged;
/// <summary>
/// Occurs when the fix method has changed.
/// </summary>
public event EventHandler<FixMethodEventArgs> FixMethodChanged;
public event EventHandler<DistanceEventArgs> GeoidalSeparationChanged;
/// <summary>
/// Occurs when the GPS-derived date and time has changed.
/// </summary>
public event EventHandler<DateTimeEventArgs> UtcDateTimeChanged;
/// <summary>
/// Occurs when the GPS-derived local time has changed.
/// </summary>
public event EventHandler<DateTimeEventArgs> DateTimeChanged;
/// <summary>
/// Occurs when at least three GPS satellite signals are available to calculate the current location.
/// </summary>
public event EventHandler FixAcquired;
/// <summary>
/// Occurs when less than three GPS satellite signals are available.
/// </summary>
public event EventHandler FixLost;
/// <summary>
/// Occurs when the current location on Earth has changed.
/// </summary>
public event EventHandler<PositionEventArgs> PositionChanged;
/// <summary>
/// Occurs when a new position report has been received, even if the value has not changed.
/// </summary>
public event EventHandler<PositionEventArgs> PositionReceived;
/// <summary>Occurs when the magnetic variation for the current location becomes known.</summary>
public event EventHandler<LongitudeEventArgs> MagneticVariationAvailable;
/// <summary>
/// Occurs when the current rate of travel has changed.
/// </summary>
public event EventHandler<SpeedEventArgs> SpeedChanged;
/// <summary>
/// Occurs when a new speed report has been received, even if the value has not changed.
/// </summary>
public event EventHandler<SpeedEventArgs> SpeedReceived;
/// <summary>
/// Occurs when precision as it relates to latitude and longitude has changed.
/// </summary>
public event EventHandler<DilutionOfPrecisionEventArgs> HorizontalDilutionOfPrecisionChanged;
/// <summary>
/// Occurs when precision as it relates to altitude has changed.
/// </summary>
public event EventHandler<DilutionOfPrecisionEventArgs> VerticalDilutionOfPrecisionChanged;
/// <summary>
/// Occurs when precision as it relates to latitude, longitude and altitude has changed.
/// </summary>
public event EventHandler<DilutionOfPrecisionEventArgs> MeanDilutionOfPrecisionChanged;
/// <summary>
/// Occurs when GPS satellite information has changed.
/// </summary>
public event EventHandler<SatelliteListEventArgs> SatellitesChanged;
/// <summary>
/// Occurs when the interpreter is about to start.
/// </summary>
public event EventHandler<DeviceEventArgs> Starting;
/// <summary>
/// Occurs when the interpreter is now processing GPS data.
/// </summary>
public event EventHandler Started;
/// <summary>
/// Occurs when the interpreter is about to stop.
/// </summary>
public event EventHandler Stopping;
/// <summary>
/// Occurs when the interpreter has stopped processing GPS data.
/// </summary>
public event EventHandler Stopped;
/// <summary>
/// Occurs when the interpreter has temporarily stopped processing GPS data.
/// </summary>
public event EventHandler Paused;
/// <summary>
/// Occurs when the interpreter is no longer paused.
/// </summary>
public event EventHandler Resumed;
/// <summary>
/// Occurs when an exception has happened during processing.
/// </summary>
public event EventHandler<ExceptionEventArgs> ExceptionOccurred;
/// <summary>
/// Occurs when the flow of GPS data has been suddenly interrupted.
/// </summary>
public event EventHandler<ExceptionEventArgs> ConnectionLost;
/// <summary>
/// Occurs immediately before a connection is attempted.
/// </summary>
protected virtual void OnStarting()
{
if (Starting != null)
Starting(this, new DeviceEventArgs(_Device));
}
/// <summary>
/// Occurs immediately before data is processed.
/// </summary>
protected virtual void OnStarted()
{
if (Started != null)
Started(this, EventArgs.Empty);
}
/// <summary>
/// Occurs immediately before the interpreter is shut down.
/// </summary>
protected virtual void OnStopping()
{
if (Stopping != null)
Stopping(this, EventArgs.Empty);
}
/// <summary>
/// Occurs immediately after the interpreter has been shut down.
/// </summary>
protected virtual void OnStopped()
{
if (Stopped != null)
Stopped(this, EventArgs.Empty);
}
///// <summary>
///// Obsolete. See compiler warnings for upgrade help.
///// </summary>
//[Obsolete("Use the 'OnDeviceChanged' override to be notified when the interpreter is using a new GPS device.")]
//public virtual void OnBaseStreamChanged(Stream obsolete) { throw new NotSupportedException(); }
/// <summary>
/// Occurs when a connection to a GPS device is suddenly lost.
/// </summary>
/// <param name="ex">An <strong>Exception</strong> which further explains why the connection was lost.</param>
protected virtual void OnConnectionLost(Exception ex)
{
if (ConnectionLost != null)
ConnectionLost(this, new ExceptionEventArgs(ex));
}
/// <summary>
/// Occurs when the interpreter has temporarily stopped processing data.
/// </summary>
protected virtual void OnPaused()
{
if (Paused != null)
Paused(this, EventArgs.Empty);
}
/// <summary>
/// Occurs when the interpreter is no longer paused.
/// </summary>
protected virtual void OnResumed()
{
if (Resumed != null)
Resumed(this, EventArgs.Empty);
}
/// <summary>
/// Occurs when an exception is trapped by the interpreter's thread.
/// </summary>
/// <param name="ex"></param>
protected virtual void OnExceptionOccurred(Exception ex)
{
if (ExceptionOccurred != null)
ExceptionOccurred(this, new ExceptionEventArgs(ex));
}
#endregion
#region Constructors
/// <summary>
/// Creates a new instance.
/// </summary>
protected Interpreter()
: base()
{ }
#endregion
#region Static members
/// <summary>
/// Controls the amount of time to wait for the next packet of GPS data to arrive.
/// </summary>
/// <remarks></remarks>
public static TimeSpan ReadTimeout
{
get
{
return _ReadTimeout;
}
set
{
// Disallow any value zero or less
if (_ReadTimeout.TotalMilliseconds <= 0)
throw new ArgumentOutOfRangeException("ReadTimeout", "The read timeout of a GPS interpreter must be greater than zero. A value of about five seconds is typical.");
// Set the new value
_ReadTimeout = value;
}
}
/// <summary>
/// Controls the amount of time allowed to perform a start, stop, pause or resume action.
/// </summary>
/// <remarks>The <strong>Interpreter</strong> class is multithreaded and is also thread-safe. Still, however, in some rare cases,
/// two threads may attempt to change the state of the interpreter at the same time. Critical sections will allow both threads to
/// succees whenever possible, but in the event of a deadlock, this property control how much time to allow before giving up.</remarks>
public static TimeSpan CommandTimeout
{
get
{
return _CommandTimeout;
}
set
{
if (_CommandTimeout.TotalSeconds < 1)
throw new ArgumentOutOfRangeException("CommandTimeout", "The command timeout of a GPS interpreter cannot be less than one second. A value of about ten seconds is recommended.");
_CommandTimeout = value;
}
}
#endregion
#region Public Properties
/// <summary>
/// Returns the device providing raw GPS data to the interpreter.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the device providing raw GPS data to the interpreter.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public Device Device
{
get
{
return _Device;
}
}
/// <summary>
/// Controls the priority of the thread which processes GPS data.
/// </summary>
#if !PocketPC
[Category("Behavior")]
[Description("Controls the priority of the thread which processes GPS data.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(ThreadPriority.Normal)]
#endif
public ThreadPriority ThreadPriority
{
get
{
return _ThreadPriority;
}
set
{
// Set the new value
_ThreadPriority = value;
// If the parsing thread is alive, update it
if (_ParsingThread != null
#if !PocketPC
&& _ParsingThread.IsAlive
#else
&& _IsParsingThreadAlive
#endif
)
_ParsingThread.Priority = _ThreadPriority;
}
}
/// <summary>
/// Returns the GPS-derived date and time, adjusted to the local time zone.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the GPS-derived date and time, adjusted to the local time zone.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public DateTime DateTime
{
get { return _dateTime; }
}
/// <summary>
/// Returns the GPS-derived date and time in UTC (GMT-0).
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the GPS-derived date and time in UTC (GMT-0).")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public DateTime UtcDateTime
{
get { return _utcDateTime; }
}
/// <summary>
/// Returns the current estimated precision as it relates to latitude and longitude.
/// </summary>
/// <remarks> Horizontal Dilution of Precision (HDOP) is the accumulated
/// error of latitude and longitude coordinates in X and Y directions
/// (displacement on the surface of the ellipsoid).
/// </remarks>
#if !PocketPC
[Category("Precision")]
[Description("Returns the current estimated precision as it relates to latitude and longitude.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public DilutionOfPrecision HorizontalDilutionOfPrecision
{
get { return _HorizontalDOP; }
}
/// <summary>
/// Returns the current estimated precision as it relates to altitude.
/// </summary>
/// <remarks> Vertical Dilution of Precision (VDOP) is the accumulated
/// error of latitude and longitude coordinates in the Z direction (measurement
/// from the center of the ellipsoid).
/// </remarks>
#if !PocketPC
[Category("Precision")]
[Description("Returns the current estimated precision as it relates to altitude.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public DilutionOfPrecision VerticalDilutionOfPrecision
{
get { return _VerticalDOP; }
}
/// <summary>
/// Returns the kind of fix acquired by the GPS device.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the kind of fix acquired by the GPS device.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public FixMode FixMode
{
get { return _FixMode; }
}
/// <summary>
/// Returns the number of satellites being used to calculate the current location.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the number of satellites being used to calculate the current location.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public int FixedSatelliteCount
{
get { return _FixedSatelliteCount; }
}
/// <summary>
/// Returns the quality of the fix and what kinds of technologies are involved.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the quality of the fix and what kinds of technologies are involved.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public FixQuality FixQuality
{
get { return _FixQuality; }
}
/// <summary>
/// Controls whether GPS data is ignored until a fix is acquired.
/// </summary>
#if !PocketPC
[Category("Behavior")]
[Description("Controls whether GPS data is ignored until a fix is acquired.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(false)]
#endif
public bool IsFixRequired
{
get { return _IsFixRequired; }
set { _IsFixRequired = value; }
}
/// <summary>
/// Returns whether a fix is currently acquired by the GPS device.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns whether a fix is currently acquired by the GPS device.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public bool IsFixed
{
get { return _FixStatus == FixStatus.Fix; }
}
/// <summary>
/// Returns the difference between magnetic North and True North.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the difference between magnetic North and True North.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public Longitude MagneticVariation
{
get { return _MagneticVariation; }
}
/// <summary>
/// Returns the current location on Earth's surface.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the current location on Earth's surface.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
#endif
public Position Position
{
get { return _Position; }
}
/// <summary>
/// Returns the avereage precision tolerance based on the fix quality reported
/// by the device.
/// </summary>
/// <remarks>
/// This property returns the estimated error attributed to the device. To get
/// a total error estimation, add the Horizontal or the Mean DOP to the
/// FixPrecisionEstimate.
/// </remarks>
public Distance FixPrecisionEstimate
{
get { return GeoFramework.Gps.IO.Device.PrecisionEstimate(_FixQuality); }
}
/// <summary>
/// Returns the current rate of travel.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the current rate of travel.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public Speed Speed
{
get { return _Speed; }
}
/// <summary>
/// Returns the current distance above sea level.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns the current distance above sea level.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public Distance Altitude
{
get { return _Altitude; }
}
#if !PocketPC
[Category("Data")]
[Description("Returns the current direction of travel.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public Azimuth Bearing
{
get { return _Bearing; }
}
/// <summary>
/// Controls the largest amount of precision error allowed before GPS data is ignored.
/// </summary>
/// <remarks>This property is important for commercial GPS softwaqre development because it helps the interpreter determine
/// when GPS data reports are precise enough to utilize. Live GPS data can be inaccurate by up to a factor of fifty, or nearly
/// the size of an American football field! As a result, setting a vlue for this property can help to reduce precision errors.
/// When set, reports of latitude, longitude, speed, and bearing are ignored if precision is not at or below the set value.
/// For more on Dilution of Precision and how to determine your precision needs, please refer to our online article here:
/// http://www.geoframeworks.com/Articles/WritingApps2_1.aspx.</remarks>
#if !PocketPC
[Category("Precision")]
[Description("Controls the largest amount of precision error allowed before GPS data is ignored..")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(typeof(DilutionOfPrecision), "Maximum")]
#endif
public DilutionOfPrecision MaximumHorizontalDilutionOfPrecision
{
get
{
return _MaximumHorizontalDOP;
}
set
{
if (_MaximumHorizontalDOP.Equals(value))
return;
if (value.Value <= 0.0f || value.Value > 50.0f)
throw new ArgumentOutOfRangeException("MaximumHorizontalDilutionOfPrecision", "The maximum allowed horizontal Dilution of Precision must be a value between 1 and 50.");
_MaximumHorizontalDOP = value;
}
}
/// <summary>
/// Controls the maximum number of consecutive reconnection retries allowed.
/// </summary>
#if !PocketPC
[Category("Behavior")]
[Description("Controls the maximum number of consecutive reconnection retries allowed.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(-1)]
#endif
public int MaximumReconnectionAttempts
{
get
{
return _MaximumReconnectionAttempts;
}
set
{
if (value < -1)
throw new ArgumentOutOfRangeException("MaximumReconnectionAttempts", "The maximum reconnection attempts for an interpreter must be -1 for infinite retries, or greater than zero for a specific amount.");
_MaximumReconnectionAttempts = value;
}
}
/// <summary>
/// Controls the largest amount of precision error allowed before GPS data is ignored.
/// </summary>
/// <remarks>This property is important for commercial GPS softwaqre development because it helps the interpreter determine
/// when GPS data reports are precise enough to utilize. Live GPS data can be inaccurate by up to a factor of fifty, or nearly
/// the size of an American football field! As a result, setting a vlue for this property can help to reduce precision errors.
/// When set, reports of altitude are ignored if precision is not at or below the set value.
/// For more on Dilution of Precision and how to determine your precision needs, please refer to our online article here:
/// http://www.geoframeworks.com/Articles/WritingApps2_1.aspx.</remarks>
#if !PocketPC
[Category("Precision")]
[Description("Controls the largest amount of precision error allowed before GPS data is ignored.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(typeof(DilutionOfPrecision), "Maximum")]
#endif
public DilutionOfPrecision MaximumVerticalDilutionOfPrecision
{
get
{
return _MaximumVerticalDOP;
}
set
{
if (_MaximumVerticalDOP.Equals(value))
return;
if (value.Value <= 0.0f || value.Value > 50.0f)
throw new ArgumentOutOfRangeException("MaximumVerticalDilutionOfPrecision", "The maximum allowed vertical Dilution of Precision must be a value between 1 and 50.");
_MaximumVerticalDOP = value;
}
}
/// <summary>
/// Controls whether real-time GPS data is made more precise using a filter.
/// </summary>
#if !PocketPC
[Category("Precision")]
[Description("Controls whether real-time GPS data is made more precise using a filter.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(true)]
#endif
public bool IsFilterEnabled
{
get { return _IsFilterEnabled; }
set { _IsFilterEnabled = value; }
}
/// <summary>
/// Controls the technology used to reduce GPS precision error.
/// </summary>
#if !PocketPC
[Category("Precision")]
[Description("Controls whether real-time GPS data is made more precise using a filter.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
#endif
public PrecisionFilter Filter
{
get { return _Filter; }
set { _Filter = value; }
}
/// <summary>
/// Controls whether the interpreter will try to automatically attempt to reconnect anytime a connection is lost.
/// </summary>
/// <remarks><para>Interpreters are able to automatically try to recover from connection failures. When this property is set to <strong>True</strong>,
/// the interpreter will detect a sudden loss of connection, then attempt to make a new connection to restore the flow of data. If multiple GPS
/// devices have been detected, any of them may be utilized as a "fail-over device." Recovery attempts will continue repeatedly until a connection
/// is restored, the interpreter is stopped, or the interpreter is disposed.</para>
/// <para>For most applications, this property should be enabled to help improve the stability of the application. In most cases, a sudden loss of
/// data is only temporary, caused by a loss of battery power or when a wireless device moves too far out of range.</para></remarks>
#if !PocketPC
[Category("Behavior")]
[Description("Controls whether the interpreter will try to automatically attempt to reconnect anytime a connection is lost.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[DefaultValue(true)]
#endif
public bool AllowAutomaticReconnection
{
get { return _AllowAutomaticReconnection; }
set { _AllowAutomaticReconnection = value; }
}
/// <summary>
/// Returns a list of known GPS satellites.
/// </summary>
#if !PocketPC
[Category("Data")]
[Description("Returns a list of known GPS satellites.")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[TypeConverter(typeof(ExpandableObjectConverter))]
#endif
public IList<Satellite> Satellites
{
get { return _Satellites; }
}
/// <summary>
/// Returns whether resources in this object has been shut down.
/// </summary>
public bool IsDisposed
{
get { return _IsDisposed; }
}
/// <summary>
/// Returns the stream used to output data received from the GPS device.
/// </summary>
public Stream RecordingStream
{
get { return _RecordingStream; }
}
#endregion
#region Public Methods
/// <summary>
/// Starts processing GPS data using any available GPS device.
/// </summary>
/// <remarks>This method is used to begin processing GPS data. If no GPS devices are known, GPS.NET will search for GPS devices and use the
/// first device it finds. If no device can be found, an exception is raised.</remarks>
public void Start()
{
// If we're disposed, complain
if (_IsDisposed)
throw new ObjectDisposedException("The Interpreter cannot be started because it has been disposed.");
// Are we already running?
if(_IsRunning)
return;
// Prevent state changes while the interpreter is started
#if !PocketPC
if (Monitor.TryEnter(SyncRoot, _CommandTimeout))
#else
if (Monitor.TryEnter(SyncRoot))
#endif
{
try
{
// Set the stream
_Device = Devices.Any;
// Is the stream null?
if (_Device == null)
{
// And report the problem
throw new InvalidOperationException("After performing a search, no GPS devices were found.");
}
// Signal that we're starting
OnStarting();
// Signal that the stream has changed
OnDeviceChanged();
// Indicate that we're running
_IsRunning = true;
// If the thread isn't alive, start it now
if (_ParsingThread == null
#if !PocketPC
|| !_ParsingThread.IsAlive
#else
|| !_IsParsingThreadAlive
#endif
)
{
_ParsingThread = new Thread(new ThreadStart(ParsingThreadProc));
_ParsingThread.IsBackground = true;
_ParsingThread.Priority = _ThreadPriority;
_ParsingThread.Name = "GPS.NET Parsing Thread (http://www.geoframeworks.com)";
_ParsingThread.Start();
// And signal it
OnStarted();
}
else
{
// Otherwise, allow parsing to continue
_PausedWaitHandle.Set();
// Signal that we've resumed
OnResumed();
}
}
catch (Exception ex)
{
// Signal that we're stopped
OnStopped();
// And report the problem
throw new InvalidOperationException("The interpreter could not be started. " + ex.Message, ex);
}
finally
{
// Release the lock
Monitor.Exit(SyncRoot);
}
}
else
{
// Signal that we're stopped
OnStopped();
// The interpreter is already busy
throw new InvalidOperationException("The interpreter cannot be started. It appears that another thread is already trying to start, stop, pause, or resume.");
}
}
/// <summary>
/// Starts processing GPS data from the specified stream.
/// </summary>
/// <remarks>
/// This method will start the <strong>Interpreter</strong> using a separate thread.
/// The <strong>OnReadPacket</strong> is then called repeatedly from that thread to process
/// incoming data. The Pause, Resume and Stop methods are typically called after this
/// method to change the interpreter's behavior. Finally, a call to
/// <strong>Dispose</strong> will close the underlying stream, stop all processing, and
/// shut down the processing thread.
/// </remarks>
/// <param name="stream">A <strong>Stream</strong> object providing GPS data to process.</param>
public void Start(Device device)
{
// If we're disposed, complain
if (_IsDisposed)
throw new ObjectDisposedException("The Interpreter cannot be started because it has been disposed.");
// Prevent state changes while the interpreter is started
#if !PocketPC
if (Monitor.TryEnter(SyncRoot, _CommandTimeout))
#else
if (Monitor.TryEnter(SyncRoot))
#endif
{
try
{
// Set the device
_Device = device;
// Signal that we're starting
OnStarting();
// If it's not open, open it now
if (!_Device.IsOpen)
_Device.Open();
// Indicate that we're running
_IsRunning = true;
// Signal that the stream has changed
OnDeviceChanged();
// If the thread isn't alive, start it now
if (_ParsingThread == null
#if !PocketPC
|| !_ParsingThread.IsAlive
#else
|| !_IsParsingThreadAlive
#endif
)
{
_ParsingThread = new Thread(new ThreadStart(ParsingThreadProc));
_ParsingThread.IsBackground = true;
_ParsingThread.Priority = _ThreadPriority;
_ParsingThread.Name = "GPS.NET Parsing Thread (http://www.geoframeworks.com)";
_ParsingThread.Start();