Skip to content

Commit aba2482

Browse files
authored
Merge pull request #30 from IPdotSetAF/21-update-events
21 update events
2 parents c1d0380 + 898b9fe commit aba2482

File tree

4 files changed

+105
-17
lines changed

4 files changed

+105
-17
lines changed

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ It will provide a webpage for updating the firmware/filesystem of `ESP8266` or `
3737
- Update credentials customization (default: `No credentials`)
3838
- Username
3939
- Password
40+
- Update Events:
41+
- onUpdateBegin
42+
- onUpdateEnd
43+
- Force Aborting Update using events
4044
- FileSystem Options:
4145
- SPIFFS
4246
- LittleFS
@@ -60,25 +64,44 @@ This Library is available in `Arduino Library Repository` and `PIO` and you can
6064
```
6165
2. Create an object from `ESPAsyncHTTPUpdateServer`
6266
``` C++
63-
ESPAsyncHTTPUpdateServer _updateServer;
64-
AsyncWebServer _server(80);
67+
ESPAsyncHTTPUpdateServer updateServer;
68+
AsyncWebServer server(80);
6569
```
6670
3. Setup the update server before starting the webServer
6771
``` C++
68-
_updateServer.setup(&_server);
69-
_server.begin();
72+
updateServer.setup(&server);
73+
server.begin();
7074
```
7175
#### Custom Route
7276
``` C++
73-
_updateServer.setup(&_server, "/customroute");
77+
updateServer.setup(&server, "/customroute");
7478
```
7579
#### Credentials
7680
``` C++
77-
_updateServer.setup(&_server, "username", "password");
81+
updateServer.setup(&server, "username", "password");
7882
```
7983
or
8084
``` C++
81-
_updateServer.setup(&_server, "/customroute", "username", "password");
85+
updateServer.setup(&server, "/customroute", "username", "password");
86+
```
87+
#### Events
88+
```c++
89+
updateServer.onUpdateBegin = [](const UpdateType type, int &result)
90+
{
91+
//...
92+
};
93+
94+
updateServer.onUpdateEnd = [](const UpdateType type, int &result)
95+
{
96+
//...
97+
};
98+
```
99+
#### Aborting the update
100+
```c++
101+
updateServer.onUpdateBegin = [](const UpdateType type, int &result)
102+
{
103+
result = UpdateResult::UPDATE_ABORT;
104+
};
82105
```
83106

84107
### Styling and Customizing OTA Page

examples/simple_server/simple_server.ino

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ void setup() {
4848

4949
//setup the updateServer with credentials
5050
updateServer.setup(&server, "admin", "admin");
51+
//hook to update events if you need to
52+
updateServer.onUpdateBegin = [](const UpdateType type, int &result)
53+
{
54+
//you can force abort the update like this if you need to:
55+
//result = UpdateResult::UPDATE_ABORT;
56+
Serial.println("Update started : " + String(type));
57+
};
58+
updateServer.onUpdateEnd = [](const UpdateType type, int &result)
59+
{
60+
Serial.println("Update finished : " + String(type) + " result: " + String(result));
61+
};
62+
5163
server.begin();
5264
}
5365

src/ESPAsyncHTTPUpdateServer.cpp

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
7878
if(_username != emptyString && _password != emptyString)
7979
if( !request->authenticate(_username.c_str(), _password.c_str()))
8080
return request->requestAuthentication();
81+
#ifdef ESP32
82+
AsyncWebServerResponse* response = request->beginResponse(200, "text/html", serverIndex, sizeof(serverIndex));
83+
#else
8184
AsyncWebServerResponse* response = request->beginResponse_P(200, "text/html", serverIndex, sizeof(serverIndex));
85+
#endif
8286
response->addHeader("Content-Encoding", "gzip");
8387
request->send(response); });
8488

@@ -105,29 +109,37 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
105109
if (!_authenticated)
106110
return request->requestAuthentication();
107111

108-
if (Update.hasError())
112+
if (_updateResult != UpdateResult::UPDATE_OK)
109113
{
110-
AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), String(F("Update error: ")) + _updaterError);
114+
AsyncWebServerResponse *response = request->beginResponse(200, F("text/html"), Update.hasError() ? String(F("Update error: ")) + _updaterError : "Update aborted by server.");
111115
response->addHeader("Access-Control-Allow-Headers", "*");
112116
response->addHeader("Access-Control-Allow-Origin", "*");
113117
response->addHeader("Connection", "close");
114118
request->send(response);
115119
}
116120
else
117121
{
122+
#ifdef ESP32
123+
request->send(200, PSTR("text/html"), successResponse);
124+
#else
118125
request->send_P(200, PSTR("text/html"), successResponse);
119-
restartTimer.once_ms(1000, ESP.restart);
126+
#endif
127+
Log("Rebooting...\n");
128+
restartTimer.once_ms(1000,[]{ ESP.restart(); });
120129
} },
121130
[&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
122131
{
123132
// handler for the file upload, gets the sketch bytes, and writes
124133
// them through the Update object
125134

126-
String inputName = request->getParam("name")->value();
135+
_updateType = request->getParam("name")->value() == "filesystem"?
136+
UpdateType::FILE_SYSTEM :
137+
UpdateType::FIRMWARE;
127138

128139
if (!index)
129140
{
130141
_updaterError.clear();
142+
131143
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
132144
ESPASYNCHTTPUPDATESERVER_SerialOutput.setDebugOutput(true);
133145
#endif
@@ -137,13 +149,27 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
137149
Log("Unauthenticated Update\n");
138150
return;
139151
}
152+
153+
if (onUpdateBegin)
154+
{
155+
_updateResult = UpdateResult::UPDATE_OK;
156+
onUpdateBegin(_updateType, _updateResult);
157+
if (_updateResult != UpdateResult::UPDATE_OK)
158+
{
159+
Log("Update aborted by server: %d\n", _updateResult);
160+
if(onUpdateEnd)
161+
onUpdateEnd(_updateType, _updateResult);
162+
return;
163+
}
164+
}
165+
140166
Log("Update: %s\n", filename.c_str());
141167
#ifdef ESP8266
142168
Update.runAsync(true);
143169
#endif
144-
if (inputName == "filesystem")
170+
if (_updateType == UpdateType::FILE_SYSTEM)
145171
{
146-
Log("updating filesystem");
172+
Log("updating filesystem\n");
147173
#ifdef ESP8266
148174
int command = U_FS;
149175
size_t fsSize = ((size_t)FS_end - (size_t)FS_start);
@@ -165,25 +191,28 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
165191
}
166192
else
167193
{
168-
Log("updating flash");
194+
Log("updating flash\n");
169195
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
170196
if (!Update.begin(maxSketchSpace, U_FLASH)) // start with max available size
171197
_setUpdaterError();
172198
}
173199
}
174200

175-
if (_authenticated && len && !_updaterError.length())
201+
if (_authenticated && len && _updateResult == UpdateResult::UPDATE_OK)
176202
{
177203
Log(".");
178204
if (Update.write(data, len) != len)
179205
_setUpdaterError();
180206
}
181207

182-
if (_authenticated && final && !_updaterError.length())
208+
if (_authenticated && final && _updateResult == UpdateResult::UPDATE_OK)
183209
{
184210
if (Update.end(true))
185211
{ // true to set the size to the current progress
186-
Log("Update Success: \nRebooting...\n");
212+
Log("Update Success.\n");
213+
_updateResult = UpdateResult::UPDATE_OK;
214+
if(onUpdateEnd)
215+
onUpdateEnd(_updateType, _updateResult);
187216
}
188217
else
189218
_setUpdaterError();
@@ -204,4 +233,8 @@ void ESPAsyncHTTPUpdateServer::_setUpdaterError()
204233
StreamString str;
205234
Update.printError(str);
206235
_updaterError = str.c_str();
236+
237+
_updateResult = UpdateResult::UPDATE_ERROR;
238+
if(onUpdateEnd)
239+
onUpdateEnd(_updateType, _updateResult);
207240
}

src/ESPAsyncHTTPUpdateServer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,27 @@
33

44
#include <ESPAsyncWebServer.h>
55

6+
enum UpdateType
7+
{
8+
FIRMWARE,
9+
FILE_SYSTEM
10+
};
11+
12+
enum UpdateResult
13+
{
14+
UPDATE_OK,
15+
UPDATE_ABORT,
16+
UPDATE_ERROR,
17+
};
18+
19+
typedef void (*ESPAsyncHTTPUpdateServer_event)(const UpdateType type, int &result);
20+
621
class ESPAsyncHTTPUpdateServer
722
{
823
public:
24+
ESPAsyncHTTPUpdateServer_event onUpdateBegin = NULL;
25+
ESPAsyncHTTPUpdateServer_event onUpdateEnd = NULL;
26+
927
ESPAsyncHTTPUpdateServer();
1028

1129
void setup(AsyncWebServer *server)
@@ -40,6 +58,8 @@ class ESPAsyncHTTPUpdateServer
4058
String _password;
4159
bool _authenticated;
4260
String _updaterError;
61+
UpdateType _updateType;
62+
int _updateResult;
4363
};
4464

4565
#endif

0 commit comments

Comments
 (0)