From 38205401845aa6a392a47b6db7239f819edb3926 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Mon, 24 Oct 2022 14:09:04 +0200
Subject: [PATCH] fixed error that cut off beginning of strings, added
 usability methods

---
 client/client/lib/caching/src/ram_caching.cpp | 19 ++++++++++++-------
 client/client/lib/caching/src/ram_caching.hpp |  9 +++------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/client/client/lib/caching/src/ram_caching.cpp b/client/client/lib/caching/src/ram_caching.cpp
index 3b16f6e..641f974 100644
--- a/client/client/lib/caching/src/ram_caching.cpp
+++ b/client/client/lib/caching/src/ram_caching.cpp
@@ -3,6 +3,7 @@
 namespace RtcMemory{
     // 2D array of 20 Strings with maxLength of 251
     RTC_DATA_ATTR char storage[maxLength_][maxSize_];
+    RTC_DATA_ATTR int storedElements;
     RTC_DATA_ATTR int headElement = 0;
     RTC_DATA_ATTR int tailElement = 0;
 
@@ -19,13 +20,11 @@ namespace RtcMemory{
         // apparently I am not allowed to copy to rtc mem
         for(int i=0; i<maxLength_; i++){
             storage[i][headElement] = jsonString[i];
-            Serial.print(jsonString[i]);
             if(jsonString[i] == '\0'){
-                Serial.println("\n");
-                Serial.flush();
                 break;
             }
         }
+        storedElements++;
         ESP_LOGE(TAG, "Moved message to storage.");
     }
 
@@ -36,16 +35,22 @@ namespace RtcMemory{
         for(int i = 0; i<maxLength_; i++){
             current = storage[i][tailElement];
             buf += current;
-            Serial.print(current);
             if(current == '\0'){
-                Serial.println("\n");
-                Serial.flush();
                 break;
             }
         }
         // move tail to next element
-        tailElement = (tailElement + 1) % maxLength_;
+        tailElement = (tailElement + 1) % maxSize_;
+        storedElements--;
         ESP_LOGE(TAG, "Retrieved message from storage");
         return buf;
     }
+
+    bool is_full(){
+        return headElement == tailElement;
+    }
+
+    int stored_amount(){
+        return storedElements;
+    }
 }
diff --git a/client/client/lib/caching/src/ram_caching.hpp b/client/client/lib/caching/src/ram_caching.hpp
index b16e248..c1a4ac5 100644
--- a/client/client/lib/caching/src/ram_caching.hpp
+++ b/client/client/lib/caching/src/ram_caching.hpp
@@ -3,17 +3,14 @@
 #include "esp_log.h"
 #include <ESP32Time.h>
 
-#define maxSize_ 2
+#define maxSize_ 20
 #define maxLength_ 251
 
 namespace RtcMemory {
     void store(std::string message);
     String get_from_storage();
+    bool is_full();
+    int stored_amount();
 }
 
-// bool ram_cache_is_empty();
-// bool ram_cache_is_full();
-// void ram(ClientDataPackage data);
-// ClientDataPackage ram_cache_pop();
-
 #endif
\ No newline at end of file
-- 
GitLab