5
5
#include < ESPAsyncWebServer.h>
6
6
#include " AsyncJson.h"
7
7
#include " ArduinoJson.h"
8
- #include " settings.h"
8
+ #include < Ticker.h>
9
+ #include " functional"
9
10
10
- UI::UI (int port, Settings *settings) : _server(port) {
11
+ UI::UI (int port, Settings *settings, Temp *temp, Heater *heater, WiFiNetwork *wifiNetwork) : _server(port)
12
+ {
11
13
_settings = settings;
14
+ _temp = temp;
15
+ _heater = heater;
16
+ _wifiNetwork = wifiNetwork;
12
17
}
13
18
14
19
void UI::begin ()
@@ -28,23 +33,55 @@ void UI::begin()
28
33
doc[" ssid" ] = _settings->ssid ();
29
34
doc[" influxdb_url" ] = _settings->influxdbUrl ();
30
35
doc[" influxdb_db_name" ] = _settings->influxdbDatabaseName ();
36
+ doc[" heater_enabled" ] = _settings->heaterEnabled ();
31
37
32
38
serializeJson (doc, *response);
33
39
34
40
request->send (response);
35
41
});
36
42
43
+ _server.on (
44
+ " /settings" , HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL ,
45
+ [&](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index , size_t total) {
46
+ DynamicJsonDocument doc (200 );
47
+ auto error = deserializeJson (doc, ((const char *)data));
48
+
49
+ if (error)
50
+ {
51
+ Serial.println (" Failed to parse config file" );
52
+ }
53
+
54
+ _settings->setSsid (doc[" ssid" ]);
55
+ _settings->setPassword (doc[" password" ]);
56
+ _settings->setInfluxdbUrl (doc[" influxdb_url" ]);
57
+ _settings->setInfluxdbDatabaseName (doc[" influxdb_db_name" ]);
58
+ _settings->setHeaterEnabled (doc[" heater_enabled" ]);
59
+ request->send (200 );
60
+ // Ticker tick;
61
+ // tick.once_ms_scheduled(1, [&]() {
62
+ // _wifiNetwork->begin();
63
+ // });
64
+ });
65
+
37
66
_server.on (" /state" , HTTP_GET, [&](AsyncWebServerRequest *request) {
38
67
AsyncResponseStream *response = request->beginResponseStream (" application/json" );
39
68
DynamicJsonDocument doc (200 );
40
- doc[" ssid" ] = _settings->ssid ();
41
- doc[" influxdb_url" ] = _settings->influxdbUrl ();
42
- doc[" influxdb_db_name" ] = _settings->influxdbDatabaseName ();
69
+ doc[" heating" ] = _heater->heating ();
70
+ doc[" avg_temp" ] = _temp->getAvgTemp ();
71
+ doc[" mcp_temp" ] = _temp->getMcpTemp ();
72
+ doc[" sht_temp" ] = _temp->getShtTemp ();
73
+ doc[" sht_humidity" ] = _temp->getShtHumidity ();
74
+ doc[" heater_enabled" ] = _settings->heaterEnabled ();
43
75
44
76
serializeJson (doc, *response);
45
77
46
78
request->send (response);
47
79
});
48
80
49
81
_server.begin ();
82
+ }
83
+
84
+ void UI::onSettingsUpdated ()
85
+ {
86
+ _wifiNetwork->begin ();
50
87
}
0 commit comments