mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-28 05:06:32 +00:00
Add TfsLoadString()
This commit is contained in:
parent
d59caa7203
commit
cd182a5814
@ -306,6 +306,16 @@ bool TfsFileExists(const char *fname){
|
|||||||
return yes;
|
return yes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t TfsFileSize(const char *fname){
|
||||||
|
if (!ffs_type) { return 0; }
|
||||||
|
|
||||||
|
File file = ffsp->open(fname, "r");
|
||||||
|
if (!file) { return 0; }
|
||||||
|
size_t flen = file.size();
|
||||||
|
file.close();
|
||||||
|
return flen;
|
||||||
|
}
|
||||||
|
|
||||||
bool TfsSaveFile(const char *fname, const uint8_t *buf, uint32_t len) {
|
bool TfsSaveFile(const char *fname, const uint8_t *buf, uint32_t len) {
|
||||||
if (!ffs_type) { return false; }
|
if (!ffs_type) { return false; }
|
||||||
#ifdef USE_WEBCAM
|
#ifdef USE_WEBCAM
|
||||||
@ -362,7 +372,6 @@ bool TfsInitFile(const char *fname, uint32_t len, uint8_t init_value) {
|
|||||||
|
|
||||||
bool TfsLoadFile(const char *fname, uint8_t *buf, uint32_t len) {
|
bool TfsLoadFile(const char *fname, uint8_t *buf, uint32_t len) {
|
||||||
if (!ffs_type) { return false; }
|
if (!ffs_type) { return false; }
|
||||||
if (!TfsFileExists(fname)) { return false; }
|
|
||||||
|
|
||||||
File file = ffsp->open(fname, "r");
|
File file = ffsp->open(fname, "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
@ -371,15 +380,19 @@ bool TfsLoadFile(const char *fname, uint8_t *buf, uint32_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t flen = file.size();
|
size_t flen = file.size();
|
||||||
if (len > flen){
|
if (len > flen) { len = flen; } // Adjust requested length to smaller file length
|
||||||
len = flen;
|
|
||||||
}
|
|
||||||
|
|
||||||
file.read(buf, len);
|
file.read(buf, len);
|
||||||
file.close();
|
file.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String TfsLoadString(const char *fname) {
|
||||||
|
// Use a reasonable amount of stack space considering 4k/8k available on ESP8266/ESP32 and manageable string length
|
||||||
|
char buf[2048] = { 0 }; // Prepare empty string of max 2047 characters on stack
|
||||||
|
TfsLoadFile(fname, (uint8_t*)buf, 2047); // Leave last position as end of string ('\0')
|
||||||
|
return String(buf); // Received string or empty on error
|
||||||
|
}
|
||||||
|
|
||||||
bool TfsDeleteFile(const char *fname) {
|
bool TfsDeleteFile(const char *fname) {
|
||||||
if (!ffs_type) { return false; }
|
if (!ffs_type) { return false; }
|
||||||
|
|
||||||
|
@ -75,7 +75,6 @@
|
|||||||
//#define ENERGY_MODBUS_DEBUG_SHOW
|
//#define ENERGY_MODBUS_DEBUG_SHOW
|
||||||
|
|
||||||
#define ENERGY_MODBUS_FILE "/modbus.json" // Modbus parameter file name used by filesystem
|
#define ENERGY_MODBUS_FILE "/modbus.json" // Modbus parameter file name used by filesystem
|
||||||
#define ENERGY_MODBUS_MAX_FSIZE 1024 // Modbus parameter file max size
|
|
||||||
|
|
||||||
const uint16_t nrg_mbs_reg_not_used = 0xFFFF; // Odd number 65535 is unused register
|
const uint16_t nrg_mbs_reg_not_used = 0xFFFF; // Odd number 65535 is unused register
|
||||||
|
|
||||||
@ -482,15 +481,9 @@ bool EnergyModbusReadRegisters(void) {
|
|||||||
String modbus = "";
|
String modbus = "";
|
||||||
|
|
||||||
#ifdef USE_UFILESYS
|
#ifdef USE_UFILESYS
|
||||||
char *modbus_file = (char*)calloc(ENERGY_MODBUS_MAX_FSIZE, 1);
|
modbus = TfsLoadString(ENERGY_MODBUS_FILE);
|
||||||
if (modbus_file) {
|
if (modbus.length()) {
|
||||||
if (TfsLoadFile(ENERGY_MODBUS_FILE, (uint8_t*)modbus_file, ENERGY_MODBUS_MAX_FSIZE -1)) {
|
AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Loaded from File"));
|
||||||
if (strlen(modbus_file) < ENERGY_MODBUS_MAX_FSIZE) {
|
|
||||||
modbus = modbus_file;
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Loaded from File"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free(modbus_file);
|
|
||||||
}
|
}
|
||||||
#endif // USE_UFILESYS
|
#endif // USE_UFILESYS
|
||||||
|
|
||||||
@ -508,13 +501,12 @@ bool EnergyModbusReadRegisters(void) {
|
|||||||
}
|
}
|
||||||
#endif // USE_SCRIPT
|
#endif // USE_SCRIPT
|
||||||
|
|
||||||
if (!modbus.length()) { return false; } // File not found
|
if (modbus.length() < 7) { return false; } // File not found or Invalid JSON
|
||||||
|
|
||||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: File '%s'"), modbus.c_str());
|
// AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: File '%s'"), modbus.c_str());
|
||||||
|
|
||||||
const char* json = modbus.c_str();
|
const char* json = modbus.c_str();
|
||||||
uint32_t len = strlen(json) +1;
|
uint32_t len = strlen(json) +1;
|
||||||
if (len < 7) { return false; } // Invalid JSON
|
|
||||||
|
|
||||||
char json_buffer[len];
|
char json_buffer[len];
|
||||||
memcpy(json_buffer, json, len); // Keep original safe
|
memcpy(json_buffer, json, len); // Keep original safe
|
||||||
JsonParser parser(json_buffer);
|
JsonParser parser(json_buffer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user