Skip to content

Commit 18099d2

Browse files
committed
implemented events
1 parent acb0eb3 commit 18099d2

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/ESPAsyncHTTPUpdateServer.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,14 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
123123
// handler for the file upload, gets the sketch bytes, and writes
124124
// them through the Update object
125125

126-
String inputName = request->getParam("name")->value();
126+
_updateType = request->getParam("name")->value() == "filesystem"?
127+
UpdateType::FILE_SYSTEM :
128+
UpdateType::FIRMWARE;
127129

128130
if (!index)
129131
{
130132
_updaterError.clear();
133+
131134
#ifdef ESPASYNCHTTPUPDATESERVER_DEBUG
132135
ESPASYNCHTTPUPDATESERVER_SerialOutput.setDebugOutput(true);
133136
#endif
@@ -137,11 +140,25 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
137140
Log("Unauthenticated Update\n");
138141
return;
139142
}
143+
144+
if (onUpdateBegin)
145+
{
146+
_updateResult = UpdateResult::UPDATE_OK;
147+
onUpdateBegin(_updateType, _updateResult);
148+
if (_updateResult != UpdateResult::UPDATE_OK)
149+
{
150+
Log("Update aborted by server: %d\n", _updateResult);
151+
if(onUpdateEnd)
152+
onUpdateEnd(_updateType, _updateResult);
153+
return;
154+
}
155+
}
156+
140157
Log("Update: %s\n", filename.c_str());
141158
#ifdef ESP8266
142159
Update.runAsync(true);
143160
#endif
144-
if (inputName == "filesystem")
161+
if (_updateType == UpdateType::FILE_SYSTEM)
145162
{
146163
Log("updating filesystem");
147164
#ifdef ESP8266
@@ -183,6 +200,9 @@ void ESPAsyncHTTPUpdateServer::setup(AsyncWebServer *server, const String &path,
183200
{
184201
if (Update.end(true))
185202
{ // true to set the size to the current progress
203+
_updateResult = UpdateResult::UPDATE_OK;
204+
if(onUpdateEnd)
205+
onUpdateEnd(_updateType, _updateResult);
186206
Log("Update Success: \nRebooting...\n");
187207
}
188208
else
@@ -204,4 +224,8 @@ void ESPAsyncHTTPUpdateServer::_setUpdaterError()
204224
StreamString str;
205225
Update.printError(str);
206226
_updaterError = str.c_str();
227+
228+
_updateResult = UpdateResult::UPDATE_ERROR;
229+
if(onUpdateEnd)
230+
onUpdateEnd(_updateType, _updateResult);
207231
}

src/ESPAsyncHTTPUpdateServer.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ enum UpdateType
99
FILE_SYSTEM
1010
};
1111

12-
enum UpdateCode
12+
enum UpdateResult
1313
{
1414
UPDATE_OK,
1515
UPDATE_ABORT,
1616
UPDATE_ERROR,
1717
};
1818

19-
typedef void (*ESPAsyncHTTPUpdateServer_event)(const UpdateType updateType, int &resultCode);
19+
typedef void (*ESPAsyncHTTPUpdateServer_event)(const UpdateType type, int &result);
2020

2121
class ESPAsyncHTTPUpdateServer
2222
{
2323
public:
24-
ESPAsyncHTTPUpdateServer_event onUpdateBegin;
25-
ESPAsyncHTTPUpdateServer_event onUpdateEnd;
24+
ESPAsyncHTTPUpdateServer_event onUpdateBegin = NULL;
25+
ESPAsyncHTTPUpdateServer_event onUpdateEnd = NULL;
2626

2727
ESPAsyncHTTPUpdateServer();
2828

@@ -58,6 +58,8 @@ class ESPAsyncHTTPUpdateServer
5858
String _password;
5959
bool _authenticated;
6060
String _updaterError;
61+
UpdateType _updateType;
62+
int _updateResult;
6163
};
6264

6365
#endif

0 commit comments

Comments
 (0)