Skip to content

Commit

Permalink
Merge pull request #360 from WildernessLabs/batteryinfo
Browse files Browse the repository at this point in the history
health reporter improvements and CCM batteryinfo fix
  • Loading branch information
ctacke authored Aug 25, 2023
2 parents 7ab2203 + 90ae70e commit e677114
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 22 deletions.
61 changes: 44 additions & 17 deletions source/Meadow.Core/HealthReporter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Meadow.Cloud;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Meadow.Cloud;

namespace Meadow;

Expand All @@ -14,21 +14,27 @@ namespace Meadow;
/// </summary>
public class HealthReporter : IHealthReporter
{
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

/// <summary>
/// Starts the health reporter based on the desired interval.
/// </summary>
/// <param name="interval">In minutes</param>
private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

/// <inheritdoc/>
public void Start(int interval)
{
System.Timers.Timer timer = new(interval: interval * 60 * 1000);
timer.Elapsed += async (sender, e) => await TimerOnElapsed(sender, e);
timer.AutoReset = true;
timer.Start();

Resolver.Device.NetworkAdapters.NetworkConnected += async (sender, args) =>
{
Resolver.Log.Trace($"starting health metrics timer");
timer.Start();

// send the first health metric
await Send();
};
}

private async Task TimerOnElapsed(object sender, ElapsedEventArgs e)
/// <inheritdoc/>
public async Task Send()
{
var connected = Resolver.Device.NetworkAdapters.Any(a => a.IsConnected);

Expand All @@ -40,22 +46,38 @@ private async Task TimerOnElapsed(object sender, ElapsedEventArgs e)

var service = Resolver.Services.Get<IMeadowCloudService>();
var device = Resolver.Device;
DirectoryInfo di = new DirectoryInfo("/meadow0");

DirectoryInfo di = new DirectoryInfo(Resolver.Device.PlatformOS.FileSystem.FileSystemRoot);

var usedDiskSpace = DirSize(di);

await service!.SendEvent(new CloudEvent()
var ce = new CloudEvent()
{
Description = "device.health",
EventId = 10,
Measurements = new Dictionary<string, object>()
{
{ "cpu_temp_celsius", device.PlatformOS.GetCpuTemperature().Celsius },
{ "memory_used", GC.GetTotalMemory(false) },
{ "disk_space_used", usedDiskSpace },
{ "battery_percentage", device.GetBatteryInfo().StateOfCharge }
{ "health.cpu_temp_celsius", device.PlatformOS.GetCpuTemperature().Celsius },
{ "health.memory_used", GC.GetTotalMemory(false) },
{ "health.disk_space_used", usedDiskSpace },
{ "info.os_version", device.Information.OSVersion },

},
Timestamp = DateTime.UtcNow
});
Timestamp = DateTimeOffset.UtcNow
};

var batteryInfo = device.GetBatteryInfo();
if (batteryInfo != null)
{
ce.Measurements.Add("health.battery_percentage", batteryInfo.StateOfCharge);
}

if (!string.IsNullOrEmpty(device.Information.CoprocessorOSVersion))
{
ce.Measurements.Add("info.coprocessor_os_version", device.Information.CoprocessorOSVersion);
}

await service!.SendEvent(ce);
Resolver.Log.Trace($"health metrics sent");
}
finally
Expand All @@ -64,6 +86,11 @@ private async Task TimerOnElapsed(object sender, ElapsedEventArgs e)
}
}

private async Task TimerOnElapsed(object sender, ElapsedEventArgs e)
{
await Send();
}

private long DirSize(DirectoryInfo d)
{
long size = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ public override IPwmPort CreatePwmPort(
return PwmPort.From(pin, this.IoController, frequency, dutyCycle, inverted, false);
}

public override BatteryInfo GetBatteryInfo()
public override BatteryInfo? GetBatteryInfo()
{
throw new NotImplementedException();
return null;
}
}
}
2 changes: 1 addition & 1 deletion source/implementations/f7/Meadow.F7/Devices/F7FeatherV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public F7FeatherV1()
/// </summary>
/// <returns>A BatteryInfo instance</returns>
/// <exception cref="Exception"></exception>
public override BatteryInfo GetBatteryInfo()
public override BatteryInfo? GetBatteryInfo()
{
if (Coprocessor != null)
{
Expand Down
2 changes: 1 addition & 1 deletion source/implementations/f7/Meadow.F7/Devices/F7FeatherV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override int GetI2CBusNumberForPins(IPin clock, IPin data)
/// Gets a BatteryInfo instance for the current state of the platform
/// </summary>
/// <returns>A BatteryInfo instance</returns>
public override BatteryInfo GetBatteryInfo()
public override BatteryInfo? GetBatteryInfo()
{
return new BatteryInfo
{
Expand Down
2 changes: 1 addition & 1 deletion source/implementations/f7/Meadow.F7/Devices/F7MicroBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract partial class F7MicroBase : IF7MeadowDevice
/// Gets the current Battery information
/// </summary>
/// <remarks>Override this method if you have an SMBus Smart Battery</remarks>
public abstract BatteryInfo GetBatteryInfo();
public abstract BatteryInfo? GetBatteryInfo();

//==== internals
protected NetworkAdapterCollection networkAdapters;
Expand Down

0 comments on commit e677114

Please sign in to comment.