Compare commits

..

No commits in common. "master" and "feature/ArduinoJson7" have entirely different histories.

5 changed files with 122 additions and 174 deletions

View File

@ -12,7 +12,7 @@
"type": "git",
"url": "https://git.xpablo.cz/xPablo.cz/sysvars.git"
},
"version": "1.0.0",
"version": "0.0.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"],

View File

@ -1,5 +1,5 @@
name=sysvars
version=1.0.0
version=0.0.2
author=Pavel Brychta
maintainer=Pavel Brychta <pablo@xpablo.cz>
sentence=Key-Value JSON file storage library.

View File

@ -2,13 +2,13 @@
#ifndef USEDFS
# if defined(ESP32)
# ifdef USE_LITTLEFS
//# if ESP_IDF_VERSION_MAJOR < 4
//# include <LITTLEFS.h>
//# define USEDFS LITTLEFS
//# else
# if ESP_IDF_VERSION_MAJOR < 4
# include <LITTLEFS.h>
# define USEDFS LITTLEFS
# else
# include <LittleFS.h>
# define USEDFS LittleFS
//# endif
# endif
# else
# include <SPIFFS.h>
# define USEDFS SPIFFS

View File

@ -1,156 +0,0 @@
/** Sysvars library
* Copyright (c) 2018-2024 Pavel Brychta, pablo@xpablo.cz
*
* Key-value store/load library with JSON file using internal filesystem
**/
#include "definefs.hpp"
#include "sysvars.hpp"
#include <ArduinoJson.h>
#include <FS.h>
#include <trace.h>
#ifndef DEBUG_SYSVARS
# define DEBUG_SYSVARS 0
#endif
template <typename T>
T svGetV(const String & name, const T deflt)
{
T rv = deflt;
File f = USEDFS.open(F(SYSVAR_FILE), "r");
if (f) {
JsonDocument doc;
const DeserializationError err = deserializeJson(doc, f);
if (err) {
TRACEPLUS(DEBUG_SYSVARS, TRACE_ERROR, "SV: Deserialize error '%s'", err.c_str()); // wrong JSON file (malformed)
} else {
if (!doc.containsKey(name)) {
TRACEPLUS(DEBUG_SYSVARS, TRACE_ERROR, "SV: Key '%s' not found", name.c_str());
} else {
rv = doc[name].as<T>();
}
}
} else {
TRACEPLUS(DEBUG_SYSVARS, TRACE_ERROR, "SV: File not found");
}
return rv;
}
template <typename T>
T svGetVFile(const String & name, const String & fname, const T deflt)
{
T rv = deflt;
File f = USEDFS.open(fname, "r");
if (f) {
JsonDocument doc;
const DeserializationError err = deserializeJson(doc, f);
if (err) {
TRACEPLUS(DEBUG_SYSVARS, TRACE_ERROR, "SV: Deserialize error '%s'", err.c_str()); // wrong JSON file (malformed)
} else {
if (!doc.containsKey(name)) {
TRACEPLUS(DEBUG_SYSVARS, TRACE_ERROR, "SV: Key '%s' not found", name.c_str());
} else {
rv = doc[name].as<T>();
}
}
} else {
TRACEPLUS(DEBUG_SYSVARS, TRACE_ERROR, "SV: File not found '%s'", fname.c_str());
}
return rv;
}
/**
* @brief Store value for the key
*
* @param[in] name Key name
* @param[in] value New value
*
* @tparam T Type of the value
*
* @return Result (SV_...)
*/
template <typename T>
int svSetV(const String & name, T value)
{
int res;
JsonDocument doc;
File f = USEDFS.open(F(SYSVAR_FILE), "r");
if (f) {
const DeserializationError err = deserializeJson(doc, f);
if (err) {
res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
}
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 Store value for the key
*
* @param[in] name Key name
* @param[in] value New value
* @param[in] fname JSON file name
*
* @tparam T Type of the value
*
* @return Result (SV_...)
*/
template <typename T>
int svSetVFile(const String & name, T value, const String & fname)
{
int res = SV_UNSPECIFIED_ERROR;
JsonDocument doc;
File f = USEDFS.open(fname, "r");
if (f) {
const DeserializationError err = deserializeJson(doc, f);
if (err) {
res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
}
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;
}
template int svGetV(const String & name, const int deflt);
template String svGetV(const String & name, const String deflt);
template unsigned svGetV(const String & name, const unsigned deflt);
template int svGetVFile(const String & name, const String & fname, const int deflt);
template String svGetVFile(const String & name, const String & fname, const String deflt);
template unsigned svGetVFile(const String & name, const String & fname, const unsigned deflt);
template int svSetV(const String & name, int value);
template int svSetV(const String & name, long value);
template int svSetV(const String & name, unsigned value);
template int svSetV(const String & name, String value);
template int svSetVFile(const String & name, int value, const String & fname);
template int svSetVFile(const String & name, unsigned value, const String & fname);
template int svSetVFile(const String & name, String value, const String & fname);

View File

@ -1,15 +1,14 @@
#pragma once
/** Sysvars library
* Copyright (c) 2018-2024 Pavel Brychta, pablo@xpablo.cz
* Copyright (c) 2018-2023 Pavel Brychta, pablo@xpablo.cz
*
* Key-value store/load library with JSON file using internal filesystem
**/
#include "definefs.hpp"
#include <Arduino.h>
#if not defined(SYSVAR_FILE)
#define SYSVAR_FILE "/cfg.json"
#endif
#include <ArduinoJson.h>
#include <FS.h>
enum {
SV_OK = 0,
@ -21,32 +20,88 @@ enum {
SV_KEY_NOT_FOUND = -6,
};
#if not defined(SYSVAR_FILE)
#define SYSVAR_FILE "/cfg.json"
#endif
/**
* @brief Get value
*
* @param[in] name Name of the key
* @param deflt Default value
* @param result Status variable (SV_...)
*
* @tparam T result type
*
* @return Value for the key given
*/
template <typename T>
T svGetV(const String & name, const T deflt = {});
T svGetV(const String & name, int * result = nullptr)
{
T rv{};
int res;
JsonDocument doc;
File f;
f = USEDFS.open(F(SYSVAR_FILE), "r");
if (f) {
DeserializationError err = deserializeJson(doc, f);
if (err) {
res = SV_DESERIALIZE_ERROR; // wrong JSON file (malformed)
} else {
if (!doc.containsKey(name)) {
res = SV_KEY_NOT_FOUND; // key not found
} else {
rv = doc[name].as<T>();
res = SV_OK;
}
}
} else {
res = SV_FILE_NOT_FOUND; // file not found
}
if (result)
*result = res; // result code if we are interested
return rv;
}
/**
* @brief Get value
*
* @param[in] name Name of the key
* @param result Status variable (SV_....)
* @param[in] fname Used JSON file name
* @param deflt Default value
*
* @tparam T Result type
*
* @return Value for the key given
*/
template <typename T>
T svGetVFile(const String & name, const String & fname, const T deflt = {});
T svGetV(const String & name, int * result, const String & fname)
{
T rv{};
int res;
JsonDocument doc;
File f;
f = USEDFS.open(fname, "r");
if (f) {
DeserializationError err = deserializeJson(doc, f);
if (err) {
res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
} else {
if (!doc.containsKey(name)) {
res = SV_KEY_NOT_FOUND; // key not found
} else {
rv = doc[name].as<T>();
res = SV_OK;
}
}
} else {
res = SV_FILE_NOT_FOUND; // file not found
}
if (result)
*result = res; // result code if we are interested
return rv;
}
/**
* @brief Store value for the key
@ -59,7 +114,31 @@ T svGetVFile(const String & name, const String & fname, const T deflt = {});
* @return Result (SV_...)
*/
template <typename T>
int svSetV(const String & name, T value);
int svSetV(const String & name, T value)
{
int res;
JsonDocument doc;
File f;
f = USEDFS.open(F(SYSVAR_FILE), "r");
if (f) {
DeserializationError err = deserializeJson(doc, f);
if (err) {
res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
}
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 Store value for the key
@ -73,4 +152,29 @@ int svSetV(const String & name, T value);
* @return Result (SV_...)
*/
template <typename T>
int svSetVFile(const String & name, T value, const String & fname);
int svSetV(const String & name, T value, const String & fname)
{
int res = SV_UNSPECIFIED_ERROR;
JsonDocument doc;
File f;
f = USEDFS.open(fname, "r");
if (f) {
DeserializationError err = deserializeJson(doc, f);
if (err) {
res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
}
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;
}