From 21a7d75c081f8cf29dd38a7535bb98895cb08deb Mon Sep 17 00:00:00 2001
From: Moritz Perschke <moritz.perschke@uibk.ac.at>
Date: Wed, 3 Aug 2022 15:00:55 +0200
Subject: [PATCH] loop functions checks for cached data to resend

---
 client/client/lib/espnow/src/espnow.cpp | 14 ++++++++++++++
 client/client/src/main.cpp              |  8 ++++++++
 2 files changed, 22 insertions(+)

diff --git a/client/client/lib/espnow/src/espnow.cpp b/client/client/lib/espnow/src/espnow.cpp
index 01673d7..36d8b8b 100644
--- a/client/client/lib/espnow/src/espnow.cpp
+++ b/client/client/lib/espnow/src/espnow.cpp
@@ -106,4 +106,18 @@ Message :: Message(){
 
     data->amountData = 0;
     data->timestamp = rtc.getMillis(); // I am assuming we are not sending data from Unix Epoch
+}
+
+Message :: Message(data_struct old_data){
+    memcpy(data, &old_data, sizeof(data));
+
+    preferences.begin("config", true);
+    if(preferences.isKey("host")){
+        preferences.getBytes("host", recipient, sizeof(uint8_t) * 6);
+    }
+    else{
+        memcpy(recipient, BROADCAST_MAC, sizeof(BROADCAST_MAC));
+        Serial.println("backup mac used");
+    }
+    preferences.end();
 }
\ No newline at end of file
diff --git a/client/client/src/main.cpp b/client/client/src/main.cpp
index 2554b91..e130d95 100644
--- a/client/client/src/main.cpp
+++ b/client/client/src/main.cpp
@@ -1,5 +1,6 @@
 #include <Arduino.h>
 #include "espnow.hpp"
+#include "ram_caching.hpp"
 
 void setup() {
  
@@ -22,5 +23,12 @@ void loop() {
   new_data->add_data(counter * 1.3, 2);
   new_data->send();
   delete new_data;
+
+  if(!ram_cache_is_empty()){
+    data_struct old_data = ram_cache_pop();
+    Message* resend_message = new Message(old_data);
+    resend_message->send();
+    delete resend_message;
+  }
   delay(5000);
 }
\ No newline at end of file
-- 
GitLab