-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainController.cs
111 lines (90 loc) · 3.01 KB
/
MainController.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
using F7Feather_AzureIoTHub.Controllers;
using Meadow;
using Meadow.Foundation.Displays;
using Meadow.Foundation.Sensors.Atmospheric;
using Meadow.Hardware;
using Meadow.Units;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace F7Feather_AzureIoTHub;
internal class MainController
{
bool useMQTT = true;
Htu21d atmosphericSensor;
DisplayController displayController;
IWiFiNetworkAdapter network;
IIoTHubController iotHubController;
public MainController(IWiFiNetworkAdapter network)
{
this.network = network;
}
public async Task Initialize()
{
var display = new St7789
(
spiBus: MeadowApp.Device.CreateSpiBus(),
chipSelectPin: MeadowApp.Device.Pins.D02,
dcPin: MeadowApp.Device.Pins.D01,
resetPin: MeadowApp.Device.Pins.D00,
width: 240, height: 240
);
displayController = new DisplayController(display);
displayController.ShowSplashScreen();
Thread.Sleep(5000);
displayController.ShowDataScreen();
if (useMQTT)
{
displayController.UpdateTitle("MQTT");
iotHubController = new IoTHubMqttController();
}
else
{
displayController.UpdateTitle("AMQP");
iotHubController = new IoTHubAmqpController();
}
await InitializeIoTHub();
atmosphericSensor = new Htu21d(MeadowApp.Device.CreateI2cBus());
atmosphericSensor.Updated += AtmosphericSensorUpdated;
}
private async Task InitializeIoTHub()
{
while (!iotHubController.isAuthenticated)
{
displayController.UpdateWiFiStatus(network.IsConnected);
if (network.IsConnected)
{
bool authenticated = await iotHubController.Initialize();
if (authenticated)
{
Resolver.Log.Info("Authenticated");
}
else
{
Resolver.Log.Info("Not Authenticated");
}
}
else
{
Resolver.Log.Info("Offline");
}
await Task.Delay(TimeSpan.FromSeconds(5));
}
}
private async void AtmosphericSensorUpdated(object sender, IChangeResult<(Temperature? Temperature, RelativeHumidity? Humidity)> e)
{
displayController.UpdateWiFiStatus(network.IsConnected);
if (network.IsConnected && iotHubController.isAuthenticated)
{
displayController.UpdateSyncStatus(true);
await iotHubController.SendEnvironmentalReading(e.New);
displayController.UpdateReadings(e.New);
displayController.UpdateSyncStatus(false);
}
}
public Task Run()
{
atmosphericSensor.StartUpdating(TimeSpan.FromSeconds(15));
return Task.CompletedTask;
}
}