Dokumentace

This commit is contained in:
2021-09-23 07:01:04 +02:00
parent b254c6f465
commit 94a5f46320
4 changed files with 244 additions and 158 deletions
+7 -2
View File
@@ -2,7 +2,7 @@
Knihovna pro přístup k proměnným, uloženým v JSON formátovaných souborech. Knihovna pro přístup k proměnným, uloženým v JSON formátovaných souborech.
## Použití ## Použití (pro Platformio)
``` ```
git submodule init git submodule init
@@ -10,4 +10,9 @@ git submodule add https://git.xpablo.cz/xPablo.cz/sysvars.git lib/sysvars
``` ```
Pro svoji práci potřebuje knihovnu ArduinoJSON Pro svoji práci potřebuje knihovnu ArduinoJSON.
Protože jsou v současnosti používané dva souborové systémy (SPIFFS a LittleFS/LITTLEFS), je možné definicí `USE_LITTLEFS` vynutit překlad pro LittleFS. Pokud tato definice není, použije se SPIFFS. Definice by se měla používat v rámci celého projektu, protože souborový systém může být použitý jen jeden!!!
POZOR!!! Protože mládenci v core pro ESP32 verze 2.0 předělali LITTLEFS na LittleFS, tak bude nutná úprava knihovny. Zatím ale není vystavená verze Core 2.0 pro Platformio, takže úprava zatím není.
+29
View File
@@ -0,0 +1,29 @@
{
"name":"sysvars",
"description":"Key-Value JSON file storage library the ESP8266 and ESP32 SoC",
"keywords":"file, storage",
"authors":
{
"name": "Pavel Brychta",
"maintainer": true
},
"repository":
{
"type": "git",
"url": "https://git.xpablo.cz/xPablo.cz/sysvars.git"
},
"dependencies": [
{
"name":"ArduinoJson",
"version":"https://github.com/bblanchon/ArduinoJson.git"
}
],
"version": "0.0.1",
"license": "MIT",
"frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"],
"build": {
"libCompatMode": 2,
"libLDFMode": "deep"
}
}
+9
View File
@@ -0,0 +1,9 @@
name=sysvars
version=0.0.1
author=Pavel Brychta
maintainer=Pavel Brychta <pablo@xpablo.cz>
sentence=Key-Value JSON file storage library.
paragraph=With this library you can easily store key-value pairs of data into JSON formatted file.
category=Other
url=https://www.xpablo.cz
architectures=esp8266,esp32
+199 -156
View File
@@ -1,156 +1,199 @@
#ifndef _SYSVARS_H_ #pragma once
#define _SYSVARS_H_ /** Knihovna sysvars
// Key-Value JSON file store unit * Copyright (c) 2018-2021 Pavel Brychta, pablo@xpablo.cz
// Ma za ukol ukladat/nacitat pary key-value do/z JSON *
* Knihovna umoznuje ukladani a vycitani paru hodnot typu klic-hodnota do souboru ve formatu JSON
#include <Arduino.h> **/
#include <ArduinoJson.h>
#include <FS.h> #include <Arduino.h>
#ifndef USEDFS #include <ArduinoJson.h>
# if defined(ESP32) #include <FS.h>
# ifdef USE_LITTLEFS #ifndef USEDFS
# include <LITTLEFS.h> # if defined(ESP32)
# define USEDFS LITTLEFS # ifdef USE_LITTLEFS
# else # include <LITTLEFS.h>
# include <SPIFFS.h> # define USEDFS LITTLEFS
# define USEDFS SPIFFS # else
# endif # include <SPIFFS.h>
# else # define USEDFS SPIFFS
# include <LittleFS.h> # endif
# define USEDFS LittleFS # else
# endif # include <LittleFS.h>
#endif # define USEDFS LittleFS
# endif
enum { #endif
SV_OK = 0,
SV_UNSPECIFIED_ERROR = -1, enum {
SV_FILE_NOT_FOUND = -2, SV_OK = 0,
SV_DESERIALIZE_ERROR = -3, SV_UNSPECIFIED_ERROR = -1,
SV_WRONG_DATA_TYPE = -4, SV_FILE_NOT_FOUND = -2,
SV_FILE_WRITE_ERROR = -5, SV_DESERIALIZE_ERROR = -3,
SV_KEY_NOT_FOUND = -6, 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_FILE)
# define SYSVAR_FILE "/cfg.json"
#if not defined(SYSVAR_DOCUMENT_SIZE) #endif
# define SYSVAR_DOCUMENT_SIZE (4096)
#endif #if not defined(SYSVAR_DOCUMENT_SIZE)
# define SYSVAR_DOCUMENT_SIZE (4096)
template <typename T> #endif
T svGetV(const String& name, int* result = nullptr)
{ /**
T retval{}; * @brief Ziskani hodnoty
int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu *
DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); * @param[in] name Jmeno klice
File f; * @param result Promenna pro ulozeni vysledku operace (SV_...)
*
f = USEDFS.open(F(SYSVAR_FILE), "r"); * @tparam T Typ vysledne hodnoty
if (f) { *
DeserializationError err = deserializeJson(doc, f); * @return Vracena hodnota zadaneho klice
if (err) { */
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor template <typename T>
} else { T svGetV(const String& name, int* result = nullptr)
if (!doc.containsKey(name)) {
{ T retval{};
res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu
} else { DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE);
retval = doc[name].as<T>(); File f;
res = SV_OK;
} f = USEDFS.open(F(SYSVAR_FILE), "r");
} if (f) {
} else { DeserializationError err = deserializeJson(doc, f);
res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor if (err) {
} res = SV_DESERIALIZE_ERROR; // spatny JSON soubor
if (result) } else {
*result = res; // navratovy kod, pokud nas zajima if (!doc.containsKey(name))
return retval; {
} res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic
} else {
template <typename T> retval = doc[name].as<T>();
T svGetV(const String& name, int* result, const String& fname) res = SV_OK;
{ }
T retval{}; }
int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu } else {
DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor
File f; }
if (result)
f = USEDFS.open(fname, "r"); *result = res; // navratovy kod, pokud nas zajima
if (f) { return retval;
DeserializationError err = deserializeJson(doc, f); }
if (err) {
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor /**
} else { * @brief Ziskani hodnoty klice
if (!doc.containsKey(name)) *
{ * @param[in] name Jmeno klice
res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic * @param result Promenna pro ulozeni vysledku operace
} else { * @param[in] fname Jmeno souboru s JSON parametry
retval = doc[name].as<T>(); *
res = SV_OK; * @tparam T Typ vysledne hodnoty
} *
} * @return Hodnota zadaneho klice
} else { */
res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor template <typename T>
} T svGetV(const String& name, int* result, const String& fname)
if (result) {
*result = res; // navratovy kod, pokud nas zajima T retval{};
return retval; int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu
} DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE);
File f;
template <typename T>
int svSetV(const String& name, T value) f = USEDFS.open(fname, "r");
{ if (f) {
int res = SV_UNSPECIFIED_ERROR; DeserializationError err = deserializeJson(doc, f);
DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); if (err) {
File f; res = SV_DESERIALIZE_ERROR; // spatny JSON soubor
} else {
f = USEDFS.open(F(SYSVAR_FILE), "r"); if (!doc.containsKey(name))
if (f) { {
DeserializationError err = deserializeJson(doc, f); res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic
if (err) { } else {
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor retval = doc[name].as<T>();
} res = SV_OK;
f.close(); }
} }
doc[name] = value; } else {
res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor
f = USEDFS.open(SYSVAR_FILE, "w"); }
if (f) { if (result)
serializeJson(doc, f); *result = res; // navratovy kod, pokud nas zajima
res = SV_OK; return retval;
} else { }
res = SV_FILE_WRITE_ERROR;
} /**
return res; * @brief Ulozeni hodnoty do klice
} *
* @param[in] name Jmeno klice
template <typename T> * @param[in] value Ukladana hodnota
int svSetV(const String& name, T value, const String& fname) *
{ * @tparam T Typ ukladane hodnoty
int res = SV_UNSPECIFIED_ERROR; *
DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); * @return Chybovy stav operace (SV_...)
File f; */
template <typename T>
f = USEDFS.open(fname, "r"); int svSetV(const String& name, T value)
if (f) { {
DeserializationError err = deserializeJson(doc, f); int res = SV_UNSPECIFIED_ERROR;
if (err) { DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE);
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor File f;
}
f.close(); f = USEDFS.open(F(SYSVAR_FILE), "r");
} if (f) {
doc[name] = value; DeserializationError err = deserializeJson(doc, f);
if (err) {
f = USEDFS.open(fname, "w"); res = SV_DESERIALIZE_ERROR; // spatny JSON soubor
if (f) { }
serializeJson(doc, f); f.close();
res = SV_OK; }
} else { doc[name] = value;
res = SV_FILE_WRITE_ERROR;
} f = USEDFS.open(SYSVAR_FILE, "w");
if (f) {
return res; serializeJson(doc, f);
} res = SV_OK;
#endif } 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;
}