diff --git a/src/sysvars.hpp b/src/sysvars.hpp new file mode 100644 index 0000000..ae2b563 --- /dev/null +++ b/src/sysvars.hpp @@ -0,0 +1,156 @@ +#ifndef _SYSVARS_H_ +#define _SYSVARS_H_ +// Key-Value JSON file store unit +// Ma za ukol ukladat/nacitat pary key-value do/z JSON + +#include +#include +#include +#ifndef USEDFS +# if defined(ESP32) +# ifdef USE_LITTLEFS +# include +# define USEDFS LITTLEFS +# else +# include +# define USEDFS SPIFFS +# endif +# else +# include +# define USEDFS LittleFS +# endif +#endif + +enum { + SV_OK = 0, + SV_UNSPECIFIED_ERROR = -1, + SV_FILE_NOT_FOUND = -2, + SV_DESERIALIZE_ERROR = -3, + SV_WRONG_DATA_TYPE = -4, + SV_FILE_WRITE_ERROR = -5, + SV_KEY_NOT_FOUND = -6, +}; + +#if not defined(SYSVAR_FILE) +# define SYSVAR_FILE "/cfg.json" +#endif + +#if not defined(SYSVAR_DOCUMENT_SIZE) +# define SYSVAR_DOCUMENT_SIZE (4096) +#endif + +template +T svGetV(const String& name, int* result = nullptr) +{ + T retval{}; + int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu + DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); + File f; + + f = USEDFS.open(F(SYSVAR_FILE), "r"); + if (f) { + DeserializationError err = deserializeJson(doc, f); + if (err) { + res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + } else { + if (!doc.containsKey(name)) + { + res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic + } else { + retval = doc[name].as(); + res = SV_OK; + } + } + } else { + res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor + } + if (result) + *result = res; // navratovy kod, pokud nas zajima + return retval; +} + +template +T svGetV(const String& name, int* result, const String& fname) +{ + T retval{}; + int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu + DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); + File f; + + f = USEDFS.open(fname, "r"); + if (f) { + DeserializationError err = deserializeJson(doc, f); + if (err) { + res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + } else { + if (!doc.containsKey(name)) + { + res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic + } else { + retval = doc[name].as(); + res = SV_OK; + } + } + } else { + res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor + } + if (result) + *result = res; // navratovy kod, pokud nas zajima + return retval; +} + +template +int svSetV(const String& name, T value) +{ + int res = SV_UNSPECIFIED_ERROR; + DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); + File f; + + f = USEDFS.open(F(SYSVAR_FILE), "r"); + if (f) { + DeserializationError err = deserializeJson(doc, f); + if (err) { + res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + } + f.close(); + } + doc[name] = value; + + f = USEDFS.open(SYSVAR_FILE, "w"); + if (f) { + serializeJson(doc, f); + res = SV_OK; + } else { + res = SV_FILE_WRITE_ERROR; + } + return res; +} + +template +int svSetV(const String& name, T value, const String& fname) +{ + int res = SV_UNSPECIFIED_ERROR; + DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); + File f; + + f = USEDFS.open(fname, "r"); + if (f) { + DeserializationError err = deserializeJson(doc, f); + if (err) { + res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + } + f.close(); + } + doc[name] = value; + + f = USEDFS.open(fname, "w"); + if (f) { + serializeJson(doc, f); + res = SV_OK; + } else { + res = SV_FILE_WRITE_ERROR; + } + + return res; +} +#endif