-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from WildernessLabs/feature/clean-up
More clean-up and refactoring of power and sensor telemetry
- Loading branch information
Showing
12 changed files
with
524 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
public enum CloudEventIds | ||
{ | ||
DeviceStarted = 100, | ||
Telemetry = 110, | ||
BootFromCrash = 200 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Meadow.Hardware; | ||
using System; | ||
using System.Threading; | ||
|
||
namespace Meadow.Devices; | ||
|
||
public class NetworkController | ||
{ | ||
public event EventHandler<bool>? ConnectionStateChanged; | ||
public event EventHandler<TimeSpan>? NetworkDown; | ||
|
||
private readonly INetworkAdapter networkAdapter; | ||
private DateTimeOffset? lastDown; | ||
private Timer downEventTimer; | ||
|
||
public bool IsConnected => networkAdapter.IsConnected; | ||
public TimeSpan DownTime => lastDown == null ? TimeSpan.Zero : DateTime.UtcNow - lastDown.Value; | ||
public TimeSpan DownEventPeriod { get; } = TimeSpan.FromSeconds(30); | ||
|
||
public NetworkController(INetworkAdapter networkAdapter) | ||
{ | ||
this.networkAdapter = networkAdapter; | ||
|
||
networkAdapter.NetworkConnected += OnNetworkConnected; | ||
networkAdapter.NetworkDisconnected += OnNetworkDisconnected; | ||
|
||
downEventTimer = new Timer(DownEventTimerProc, null, -1, -1); | ||
} | ||
|
||
private void DownEventTimerProc(object _) | ||
{ | ||
if (networkAdapter.IsConnected) | ||
{ | ||
downEventTimer.Change(-1, -1); | ||
return; | ||
} | ||
|
||
NetworkDown?.Invoke(this, DownTime); | ||
downEventTimer.Change(DownEventPeriod, TimeSpan.FromMilliseconds(-1)); | ||
} | ||
|
||
private void OnNetworkDisconnected(INetworkAdapter sender, NetworkDisconnectionEventArgs args) | ||
{ | ||
lastDown = DateTimeOffset.UtcNow; | ||
downEventTimer.Change(DownEventPeriod, TimeSpan.FromMilliseconds(-1)); | ||
ConnectionStateChanged?.Invoke(this, false); | ||
} | ||
|
||
private void OnNetworkConnected(INetworkAdapter sender, NetworkConnectionEventArgs args) | ||
{ | ||
lastDown = null; | ||
ConnectionStateChanged?.Invoke(this, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,58 @@ | ||
using Meadow; | ||
using Meadow.Peripherals.Leds; | ||
using Meadow.Peripherals.Leds; | ||
using System; | ||
|
||
namespace Clima_Demo; | ||
|
||
public class NotificationController | ||
{ | ||
[Flags] | ||
public enum Warnings | ||
{ | ||
None = 0, | ||
NetworkDisconnected = 1 << 0, | ||
SolarLoadLow = 1 << 1, | ||
BatteryLow = 1 << 2, | ||
} | ||
|
||
private readonly IRgbPwmLed? rgbLed; | ||
private Warnings activeWarnings = Warnings.None; | ||
|
||
public NotificationController(IRgbPwmLed? rgbLed) | ||
{ | ||
this.rgbLed = rgbLed; | ||
} | ||
|
||
public void Starting() | ||
public void SystemStarting() | ||
{ | ||
rgbLed?.SetColor(RgbLedColors.Red); | ||
} | ||
|
||
public void NetworkConnected() | ||
public void SystemUp() | ||
{ | ||
ReportWarnings(); | ||
} | ||
|
||
public void SetWarning(Warnings warning) | ||
{ | ||
activeWarnings |= warning; | ||
ReportWarnings(); | ||
} | ||
|
||
public void ClearWarning(Warnings warning) | ||
{ | ||
Resolver.Log.Info("Network connected"); | ||
rgbLed?.SetColor(RgbLedColors.Green); | ||
activeWarnings &= ~warning; | ||
ReportWarnings(); | ||
} | ||
|
||
public void NetworkDisconnected() | ||
private void ReportWarnings() | ||
{ | ||
Resolver.Log.Info("Network disconnected"); | ||
rgbLed?.SetColor(RgbLedColors.Yellow); | ||
if (activeWarnings != Warnings.None) | ||
{ | ||
rgbLed?.SetColor(RgbLedColors.Yellow); | ||
} | ||
else | ||
{ | ||
rgbLed?.SetColor(RgbLedColors.Green); | ||
} | ||
} | ||
} |
Oops, something went wrong.