-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainlogic.cpp
More file actions
143 lines (111 loc) · 2.97 KB
/
mainlogic.cpp
File metadata and controls
143 lines (111 loc) · 2.97 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
#include <Arduino_JSON.h>
#include <ArduinoOTA.h>
#include <HTTPClient.h>
#include "html.h"
#include "mainlogic.h"
MainLogic::MainLogic()
: _server(new WiFiServer(80)),
_led(new LedController)
{
}
void MainLogic::run()
{
checkWebServer();
checkWeather();
ArduinoOTA.handle();
}
void MainLogic::checkWebServer()
{
WiFiClient client = _server->available();
processClientRequest(client);
}
void MainLogic::checkWeather()
{
if ((millis() - _lastWeatherRequestTime) > _weatherRequestDelay)
{
Weather w = getWeatherCurrent();
reactToNewWeather(w);
}
}
void MainLogic::processClientRequest(WiFiClient& client)
{
if (client)
{
Serial.println("\n[Client connected]");
while (client.connected())
{
if (client.available())
{
String request = client.readStringUntil('\r');
Serial.print(request);
if (request.length() == 1 && request[0] == '\n')
{
String html = Html::currentWeatherInterface();
client.println(html);
break;
}
}
}
while (client.available())
{
client.read();
}
client.stop();
Serial.println("[Client disconnected]");
}
}
Weather MainLogic::getWeatherCurrent() // все работает
{
Serial.println("Getting current weather data...");
String jsonBuffer;
Weather w;
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi is Disconnected");
return w;
}
jsonBuffer = httpGETRequest(OWM_REQUEST_CURRENT.c_str());
JSONVar jsonWeatherData = JSON.parse(jsonBuffer);
if (JSON.typeof(jsonWeatherData) == "undefined")
{
Serial.println("Parsing input failed!");
return w;
}
WeatherData wd(jsonWeatherData);
w = wd.weather();
_lastWeatherRequestTime = millis();
Serial.println("Data parsed");
}
String MainLogic::httpGETRequest(const char* request) // все работает
{
HTTPClient http;
http.begin(request);
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else
{
Serial.print("HTTP Response error code: ");
Serial.println(httpResponseCode);
}
http.end();
return payload;
}
Weather MainLogic::getWeatherInAnHour()
{
}
Weather MainLogic::getWeatherTomorrow()
{
}
void MainLogic::reactToNewWeather(const Weather& w) // Добавить логику изменения цвета в зависимости от погоды
{
_sun.setState(w.main.tempertatures, w.system.sunriseTimeUtc, w.system.sunsetTimeUtc, w.cloudPercentage);
_cloud.setState(w.rain, w.snow, w.cloudPercentage);
_led->updateLeds(_sun);
_led->updateLeds(_cloud);
}