11using Meadow . Devices . Clima . Hardware ;
22using Meadow . Peripherals . Sensors . Location . Gnss ;
3+ using Meadow . Units ;
34using System ;
5+ using System . Threading ;
46
57namespace Meadow . Devices . Clima . Controllers ;
68
@@ -21,6 +23,8 @@ public class LocationController
2123 /// </summary>
2224 public event EventHandler < GnssPositionInfo > ? PositionReceived = null ;
2325
26+ private ManualResetEvent positionReceived = new ManualResetEvent ( false ) ;
27+
2428 /// <summary>
2529 /// Initializes a new instance of the <see cref="LocationController"/> class.
2630 /// </summary>
@@ -31,21 +35,66 @@ public LocationController(IClimaHardware clima)
3135 {
3236 this . gnss = gnss ;
3337 this . gnss . GnssDataReceived += OnGnssDataReceived ;
34- this . gnss . StartUpdating ( ) ;
3538 }
3639 }
3740
41+ /// <summary>
42+ /// Gets the current geographic position as a <see cref="GeographicCoordinate"/>.
43+ /// </summary>
44+ /// <value>
45+ /// The geographic position, including latitude, longitude, and altitude, if available.
46+ /// </value>
47+ /// <remarks>
48+ /// This property is updated when valid GNSS data is received. It represents the last known position
49+ /// and remains unchanged until new valid data is processed.
50+ /// </remarks>
51+ public GeographicCoordinate ? Position { get ; private set ; } = default ;
52+
3853 private void OnGnssDataReceived ( object sender , IGnssResult e )
3954 {
4055 if ( e is GnssPositionInfo pi )
4156 {
4257 if ( pi . IsValid && pi . Position != null )
4358 {
59+ // remember our position
60+ Position = pi . Position ;
4461 // we only need one position fix - weather stations don't move
4562 Resolver . Log . InfoIf ( LogData , $ "GNSS Position: lat: [{ pi . Position . Latitude } ], long: [{ pi . Position . Longitude } ]") ;
63+ positionReceived . Set ( ) ;
4664 PositionReceived ? . Invoke ( this , pi ) ;
47- gnss ? . StopUpdating ( ) ;
65+ StopUpdating ( ) ;
4866 }
4967 }
5068 }
51- }
69+
70+ /// <summary>
71+ /// Starts the GNSS sensor to begin updating location data.
72+ /// </summary>
73+ /// <remarks>
74+ /// This method invokes the <see cref="IGnssSensor.StartUpdating"/> method on the associated GNSS sensor,
75+ /// if it is available, to start receiving GNSS data updates.
76+ /// </remarks>
77+ public void StartUpdating ( bool forced = false )
78+ {
79+ // start updating if forced to find new data or we don;t have current location
80+ if ( forced || ! positionReceived . WaitOne ( 0 ) )
81+ {
82+ gnss ? . StartUpdating ( ) ;
83+ } ;
84+ }
85+
86+ /// <summary>
87+ /// Stops the GNSS sensor from updating location data.
88+ /// </summary>
89+ /// <remarks>
90+ /// This method halts the GNSS data updates by invoking the <see cref="IGnssSensor.StopUpdating"/>
91+ /// method on the associated GNSS sensor, if it is available.
92+ /// </remarks>
93+ public void StopUpdating ( )
94+ {
95+ // stop listening to data arriving from GNSS
96+ gnss ? . StopUpdating ( ) ;
97+
98+ // TODO: can we tell GNSS sensor to stop calculating GPS location and stop sending data to reduce power consumption?
99+ }
100+ }
0 commit comments