157 lines
3.8 KiB
C++
157 lines
3.8 KiB
C++
|
#ifndef _SYSVARS_H_
|
||
|
#define _SYSVARS_H_
|
||
|
// Key-Value JSON file store unit
|
||
|
// Ma za ukol ukladat/nacitat pary key-value do/z JSON
|
||
|
|
||
|
#include <Arduino.h>
|
||
|
#include <ArduinoJson.h>
|
||
|
#include <FS.h>
|
||
|
#ifndef USEDFS
|
||
|
# if defined(ESP32)
|
||
|
# ifdef USE_LITTLEFS
|
||
|
# include <LITTLEFS.h>
|
||
|
# define USEDFS LITTLEFS
|
||
|
# else
|
||
|
# include <SPIFFS.h>
|
||
|
# define USEDFS SPIFFS
|
||
|
# endif
|
||
|
# else
|
||
|
# include <LittleFS.h>
|
||
|
# 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 <typename T>
|
||
|
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<T>();
|
||
|
res = SV_OK;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor
|
||
|
}
|
||
|
if (result)
|
||
|
*result = res; // navratovy kod, pokud nas zajima
|
||
|
return retval;
|
||
|
}
|
||
|
|
||
|
template <typename T>
|
||
|
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<T>();
|
||
|
res = SV_OK;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor
|
||
|
}
|
||
|
if (result)
|
||
|
*result = res; // navratovy kod, pokud nas zajima
|
||
|
return retval;
|
||
|
}
|
||
|
|
||
|
template <typename T>
|
||
|
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 <typename T>
|
||
|
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
|