Dokumentace

This commit is contained in:
Pavel Brychta 2021-09-23 07:01:04 +02:00
parent b254c6f465
commit 94a5f46320
4 changed files with 244 additions and 158 deletions

View File

@ -2,7 +2,7 @@
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
@ -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
library.json Normal file
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
library.properties Normal file
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

View File

@ -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;
}