diff --git a/lib/session/MeasurementSession.h b/lib/session/MeasurementSession.h index 40c3b10..0924662 100644 --- a/lib/session/MeasurementSession.h +++ b/lib/session/MeasurementSession.h @@ -12,7 +12,9 @@ public: void init(uint32_t epochStartTime) { currentChunk = &chunks[0]; + otherChunk = &chunks[1]; currentChunk->init(epochStartTime, 0); + otherChunk->init(0, 0); } bool addPoint(Measurement_T measurement) { @@ -22,7 +24,7 @@ public: rotate(); const bool secondInsertSuccess = currentChunk->addPoint(measurement); assert(secondInsertSuccess, "Session: insertion after rotation failed"); - //TODO check that there is place for file - remove old files + // TODO check that there is place for file - remove old files } return true; } @@ -80,7 +82,7 @@ private: const auto fileName = chunkFileName(chunkNr, chunk->getStartTime()); Serial.print("Writing session to file "); Serial.println(fileName); - Writer writer( fileName ); + Writer writer(fileName); chunk->serialize(writer.encoder()); }; @@ -91,7 +93,7 @@ private: if( startIdx >= currentChunk->getStartIndex() ) { encoder.sendArrayPartialContents( currentChunk->getDataPointer(), currentChunk->numMeasurements() ); - return currentChunk->getStartIndex() + currentChunk->numMeasurements(); + return currentChunk->getStartIndex() + currentChunk->numMeasurements(); } else if( startIdx >= otherChunk->getStartIndex() && otherChunkFilled() ) { encoder.sendArrayPartialContents( otherChunk->getDataPointer(), otherChunk->numMeasurements() ); assert( otherChunk->numMeasurements(), CHUNK_SIZE ); diff --git a/lib/session/SpiffsStorage.h b/lib/session/SpiffsStorage.h index bc075eb..33ebf01 100644 --- a/lib/session/SpiffsStorage.h +++ b/lib/session/SpiffsStorage.h @@ -10,15 +10,12 @@ public: 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 &encoder() { return encoder_; } @@ -34,7 +31,7 @@ class SpiffsStorageReader { public: SpiffsStorageReader(const String &fileName) : - f_(SPIFFS.open(fileName, "w")) + f_(SPIFFS.open(fileName, "r")) {} ~SpiffsStorageReader() { f_.close(); diff --git a/lib/webdav/AsyncWebDav.h b/lib/webdav/AsyncWebDav.h index abe9329..f830699 100644 --- a/lib/webdav/AsyncWebDav.h +++ b/lib/webdav/AsyncWebDav.h @@ -17,6 +17,7 @@ namespace webdav_constants { FLASH_TEXT(RESOURCETYPE_START) = ""; FLASH_TEXT(RESOURCETYPE_END) = ""; FLASH_TEXT(RESOURCE_COLLECTION) = ""; + FLASH_TEXT(HTTP_204_NO_CONTENT) = "HTTP/1.1 204 No Content"; FLASH_TEXT(CONTENTLEN_START) = ""; FLASH_TEXT(CONTENTLEN_END) = ""; @@ -31,8 +32,14 @@ void listFiles(AsyncResponseStream * response, const char *folderPath, Dir * dir { using namespace webdav_constants; response->println(MULTISTATUS_START); + Serial.println("Before rewind"); dir->rewind(); + Serial.println("After rewind"); while (dir->next()) { + Serial.println("Inside dir loop"); + Serial.println(folderPath); + Serial.println(dir->fileName()); + response->print(RESPONSE_START); response->print(HREF_START); response->print(folderPath); @@ -53,7 +60,7 @@ void listFiles(AsyncResponseStream * response, const char *folderPath, Dir * dir response->print(PROP_END); response->print(STATUS_OK); response->print(PROPSTAT_END); - response->print(webdav_constants::RESPONSE_END); + response->println(webdav_constants::RESPONSE_END); } response->println(MULTISTATUS_END); } @@ -75,14 +82,14 @@ public: virtual void handleRequest(AsyncWebServerRequest *request) override final { - if (request->url() == prefix_ && request->method() == HTTP_PROPFIND) { + 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"); + } else if(request->url() == prefix_ + "/" && request->method() == HTTP_GET) { + AsyncResponseStream * response = request->beginResponseStream("text/plain", 1460*10); Dir dir = SPIFFS.openDir(folder_); Serial.print("Opening folder "); Serial.println(folder_); @@ -95,15 +102,21 @@ public: } 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 if (request->method() == HTTP_DELETE) { + auto path = folder_ + request->url().substring(prefix_.length()); + if (SPIFFS.exists(path)) { + if(SPIFFS.remove(path)) + request->send(204, "text/plain", "Success"); + else + request->send(404, "text/plain", "Webdav: Invalid delete request"); + } else { + request->send(404, "text/plain", "Webdav: File to delete not found"); + } } else { request->send(404, "text/plain", "Webdav: Invalid request"); } diff --git a/src/ConfigWifi.h b/src/ConfigWifi.h index 0e18227..5d32b30 100644 --- a/src/ConfigWifi.h +++ b/src/ConfigWifi.h @@ -2,4 +2,4 @@ const char *CONFIG_WIFI_SSID = "WLAN"; const char *CONFIG_WIFI_PASSWORD = "Bau3rWLAN"; -const char* CONFIG_HOSTNAME = "smartcords"; +const char* CONFIG_HOSTNAME = "smartswim"; diff --git a/src/DeviceInfoLog.h b/src/DeviceInfoLog.h new file mode 100644 index 0000000..fafa27d --- /dev/null +++ b/src/DeviceInfoLog.h @@ -0,0 +1,69 @@ +#include + +inline void printDeviceInfo() +{ + FSInfo fs_info; + SPIFFS.info(fs_info); + + float fileTotalKB = (float)fs_info.totalBytes / 1024.0; + float fileUsedKB = (float)fs_info.usedBytes / 1024.0; + + float flashChipSize = (float)ESP.getFlashChipSize() / 1024.0 / 1024.0; + float realFlashChipSize = (float)ESP.getFlashChipRealSize() / 1024.0 / 1024.0; + float flashFreq = (float)ESP.getFlashChipSpeed() / 1000.0 / 1000.0; + FlashMode_t ideMode = ESP.getFlashChipMode(); + + Serial.printf("\n#####################\n"); + + Serial.printf("__________________________\n\n"); + Serial.println("Firmware: "); + Serial.printf(" Chip Id: %08X\n", ESP.getChipId()); + Serial.print(" Core version: "); + Serial.println(ESP.getCoreVersion()); + Serial.print(" SDK version: "); + Serial.println(ESP.getSdkVersion()); + Serial.print(" Boot version: "); + Serial.println(ESP.getBootVersion()); + Serial.print(" Boot mode: "); + Serial.println(ESP.getBootMode()); + + Serial.printf("__________________________\n\n"); + + Serial.println("Flash chip information: "); + Serial.printf(" Flash chip Id: %08X (for example: Id=001640E0 Manuf=E0, Device=4016 (swap bytes))\n", ESP.getFlashChipId()); + Serial.printf(" Sketch thinks Flash RAM is size: "); + Serial.print(flashChipSize); + Serial.println(" MB"); + Serial.print(" Actual size based on chip Id: "); + Serial.print(realFlashChipSize); + Serial.println(" MB ... given by (2^( Device - 1) / 8 / 1024"); + Serial.print(" Flash frequency: "); + Serial.print(flashFreq); + Serial.println(" MHz"); + Serial.printf(" Flash write mode: %s\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN")); + + Serial.printf("__________________________\n\n"); + + Serial.println("File system (SPIFFS): "); + Serial.print(" Total KB: "); + Serial.print(fileTotalKB); + Serial.println(" KB"); + Serial.print(" Used KB: "); + Serial.print(fileUsedKB); + Serial.println(" KB"); + Serial.printf(" Block size: %u\n", fs_info.blockSize); + Serial.printf(" Page size: %u\n", fs_info.pageSize); + Serial.printf(" Maximum open files: %u\n", fs_info.maxOpenFiles); + Serial.printf(" Maximum path length: %u\n\n", fs_info.maxPathLength); + + String str = ""; + Dir dir = SPIFFS.openDir("/dat"); + while (dir.next()) + { + str += dir.fileName(); + str += " / "; + str += dir.fileSize(); + str += "\r\n"; + } + Serial.print(str); +} \ No newline at end of file diff --git a/src/firmware_main.cpp b/src/firmware_main.cpp index ba3f250..8cc8963 100644 --- a/src/firmware_main.cpp +++ b/src/firmware_main.cpp @@ -11,6 +11,7 @@ #include "MockScale.h" #include "MeasurementSession.h" #include "SpiffsStorage.h" +#include "DeviceInfoLog.h" // Configuration #include "ConfigWifi.h" @@ -39,6 +40,7 @@ public: void startMeasurements() { measuring_ = true; + lastCallTime_ = 0; session.init( timeClient.getEpochTime() ); } @@ -97,12 +99,12 @@ void onNotFound(AsyncWebServerRequest *request) { template void httpSetup(SessionManager * sessionManager) { - server.on("/api/session/start", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) { + server.on("/api/session/start", HTTP_POST | HTTP_GET, [sessionManager](AsyncWebServerRequest * req) { req->send(200, "text/plain", F("OK")); sessionManager->startMeasurements(); Serial.println("Started measurements"); }); - server.on("/api/session/stop", HTTP_POST, [sessionManager](AsyncWebServerRequest * req) { + server.on("/api/session/stop", HTTP_POST | HTTP_GET, [sessionManager](AsyncWebServerRequest * req) { req->send(200, "text/plain", F("OK")); sessionManager->stopMeasurements(); Serial.println("Stopped measurements"); @@ -152,53 +154,13 @@ void setup() // File system SPIFFS.begin(); - Serial.print("SPIFFS begin "); - - FSInfo fs_info; - SPIFFS.info(fs_info); - - float fileTotalKB = (float)fs_info.totalBytes / 1024.0; - float fileUsedKB = (float)fs_info.usedBytes / 1024.0; - - float flashChipSize = (float)ESP.getFlashChipSize() / 1024.0 / 1024.0; - float realFlashChipSize = (float)ESP.getFlashChipRealSize() / 1024.0 / 1024.0; - float flashFreq = (float)ESP.getFlashChipSpeed() / 1000.0 / 1000.0; - FlashMode_t ideMode = ESP.getFlashChipMode(); - - Serial.printf("\n#####################\n"); - - Serial.printf("__________________________\n\n"); - Serial.println("Firmware: "); - Serial.printf(" Chip Id: %08X\n", ESP.getChipId()); - Serial.print(" Core version: "); Serial.println(ESP.getCoreVersion()); - Serial.print(" SDK version: "); Serial.println(ESP.getSdkVersion()); - Serial.print(" Boot version: "); Serial.println(ESP.getBootVersion()); - Serial.print(" Boot mode: "); Serial.println(ESP.getBootMode()); - - Serial.printf("__________________________\n\n"); - - Serial.println("Flash chip information: "); - Serial.printf(" Flash chip Id: %08X (for example: Id=001640E0 Manuf=E0, Device=4016 (swap bytes))\n", ESP.getFlashChipId()); - Serial.printf(" Sketch thinks Flash RAM is size: "); Serial.print(flashChipSize); Serial.println(" MB"); - Serial.print(" Actual size based on chip Id: "); Serial.print(realFlashChipSize); - Serial.println(" MB ... given by (2^( Device - 1) / 8 / 1024"); - Serial.print(" Flash frequency: "); Serial.print(flashFreq); Serial.println(" MHz"); - Serial.printf(" Flash write mode: %s\n", (ideMode == FM_QIO ? "QIO" : ideMode == FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" : "UNKNOWN")); - - Serial.printf("__________________________\n\n"); - - Serial.println("File system (SPIFFS): "); - Serial.print(" Total KB: "); Serial.print(fileTotalKB); Serial.println(" KB"); - Serial.print(" Used KB: "); Serial.print(fileUsedKB); Serial.println(" KB"); - Serial.printf(" Block size: %lu\n", fs_info.blockSize); - Serial.printf(" Page size: %lu\n", fs_info.pageSize); - Serial.printf(" Maximum open files: %lu\n", fs_info.maxOpenFiles); - Serial.printf(" Maximum path length: %lu\n\n", fs_info.maxPathLength); + printDeviceInfo(); // WiFi WiFi.mode(WIFI_STA); WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD); + WiFi.hostname(CONFIG_HOSTNAME); Serial.print(F("\n\n")); Serial.println(F("Waiting for WIFI connection...")); while (WiFi.status() != WL_CONNECTED) {