From fc087f00d2ee61973ec5b29a7b5759c51e657f3e Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 5 Feb 2025 09:37:30 +1300 Subject: [PATCH] Optimise Start of LocationController to after Network is connected GNSS generates a lot of UART data that trriggers many interrupts that slow the logon to wifi and MQTT connect to the Cloud. Starting the LocationController after connecting to Meadow Cloud makes overall startup to valiad logging of data faster and reduces number of attempts needed to sign on to Meadow Cloud. Added Property to store the last measured location. --- .../Controllers/LocationController.cs | 55 ++++++++++++++++++- Source/Meadow.Clima/MainController.cs | 19 ++++++- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Source/Meadow.Clima/Controllers/LocationController.cs b/Source/Meadow.Clima/Controllers/LocationController.cs index 9b93946..8b82911 100644 --- a/Source/Meadow.Clima/Controllers/LocationController.cs +++ b/Source/Meadow.Clima/Controllers/LocationController.cs @@ -1,6 +1,8 @@ using Meadow.Devices.Clima.Hardware; using Meadow.Peripherals.Sensors.Location.Gnss; +using Meadow.Units; using System; +using System.Threading; namespace Meadow.Devices.Clima.Controllers; @@ -21,6 +23,8 @@ public class LocationController /// public event EventHandler? PositionReceived = null; + private ManualResetEvent positionReceived = new ManualResetEvent(false); + /// /// Initializes a new instance of the class. /// @@ -31,21 +35,66 @@ public LocationController(IClimaHardware clima) { this.gnss = gnss; this.gnss.GnssDataReceived += OnGnssDataReceived; - this.gnss.StartUpdating(); } } + /// + /// Gets the current geographic position as a . + /// + /// + /// The geographic position, including latitude, longitude, and altitude, if available. + /// + /// + /// This property is updated when valid GNSS data is received. It represents the last known position + /// and remains unchanged until new valid data is processed. + /// + public GeographicCoordinate? Position { get; private set; } = default; + private void OnGnssDataReceived(object sender, IGnssResult e) { if (e is GnssPositionInfo pi) { if (pi.IsValid && pi.Position != null) { + // remember our position + Position = pi.Position; // we only need one position fix - weather stations don't move Resolver.Log.InfoIf(LogData, $"GNSS Position: lat: [{pi.Position.Latitude}], long: [{pi.Position.Longitude}]"); + positionReceived.Set(); PositionReceived?.Invoke(this, pi); - gnss?.StopUpdating(); + StopUpdating(); } } } -} \ No newline at end of file + + /// + /// Starts the GNSS sensor to begin updating location data. + /// + /// + /// This method invokes the method on the associated GNSS sensor, + /// if it is available, to start receiving GNSS data updates. + /// + public void StartUpdating(bool forced = false) + { + // start updating if forced to find new data or we don;t have current location + if (forced || !positionReceived.WaitOne(0)) + { + gnss?.StartUpdating(); + }; + } + + /// + /// Stops the GNSS sensor from updating location data. + /// + /// + /// This method halts the GNSS data updates by invoking the + /// method on the associated GNSS sensor, if it is available. + /// + public void StopUpdating() + { + // stop listening to data arriving from GNSS + gnss?.StopUpdating(); + + // TODO: can we tell GNSS sensor to stop calculating GPS location and stop sending data to reduce power consumption? + } +} diff --git a/Source/Meadow.Clima/MainController.cs b/Source/Meadow.Clima/MainController.cs index 35710eb..ff0245e 100644 --- a/Source/Meadow.Clima/MainController.cs +++ b/Source/Meadow.Clima/MainController.cs @@ -58,9 +58,9 @@ public Task Initialize(IClimaHardware hardware, INetworkAdapter? networkAdapter) powerController.SolarVoltageWarning += OnSolarVoltageWarning; powerController.BatteryVoltageWarning += OnBatteryVoltageWarning; - locationController = new LocationController(hardware); - locationController.PositionReceived += OnPositionReceived; + + if (networkAdapter == null) { @@ -91,6 +91,9 @@ public Task Initialize(IClimaHardware hardware, INetworkAdapter? networkAdapter) Resolver.MeadowCloudService.ConnectionStateChanged += OnMeadowCloudServiceConnectionStateChanged; cloudController.LogAppStartup(hardware.RevisionString); + locationController = new LocationController(hardware); + locationController.PositionReceived += OnPositionReceived; + Resolver.Device.PlatformOS.AfterWake += PlatformOS_AfterWake; if (!lowPowerMode) @@ -113,6 +116,7 @@ public void StartUpdating() Resolver.Log.Info($"Start Updating"); sensorController.StartUpdating(sensorController.UpdateInterval); powerController.StartUpdating(powerController.UpdateInterval); + locationController.StartUpdating(); } /// @@ -123,6 +127,7 @@ public void StopUpdating() Resolver.Log.Info($"Stop Updating"); sensorController.StopUpdating(); powerController.StopUpdating(); + locationController.StopUpdating(); sleepSimulationTimer.Change(-1, -1); // stop timer } @@ -206,7 +211,15 @@ private void OnMeadowCloudServiceConnectionStateChanged(object sender, CloudConn { case CloudConnectionState.Connected: notificationController.SetSystemStatus(NotificationController.SystemStatus.Connected); + + locationController.StartUpdating(); break; + + case CloudConnectionState.Disconnected: + case CloudConnectionState.Unknown: + locationController.StopUpdating(); + break; + default: notificationController.SetSystemStatus(NotificationController.SystemStatus.ConnectingToCloud); break; @@ -312,4 +325,4 @@ public void LogAppStartupAfterCrash(IEnumerable crashReports) Resolver.Log.Info(report); } } -} \ No newline at end of file +}