-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainController.cs
166 lines (130 loc) · 4.93 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using Meadow;
using Meadow.Foundation.Displays;
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
using WifiWeather.Controllers;
namespace WifiWeather;
internal class MainController
{
bool firstWeatherForecast = true;
private int currentGraphType = 0;
private DisplayController displayController;
private RestClientController restClientController;
private List<double> temperatureReadings = new List<double>();
private List<double> pressureReadings = new List<double>();
private List<double> humidityReadings = new List<double>();
public MainController()
{
}
public void Initialize()
{
//hardware.Initialize();
//hardware.UpButton.Clicked += (s, e) =>
//{
// currentGraphType = currentGraphType - 1 < 0 ? 2 : currentGraphType - 1;
// UpdateGraph();
//};
//hardware.DownButton.Clicked += (s, e) =>
//{
// currentGraphType = currentGraphType + 1 > 2 ? 0 : currentGraphType + 1;
// UpdateGraph();
//};
var spiBus = MeadowApp.Device.CreateSpiBus(
MeadowApp.Device.Pins.SPI0_SCLK,
MeadowApp.Device.Pins.SPI0_MOSI,
MeadowApp.Device.Pins.SPI0_MISO,
new Meadow.Units.Frequency(48, Meadow.Units.Frequency.UnitType.Megahertz));
var display = new Ili9341
(
spiBus,
chipSelectPin: MeadowApp.Device.Pins.GPIO16,
dcPin: MeadowApp.Device.Pins.GPIO21,
resetPin: MeadowApp.Device.Pins.GPIO20,
width: 240, height: 320
);
displayController = new DisplayController(display);
restClientController = new RestClientController();
displayController.ShowSplashScreen();
Thread.Sleep(3000);
displayController.ShowDataScreen();
}
private void UpdateGraph()
{
switch (currentGraphType)
{
case 0:
displayController.UpdateGraph(currentGraphType, temperatureReadings);
break;
case 1:
displayController.UpdateGraph(currentGraphType, pressureReadings);
break;
case 2:
displayController.UpdateGraph(currentGraphType, humidityReadings);
break;
}
}
async Task UpdateOutdoorValues()
{
displayController.UpdateSyncStatus(true);
var outdoorConditions = await restClientController.GetWeatherForecast();
if (outdoorConditions != null)
{
firstWeatherForecast = false;
temperatureReadings.Add(outdoorConditions.Value.Item2);
pressureReadings.Add(outdoorConditions.Value.Item3);
humidityReadings.Add(outdoorConditions.Value.Item4);
if (temperatureReadings.Count > 10)
{
temperatureReadings.RemoveAt(0);
pressureReadings.RemoveAt(0);
humidityReadings.RemoveAt(0);
}
displayController.UpdateReadings(
readingType: currentGraphType,
icon: outdoorConditions.Value.Item1,
temperature: outdoorConditions.Value.Item2,
pressure: outdoorConditions.Value.Item3,
humidity: outdoorConditions.Value.Item4,
feelsLike: outdoorConditions.Value.Item5,
sunrise: outdoorConditions.Value.Item6,
sunset: outdoorConditions.Value.Item7);
UpdateGraph();
}
displayController.UpdateSyncStatus(false);
}
public async Task Run()
{
while (true)
{
bool IsOnline = NetworkInterface.GetIsNetworkAvailable();
displayController.UpdateWiFiStatus(IsOnline);
if (IsOnline)
{
var today = DateTime.Now;
Resolver.Log.Trace("Connected!");
if (today.Minute == 0 ||
today.Minute == 10 ||
today.Minute == 20 ||
today.Minute == 30 ||
today.Minute == 40 ||
today.Minute == 50 ||
firstWeatherForecast)
{
Resolver.Log.Trace("Getting forecast values...");
await UpdateOutdoorValues();
Resolver.Log.Trace("Forecast acquired!");
}
displayController.UpdateStatus(today.ToString("hh:mm tt | dd/MM/yyyy").ToUpper());
await Task.Delay(TimeSpan.FromMinutes(1));
}
else
{
Resolver.Log.Trace("Not connected, checking in 10s");
await Task.Delay(TimeSpan.FromSeconds(10));
}
}
}
}