Webdav test - SPIFFS setup

This commit is contained in:
Martin Bauer
2019-09-10 22:05:28 +02:00
parent 5577490693
commit 2418fde01e
6 changed files with 193 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ public:
bool addPoint(Measurement_T measurement) {
const bool successful = currentChunk->addPoint(measurement);
if (!successful) {
Serial.println("Starting session rotate");
rotate();
const bool secondInsertSuccess = currentChunk->addPoint(measurement);
assert(secondInsertSuccess, "Session: insertion after rotation failed");
@@ -77,6 +78,8 @@ private:
void saveChunkToFile(Chunk_T *chunk) const {
const uint32_t chunkNr = chunk->getStartIndex() / CHUNK_SIZE;
const auto fileName = chunkFileName(chunkNr, chunk->getStartTime());
Serial.print("Writing session to file ");
Serial.println(fileName);
Writer writer( fileName );
chunk->serialize(writer.encoder());
};

View File

@@ -7,13 +7,26 @@ class SpiffsStorageWriter {
public:
SpiffsStorageWriter(const String &fileName) :
f_(SPIFFS.open(fileName, "w")),
encoder_(&f_) {}
encoder_(&f_),
fileName_(fileName)
{
Serial.println("Opened file for writing successful?");
bool success = f_;
Serial.println(success);
}
~SpiffsStorageWriter() {
f_.close();
Serial.print("Closing file: ");
Serial.println(fileName_);
Serial.print("File exists: ");
Serial.println(SPIFFS.exists(fileName_));
}
StreamingMsgPackEncoder<File> &encoder() { return encoder_; }
private:
File f_;
StreamingMsgPackEncoder<File> encoder_;
String fileName_;
};
@@ -23,6 +36,9 @@ public:
SpiffsStorageReader(const String &fileName) :
f_(SPIFFS.open(fileName, "w"))
{}
~SpiffsStorageReader() {
f_.close();
}
uint32_t readBytes(char *buffer, size_t length) {
return f_.readBytes(buffer, length);

117
lib/webdav/AsyncWebDav.h Normal file
View File

@@ -0,0 +1,117 @@
#include <ESPAsyncWebServer.h>
#include <FS.h>
#define FLASH_TEXT(name) const char *name
namespace webdav_constants {
FLASH_TEXT(MULTISTATUS_START) = "<?xml version=\"1.0\" ?><D:multistatus xmlns:D=\"DAV:\">";
FLASH_TEXT(MULTISTATUS_END) = "</D:multistatus>";
FLASH_TEXT(RESPONSE_START) = "<D:response>";
FLASH_TEXT(RESPONSE_END) = "</D:response>";
FLASH_TEXT(HREF_START) = "<D:href>";
FLASH_TEXT(HREF_END) = "</D:href>";
FLASH_TEXT(PROPSTAT_START) = "<D:propstat>";
FLASH_TEXT(PROPSTAT_END) = "</D:propstat>";
FLASH_TEXT(PROP_START) = "<D:prop>";
FLASH_TEXT(PROP_END) = "</D:prop>";
FLASH_TEXT(RESOURCETYPE_START) = "<D:resourcetype>";
FLASH_TEXT(RESOURCETYPE_END) = "</D:resourcetype>";
FLASH_TEXT(RESOURCE_COLLECTION) = "<D:collection/>";
FLASH_TEXT(CONTENTLEN_START) = "<D:getcontentlength>";
FLASH_TEXT(CONTENTLEN_END) = "</D:getcontentlength>";
FLASH_TEXT(CREATEDATE_START) = "<D:creationdate>";
FLASH_TEXT(CREATEDATE_END) = "</D:creationdate>";
FLASH_TEXT(MODDATE_START) = "<D:getlastmodified>";
FLASH_TEXT(MODDATE_END) = "</D:getlastmodified>";
FLASH_TEXT(STATUS_OK) = "<D:status>HTTP/1.1 200 OK</D:status>";
}
void listFiles(AsyncResponseStream * response, const char *folderPath, Dir * dir)
{
using namespace webdav_constants;
response->println(MULTISTATUS_START);
dir->rewind();
while (dir->next()) {
response->print(RESPONSE_START);
response->print(HREF_START);
response->print(folderPath);
response->print(dir->fileName());
response->print(HREF_END);
response->print(PROPSTAT_START);
response->print(PROP_START);
if (dir->isDirectory()) {
response->print(RESOURCETYPE_START);
response->print(RESOURCE_COLLECTION);
response->print(RESOURCETYPE_END);
} else {
response->print(CONTENTLEN_START);
response->print(dir->fileSize(), DEC);
response->print(CONTENTLEN_END);
}
response->print(PROP_END);
response->print(STATUS_OK);
response->print(PROPSTAT_END);
response->print(webdav_constants::RESPONSE_END);
}
response->println(MULTISTATUS_END);
}
class SpiffsWebDavHandler : public AsyncWebHandler
{
public:
SpiffsWebDavHandler(const String & prefix, const String & folder)
: prefix_(prefix), folder_(folder)
{}
virtual bool canHandle(AsyncWebServerRequest *request) override final
{
Serial.print("Can handle for url : ");
Serial.println(request->url());
return request->url().startsWith(prefix_);
}
virtual void handleRequest(AsyncWebServerRequest *request) override final
{
if (request->url() == prefix_ && request->method() == HTTP_PROPFIND) {
Serial.println("Propfind start");
AsyncResponseStream * response = request->beginResponseStream("application/xml");
Dir dir = SPIFFS.openDir(folder_);
listFiles(response, "/", &dir);
request->send(response);
} else if(request->url() == prefix_ && request->method() == HTTP_GET) {
AsyncResponseStream * response = request->beginResponseStream("text/plain");
Dir dir = SPIFFS.openDir(folder_);
Serial.print("Opening folder ");
Serial.println(folder_);
while (dir.next()) {
Serial.print(" File: ");
Serial.println(dir.fileName());
response->println(dir.fileName());
}
request->send(response);
}
else if (request->method() == HTTP_GET) {
auto path = folder_ + request->url().substring(prefix_.length());
Serial.print("Testing if path exists: ");
Serial.println(path);
if (SPIFFS.exists(path)) {
Serial.println("Exists!");
request->send(SPIFFS, path, "application/x-msgpack");
} else {
Serial.println("Does not exist :(");
request->send(404, "text/plain", "Webdav: File not found");
}
} else {
request->send(404, "text/plain", "Webdav: Invalid request");
}
}
virtual bool isRequestHandlerTrivial() override final {return false;}
private:
String prefix_;
String folder_;
};