mirror of
https://gitlab.com/wirelos/sprocket-plugin-web.git
synced 2025-12-16 06:34:31 +01:00
initial commit
This commit is contained in:
126
src/WebApiPlugin.cpp
Normal file
126
src/WebApiPlugin.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#ifndef __WEBAPI_PLUGIN__
|
||||
#define __WEBAPI_PLUGIN__
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <Sprocket.h>
|
||||
#include <Updater.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "utils/utils_print.h"
|
||||
#include "utils/utils_web.h"
|
||||
#include "WebServerPlugin.cpp"
|
||||
#include "WebConfigPlugin.cpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
// TODO headerfile
|
||||
// FIXME constants
|
||||
|
||||
class WebApiPlugin : public Plugin
|
||||
{
|
||||
|
||||
public:
|
||||
AsyncWebServer *server;
|
||||
AsyncWebSocket *ws;
|
||||
SprocketMessage currentMessage;
|
||||
|
||||
WebApiPlugin(AsyncWebServer *_server)
|
||||
{
|
||||
server = _server;
|
||||
Update.runAsync(true);
|
||||
}
|
||||
|
||||
void activate(Scheduler *_scheduler)
|
||||
{
|
||||
ws = new AsyncWebSocket("/ws");
|
||||
ws->onEvent(bind(&WebApiPlugin::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||
server->addHandler(ws);
|
||||
server->on("/api", HTTP_POST, bind(&WebApiPlugin::postRequestHandler, this, _1));
|
||||
server->on("/update", HTTP_GET, bind(&WebApiPlugin::simpleFirmwareUploadFormvoid, this, _1));
|
||||
server->on("/update", HTTP_POST, bind(&WebApiPlugin::onFirmwareUpdateRequest, this, _1), bind(&WebApiPlugin::onFirmwareUpload, this, _1, _2, _3, _4, _5, _6));
|
||||
subscribe("ws/broadcast", bind(&WebApiPlugin::wsBroadcast, this, _1));
|
||||
PRINT_MSG(Serial, "WEB", "API activated");
|
||||
}
|
||||
|
||||
void wsBroadcast(String msg)
|
||||
{
|
||||
ws->textAll(msg);
|
||||
}
|
||||
|
||||
void postRequestHandler(AsyncWebServerRequest *request)
|
||||
{
|
||||
PRINT_MSG(Serial, "WEB", "POST WebApiPlugin");
|
||||
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
||||
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
||||
currentMessage.broadcast = atoi(WebUtils::getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
||||
String msg = currentMessage.toJsonString();
|
||||
publish(currentMessage.topic, currentMessage.payload);
|
||||
request->send(200, "text/plain", msg);
|
||||
}
|
||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{
|
||||
// FIXME to limitted
|
||||
if (type == WS_EVT_DATA)
|
||||
{
|
||||
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||
dispatch(frame);
|
||||
}
|
||||
}
|
||||
|
||||
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, "OTA", "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, "OTA", "Update Success with %uB", index + len);
|
||||
}
|
||||
else
|
||||
{
|
||||
Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dispatch(String &msg)
|
||||
{
|
||||
currentMessage.fromJsonString(msg);
|
||||
if (currentMessage.valid)
|
||||
{
|
||||
publish(currentMessage.topic, currentMessage.payload);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
56
src/WebConfigPlugin.cpp
Normal file
56
src/WebConfigPlugin.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef __WEB_CONFIG_PLUGIN_H__
|
||||
#define __WEB_CONFIG_PLUGIN_H__
|
||||
|
||||
#include <FS.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include "Plugin.h"
|
||||
#include "WebServerConfig.h"
|
||||
#include "utils/utils_print.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
class WebConfigPlugin : public Plugin
|
||||
{
|
||||
private:
|
||||
AsyncWebServer *server;
|
||||
|
||||
public:
|
||||
WebConfigPlugin(AsyncWebServer *webServer)
|
||||
{
|
||||
server = webServer;
|
||||
server->serveStatic("/config.json", SPIFFS, "config.json");
|
||||
}
|
||||
void activate(Scheduler *userScheduler)
|
||||
{
|
||||
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
PRINT_MSG(Serial, "WEB", "GET /heap");
|
||||
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
||||
});
|
||||
server->on("/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||
PRINT_MSG(Serial, "WEB", "POST /restart");
|
||||
ESP.restart();
|
||||
});
|
||||
server->on("/config", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||
PRINT_MSG(Serial, "WEB", "POST /config");
|
||||
if (request->hasParam("config", true) && request->hasParam("fileName", true))
|
||||
{
|
||||
String inStr = request->getParam("config", true)->value();
|
||||
String fileName = request->getParam("fileName", true)->value();
|
||||
File f = SPIFFS.open(fileName, "w");
|
||||
if (!f)
|
||||
{
|
||||
PRINT_MSG(Serial, "WEB", String(String("file open for read failed: ") + fileName).c_str());
|
||||
}
|
||||
PRINT_MSG(Serial, "WEB", String(String("writing to SPIFFS file: ") + fileName).c_str());
|
||||
f.print(inStr);
|
||||
f.close();
|
||||
}
|
||||
request->redirect("/");
|
||||
});
|
||||
PRINT_MSG(Serial, "WEB", "WebConfig activated");
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
15
src/WebServerConfig.h
Normal file
15
src/WebServerConfig.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef __WEB_SERVER_CONFIG__
|
||||
#define __WEB_SERVER_CONFIG__
|
||||
|
||||
struct WebServerConfig
|
||||
{
|
||||
const char *contextPath;
|
||||
const char *docRoot;
|
||||
const char *defaultFile;
|
||||
int port;
|
||||
int useBasicAuth;
|
||||
const char *user;
|
||||
const char *password;
|
||||
};
|
||||
|
||||
#endif
|
||||
43
src/WebServerPlugin.cpp
Normal file
43
src/WebServerPlugin.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef __WEB_SERVER_PLUGIN__
|
||||
#define __WEB_SERVER_PLUGIN__
|
||||
|
||||
#include <FS.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include "TaskSchedulerDeclarations.h"
|
||||
#include "Plugin.h"
|
||||
#include "WebServerConfig.h"
|
||||
#include "utils/utils_print.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
class WebServerPlugin : public Plugin
|
||||
{
|
||||
private:
|
||||
WebServerConfig config;
|
||||
|
||||
public:
|
||||
AsyncWebServer *server;
|
||||
WebServerPlugin(WebServerConfig cfg)
|
||||
{
|
||||
config = cfg;
|
||||
server = new AsyncWebServer(config.port);
|
||||
}
|
||||
WebServerPlugin(WebServerConfig cfg, AsyncWebServer *webServer)
|
||||
{
|
||||
config = cfg;
|
||||
server = webServer;
|
||||
}
|
||||
void activate(Scheduler *userScheduler)
|
||||
{
|
||||
AsyncStaticWebHandler &staticWebHandler = server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
|
||||
if (config.useBasicAuth)
|
||||
{
|
||||
staticWebHandler.setAuthentication(config.user, config.password);
|
||||
}
|
||||
server->begin();
|
||||
PRINT_MSG(Serial, "WEB", "Server activated");
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
24
src/examples/basic/config.h
Normal file
24
src/examples/basic/config.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef __DEVICE_CONFIG__
|
||||
#define __DEVICE_CONFIG__
|
||||
|
||||
// Scheduler config
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
#define _TASK_PRIORITY
|
||||
|
||||
// Chip config
|
||||
#define SPROCKET_TYPE "SPROCKET"
|
||||
#define SERIAL_BAUD_RATE 115200
|
||||
#define STARTUP_DELAY 1000
|
||||
|
||||
// network config
|
||||
#define SPROCKET_MODE 1
|
||||
#define WIFI_CHANNEL 11
|
||||
#define AP_SSID "sprocket"
|
||||
#define AP_PASSWORD "th3r31sn0sp00n"
|
||||
#define STATION_SSID "MyAP"
|
||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||
#define HOSTNAME "sprocket"
|
||||
#define CONNECT_TIMEOUT 10000
|
||||
|
||||
#endif
|
||||
46
src/examples/basic/main.cpp
Normal file
46
src/examples/basic/main.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "config.h"
|
||||
#include "WiFiNet.h"
|
||||
#include "Sprocket.h"
|
||||
#include "ESPAsyncWebServer.h"
|
||||
|
||||
#include "WebServerConfig.h"
|
||||
#include "WebServerPlugin.cpp"
|
||||
#include "WebConfigPlugin.cpp"
|
||||
#include "WebApiPlugin.cpp"
|
||||
|
||||
WiFiNet *network;
|
||||
Sprocket *sprocket;
|
||||
AsyncWebServer *server;
|
||||
WebServerPlugin *webServerPlugin;
|
||||
WebConfigPlugin *webConfigPlugin;
|
||||
WebApiPlugin *webApiPlugin;
|
||||
|
||||
void setup()
|
||||
{
|
||||
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
||||
webServerPlugin = new WebServerPlugin({"/", "/www", "index.html", 80});
|
||||
webConfigPlugin = new WebConfigPlugin(webServerPlugin->server);
|
||||
webApiPlugin = new WebApiPlugin(webServerPlugin->server);
|
||||
|
||||
sprocket->addPlugin(webServerPlugin);
|
||||
sprocket->addPlugin(webConfigPlugin);
|
||||
sprocket->addPlugin(webApiPlugin);
|
||||
|
||||
network = new WiFiNet(
|
||||
SPROCKET_MODE,
|
||||
STATION_SSID,
|
||||
STATION_PASSWORD,
|
||||
AP_SSID,
|
||||
AP_PASSWORD,
|
||||
HOSTNAME,
|
||||
CONNECT_TIMEOUT);
|
||||
network->connect();
|
||||
|
||||
sprocket->activate();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
sprocket->loop();
|
||||
yield();
|
||||
}
|
||||
Reference in New Issue
Block a user