From f0de9096ae23d9b580c990a8c70ef01ce91526b4 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Mon, 24 Oct 2022 12:28:01 +0200
Subject: [PATCH] moved caching to namespace, using strings now, compiles

---
 client/client/lib/caching/src/ram_caching.cpp | 67 ++++++++++---------
 client/client/lib/caching/src/ram_caching.hpp |  7 +-
 client/client/src/main.cpp                    |  4 +-
 3 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/client/client/lib/caching/src/ram_caching.cpp b/client/client/lib/caching/src/ram_caching.cpp
index 9e9c299..62b2807 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(&current, &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(&current, &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 6b0f863..7a0dd97 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 ef7bf65..6281ac7 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) {
-- 
GitLab