diff --git a/firmware/lib/session/SessionChunk.h b/firmware/lib/session/SessionChunk.h index 4c82607..7602f59 100644 --- a/firmware/lib/session/SessionChunk.h +++ b/firmware/lib/session/SessionChunk.h @@ -3,14 +3,14 @@ #include "StreamingMsgPackEncoder.h" #include - -template +template class SessionChunk { public: SessionChunk() : nextFree_(0), sessionStartTime(0), startIndex(0) - {} + { + } void init(uint32_t epochStartTime, uint32_t startIdx) { @@ -19,21 +19,24 @@ public: startIndex = startIdx; } - uint32_t getStartTime() const { + uint32_t getStartTime() const + { return sessionStartTime; } - uint32_t getStartIndex() const { + uint32_t getStartIndex() const + { return startIndex; } - uint32_t numMeasurements() const { + uint32_t numMeasurements() const + { return nextFree_; } bool addPoint(Measurement_T measurement) { - if( nextFree_ >= SIZE) + if (nextFree_ >= SIZE) return false; values[nextFree_] = measurement; nextFree_++; @@ -41,8 +44,8 @@ public: return true; } - template - void serialize(StreamingMsgPackEncoder & encoder) const + template + void serialize(StreamingMsgPackEncoder &encoder) const { sendHeader(encoder, sessionStartTime, startIndex); encoder.sendArray(values, nextFree_); @@ -57,16 +60,26 @@ public: return encoder.getContentLength(); } - Measurement_T * getDataPointer() { + static uint32_t arrayHeaderOffset() + { + StreamingMsgPackEncoder encoder(nullptr); + encoder.setSizeCountMode(true); + sendHeader(encoder, 0, 0); + return encoder.getContentLength(); + } + + Measurement_T *getDataPointer() + { return values; } - const Measurement_T * getDataPointer() const { + const Measurement_T *getDataPointer() const + { return values; } - template - static void sendHeader(Encoder_T & encoder, uint32_t sessionStartTime, uint32_t startIndex) + template + static void sendHeader(Encoder_T &encoder, uint32_t sessionStartTime, uint32_t startIndex) { encoder.sendMap16(3); diff --git a/firmware/lib/session/SessionManager.h b/firmware/lib/session/SessionManager.h index e9f812c..e6f95b3 100644 --- a/firmware/lib/session/SessionManager.h +++ b/firmware/lib/session/SessionManager.h @@ -93,8 +93,10 @@ void SessionManager::stopMeasurements() template void SessionManager::iteration() { - if (!measuring_) - return; + if (!measuring_) { + delay(1); + return; // give control to HTTP server thread + } uint16_t measurement = -1; bool measurementDone = false; @@ -106,6 +108,7 @@ void SessionManager::iteration() { Serial.println("Maximum time of session reached - stopping"); stopMeasurements(); + delay(1); // give control to HTTP server thread return; } if (lastCallTime_ != 0) diff --git a/firmware/lib/session/SimpleMeasurementSession.h b/firmware/lib/session/SimpleMeasurementSession.h index 8d9fbd8..4dcf3fb 100644 --- a/firmware/lib/session/SimpleMeasurementSession.h +++ b/firmware/lib/session/SimpleMeasurementSession.h @@ -9,7 +9,7 @@ public: using ChunkT = SessionChunk; using MeasurementType = Measurement_T; // save interval in number of measurements (by default every minute) - SimpleMeasurementSession(uint32_t saveInterval = 10 * 60) + SimpleMeasurementSession(uint32_t saveInterval = 10 * 20) : chunk(nullptr), saveInterval_(saveInterval) { } @@ -76,22 +76,25 @@ private: { // todo: check this! free doesn't mean that the file writing actually works ok // use error codes of write instead? anyway: test it! + Serial.printf("%ld saveToFileSystem start()\n", millis()); deleteUntilBytesFree(CONFIG_SESSION_MAX_SIZE); + Serial.printf("%ld after deleteUntilBytesFree()\n", millis()); String filename = String(CONFIG_DATA_PATH) + "/" + String(chunk->getStartTime()); if (portablefs::exists(filename.c_str())) { auto file = portablefs::open(filename.c_str(), "a"); - file.seek(0, SeekSet); - StreamingMsgPackEncoder encoder(&file); - chunk->sendHeader(encoder, chunk->getStartTime(), 0); - file.seek(0, SeekEnd); size_t existingMeasurements = (file.size() - ChunkT::valueOffset()) / sizeof(Measurement_T); Serial.printf("Incremental save, existing %d\n", existingMeasurements); size_t measurementsToWrite = chunk->numMeasurements() - existingMeasurements; Measurement_T *startPtr = chunk->getDataPointer() + existingMeasurements; file.write((uint8_t *)(startPtr), measurementsToWrite * sizeof(Measurement_T)); + + Serial.printf("%ld Incr Save: before header patch\n", millis()); + file.seek(ChunkT::arrayHeaderOffset(), SeekSet); + StreamingMsgPackEncoder encoder(&file); + encoder.template sendArrayHeader(numMeasurements()); } else { @@ -100,6 +103,7 @@ private: StreamingMsgPackEncoder encoder(&file); chunk->serialize(encoder); } + Serial.printf("%ld saveToFileSystem done\n", millis()); } void deleteUntilBytesFree(size_t requiredSpace) diff --git a/firmware/src/firmware_main.cpp b/firmware/src/firmware_main.cpp index db22030..32736a9 100644 --- a/firmware/src/firmware_main.cpp +++ b/firmware/src/firmware_main.cpp @@ -189,33 +189,4 @@ void loop() { sessionManager.iteration(); webSocketServer.iteration(); - /* - if (webSocketServer.poll()) - { - websocketClients[nextFreeWebsocketClient] = webSocketServer.accept(); - websocketClients[nextFreeWebsocketClient].onMessage(onMessage); - Serial.println("Websocket connection"); - nextFreeWebsocketClient = (nextFreeWebsocketClient + 1) % MAX_WEBSOCKET_CONNECTIONS; - } - for (int i = 0; i < MAX_WEBSOCKET_CONNECTIONS; ++i) - //if (websocketClients[i].available()) { - //Serial.printf("Polling client %d\n", i); - websocketClients[i].poll(); - //} - - auto &session = sessionManager.session(); - if (session.numMeasurements() < measurementsSent) - measurementsSent = 0; - else if (session.numMeasurements() > measurementsSent) - { - for (int i = 0; i < MAX_WEBSOCKET_CONNECTIONS; ++i) - if (websocketClients[i].available()) - { - auto dataToSend = (const char*)(session.getDataPointer() + measurementsSent); - auto numBytes = (session.numMeasurements() - measurementsSent) * sizeof(MeasurementT); - Serial.printf("Sent %d bytes via websocket\n", numBytes); - websocketClients[i].sendBinary(dataToSend, numBytes); - measurementsSent = session.numMeasurements(); - } - }*/ }