-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMeadowApp.cs
More file actions
144 lines (124 loc) · 5.07 KB
/
Copy pathMeadowApp.cs
File metadata and controls
144 lines (124 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Meadow;
using Meadow.Devices;
using Meadow.Hardware;
using System;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
namespace Ethernet_Basics;
public class MeadowApp : App<F7CoreComputeV2>
{
public override async Task Run()
{
Resolver.Log.Info("Run...");
var ethernet = Device.NetworkAdapters.Primary<IWiredNetworkAdapter>();
if (ethernet == null)
{
Resolver.Log.Error("Wired network adapters not found");
}
else
{
ethernet.NetworkConnected += EthernetAdapterConnected;
ethernet.NetworkDisconnected += EthernetAdapterDisconnected;
if (ethernet.IsConnected)
{
DisplayNetworkInformation();
while (true)
{
try
{
await GetWebPageViaHttpClient("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
}
catch (Exception ex)
{
Resolver.Log.Error($"{ex.Message}");
await Task.Delay(3000);
}
}
}
}
}
private void EthernetAdapterDisconnected(INetworkAdapter sender, NetworkDisconnectionEventArgs args)
{
Resolver.Log.Info("Network cable disconnected");
}
private void EthernetAdapterConnected(INetworkAdapter networkAdapter, NetworkConnectionEventArgs e)
{
Resolver.Log.Info("Network cable connected");
}
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)
{
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);
try
{
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}");
}
}
}
}