Dokumentace
This commit is contained in:
355
src/sysvars.hpp
355
src/sysvars.hpp
@ -1,156 +1,199 @@
|
||||
#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
|
||||
#pragma once
|
||||
/** Knihovna sysvars
|
||||
* Copyright (c) 2018-2021 Pavel Brychta, pablo@xpablo.cz
|
||||
*
|
||||
* Knihovna umoznuje ukladani a vycitani paru hodnot typu klic-hodnota do souboru ve formatu 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
|
||||
|
||||
/**
|
||||
* @brief Ziskani hodnoty
|
||||
*
|
||||
* @param[in] name Jmeno klice
|
||||
* @param result Promenna pro ulozeni vysledku operace (SV_...)
|
||||
*
|
||||
* @tparam T Typ vysledne hodnoty
|
||||
*
|
||||
* @return Vracena hodnota zadaneho klice
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Ziskani hodnoty klice
|
||||
*
|
||||
* @param[in] name Jmeno klice
|
||||
* @param result Promenna pro ulozeni vysledku operace
|
||||
* @param[in] fname Jmeno souboru s JSON parametry
|
||||
*
|
||||
* @tparam T Typ vysledne hodnoty
|
||||
*
|
||||
* @return Hodnota zadaneho klice
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Ulozeni hodnoty do klice
|
||||
*
|
||||
* @param[in] name Jmeno klice
|
||||
* @param[in] value Ukladana hodnota
|
||||
*
|
||||
* @tparam T Typ ukladane hodnoty
|
||||
*
|
||||
* @return Chybovy stav operace (SV_...)
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Ulozeni hodnoty klice
|
||||
*
|
||||
* @param[in] name Jmeno klice
|
||||
* @param[in] value Ukladana hodnota
|
||||
* @param[in] fname Jmeno souboru s JSON daty
|
||||
*
|
||||
* @tparam T Typ ukladane hodnoty
|
||||
*
|
||||
* @return Chybovy stav operace (SV_...)
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user