-
Notifications
You must be signed in to change notification settings - Fork 2
/
MeadowApp.cs
143 lines (122 loc) · 4.85 KB
/
MeadowApp.cs
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
using Meadow;
using Meadow.Devices;
using Meadow.Hardware;
using Meadow.Networking;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
namespace Cell_Basics;
public class MeadowApp : App<F7CoreComputeV2>
{
public override Task Run()
{
var cell = Device.NetworkAdapters.Primary<ICellNetworkAdapter>();
cell.NetworkConnected += CellAdapter_NetworkConnected;
cell.NetworkDisconnected += CellAdapter_NetworkDisconnected;
// (Optional) Call to retrieve cell connection logs, useful for troubleshooting
// GetCellConnectionLogs(cell);
// (Optional) Scan for available cellular networks
// CellNetworkScanner(cell);
// (Optional) Get current Cell Signal Quality (CSQ)
// FetchSignalQuality(cell);
return Task.CompletedTask;
}
void FetchSignalQuality(ICellNetworkAdapter cell)
{
int csq = cell.GetSignalQuality();
Console.WriteLine("Current Cell Signal Quality (dbm): " + csq);
}
// Useful method for troubleshooting by inspecting cellular connection logs
async void GetCellConnectionLogs(ICellNetworkAdapter cell)
{
while (!cell.IsConnected)
{
await Task.Delay(10000);
Console.WriteLine($"Cell AT commands output: {cell.AtCmdsOutput}");
}
}
// Get the available cellular networks, including their Operator codes
async void CellNetworkScanner(ICellNetworkAdapter cell)
{
try
{
CellNetwork[] operatorList = cell.ScanForAvailableNetworks();
foreach (CellNetwork data in operatorList)
{
Console.WriteLine($"Operator Status: {data.Status}, Operator Name: {data.Name}, Operator: {data.Operator}, Operator Code: {data.Code}, Mode: {data.Mode}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
async void CellAdapter_NetworkConnected(INetworkAdapter networkAdapter, NetworkConnectionEventArgs e)
{
Console.WriteLine("Cell network connected!");
var cell = networkAdapter as ICellNetworkAdapter;
if (cell != null)
{
Console.WriteLine("Cell CSQ at the time of connection (dbm): " + cell.Csq);
Console.WriteLine("Cell IMEI: " + cell.Imei);
await GetWebPageViaHttpClient("https://postman-echo.com/get?fool=bar1&foo2=bar2");
}
}
void CellAdapter_NetworkDisconnected(INetworkAdapter sender, NetworkDisconnectionEventArgs args)
{
Console.WriteLine("Cell network disconnected!");
}
void CellAdapter_NetworkConnectFailed(INetworkAdapter sender)
{
Console.WriteLine("Cell network connect failed!");
}
void CellAdapter_NetworkConnecting(INetworkAdapter sender)
{
Console.WriteLine("Cell network connecting!");
}
async Task GetWebPageViaHttpClient(string uri)
{
Console.WriteLine($"Requesting {uri} - {DateTime.Now}");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (HttpClient client = new HttpClient())
{
// In weak signal connections and/or large download scenarios, it's recommended to increase the client timeout
client.Timeout = TimeSpan.FromMinutes(5);
using (HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead))
{
try
{
response.EnsureSuccessStatusCode();
var contentLength = response.Content.Headers.ContentLength ?? -1L;
var progress = new Progress<long>(totalBytes =>
{
Console.WriteLine($"{totalBytes} bytes downloaded ({(double)totalBytes / contentLength:P2})");
});
using (var stream = await response.Content.ReadAsStreamAsync())
{
var buffer = new byte[4096];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
totalBytesRead += bytesRead;
((IProgress<long>)progress).Report(totalBytesRead);
}
}
stopwatch.Stop();
Console.WriteLine($"Download complete. Time taken: {stopwatch.Elapsed.TotalSeconds:F2} seconds");
}
catch (TaskCanceledException)
{
Console.WriteLine("Request timed out.");
}
catch (Exception e)
{
Console.WriteLine($"Request went sideways: {e.Message}");
}
}
}
}
}