Switched to native ESP32 webserver
- new status API - webdav without crashing
This commit is contained in:
parent
e929c8d3d4
commit
fc469f47a6
|
@ -74,6 +74,16 @@ namespace portablefs
|
||||||
{
|
{
|
||||||
return SPIFFS.mkdir(name);
|
return SPIFFS.mkdir(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline size_t usedBytes()
|
||||||
|
{
|
||||||
|
return SPIFFS.usedBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t totalBytes()
|
||||||
|
{
|
||||||
|
return SPIFFS.totalBytes();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace portablefs
|
} // namespace portablefs
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
#include "EspHttp.h"
|
||||||
|
|
||||||
|
esp_err_t rawCallback(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
EspHttp::Callback *userCb = (EspHttp::Callback *)(req->user_ctx);
|
||||||
|
(*userCb)(req);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getUrlQueryParameter(httpd_req_t *req, const char *name, int defaultValue)
|
||||||
|
{
|
||||||
|
size_t bufferLength = httpd_req_get_url_query_len(req) + 1;
|
||||||
|
int result = defaultValue;
|
||||||
|
if (bufferLength > 1)
|
||||||
|
{
|
||||||
|
char* buf = (char*)malloc(bufferLength);
|
||||||
|
if (httpd_req_get_url_query_str(req, buf, bufferLength) == ESP_OK)
|
||||||
|
{
|
||||||
|
char param[32];
|
||||||
|
/* Get value of expected key from query string */
|
||||||
|
if (httpd_query_key_value(buf, name, param, sizeof(param)) == ESP_OK)
|
||||||
|
result = atoi(param);
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <esp_http_server.h>
|
||||||
|
#include <functional>
|
||||||
|
#include "Dtypes.h"
|
||||||
|
|
||||||
|
constexpr int MAX_URI_HANDLERS = 10;
|
||||||
|
|
||||||
|
esp_err_t rawCallback(httpd_req_t *req);
|
||||||
|
|
||||||
|
class EspHttp
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Callback = std::function<void(httpd_req_t *)>;
|
||||||
|
|
||||||
|
EspHttp(int port = 80) : server_(0), nextFreeHandler_(0)
|
||||||
|
{
|
||||||
|
config_ = HTTPD_DEFAULT_CONFIG();
|
||||||
|
config_.server_port = port;
|
||||||
|
config_.max_uri_handlers = MAX_URI_HANDLERS;
|
||||||
|
config_.uri_match_fn = httpd_uri_match_wildcard;
|
||||||
|
}
|
||||||
|
~EspHttp()
|
||||||
|
{
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void start()
|
||||||
|
{
|
||||||
|
if (httpd_start(&server_, &config_) != ESP_OK)
|
||||||
|
assert_msg(false, "HTTPD server didn't start");
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop()
|
||||||
|
{
|
||||||
|
if (server_)
|
||||||
|
httpd_stop(server_);
|
||||||
|
server_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on(const char *uri, httpd_method_t method, Callback cb)
|
||||||
|
{
|
||||||
|
if (nextFreeHandler_ == MAX_URI_HANDLERS)
|
||||||
|
{
|
||||||
|
assert_msg(nextFreeHandler_ < MAX_URI_HANDLERS, "Too many handlers - increase MAX_URI_HANDLERS");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handlerCallbacks_[nextFreeHandler_] = cb;
|
||||||
|
|
||||||
|
httpd_uri_t uri_endpoint_cfg = {
|
||||||
|
.uri = uri,
|
||||||
|
.method = method,
|
||||||
|
.handler = rawCallback,
|
||||||
|
.user_ctx = (void *)(&handlerCallbacks_[nextFreeHandler_])};
|
||||||
|
if (httpd_register_uri_handler(server_, &uri_endpoint_cfg) != ESP_OK)
|
||||||
|
Serial.println("Failed to register url handler");
|
||||||
|
++nextFreeHandler_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(const char *data, ssize_t length);
|
||||||
|
|
||||||
|
private:
|
||||||
|
httpd_config_t config_;
|
||||||
|
httpd_handle_t server_;
|
||||||
|
int nextFreeHandler_;
|
||||||
|
Callback handlerCallbacks_[MAX_URI_HANDLERS];
|
||||||
|
};
|
||||||
|
|
||||||
|
esp_err_t rawCallback(httpd_req_t *req);
|
||||||
|
|
||||||
|
int getUrlQueryParameter(httpd_req_t *req, const char *name, int defaultValue);
|
|
@ -0,0 +1,137 @@
|
||||||
|
#include "FilesystemAbstraction.h"
|
||||||
|
#include "WebDAV.h"
|
||||||
|
|
||||||
|
namespace webdav_constants
|
||||||
|
{
|
||||||
|
constexpr const char *MULTISTATUS_START = "<?xml version=\"1.0\" ?><D:multistatus xmlns:D=\"DAV:\">";
|
||||||
|
constexpr const char *MULTISTATUS_END = "</D:multistatus>";
|
||||||
|
constexpr const char *RESPONSE_START = "<D:response>";
|
||||||
|
constexpr const char *RESPONSE_END = "</D:response>\n";
|
||||||
|
constexpr const char *HREF_START = "<D:href>";
|
||||||
|
constexpr const char *HREF_END = "</D:href>";
|
||||||
|
constexpr const char *PROPSTAT_START = "<D:propstat>";
|
||||||
|
constexpr const char *PROPSTAT_END = "</D:propstat>";
|
||||||
|
constexpr const char *PROP_START = "<D:prop>";
|
||||||
|
constexpr const char *PROP_END = "</D:prop>";
|
||||||
|
constexpr const char *RESOURCETYPE_START = "<D:resourcetype>";
|
||||||
|
constexpr const char *RESOURCETYPE_END = "</D:resourcetype>";
|
||||||
|
constexpr const char *RESOURCE_COLLECTION = "<D:collection/>";
|
||||||
|
constexpr const char *HTTP_204_NO_CONTENT = "HTTP/1.1 204 No Content";
|
||||||
|
|
||||||
|
constexpr const char *CONTENTLEN_START = "<D:getcontentlength>";
|
||||||
|
constexpr const char *CONTENTLEN_END = "</D:getcontentlength>";
|
||||||
|
constexpr const char *CREATEDATE_START = "<D:creationdate>";
|
||||||
|
constexpr const char *CREATEDATE_END = "</D:creationdate>";
|
||||||
|
constexpr const char *MODDATE_START = "<D:getlastmodified>";
|
||||||
|
constexpr const char *MODDATE_END = "</D:getlastmodified>";
|
||||||
|
constexpr const char *STATUS_OK = "<D:status>HTTP/1.1 200 OK</D:status>";
|
||||||
|
constexpr const char *USED_BYTES_START = "<D:quota-used-bytes>";
|
||||||
|
constexpr const char *USED_BYTES_END = "</D:quota-used-bytes>";
|
||||||
|
} // namespace webdav_constants
|
||||||
|
|
||||||
|
size_t webdavFileListingSpiffs(char *buffer, size_t maxLength,
|
||||||
|
const char *spiffsPath)
|
||||||
|
{
|
||||||
|
using namespace webdav_constants;
|
||||||
|
|
||||||
|
size_t bytesWritten = 0;
|
||||||
|
|
||||||
|
auto toBuffer = [&buffer, &bytesWritten](const char *text) {
|
||||||
|
auto len = strlen(text);
|
||||||
|
memcpy(buffer, text, len);
|
||||||
|
buffer += len;
|
||||||
|
bytesWritten += len;
|
||||||
|
};
|
||||||
|
|
||||||
|
// crude upper bound on how much space a file entry + footer needs
|
||||||
|
constexpr size_t sizePerFile = 512;
|
||||||
|
assert(maxLength >= sizePerFile);
|
||||||
|
|
||||||
|
int fileIdx = 0;
|
||||||
|
|
||||||
|
auto dir = portablefs::openDir(spiffsPath);
|
||||||
|
|
||||||
|
toBuffer(MULTISTATUS_START);
|
||||||
|
|
||||||
|
bool incomplete = false;
|
||||||
|
while (dir.next())
|
||||||
|
{
|
||||||
|
const auto freeSpace = maxLength - bytesWritten;
|
||||||
|
if (freeSpace < sizePerFile)
|
||||||
|
{
|
||||||
|
incomplete = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
fileIdx += 1;
|
||||||
|
toBuffer(RESPONSE_START);
|
||||||
|
toBuffer(HREF_START);
|
||||||
|
const auto fileName = dir.fileName();
|
||||||
|
const auto fileNameWithoutDir = fileName.substring(fileName.lastIndexOf("/") + 1);
|
||||||
|
toBuffer((fileNameWithoutDir + ".st").c_str());
|
||||||
|
toBuffer(HREF_END);
|
||||||
|
toBuffer(PROPSTAT_START);
|
||||||
|
toBuffer(PROP_START);
|
||||||
|
if (dir.isDirectory())
|
||||||
|
{
|
||||||
|
toBuffer(RESOURCETYPE_START);
|
||||||
|
toBuffer(RESOURCE_COLLECTION);
|
||||||
|
toBuffer(RESOURCETYPE_END);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
toBuffer(CONTENTLEN_START);
|
||||||
|
String fileSizeStr(dir.fileSize());
|
||||||
|
toBuffer(fileSizeStr.c_str());
|
||||||
|
toBuffer(CONTENTLEN_END);
|
||||||
|
}
|
||||||
|
|
||||||
|
toBuffer(PROP_END);
|
||||||
|
toBuffer(STATUS_OK);
|
||||||
|
toBuffer(PROPSTAT_END);
|
||||||
|
toBuffer(webdav_constants::RESPONSE_END);
|
||||||
|
}
|
||||||
|
toBuffer(MULTISTATUS_END);
|
||||||
|
|
||||||
|
if (incomplete)
|
||||||
|
Serial.println("WebDAV listing response is incomplete, because buffer was too small");
|
||||||
|
return bytesWritten;
|
||||||
|
}
|
||||||
|
|
||||||
|
String uriToFileName(const String &uriStr)
|
||||||
|
{
|
||||||
|
String filename;
|
||||||
|
if (uriStr.endsWith(".st"))
|
||||||
|
filename = uriStr.substring(0, uriStr.length() - strlen(".st"));
|
||||||
|
|
||||||
|
filename = "/dat/" + filename;
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
void webdavHandler(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
String uri = String(req->uri).substring(strlen("/webdav/"));
|
||||||
|
constexpr size_t WEBDAV_BUFF_LEN = 1024 * 256;
|
||||||
|
static char *webdavBuffer = (char *)heap_caps_malloc(WEBDAV_BUFF_LEN, MALLOC_CAP_SPIRAM);
|
||||||
|
switch (req->method)
|
||||||
|
{
|
||||||
|
case HTTP_GET:
|
||||||
|
case HTTP_PROPFIND:
|
||||||
|
{
|
||||||
|
size_t bytesWritten = webdavFileListingSpiffs(webdavBuffer, WEBDAV_BUFF_LEN, "/dat");
|
||||||
|
httpd_resp_send(req, webdavBuffer, bytesWritten);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case HTTP_DELETE:
|
||||||
|
{
|
||||||
|
httpd_resp_set_hdr(req, "Content-Type", "text/plain");
|
||||||
|
|
||||||
|
if (portablefs::remove(uriToFileName(uri).c_str()))
|
||||||
|
httpd_resp_send(req, "Deleted file", -1);
|
||||||
|
else
|
||||||
|
httpd_resp_send_404(req);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
httpd_resp_send_404(req);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
#include "Dtypes.h"
|
||||||
|
#include "EspHttp.h"
|
||||||
|
|
||||||
|
void webdavHandler(httpd_req_t *req);
|
|
@ -39,6 +39,10 @@ public:
|
||||||
otherChunk->init(0, 0);
|
otherChunk->init(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t numMeasurements() const {
|
||||||
|
return currentChunk->getStartIndex() + currentChunk->numMeasurements();
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Encoder_T>
|
template<typename Encoder_T>
|
||||||
void serialize(Encoder_T & encoder, uint32_t startIdx) const
|
void serialize(Encoder_T & encoder, uint32_t startIdx) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
#define _HW_V_20
|
//#define _HW_V_20
|
||||||
|
|
||||||
// HX711 load cell
|
// HX711 load cell
|
||||||
#ifdef PLATFORM_ESP32
|
#ifdef PLATFORM_ESP32
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESPAsyncTCP.h>
|
#include <ESPAsyncTCP.h>
|
||||||
#endif
|
#endif
|
||||||
#include <ESPAsyncWebServer.h>
|
//#include <ESPAsyncWebServer.h>
|
||||||
#include <WiFiUdp.h> // for NTP
|
#include <WiFiUdp.h> // for NTP
|
||||||
#include <NTPClient.h> // for NTP
|
#include <NTPClient.h> // for NTP
|
||||||
|
|
||||||
|
@ -23,55 +23,65 @@
|
||||||
#include "ConfigWifi.h"
|
#include "ConfigWifi.h"
|
||||||
#include "ConfigHardware.h"
|
#include "ConfigHardware.h"
|
||||||
|
|
||||||
#include "AsyncWebDav.h"
|
#include "EspHttp.h"
|
||||||
|
#include "WebDAV.h"
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
|
||||||
WiFiUDP ntpUDP;
|
WiFiUDP ntpUDP;
|
||||||
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
NTPClient timeClient(ntpUDP, "pool.ntp.org");
|
||||||
|
|
||||||
typedef MeasurementSession<uint16_t, SpiffsStorageReader, SpiffsStorageWriter, CONFIG_SESSION_CHUNK_SIZE> Session_T;
|
typedef MeasurementSession<uint16_t, SpiffsStorageReader, SpiffsStorageWriter, CONFIG_SESSION_CHUNK_SIZE> Session_T;
|
||||||
|
|
||||||
template<typename Session_T>
|
template <typename Session_T>
|
||||||
class SessionManager
|
class SessionManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SessionManager() : measuring_(false), lastCallTime_(0)
|
SessionManager() : measuring_(false), lastCallTime_(0)
|
||||||
{}
|
{
|
||||||
|
|
||||||
void begin() {
|
|
||||||
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
|
|
||||||
scale.tare( CONFIG_TARE_AVG_COUNT );
|
|
||||||
session.init( timeClient.getEpochTime() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void startMeasurements() {
|
void begin()
|
||||||
|
{
|
||||||
|
Serial.println("Beginning tare");
|
||||||
|
scale.begin(CONFIG_SCALE_DOUT_PIN, CONFIG_SCALE_SCK_PIN);
|
||||||
|
scale.tare(CONFIG_TARE_AVG_COUNT);
|
||||||
|
Serial.println("Finished tare");
|
||||||
|
session.init(timeClient.getEpochTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
void startMeasurements()
|
||||||
|
{
|
||||||
measuring_ = true;
|
measuring_ = true;
|
||||||
lastCallTime_ = 0;
|
lastCallTime_ = 0;
|
||||||
session.init( timeClient.getEpochTime() );
|
session.init(timeClient.getEpochTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopMeasurements() {
|
void stopMeasurements()
|
||||||
|
{
|
||||||
measuring_ = false;
|
measuring_ = false;
|
||||||
session.finalize();
|
session.finalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isMeasuring() const {
|
bool isMeasuring() const
|
||||||
|
{
|
||||||
return measuring_;
|
return measuring_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void iteration() {
|
void iteration()
|
||||||
if( ! measuring_ ) {
|
{
|
||||||
|
if (!measuring_)
|
||||||
|
{
|
||||||
//Serial.println("Disabled");
|
//Serial.println("Disabled");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint16_t measurement=-1;
|
uint16_t measurement = -1;
|
||||||
scale.measure(measurement);
|
scale.measure(measurement);
|
||||||
session.addPoint(measurement);
|
session.addPoint(measurement);
|
||||||
Serial.print("Measurement: ");
|
Serial.print("Measurement: ");
|
||||||
Serial.println(measurement);
|
Serial.println(measurement);
|
||||||
if( lastCallTime_ != 0) {
|
if (lastCallTime_ != 0)
|
||||||
|
{
|
||||||
const long cycleDuration = millis() - lastCallTime_;
|
const long cycleDuration = millis() - lastCallTime_;
|
||||||
if( cycleDuration <= CONFIG_MEASURE_DELAY)
|
if (cycleDuration <= CONFIG_MEASURE_DELAY)
|
||||||
{
|
{
|
||||||
delay(CONFIG_MEASURE_DELAY - cycleDuration);
|
delay(CONFIG_MEASURE_DELAY - cycleDuration);
|
||||||
}
|
}
|
||||||
|
@ -80,8 +90,8 @@ public:
|
||||||
const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY);
|
const long skipped = (cycleDuration / CONFIG_MEASURE_DELAY);
|
||||||
//Serial.printf("Warning: measurements skipped: %d, cycleDuration %d", skipped, cycleDuration);
|
//Serial.printf("Warning: measurements skipped: %d, cycleDuration %d", skipped, cycleDuration);
|
||||||
|
|
||||||
for(int i=0; i < skipped; ++i)
|
for (int i = 0; i < skipped; ++i)
|
||||||
session.addPoint(measurement);
|
session.addPoint(measurement);
|
||||||
|
|
||||||
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
|
delay(CONFIG_MEASURE_DELAY * (skipped + 1) - cycleDuration);
|
||||||
}
|
}
|
||||||
|
@ -89,7 +99,7 @@ public:
|
||||||
lastCallTime_ = millis();
|
lastCallTime_ = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
Session_T & getSession() { return session; }
|
Session_T &getSession() { return session; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Scale<CONFIG_VALUE_DIVIDER> scale;
|
Scale<CONFIG_VALUE_DIVIDER> scale;
|
||||||
|
@ -101,99 +111,105 @@ private:
|
||||||
|
|
||||||
SessionManager<Session_T> sessionManager;
|
SessionManager<Session_T> sessionManager;
|
||||||
|
|
||||||
void onNotFound(AsyncWebServerRequest *request) {
|
EspHttp espHttpServer;
|
||||||
request->send(404, "text/plain", "Not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Session_T>
|
template <typename Session_T>
|
||||||
void httpSetup(SessionManager<Session_T> * sessionManager)
|
void httpSetup(SessionManager<Session_T> *sessionManager)
|
||||||
{
|
{
|
||||||
server.on("/api/session/start", HTTP_POST | HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
auto cbStartSession = [sessionManager](httpd_req_t *req) {
|
||||||
AsyncWebServerResponse *response = req->beginResponse(200, "text/plain", F("OK"));
|
httpd_resp_send(req, "Session started", -1);
|
||||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
|
||||||
req->send(response);
|
|
||||||
//req->send(200, "text/plain", F("OK"));
|
|
||||||
sessionManager->startMeasurements();
|
sessionManager->startMeasurements();
|
||||||
Serial.println("Started measurements");
|
Serial.println("Started session");
|
||||||
});
|
};
|
||||||
server.on("/api/session/stop", HTTP_POST | HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
auto cbStopSession = [sessionManager](httpd_req_t *req) {
|
||||||
AsyncWebServerResponse *response = req->beginResponse(200, "text/plain", F("OK"));
|
httpd_resp_send(req, "Session stopped", -1);
|
||||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
|
||||||
req->send(response);
|
|
||||||
//req->send(200, "text/plain", F("OK"));
|
|
||||||
sessionManager->stopMeasurements();
|
sessionManager->stopMeasurements();
|
||||||
Serial.println("Stopped measurements");
|
Serial.println("Stopped session");
|
||||||
});
|
};
|
||||||
server.on("/api/session/data", HTTP_GET, [sessionManager](AsyncWebServerRequest * req) {
|
auto cbStatus = [sessionManager](httpd_req_t *req) {
|
||||||
uint32_t startIdx = 0;
|
String result;
|
||||||
if( req->hasParam("startIdx") ) {
|
result.reserve(512);
|
||||||
startIdx = req->getParam("startIdx")->value().toInt();
|
httpd_resp_set_hdr(req, "Content-Type", "application/json");
|
||||||
}
|
|
||||||
Serial.print("Data request, start index: ");
|
|
||||||
Serial.println(startIdx);
|
|
||||||
|
|
||||||
|
// session
|
||||||
|
{
|
||||||
|
result += "{ \"session\": ";
|
||||||
|
if (sessionManager->isMeasuring())
|
||||||
|
{
|
||||||
|
const auto &session = sessionManager->getSession();
|
||||||
|
result += "{ \"started\":" + String(session.getStartTime()) + ", ";
|
||||||
|
result += "\"num_measurements\":" + String(session.numMeasurements()) + "},\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
result += "{},\n";
|
||||||
|
}
|
||||||
|
// flash
|
||||||
|
{
|
||||||
|
const String usedBytes(portablefs::usedBytes());
|
||||||
|
const String freeBytes(portablefs::totalBytes() - portablefs::usedBytes());
|
||||||
|
result += "\"file_system\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "},\n";
|
||||||
|
}
|
||||||
|
// RAM
|
||||||
|
{
|
||||||
|
const String freeBytes(ESP.getFreeHeap());
|
||||||
|
const String usedBytes(ESP.getHeapSize() - ESP.getFreeHeap());
|
||||||
|
result += "\"ram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "},\n";
|
||||||
|
}
|
||||||
|
// PSRAM
|
||||||
|
{
|
||||||
|
const String freeBytes(ESP.getFreePsram());
|
||||||
|
const String usedBytes(ESP.getPsramSize() - ESP.getFreePsram());
|
||||||
|
result += "\"psram\": { \"used\": " + usedBytes + ", \"free\":" + freeBytes + "}\n";
|
||||||
|
}
|
||||||
|
result += "}";
|
||||||
|
httpd_resp_send(req, result.c_str(), result.length());
|
||||||
|
};
|
||||||
|
auto cbGetData = [sessionManager](httpd_req_t *req) {
|
||||||
|
auto sessionId = sessionManager->getSession().getStartTime();
|
||||||
|
uint32_t startIdx = getUrlQueryParameter(req, "startIdx", 0);
|
||||||
|
Serial.printf("Data request, start index: %d", startIdx);
|
||||||
|
|
||||||
|
//headers
|
||||||
|
httpd_resp_set_hdr(req, "Content-Type", "application/x-msgpack");
|
||||||
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||||
|
httpd_resp_set_hdr(req, "content-disposition", ("attachment; filename=\"" + String(sessionId) + ".st\"").c_str());
|
||||||
|
|
||||||
|
//data
|
||||||
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
StreamingMsgPackEncoder<DummyWriter> encoderToDetermineSize(nullptr);
|
||||||
encoderToDetermineSize.setSizeCountMode(true);
|
encoderToDetermineSize.setSizeCountMode(true);
|
||||||
sessionManager->getSession().serialize(encoderToDetermineSize, startIdx);
|
sessionManager->getSession().serialize(encoderToDetermineSize, startIdx);
|
||||||
auto totalSize = encoderToDetermineSize.getContentLength();
|
auto totalSize = encoderToDetermineSize.getContentLength();
|
||||||
Serial.print("Sending started of total size ");
|
|
||||||
Serial.println(totalSize);
|
|
||||||
auto callback = [=](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
|
|
||||||
CopyWriter copyWriter(buffer);
|
|
||||||
ChunkedStreamingMsgPackEncoder<CopyWriter> encoder(©Writer, index, index + maxLen);
|
|
||||||
sessionManager->getSession().serialize(encoder, startIdx);
|
|
||||||
return encoder.sentBytes() - index;
|
|
||||||
};
|
|
||||||
AsyncWebServerResponse *response = req->beginResponse("application/x-msgpack", totalSize, callback);
|
|
||||||
response->addHeader("Access-Control-Allow-Origin", "*");
|
|
||||||
|
|
||||||
auto sessionId = sessionManager->getSession().getStartTime();
|
char *buf = (char *)malloc(totalSize);
|
||||||
response->addHeader("content-disposition", "attachment; filename=\"" + String(sessionId) + ".st\"");
|
CopyWriter copyWriter((uint8_t *)buf);
|
||||||
req->send(response);
|
StreamingMsgPackEncoder<CopyWriter> encoder(©Writer);
|
||||||
});
|
sessionManager->getSession().serialize(encoder, startIdx);
|
||||||
server.addHandler(new SpiffsWebDavHandler("/webdav", "/dat"));
|
httpd_resp_send(req, buf, totalSize);
|
||||||
server.onNotFound(onNotFound);
|
free(buf);
|
||||||
|
};
|
||||||
|
|
||||||
server.begin();
|
espHttpServer.start();
|
||||||
|
espHttpServer.on("/api/session/start", HTTP_GET, cbStartSession);
|
||||||
|
espHttpServer.on("/api/session/start", HTTP_POST, cbStartSession);
|
||||||
|
|
||||||
|
espHttpServer.on("/api/session/stop", HTTP_GET, cbStopSession);
|
||||||
|
espHttpServer.on("/api/session/stop", HTTP_POST, cbStopSession);
|
||||||
|
|
||||||
|
espHttpServer.on("/api/session/data", HTTP_GET, cbGetData);
|
||||||
|
espHttpServer.on("/api/status", HTTP_GET, cbStatus);
|
||||||
|
|
||||||
|
espHttpServer.on("/webdav/*?", HTTP_GET, webdavHandler);
|
||||||
|
espHttpServer.on("/webdav/*?", HTTP_PROPFIND, webdavHandler);
|
||||||
|
espHttpServer.on("/webdav/*?", HTTP_DELETE, webdavHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
|
|
||||||
Serial.printf("Listing directory: %s\r\n", dirname);
|
|
||||||
|
|
||||||
File root = fs.open(dirname);
|
|
||||||
if(!root){
|
|
||||||
Serial.println("- failed to open directory");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(!root.isDirectory()){
|
|
||||||
Serial.println(" - not a directory");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
File file = root.openNextFile();
|
|
||||||
while(file){
|
|
||||||
if(file.isDirectory()){
|
|
||||||
Serial.print(" DIR : ");
|
|
||||||
Serial.println(file.name());
|
|
||||||
if(levels){
|
|
||||||
listDir(fs, file.name(), levels -1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.print(" FILE: ");
|
|
||||||
Serial.print(file.name());
|
|
||||||
Serial.print("\tSIZE: ");
|
|
||||||
Serial.println(file.size());
|
|
||||||
}
|
|
||||||
file = root.openNextFile();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
// Serial
|
// Serial
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
while(!Serial) {}
|
while (!Serial)
|
||||||
|
{
|
||||||
|
}
|
||||||
Serial.println(" ");
|
Serial.println(" ");
|
||||||
Serial.println("----- New start -----");
|
Serial.println("----- New start -----");
|
||||||
|
|
||||||
|
@ -203,18 +219,27 @@ void setup()
|
||||||
|
|
||||||
printDeviceInfo();
|
printDeviceInfo();
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
// WiFi
|
// WiFi
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
||||||
#ifdef PLATFORM_ESP32
|
#ifdef PLATFORM_ESP32
|
||||||
WiFi.setHostname(CONFIG_HOSTNAME);
|
WiFi.setHostname(CONFIG_HOSTNAME);
|
||||||
#else
|
#else
|
||||||
WIFI.hostname(CONFIG_HOSTNAME);
|
WIFI.hostname(CONFIG_HOSTNAME);
|
||||||
#endif
|
#endif
|
||||||
Serial.print(F("\n\n"));
|
Serial.print(F("\n\n"));
|
||||||
Serial.println(F("Waiting for WIFI connection..."));
|
Serial.println(F("Waiting for WIFI connection..."));
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
int connectCounter = 0;
|
||||||
|
while (WiFi.status() != WL_CONNECTED)
|
||||||
|
{
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
connectCounter += 1;
|
||||||
|
if (connectCounter > 5)
|
||||||
|
{
|
||||||
|
Serial.println("Couldn't obtain WIFI - trying begin() again");
|
||||||
|
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Serial.print(F("Connected to WiFi. IP:"));
|
Serial.print(F("Connected to WiFi. IP:"));
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
@ -229,14 +254,10 @@ void setup()
|
||||||
// HTTP & Websocket server
|
// HTTP & Websocket server
|
||||||
httpSetup(&sessionManager);
|
httpSetup(&sessionManager);
|
||||||
|
|
||||||
Serial.println("Spiffs listing:");
|
|
||||||
listDir(SPIFFS, "/", 3);
|
|
||||||
|
|
||||||
//
|
|
||||||
Serial.print("Free Heap");
|
|
||||||
Serial.println(ESP.getFreeHeap());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop()
|
||||||
sessionManager.iteration();
|
{
|
||||||
|
delay(10000);
|
||||||
|
//sessionManager.iteration();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue