-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainController.cs
144 lines (113 loc) · 4.21 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
using Meadow;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using WifiWeather.Controllers;
using WifiWeather.Hardware;
namespace WifiWeather;
public class MainController
{
bool firstWeatherForecast = true;
private int currentGraphType = 0;
private IWifiWeatherHardware hardware;
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(IWifiWeatherHardware hardware)
{
this.hardware = hardware;
hardware.LeftButton.Clicked += (s, e) =>
{
currentGraphType = currentGraphType - 1 < 0 ? 2 : currentGraphType - 1;
UpdateGraph();
};
hardware.RightButton.Clicked += (s, e) =>
{
currentGraphType = currentGraphType + 1 > 2 ? 0 : currentGraphType + 1;
UpdateGraph();
};
displayController = new DisplayController(hardware.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)
{
displayController.UpdateWiFiStatus(hardware.NetworkAdapter.IsConnected);
if (hardware.NetworkAdapter.IsConnected)
{
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"));
await Task.Delay(TimeSpan.FromMinutes(1));
}
else
{
Resolver.Log.Trace("Not connected, checking in 10s");
await Task.Delay(TimeSpan.FromSeconds(10));
}
}
}
}