From c012753a9e889fbcaef988293eed6d534759dba8 Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Thu, 20 Oct 2022 14:29:40 +0200
Subject: [PATCH] ram caching implemented, not compiling: 'ArduinoJson.h file
 not found'

---
 client/client/lib/caching/src/ram_caching.cpp | 41 ++++++++++++++++++-
 client/client/lib/caching/src/ram_caching.hpp | 16 +++++---
 .../lib/espnow/src/ClientDataPackage.hpp      |  2 +-
 3 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/client/client/lib/caching/src/ram_caching.cpp b/client/client/lib/caching/src/ram_caching.cpp
index b7df125..9e9c299 100644
--- a/client/client/lib/caching/src/ram_caching.cpp
+++ b/client/client/lib/caching/src/ram_caching.cpp
@@ -1,7 +1,46 @@
 #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;
+
 static const char* TAG = "CACHING";
-const int NUM_SENSORS = 10;
+
+
+void put(ClientDataPackage data){
+
+    // 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)
+    );
+}
+
+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;
+}
+
 
 //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 f4152ce..6b0f863 100644
--- a/client/client/lib/caching/src/ram_caching.hpp
+++ b/client/client/lib/caching/src/ram_caching.hpp
@@ -1,12 +1,18 @@
 #ifndef _RAM_CACHE
 #define _RAM_CACHE
-//#include "ClientDataPackage.hpp"
+#include <ClientDataPackage.hpp>
 #include "esp_log.h"
 #include <ESP32Time.h>
 
-bool ram_cache_is_empty();
-bool ram_cache_is_full();
-//void ram_cache_push(ClientDataPackage data);
-//ClientDataPackage ram_cache_pop();
+#define maxSize_ 20
+#define maxLength_ 251
+
+void put(ClientDataPackage data);
+ClientDataPackage get();
+
+// 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
diff --git a/client/client/lib/espnow/src/ClientDataPackage.hpp b/client/client/lib/espnow/src/ClientDataPackage.hpp
index 7cdd66f..f08e6bb 100644
--- a/client/client/lib/espnow/src/ClientDataPackage.hpp
+++ b/client/client/lib/espnow/src/ClientDataPackage.hpp
@@ -1,6 +1,6 @@
 #pragma once
 
-#include "ArduinoJson.h"
+#include <ArduinoJson.h>
 #include "MeasurementData.hpp"
 #include "Protocol.hpp"
 #include "SensorInformation.hpp"
-- 
GitLab