mirror of
https://gitlab.com/zwirbel/illucat.git
synced 2025-12-16 10:04:30 +01:00
Merge branch 'feature/6-http-firmware-update' into 'master'
Resolve #6 firmware upload via http post Closes #6 See merge request 0x1d/illucat!6
This commit is contained in:
@@ -12,6 +12,10 @@
|
|||||||
|
|
||||||
<body class="sui">
|
<body class="sui">
|
||||||
<div class="content">
|
<div class="content">
|
||||||
|
<form id="fwUpload" enctype="multipart/form-data" method="post" action="#">
|
||||||
|
<input id="fileupload" name="myfile" type="file" />
|
||||||
|
<input type="submit" value="submit" id="submit" />
|
||||||
|
</form>
|
||||||
<form class="param-control container collapsible open" action="#" method="POST">
|
<form class="param-control container collapsible open" action="#" method="POST">
|
||||||
<span class="heading">Pixels</span>
|
<span class="heading">Pixels</span>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
|
|||||||
@@ -18,15 +18,16 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class IlluCat : public Sprocket
|
||||||
class IlluCat : public Sprocket {
|
{
|
||||||
public:
|
public:
|
||||||
AsyncWebServer* server;
|
AsyncWebServer *server;
|
||||||
SprocketConfig sprocketConfig;
|
SprocketConfig sprocketConfig;
|
||||||
OtaConfig otaConfig;
|
OtaConfig otaConfig;
|
||||||
WebServerConfig webConfig;
|
WebServerConfig webConfig;
|
||||||
|
|
||||||
IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) {
|
IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg)
|
||||||
|
{
|
||||||
sprocketConfig = cfg;
|
sprocketConfig = cfg;
|
||||||
otaConfig = otaCfg;
|
otaConfig = otaCfg;
|
||||||
webConfig = webCfg;
|
webConfig = webCfg;
|
||||||
@@ -34,7 +35,8 @@ class IlluCat : public Sprocket {
|
|||||||
server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json");
|
server->serveStatic(PIXEL_CONFIG_FILE, SPIFFS, "pixelConfig.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup(){
|
void setup()
|
||||||
|
{
|
||||||
addPlugin(new PixelPlugin());
|
addPlugin(new PixelPlugin());
|
||||||
addPlugin(new WebServerPlugin(webConfig, server));
|
addPlugin(new WebServerPlugin(webConfig, server));
|
||||||
addPlugin(new WebConfigPlugin(server));
|
addPlugin(new WebConfigPlugin(server));
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
#include <Sprocket.h>
|
#include <Sprocket.h>
|
||||||
|
#include <Updater.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "utils_print.h"
|
#include "utils_print.h"
|
||||||
@@ -14,61 +15,120 @@ using namespace std;
|
|||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
// TODO headerfile
|
// TODO headerfile
|
||||||
|
// FIXME constants
|
||||||
|
|
||||||
class WebApi : public Plugin {
|
class WebApi : public Plugin
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
Network* network;
|
Network *network;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AsyncWebServer* server;
|
AsyncWebServer *server;
|
||||||
AsyncWebSocket* ws;
|
AsyncWebSocket *ws;
|
||||||
SprocketMessage currentMessage;
|
SprocketMessage currentMessage;
|
||||||
|
|
||||||
int broadcast;
|
int broadcast;
|
||||||
|
|
||||||
WebApi(AsyncWebServer* _server, int _broadcast = 0){
|
WebApi(AsyncWebServer *_server, int _broadcast = 0)
|
||||||
|
{
|
||||||
server = _server;
|
server = _server;
|
||||||
broadcast = _broadcast;
|
broadcast = _broadcast;
|
||||||
|
Update.runAsync(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void activate(Scheduler* _scheduler, Network* _network) {
|
void activate(Scheduler *_scheduler, Network *_network)
|
||||||
|
{
|
||||||
network = _network;
|
network = _network;
|
||||||
ws = new AsyncWebSocket("/ws"); // FIXME constant /ws
|
ws = new AsyncWebSocket("/ws");
|
||||||
ws->onEvent(bind(&WebApi::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
ws->onEvent(bind(&WebApi::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||||
server->addHandler(ws);
|
server->addHandler(ws);
|
||||||
server->on("/api", HTTP_POST, bind(&WebApi::postRequestHandler, this, _1));
|
server->on("/api", HTTP_POST, bind(&WebApi::postRequestHandler, this, _1));
|
||||||
|
server->on("/update", HTTP_GET, bind(&WebApi::simpleFirmwareUploadFormvoid, this, _1));
|
||||||
|
server->on("/update", HTTP_POST, bind(&WebApi::onFirmwareUpdateRequest, this, _1), bind(&WebApi::onFirmwareUpload, this, _1, _2, _3, _4, _5, _6));
|
||||||
}
|
}
|
||||||
|
|
||||||
void postRequestHandler(AsyncWebServerRequest *request) {
|
void postRequestHandler(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
PRINT_MSG(Serial, SPROCKET_TYPE, "POST WebApi");
|
PRINT_MSG(Serial, SPROCKET_TYPE, "POST WebApi");
|
||||||
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
||||||
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
||||||
currentMessage.broadcast = atoi(WebUtils::getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
currentMessage.broadcast = atoi(WebUtils::getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
||||||
String msg = currentMessage.toJsonString();
|
String msg = currentMessage.toJsonString();
|
||||||
publish(currentMessage.topic, currentMessage.payload);
|
publish(currentMessage.topic, currentMessage.payload);
|
||||||
if(currentMessage.broadcast){
|
if (currentMessage.broadcast)
|
||||||
|
{
|
||||||
network->broadcast(msg);
|
network->broadcast(msg);
|
||||||
}
|
}
|
||||||
request->send(200, "text/plain", msg);
|
request->send(200, "text/plain", msg);
|
||||||
}
|
}
|
||||||
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
// FIXME to limitted
|
// FIXME to limitted
|
||||||
if(type == WS_EVT_DATA){
|
if (type == WS_EVT_DATA)
|
||||||
|
{
|
||||||
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||||
dispatch(0, frame);
|
dispatch(0, frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dispatch( uint32_t from, String &msg ) {
|
void simpleFirmwareUploadFormvoid(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
request->send(200, "text/html", "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>");
|
||||||
|
}
|
||||||
|
|
||||||
|
void onFirmwareUpdateRequest(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
bool hasError = !Update.hasError();
|
||||||
|
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", hasError ? "OK" : "FAIL");
|
||||||
|
response->addHeader("Connection", "close");
|
||||||
|
request->send(response);
|
||||||
|
publish("esp/reboot", String(hasError));
|
||||||
|
}
|
||||||
|
|
||||||
|
void onFirmwareUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
|
||||||
|
{
|
||||||
|
if (!index)
|
||||||
|
{
|
||||||
|
PRINT_MSG(Serial, SPROCKET_TYPE, "Update Start %s", filename.c_str());
|
||||||
|
Update.runAsync(true);
|
||||||
|
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000))
|
||||||
|
{
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!Update.hasError())
|
||||||
|
{
|
||||||
|
if (Update.write(data, len) != len)
|
||||||
|
{
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (final)
|
||||||
|
{
|
||||||
|
if (Update.end(true))
|
||||||
|
{
|
||||||
|
PRINT_MSG(Serial, SPROCKET_TYPE, "Update Success with %uB", index + len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Update.printError(Serial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dispatch(uint32_t from, String &msg)
|
||||||
|
{
|
||||||
currentMessage.fromJsonString(msg);
|
currentMessage.fromJsonString(msg);
|
||||||
if(currentMessage.valid){
|
if (currentMessage.valid)
|
||||||
|
{
|
||||||
currentMessage.from = from;
|
currentMessage.from = from;
|
||||||
publish(currentMessage.topic, currentMessage.payload);
|
publish(currentMessage.topic, currentMessage.payload);
|
||||||
if(broadcast){
|
if (broadcast)
|
||||||
|
{
|
||||||
network->broadcast(msg);
|
network->broadcast(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user