From b49fa079b4dbede874c228f9fde54ac4df6f96d9 Mon Sep 17 00:00:00 2001
From: Malaurie Bernard <mbernard@kinouby>
Date: Wed, 5 Jul 2023 14:35:07 +0200
Subject: [PATCH] Creation of the read_json and write_json methods in syringe
 and syringefilled class.

---
 syringe.cpp       | 30 ++++++++++++++++++++++++++++++
 syringe.h         |  9 +++++++++
 syringefilled.cpp | 30 ++++++++++++++++++++++++++++++
 syringefilled.h   | 10 ++++++++++
 4 files changed, 79 insertions(+)

diff --git a/syringe.cpp b/syringe.cpp
index e2e3a56..0fe5407 100644
--- a/syringe.cpp
+++ b/syringe.cpp
@@ -38,6 +38,36 @@ string Syringe :: get_id()
 }
 
 
+//Example : File f = ({ InterruptLock lock; filesystem->open(filename, "w"); }); and the f becomes the argument output_stream
+//useful in web.cpp via a button to save data
+void Syringe :: write_Json (Stream& output_stream)
+{ 
+/***
+-Argument : output_stream (= serial or a file)
+-Return   : /
+-Action   : Save the Json structure in a file (convert it to text)
+***/
+
+InterruptLock lock; //useful thanks to its constructor (so a is not visible in the code)
+serializeJson(SyringeJSON, output_stream);
+
+}
+
+
+//useful in setup of pousse-seringue.cpp(to check at the begining what has already been saved) and web.cpp via a button to recover data
+void Syringe :: read_Json (Stream& input_stream)
+{
+/***
+-Argument : input_stream (= serial or a file)
+-Return   : /
+-Action   : Check if a Json Document alredy exist or not
+***/
+
+InterruptLock lock; //useful thanks to its constructor (so a is not visible in the code)
+deserializeJson(SyringeJSON, input_stream);
+
+}
+
 
 
 
diff --git a/syringe.h b/syringe.h
index 0536ac8..614777a 100644
--- a/syringe.h
+++ b/syringe.h
@@ -4,6 +4,12 @@
 #include "motor.h"
 
 #include <ArduinoJson.h>
+#include <interrupts.h> // InterruptLock
+using InterruptLock = esp8266::InterruptLock;
+
+#include <iostream>
+using namespace std;
+
 
 class Syringe
 {
@@ -27,6 +33,9 @@ class Syringe
   //JSON DOCUMENT
   StaticJsonDocument<200> SyringeJSON;  //200 = RAM allocated to this document
 
+  //JSON METHODS
+  void write_Json (Stream& output_stream);
+  void read_Json (Stream& input_stream);
 
 };
 
diff --git a/syringefilled.cpp b/syringefilled.cpp
index 17f42b7..b2e116f 100644
--- a/syringefilled.cpp
+++ b/syringefilled.cpp
@@ -288,3 +288,33 @@ void SyringeFilled :: show_configuration()
                   stepToMm(step));
 }
 
+//Example : File f = ({ InterruptLock lock; filesystem->open(filename, "w"); }); and the f becomes the argument output_stream
+//useful in web.cpp via a button to save data
+void SyringeFilled :: write_Json (Stream& output_stream) //file_name sera le nom du fichier Json, doc sera la structure Json qu'on veut transformer en fichier
+{ 
+/***
+-Argument : output_stream (= serial or a file)
+-Return   : /
+-Action   : Save the Json structure in a file (convert it to text)
+***/
+
+InterruptLock lock; //useful thanks to its constructor (so a is not visible in the code)
+serializeJson(SyringeFilledJSON, output_stream);
+  
+}
+
+
+//useful in setup of pousse-seringue.cpp(to check at the begining what has already been saved) and web.cpp via a button to recover data
+void SyringeFilled :: read_Json (Stream& input_stream) //file_name sera le nom du fichier Json, doc sera la structure Json qu'on veut transformer en fichier
+{
+/***
+-Argument : input_stream (= serial or a file)
+-Return   : /
+-Action   : Check if a Json Document alredy exist or not
+***/
+
+InterruptLock lock; //useful thanks to its constructor (so a is not visible in the code)
+deserializeJson(SyringeFilledJSON, input_stream);
+  
+
+}
diff --git a/syringefilled.h b/syringefilled.h
index 4724fd7..31167bd 100644
--- a/syringefilled.h
+++ b/syringefilled.h
@@ -3,6 +3,9 @@
 #include "syringe.h"
 #include <ArduinoJson.h>
 #include "math.h"
+#include <interrupts.h> // InterruptLock
+using InterruptLock = esp8266::InterruptLock;
+
 
 #define syringe_pump_length_mm 50
 
@@ -83,6 +86,13 @@ class SyringeFilled : public Motor
     bool check_configuration(); //CLI
     void show_configuration(); //CLI
     
+    //JSON
+    void write_Json (Stream& output_stream);
+    void read_Json (Stream& input_stream);
+
+
+
+
 };
 
 extern SyringeFilled syringe_filled;
\ No newline at end of file
-- 
GitLab