Skip to content

Commit 4d62074

Browse files
authored
Merge pull request #97 from doingnz/Optimize-starting-LocationController
Optimise start of LocationController to after Network is connected
2 parents 202de96 + fc087f0 commit 4d62074

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

Source/Meadow.Clima/Controllers/LocationController.cs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Meadow.Devices.Clima.Hardware;
22
using Meadow.Peripherals.Sensors.Location.Gnss;
3+
using Meadow.Units;
34
using System;
5+
using System.Threading;
46

57
namespace 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+
}

Source/Meadow.Clima/MainController.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public Task Initialize(IClimaHardware hardware, INetworkAdapter? networkAdapter)
5858
powerController.SolarVoltageWarning += OnSolarVoltageWarning;
5959
powerController.BatteryVoltageWarning += OnBatteryVoltageWarning;
6060

61-
locationController = new LocationController(hardware);
6261

63-
locationController.PositionReceived += OnPositionReceived;
62+
63+
6464

6565
if (networkAdapter == null)
6666
{
@@ -91,6 +91,9 @@ public Task Initialize(IClimaHardware hardware, INetworkAdapter? networkAdapter)
9191
Resolver.MeadowCloudService.ConnectionStateChanged += OnMeadowCloudServiceConnectionStateChanged;
9292
cloudController.LogAppStartup(hardware.RevisionString);
9393

94+
locationController = new LocationController(hardware);
95+
locationController.PositionReceived += OnPositionReceived;
96+
9497
Resolver.Device.PlatformOS.AfterWake += PlatformOS_AfterWake;
9598

9699
if (!lowPowerMode)
@@ -113,6 +116,7 @@ public void StartUpdating()
113116
Resolver.Log.Info($"Start Updating");
114117
sensorController.StartUpdating(sensorController.UpdateInterval);
115118
powerController.StartUpdating(powerController.UpdateInterval);
119+
locationController.StartUpdating();
116120
}
117121

118122
/// <summary>
@@ -123,6 +127,7 @@ public void StopUpdating()
123127
Resolver.Log.Info($"Stop Updating");
124128
sensorController.StopUpdating();
125129
powerController.StopUpdating();
130+
locationController.StopUpdating();
126131
sleepSimulationTimer.Change(-1, -1); // stop timer
127132
}
128133

@@ -206,7 +211,15 @@ private void OnMeadowCloudServiceConnectionStateChanged(object sender, CloudConn
206211
{
207212
case CloudConnectionState.Connected:
208213
notificationController.SetSystemStatus(NotificationController.SystemStatus.Connected);
214+
215+
locationController.StartUpdating();
209216
break;
217+
218+
case CloudConnectionState.Disconnected:
219+
case CloudConnectionState.Unknown:
220+
locationController.StopUpdating();
221+
break;
222+
210223
default:
211224
notificationController.SetSystemStatus(NotificationController.SystemStatus.ConnectingToCloud);
212225
break;
@@ -312,4 +325,4 @@ public void LogAppStartupAfterCrash(IEnumerable<string> crashReports)
312325
Resolver.Log.Info(report);
313326
}
314327
}
315-
}
328+
}

0 commit comments

Comments
 (0)