Skip to content

Commit

Permalink
Merge pull request #46 from WildernessLabs/feature/iapp
Browse files Browse the repository at this point in the history
Convert ProjLab samples to use new IApp pattern
  • Loading branch information
ctacke authored Jun 28, 2024
2 parents c4310c1 + 69d2c41 commit 5a76673
Show file tree
Hide file tree
Showing 47 changed files with 619 additions and 448 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ internal class MeadowAzureIoTHubHardware : IMeadowAzureIoTHubHardware

public IRgbPwmLed RgbPwmLed { get; set; }

public void Initialize()
public MeadowAzureIoTHubHardware(IProjectLabHardware projLab)
{
ProjLab = ProjectLab.Create();
ProjLab = projLab;
}

public void Initialize()
{
Display = ProjLab.Display;

RgbPwmLed = ProjLab.RgbLed;
Expand Down
9 changes: 5 additions & 4 deletions Source/Azure/ProjectLab_AzureIoTHub/MeadowApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

namespace ProjectLab_AzureIoTHub;

public class MeadowApp : App<F7CoreComputeV2>
// Change ProjectLabCoreComputeApp to ProjectLabFeatherApp for ProjectLab v2
public class MeadowApp : ProjectLabCoreComputeApp
{
MainController mainController;
private MainController mainController;

public override async Task Initialize()
{
Resolver.Log.Info("Initialize...");

var hardware = new MeadowAzureIoTHubHardware();
var network = Device.NetworkAdapters.Primary<IWiFiNetworkAdapter>();
var hardware = new MeadowAzureIoTHubHardware(Hardware);
var network = Hardware.ComputeModule.NetworkAdapters.Primary<IWiFiNetworkAdapter>();

mainController = new MainController(hardware, network);
await mainController.Initialize();
Expand Down
321 changes: 160 additions & 161 deletions Source/Meadow F7/Network/WiFi_Basics/MeadowApp.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using Meadow;
using Meadow.Devices;
using Meadow.Gateway.WiFi;
using Meadow.Hardware;
using System;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace WiFi_Basics;

public class CoreComputeApp : MeadowApp<F7CoreComputeV2> { }
public class F7FeatherV2App : MeadowApp<F7FeatherV2> { }
public class F7FeatherV1App : MeadowApp<F7FeatherV1> { }

public class MeadowApp<T> : App<T>
where T : F7MicroBase
{
private const string WIFI_NAME = "myWiFi";
private const string WIFI_PASSWORD = "myPassword";

IWiFiNetworkAdapter wifi;

public override async Task Initialize()
using Meadow;
using Meadow.Devices;
using Meadow.Gateway.WiFi;
using Meadow.Hardware;
using System;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace WiFi_Basics;

public class CoreComputeApp : MeadowApp<F7CoreComputeV2> { }
public class F7FeatherV2App : MeadowApp<F7FeatherV2> { }
public class F7FeatherV1App : MeadowApp<F7FeatherV1> { }

public class MeadowApp<T> : App<T>
where T : F7MicroBase
{
private const string WIFI_NAME = "myWiFi";
private const string WIFI_PASSWORD = "myPassword";
private IWiFiNetworkAdapter wifi;

public override async Task Initialize()
{
Resolver.Log.Info("Initialize...");

Expand All @@ -34,22 +33,22 @@ public override async Task Initialize()

wifi.NetworkDisconnected += NetworkDisconnected;

// Enumerate the public WiFi channels
// Enumerate the public WiFi channels
await ScanForAccessPoints(wifi);

try
{
Resolver.Log.Info($"Connecting to WiFi Network {WIFI_NAME}");

// connect to the wifi network.
await wifi.Connect(WIFI_NAME, WIFI_PASSWORD, TimeSpan.FromSeconds(45));
}
catch (Exception ex)
{
Resolver.Log.Error($"Failed to Connect: {ex.Message}");
try
{
Resolver.Log.Info($"Connecting to WiFi Network {WIFI_NAME}");

// connect to the wifi network.
await wifi.Connect(WIFI_NAME, WIFI_PASSWORD, TimeSpan.FromSeconds(45));
}
catch (Exception ex)
{
Resolver.Log.Error($"Failed to Connect: {ex.Message}");
}
}

}

private void NetworkConnected(INetworkAdapter sender, NetworkConnectionEventArgs args)
{
Resolver.Log.Info($"Joined network - IP Address: {args.IpAddress}");
Expand All @@ -63,128 +62,128 @@ private void NetworkConnecting(INetworkAdapter sender)
private void NetworkDisconnected(INetworkAdapter sender, NetworkDisconnectionEventArgs args)
{
Resolver.Log.Info($"Network disconnected because {args.Reason}");
}

public override async Task Run()
{
Resolver.Log.Info("Run...");

if (wifi.IsConnected)
{
DisplayNetworkInformation();

while (true)
{
await GetWebPageViaHttpClient("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
}
}
}

private async Task ScanForAccessPoints(IWiFiNetworkAdapter wifi)
{
Resolver.Log.Info("Getting list of access points");
var networks = await wifi.Scan(TimeSpan.FromSeconds(60));

if (networks.Count > 0)
{
Resolver.Log.Info("|-------------------------------------------------------------|---------|");
Resolver.Log.Info("| Network Name | RSSI | BSSID | Channel |");
Resolver.Log.Info("|-------------------------------------------------------------|---------|");

foreach (WifiNetwork accessPoint in networks)
{
Resolver.Log.Info($"| {accessPoint.Ssid,-32} | {accessPoint.SignalDbStrength,4} | {accessPoint.Bssid,17} | {accessPoint.ChannelCenterFrequency,3} |");
}
}
else
{
Resolver.Log.Info($"No access points detected");
}
}

public void DisplayNetworkInformation()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

if (adapters.Length == 0)
{
Resolver.Log.Warn("No adapters available");
}
else
{
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Resolver.Log.Info("");
Resolver.Log.Info(adapter.Description);
Resolver.Log.Info(string.Empty.PadLeft(adapter.Description.Length, '='));
Resolver.Log.Info($" Adapter name: {adapter.Name}");
Resolver.Log.Info($" Interface type .......................... : {adapter.NetworkInterfaceType}");
Resolver.Log.Info($" Physical Address ........................ : {adapter.GetPhysicalAddress()}");
Resolver.Log.Info($" Operational status ...................... : {adapter.OperationalStatus}");

string versions = string.Empty;

if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
versions = "IPv4";
}

if (adapter.Supports(NetworkInterfaceComponent.IPv6))
{
if (versions.Length > 0)
{
versions += " ";
}
versions += "IPv6";
}

Resolver.Log.Info($" IP version .............................. : {versions}");

if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
Resolver.Log.Info($" MTU ..................................... : {ipv4.Mtu}");
}

if ((adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) || (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet))
{
foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Resolver.Log.Info($" IP address .............................. : {ip.Address}");
Resolver.Log.Info($" Subnet mask ............................. : {ip.IPv4Mask}");
}
}
}
}
}
}

public async Task GetWebPageViaHttpClient(string uri)
{
try
{
Resolver.Log.Info($"Requesting {uri} - {DateTime.Now}");

using HttpClient client = new HttpClient();
client.Timeout = new TimeSpan(0, 5, 0);

HttpResponseMessage response = await client.GetAsync(uri);

response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Resolver.Log.Info(responseBody);
}
catch (TaskCanceledException)
{
Resolver.Log.Info("Request time out.");
}
catch (Exception e)
{
Resolver.Log.Info($"Request went sideways: {e.Message}");
await Task.Delay(5000);
}
}
}

public override async Task Run()
{
Resolver.Log.Info("Run...");

if (wifi.IsConnected)
{
DisplayNetworkInformation();

while (true)
{
await GetWebPageViaHttpClient("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
}
}
}

private async Task ScanForAccessPoints(IWiFiNetworkAdapter wifi)
{
Resolver.Log.Info("Getting list of access points");
var networks = await wifi.Scan(TimeSpan.FromSeconds(60));

if (networks.Count > 0)
{
Resolver.Log.Info("|-------------------------------------------------------------|---------|");
Resolver.Log.Info("| Network Name | RSSI | BSSID | Channel |");
Resolver.Log.Info("|-------------------------------------------------------------|---------|");

foreach (WifiNetwork accessPoint in networks)
{
Resolver.Log.Info($"| {accessPoint.Ssid,-32} | {accessPoint.SignalDbStrength,4} | {accessPoint.Bssid,17} | {accessPoint.ChannelCenterFrequency,3} |");
}
}
else
{
Resolver.Log.Info($"No access points detected");
}
}

public void DisplayNetworkInformation()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();

if (adapters.Length == 0)
{
Resolver.Log.Warn("No adapters available");
}
else
{
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
Resolver.Log.Info("");
Resolver.Log.Info(adapter.Description);
Resolver.Log.Info(string.Empty.PadLeft(adapter.Description.Length, '='));
Resolver.Log.Info($" Adapter name: {adapter.Name}");
Resolver.Log.Info($" Interface type .......................... : {adapter.NetworkInterfaceType}");
Resolver.Log.Info($" Physical Address ........................ : {adapter.GetPhysicalAddress()}");
Resolver.Log.Info($" Operational status ...................... : {adapter.OperationalStatus}");

string versions = string.Empty;

if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
versions = "IPv4";
}

if (adapter.Supports(NetworkInterfaceComponent.IPv6))
{
if (versions.Length > 0)
{
versions += " ";
}
versions += "IPv6";
}

Resolver.Log.Info($" IP version .............................. : {versions}");

if (adapter.Supports(NetworkInterfaceComponent.IPv4))
{
IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
Resolver.Log.Info($" MTU ..................................... : {ipv4.Mtu}");
}

if ((adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) || (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet))
{
foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Resolver.Log.Info($" IP address .............................. : {ip.Address}");
Resolver.Log.Info($" Subnet mask ............................. : {ip.IPv4Mask}");
}
}
}
}
}
}

public async Task GetWebPageViaHttpClient(string uri)
{
try
{
Resolver.Log.Info($"Requesting {uri} - {DateTime.Now}");

using HttpClient client = new HttpClient();
client.Timeout = new TimeSpan(0, 5, 0);

HttpResponseMessage response = await client.GetAsync(uri);

response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Resolver.Log.Info(responseBody);
}
catch (TaskCanceledException)
{
Resolver.Log.Info("Request time out.");
}
catch (Exception e)
{
Resolver.Log.Info($"Request went sideways: {e.Message}");
await Task.Delay(5000);
}
}
}
4 changes: 2 additions & 2 deletions Source/Meadow F7/OS/CrashDetect/MeadowApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override Task Initialize()
{
var reliabilityService = Resolver.Services.Get<IReliabilityService>();

reliabilityService.MeadowSystemError += OnMeadowSystemError;
// reliabilityService.MeadowSystemError += OnMeadowSystemError;

if (reliabilityService.LastBootWasFromCrash)
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public override async Task Run()
{
await Task.Delay(TimeSpan.FromSeconds(_random.Next(3, 20)));
Resolver.Log.Info("FORCING A CRASH!");
throw new Exception($"OMG! My App Died with a random code {_random.Next(1, 101)}");
// throw new Exception($"OMG! My App Died with a random code {_random.Next(1, 101)}");
}

private void CreateAnOOMError()
Expand Down
Loading

0 comments on commit 5a76673

Please sign in to comment.