From 4d77d9e3287f03768e49df8fd85515e03026a62c Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Mon, 2 Oct 2023 20:32:13 +0200 Subject: [PATCH] no delay on first filesystem save - loggin over websocket --- .../lib/session/SimpleMeasurementSession.h | 32 +++++++++++++------ firmware/src/LoggingAPI.h | 2 +- firmware/src/SessionAPI.h | 10 +++--- firmware/src/WebsocketServer.h | 10 +++--- firmware/src/WifiAPI.cpp | 2 +- firmware/src/WifiAPI.h | 2 +- 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/firmware/lib/session/SimpleMeasurementSession.h b/firmware/lib/session/SimpleMeasurementSession.h index 05bb185..7a3589b 100644 --- a/firmware/lib/session/SimpleMeasurementSession.h +++ b/firmware/lib/session/SimpleMeasurementSession.h @@ -33,8 +33,12 @@ public: new (chunk) ChunkT(); // placement new to init chunk } chunk->init(epochStartTime, 0); + + // write header here - since this takes a long time later when measurement is running + writeToNewFile(); } + bool addPoint(Measurement_T measurement) { bool success = chunk->addPoint(measurement); @@ -76,6 +80,19 @@ public: } private: + portablefs::string getFilename() { + return portablefs::string(CONFIG_DATA_PATH) + "/" + portablefs::to_string(chunk->getStartTime()); + } + + void writeToNewFile() { + LOG_INFO("Initializing file"); + const auto filename = getFilename(); + auto file = portablefs::open(filename.c_str(), "w"); + StreamingMsgPackEncoder encoder(&file); + chunk->serialize(encoder); + LOG_INFO("Initializing file done"); + } + void saveToFileSystem() { static const uint32_t arrayHeaderOffset = ChunkT::arrayHeaderOffset(); @@ -83,12 +100,11 @@ private: // todo: check this! free doesn't mean that the file writing actually works ok // use error codes of write instead? anyway: test it! - LOG_INFO("%ld saveToFileSystem start", millis()); + LOG_INFO("saveToFileSystem start"); deleteUntilBytesFree(CONFIG_SESSION_MAX_SIZE); - LOG_INFO("%ld after deleteUntilBytesFree()", millis()); + LOG_TRACE("after deleteUntilBytesFree()"); - using fs_string = portablefs::string; - fs_string filename = fs_string(CONFIG_DATA_PATH) + "/" + portablefs::to_string(chunk->getStartTime()); + const auto filename = getFilename(); if (portablefs::exists(filename.c_str())) { auto file = portablefs::open(filename.c_str(), "r+"); @@ -105,12 +121,10 @@ private: } else { - LOG_INFO("Creating new session file"); - auto file = portablefs::open(filename.c_str(), "w"); - StreamingMsgPackEncoder encoder(&file); - chunk->serialize(encoder); + LOG_INFO("Creating new session file, should have been done already"); + writeToNewFile(); } - LOG_INFO("%ld saveToFileSystem done", millis()); + LOG_INFO("saveToFileSystem done"); } void deleteUntilBytesFree(size_t requiredSpace) diff --git a/firmware/src/LoggingAPI.h b/firmware/src/LoggingAPI.h index dc060c9..88a359e 100644 --- a/firmware/src/LoggingAPI.h +++ b/firmware/src/LoggingAPI.h @@ -10,7 +10,7 @@ class LoggingAPI { public: - void onClientConnect(websockets::WebsocketsClient &client) {} + void onClientConnect(websockets::WebsocketsClient &client, int /*clientId*/) {} bool handleMessage(websockets::WebsocketsClient &client, MessageCode code, const char *payload, size_t size) { switch(code) { diff --git a/firmware/src/SessionAPI.h b/firmware/src/SessionAPI.h index 0f9ef9b..9927178 100644 --- a/firmware/src/SessionAPI.h +++ b/firmware/src/SessionAPI.h @@ -20,7 +20,7 @@ public: numSentMeasurements_[i] = 0; } - void onClientConnect(websockets::WebsocketsClient &client); + void onClientConnect(websockets::WebsocketsClient &client, int clientId); bool handleMessage(websockets::WebsocketsClient &client, MessageCode code, const char *payload, size_t size); template @@ -41,7 +41,7 @@ private: // sending message about current session template -void SessionAPI::onClientConnect(websockets::WebsocketsClient &client) +void SessionAPI::onClientConnect(websockets::WebsocketsClient &client, int clientId) { // TODO write msgpack instead for consistency? @@ -73,10 +73,12 @@ void SessionAPI::onClientConnect(websockets::WebsocketsClient &client) assert(writeHead - msg == msgSize - sizeof(MeasurementT) * numMeasurements); + LOG_INFO("Start sending existing measurements"); memcpy(writeHead, session.getDataPointer(), sizeof(MeasurementT) * numMeasurements); client.sendBinary(msg, msgSize); - + numSentMeasurements_[clientId] = numMeasurements; free(msg); + LOG_INFO("Finished sending existing measurements"); } template @@ -157,7 +159,7 @@ template void SessionAPI::sendNewDataMessages(TServer &server) { - constexpr size_t MAX_MEASUREMENTS_PER_MSG = 15; + constexpr size_t MAX_MEASUREMENTS_PER_MSG = 50; constexpr size_t WAIT_UNTIL_AT_LEAST_NUM_MEASUREMENTS = 1; // new data messages are the only messages not sent in msgpack format diff --git a/firmware/src/WebsocketServer.h b/firmware/src/WebsocketServer.h index cca332f..caefa0e 100644 --- a/firmware/src/WebsocketServer.h +++ b/firmware/src/WebsocketServer.h @@ -52,7 +52,7 @@ public: { clients_[nextFreeClient_] = server_.accept(); clients_[nextFreeClient_].onMessage(onMessage); - this->onClientConnectImpl(clients_[nextFreeClient_]); + this->onClientConnectImpl(clients_[nextFreeClient_], nextFreeClient_); nextFreeClient_ = (nextFreeClient_ + 1) % MAX_WEBSOCKET_CONNECTIONS; if(MAX_WEBSOCKET_CONNECTIONS == 3) { @@ -128,13 +128,13 @@ private: bool handleMessageImpl(websockets::WebsocketsClient &, MessageCode, const char *, size_t) { return false; } template ::value), bool>::type = true> - void onClientConnectImpl(websockets::WebsocketsClient &client) + void onClientConnectImpl(websockets::WebsocketsClient &client, int clientId) { - std::get(apiManagers_).onClientConnect(client); - onClientConnectImpl(client); + std::get(apiManagers_).onClientConnect(client, clientId); + onClientConnectImpl(client, clientId); } template ::value, bool>::type = true> - void onClientConnectImpl(websockets::WebsocketsClient &client) {} + void onClientConnectImpl(websockets::WebsocketsClient &client, int clientId) {} // -- Members diff --git a/firmware/src/WifiAPI.cpp b/firmware/src/WifiAPI.cpp index 1f639e9..2a49bb1 100644 --- a/firmware/src/WifiAPI.cpp +++ b/firmware/src/WifiAPI.cpp @@ -11,7 +11,7 @@ void WifiAPI::sendWifiState(websockets::WebsocketsClient &client) sendToClient<192>(client, MessageCode::WIFI_STATE_RESPONSE, data); } -void WifiAPI::onClientConnect(websockets::WebsocketsClient &client) +void WifiAPI::onClientConnect(websockets::WebsocketsClient &client, int /*clientId*/) { sendWifiState(client); } diff --git a/firmware/src/WifiAPI.h b/firmware/src/WifiAPI.h index b7437b6..546b587 100644 --- a/firmware/src/WifiAPI.h +++ b/firmware/src/WifiAPI.h @@ -15,7 +15,7 @@ public: { } - void onClientConnect(websockets::WebsocketsClient &client); + void onClientConnect(websockets::WebsocketsClient &client, int clientId); bool handleMessage(websockets::WebsocketsClient &client, MessageCode code, const char *payload, size_t size); template