Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

record device name and position when position data arrives #83

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Source/Meadow.Clima/Controllers/CloudController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ public void LogAppStartup(string hardwareRevision)
SendEvent(CloudEventIds.DeviceStarted, $"Device started (hardware {hardwareRevision})");
}

public void LogDeviceInfo(string deviceName, double latitiude, double longitude)
{
// {
// "description": "Clima Boot Telemetry",
// "eventId": 109,
// "timestamp": "2024-05-20T22:25:15.862Z",
// "measurements": {
// "lat": 34.2277472,
// "long": -118.2273136
// }
// }
var cloudEvent = new CloudEvent
{
Description = "Clima Position Telemetry",
Timestamp = DateTime.UtcNow,
EventId = 109,
};

cloudEvent.Measurements.Add("device_name", deviceName);
cloudEvent.Measurements.Add("lat", latitiude);
cloudEvent.Measurements.Add("long", longitude);
}
public void LogWarning(string message)
{
SendLog(message, "warning");
Expand Down
55 changes: 17 additions & 38 deletions Source/Meadow.Clima/Controllers/LocationController.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,39 @@
using Meadow;
using Meadow.Devices;
using Meadow.Peripherals.Sensors.Location.Gnss;
using System;

namespace Clima_Demo;

public class LocationController
{
private IGnssSensor gnss;

public bool LogData { get; set; } = false;

public event EventHandler<GnssPositionInfo> PositionReceived;

public LocationController(IClimaHardware clima)
{
if (clima.Gnss is { } gnss)
{
//gnss.GsaReceived += GnssGsaReceived;
//gnss.GsvReceived += GnssGsvReceived;
//gnss.VtgReceived += GnssVtgReceived;
gnss.RmcReceived += GnssRmcReceived;
gnss.GllReceived += GnssGllReceived;
gnss.StartUpdating();
}

}
private void GnssGsaReceived(object _, ActiveSatellites e)
{
if (e.SatellitesUsedForFix is { } sats)
{
Resolver.Log.Info($"Number of active satellites: {sats.Length}");
}
}

private void GnssGsvReceived(object _, SatellitesInView e)
{
Resolver.Log.Info($"Satellites in view: {e.Satellites.Length}");
}

private void GnssVtgReceived(object _, CourseOverGround e)
{
if (e is { } cv)
{
Resolver.Log.Info($"{cv}");
};
}

private void GnssRmcReceived(object _, GnssPositionInfo e)
{
if (e.Valid)
{
Resolver.Log.InfoIf(LogData, $"GNSS Position: lat: [{e.Position.Latitude}], long: [{e.Position.Longitude}]");
this.gnss = gnss;
this.gnss.GnssDataReceived += OnGnssDataReceived;
this.gnss.StartUpdating();
}
}

private void GnssGllReceived(object _, GnssPositionInfo e)
private void OnGnssDataReceived(object sender, IGnssResult e)
{
if (e.Valid)
if (e is GnssPositionInfo pi)
{
Resolver.Log.InfoIf(LogData, $"GNSS Position: lat: [{e.Position.Latitude}], long: [{e.Position.Longitude}]");
if (pi.IsValid && pi.Position != null)
{
// 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?.Invoke(this, pi);
gnss.StopUpdating();
}
}
}
}
15 changes: 14 additions & 1 deletion Source/Meadow.Clima/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public Task Initialize(IClimaHardware hardware, INetworkAdapter? networkAdapter)

cloudController = new CloudController();

Resolver.Services.Get<CloudController>()?.LogAppStartup(hardware.RevisionString);
Resolver.Log.Info($"Running on Clima Hardware {hardware.RevisionString}");

sensorController = new SensorController(hardware);
Expand All @@ -49,6 +48,8 @@ public Task Initialize(IClimaHardware hardware, INetworkAdapter? networkAdapter)

locationController = new LocationController(hardware);

locationController.PositionReceived += OnPositionReceived;

if (networkAdapter == null)
{
Resolver.Log.Error("No network adapter found!");
Expand Down Expand Up @@ -89,6 +90,18 @@ public Task Initialize(IClimaHardware hardware, INetworkAdapter? networkAdapter)
return Task.CompletedTask;
}

private void OnPositionReceived(object sender, Peripherals.Sensors.Location.Gnss.GnssPositionInfo e)
{
if (e.Position != null)
{
// crop to 2 decimal places (~1km accuracy) for privacy
var lat = Math.Round(e.Position.Latitude, 2);
var lon = Math.Round(e.Position.Longitude, 2);

cloudController.LogDeviceInfo(Resolver.Device.Information.DeviceName, lat, lon);
}
}

private void PlatformOS_AfterWake(object sender, WakeSource e)
{
Resolver.Log.Info("PlatformOS_AfterWake");
Expand Down
Loading