diff --git a/client/client/lib/caching/src/ram_caching.cpp b/client/client/lib/caching/src/ram_caching.cpp index 9e9c299c9eff5d776bd00ebca15ced56837ccca2..62b28071f82620b61a7e7933f96342db9641a27f 100644 --- a/client/client/lib/caching/src/ram_caching.cpp +++ b/client/client/lib/caching/src/ram_caching.cpp @@ -1,47 +1,48 @@ #include "ram_caching.hpp" -// 2D array of 20 Strings with maxLength of 251 -RTC_DATA_ATTR char storage[maxSize_][maxLength_]; -RTC_DATA_ATTR int headElement = 0; -RTC_DATA_ATTR int tailElement = 0; +namespace RtcMemory{ + // 2D array of 20 Strings with maxLength of 251 + RTC_DATA_ATTR char storage[maxSize_][maxLength_]; + RTC_DATA_ATTR int headElement = 0; + RTC_DATA_ATTR int tailElement = 0; -static const char* TAG = "CACHING"; + static const char* TAG = "CACHING"; -void put(ClientDataPackage data){ + void store(std::string message){ - // turn data into char array - const char* jsonString = data.getDataPackageAsMinifiedJsonString().c_str(); - - // move head to beginning of new element - // head = (head + (sizeof(char) * 251)) % sizeof(storage); - headElement = (headElement + 1) % maxSize_; - // copy data to earliest free block - memcpy( &storage + headElement*maxSize_, - jsonString, - sizeof(jsonString) - ); -} + // turn data into char array + const char* jsonString = message.c_str(); + + // move head to beginning of new element + // head = (head + (sizeof(char) * 251)) % sizeof(storage); + headElement = (headElement + 1) % maxSize_; + // copy data to earliest free block + memcpy( &storage + headElement*maxSize_, + jsonString, + sizeof(jsonString) + ); + } -String get_from_storage(){ - // remove element pointed at by tail - String buf = ""; - char current; - for(int i = 0; i<251; i++){ - // i dont like this - memcpy(¤t, &storage + (tailElement*maxSize_) + i, sizeof(char)); - buf += current; - if(current = '\0'){ - break; + String get_from_storage(){ + // remove element pointed at by tail + String buf = ""; + char current; + for(int i = 0; i<251; i++){ + // i dont like this + memcpy(¤t, &storage + (tailElement*maxSize_) + i, sizeof(char)); + buf += current; + if(current = '\0'){ + break; + } } + // move tail to next element + tailElement = (tailElement + 1) % maxLength_; + Serial.println(buf); + return buf; } - // move tail to next element - tailElement = (tailElement + 1) % maxLength_; - Serial.println(buf); - return buf; } - //RTC_DATA_ATTR int cachedAmount = -1; //RTC_DATA_ATTR ClientDataPackage backup[NUM_SENSORS]; // diff --git a/client/client/lib/caching/src/ram_caching.hpp b/client/client/lib/caching/src/ram_caching.hpp index 6b0f8633d7437e141c839b9daa683e2635299001..7a0dd9716bf02bd124ce4533086664a0ec880d53 100644 --- a/client/client/lib/caching/src/ram_caching.hpp +++ b/client/client/lib/caching/src/ram_caching.hpp @@ -1,14 +1,15 @@ #ifndef _RAM_CACHE #define _RAM_CACHE -#include <ClientDataPackage.hpp> #include "esp_log.h" #include <ESP32Time.h> #define maxSize_ 20 #define maxLength_ 251 -void put(ClientDataPackage data); -ClientDataPackage get(); +namespace RtcMemory { + void store(std::string message); + String get_from_storage(); +} // bool ram_cache_is_empty(); // bool ram_cache_is_full(); diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp index ef7bf65ae4aeae2de99285eb1b1ff5186afb1967..6281ac79e00f954e0380cab41ea25c9c3c320448 100644 --- a/client/client/src/main.cpp +++ b/client/client/src/main.cpp @@ -32,7 +32,9 @@ void loop() auto messages = drs26.buildMessages(); for (const Message &message : messages) { - message.send(); + if(message.send() != ESP_OK){ + RtcMemory::store(message.getMessageAsMinifiedJsonString()); + } } } catch (const NoDataAvailableException &e) {