better identifiers
This commit is contained in:
parent
2e5be19a60
commit
91ae5ef60c
@ -3,14 +3,14 @@
|
|||||||
|
|
||||||
#include "..\drivers.h"
|
#include "..\drivers.h"
|
||||||
#include "storage.h"
|
#include "storage.h"
|
||||||
#include "SPIStorage.h"
|
#include "nvMemory.h"
|
||||||
|
|
||||||
class SDCard
|
class SDCard
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SDCard();
|
SDCard();
|
||||||
~SDCard();
|
~SDCard();
|
||||||
void SD2SPIStorage(SPIStorage* spifs);
|
void SD2nvMemory(nvMemory* nvMem);
|
||||||
bool loadConfigFile(TSettings* Settings);
|
bool loadConfigFile(TSettings* Settings);
|
||||||
private:
|
private:
|
||||||
bool initSDcard();
|
bool initSDcard();
|
||||||
@ -37,12 +37,12 @@ SDCard::~SDCard()
|
|||||||
SD_MMC.end();
|
SD_MMC.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SDCard::SD2SPIStorage(SPIStorage* spifs)
|
void SDCard::SD2nvMemory(NVMem* nvMem)
|
||||||
{
|
{
|
||||||
TSettings Settings;
|
TSettings Settings;
|
||||||
if (loadConfigFile(&Settings))
|
if (loadConfigFile(&Settings))
|
||||||
{
|
{
|
||||||
spifs->saveConfigFile(&Settings);
|
nvMem->saveConfig(&Settings);
|
||||||
WiFi.begin(Settings.WifiSSID, Settings.WifiPW);
|
WiFi.begin(Settings.WifiSSID, Settings.WifiPW);
|
||||||
Serial.println("SDCard: Settings transfered to internal memory. Restarting now.");
|
Serial.println("SDCard: Settings transfered to internal memory. Restarting now.");
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
@ -143,7 +143,7 @@ bool SDCard::initSDcard()
|
|||||||
|
|
||||||
SDCard::SDCard() {}
|
SDCard::SDCard() {}
|
||||||
SDCard::~SDCard() {}
|
SDCard::~SDCard() {}
|
||||||
void SDCard::SD2SPIStorage(SPIStorage* spifs) {};
|
void SDCard::SD2nvMemory(nvMemory* nvMem) {};
|
||||||
bool SDCard::loadConfigFile(TSettings* Settings) { return false; }
|
bool SDCard::loadConfigFile(TSettings* Settings) { return false; }
|
||||||
bool SDCard::initSDcard() { return false; }
|
bool SDCard::initSDcard() { return false; }
|
||||||
|
|
||||||
|
@ -1,134 +0,0 @@
|
|||||||
#ifndef _SPISTORAGE_H_
|
|
||||||
#define _SPISTORAGE_H_
|
|
||||||
|
|
||||||
#define ESP_DRD_USE_SPIFFS true
|
|
||||||
|
|
||||||
#include <WiFiManager.h>
|
|
||||||
#include <SPIFFS.h>
|
|
||||||
#include <FS.h>
|
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
|
|
||||||
#include "..\drivers.h"
|
|
||||||
#include "storage.h"
|
|
||||||
|
|
||||||
class SPIStorage
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
bool SPIFFSInitialized_;
|
|
||||||
public:
|
|
||||||
SPIStorage()
|
|
||||||
{
|
|
||||||
SPIFFSInitialized_ = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
~SPIStorage()
|
|
||||||
{
|
|
||||||
if (SPIFFSInitialized_)
|
|
||||||
SPIFFS.end();
|
|
||||||
};
|
|
||||||
|
|
||||||
void saveConfigFile(TSettings*Settings)
|
|
||||||
{
|
|
||||||
if (init())
|
|
||||||
{
|
|
||||||
// Save Config in JSON format
|
|
||||||
Serial.println(F("SPIFS: Saving configuration..."));
|
|
||||||
|
|
||||||
// Create a JSON document
|
|
||||||
StaticJsonDocument<512> json;
|
|
||||||
json[JSON_KEY_POOLURL] = Settings->PoolAddress;
|
|
||||||
json[JSON_KEY_POOLPORT] = Settings->PoolPort;
|
|
||||||
json[JSON_KEY_WALLETID] = Settings->BtcWallet;
|
|
||||||
json[JSON_KEY_TIMEZONE] = Settings->Timezone;
|
|
||||||
|
|
||||||
// Open config file
|
|
||||||
File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w");
|
|
||||||
if (!configFile)
|
|
||||||
{
|
|
||||||
// Error, file did not open
|
|
||||||
Serial.println("SPIFS: failed to open config file for writing");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize JSON data to write to file
|
|
||||||
serializeJsonPretty(json, Serial);
|
|
||||||
Serial.print('\n');
|
|
||||||
if (serializeJson(json, configFile) == 0)
|
|
||||||
{
|
|
||||||
// Error writing file
|
|
||||||
Serial.println(F("SPIFS: Failed to write to file"));
|
|
||||||
}
|
|
||||||
// Close file
|
|
||||||
configFile.close();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
bool loadConfigFile(TSettings* Settings)
|
|
||||||
{
|
|
||||||
// Uncomment if we need to format filesystem
|
|
||||||
// SPIFFS.format();
|
|
||||||
|
|
||||||
// Load existing configuration file
|
|
||||||
// Read configuration from FS json
|
|
||||||
|
|
||||||
if (init())
|
|
||||||
{
|
|
||||||
if (SPIFFS.exists(JSON_CONFIG_FILE))
|
|
||||||
{
|
|
||||||
// The file exists, reading and loading
|
|
||||||
Serial.println("SPIFS: Reading config file");
|
|
||||||
File configFile = SPIFFS.open(JSON_CONFIG_FILE, "r");
|
|
||||||
if (configFile)
|
|
||||||
{
|
|
||||||
Serial.println("SPIFS: Opened configuration file");
|
|
||||||
StaticJsonDocument<512> json;
|
|
||||||
DeserializationError error = deserializeJson(json, configFile);
|
|
||||||
configFile.close();
|
|
||||||
serializeJsonPretty(json, Serial);
|
|
||||||
Serial.print('\n');
|
|
||||||
if (!error)
|
|
||||||
{
|
|
||||||
Serial.println("SPIFS: Parsing JSON");
|
|
||||||
strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress);
|
|
||||||
strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet);
|
|
||||||
if(json.containsKey(JSON_KEY_POOLPORT))
|
|
||||||
Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>();
|
|
||||||
if (json.containsKey(JSON_KEY_TIMEZONE))
|
|
||||||
Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Error loading JSON data
|
|
||||||
Serial.println("SPIFS: Failed to load json config");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void deleteConfigFile()
|
|
||||||
{
|
|
||||||
Serial.println("SPIFS: Erasing config file..");
|
|
||||||
SPIFFS.remove(JSON_CONFIG_FILE); //Borramos fichero
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
bool init()
|
|
||||||
{
|
|
||||||
if (!SPIFFSInitialized_)
|
|
||||||
{
|
|
||||||
Serial.println("SPIFS: Mounting File System...");
|
|
||||||
// May need to make it begin(true) first time you are using SPIFFS
|
|
||||||
SPIFFSInitialized_ = SPIFFS.begin(false) || SPIFFS.begin(true);
|
|
||||||
SPIFFSInitialized_ ? Serial.println("SPIFS: Mounted") : Serial.println("SPIFS: Mounting failed.");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Serial.println("SPIFS: Already Mounted");
|
|
||||||
}
|
|
||||||
return SPIFFSInitialized_;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // _SPISTORAGE_H_
|
|
167
src/drivers/storage/nvMemory.h
Normal file
167
src/drivers/storage/nvMemory.h
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
#ifndef _NVMEMORY_H_
|
||||||
|
#define _NVMEMORY_H_
|
||||||
|
|
||||||
|
#define BUILD_SPIFFS
|
||||||
|
|
||||||
|
#include "..\drivers.h"
|
||||||
|
#include "storage.h"
|
||||||
|
|
||||||
|
class nvMemory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
nvMemory();
|
||||||
|
~nvMemory();
|
||||||
|
bool saveConfig(TSettings* Settings);
|
||||||
|
bool loadConfig(TSettings* Settings);
|
||||||
|
bool deleteConfig();
|
||||||
|
private:
|
||||||
|
bool init();
|
||||||
|
bool Initialized_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef BUILD_SPIFFS
|
||||||
|
|
||||||
|
#define ESP_DRD_USE_SPIFFS true
|
||||||
|
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
#include <FS.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <WiFiManager.h>
|
||||||
|
|
||||||
|
nvMemory::nvMemory()
|
||||||
|
{
|
||||||
|
Initialized_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nvMemory::~nvMemory()
|
||||||
|
{
|
||||||
|
if (Initialized_)
|
||||||
|
SPIFFS.end();
|
||||||
|
};
|
||||||
|
|
||||||
|
bool nvMemory::saveConfig(TSettings* Settings)
|
||||||
|
{
|
||||||
|
if (init())
|
||||||
|
{
|
||||||
|
// Save Config in JSON format
|
||||||
|
Serial.println(F("SPIFS: Saving configuration..."));
|
||||||
|
|
||||||
|
// Create a JSON document
|
||||||
|
StaticJsonDocument<512> json;
|
||||||
|
json[JSON_KEY_POOLURL] = Settings->PoolAddress;
|
||||||
|
json[JSON_KEY_POOLPORT] = Settings->PoolPort;
|
||||||
|
json[JSON_KEY_WALLETID] = Settings->BtcWallet;
|
||||||
|
json[JSON_KEY_TIMEZONE] = Settings->Timezone;
|
||||||
|
|
||||||
|
// Open config file
|
||||||
|
File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w");
|
||||||
|
if (!configFile)
|
||||||
|
{
|
||||||
|
// Error, file did not open
|
||||||
|
Serial.println("SPIFS: failed to open config file for writing");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize JSON data to write to file
|
||||||
|
serializeJsonPretty(json, Serial);
|
||||||
|
Serial.print('\n');
|
||||||
|
if (serializeJson(json, configFile) == 0)
|
||||||
|
{
|
||||||
|
// Error writing file
|
||||||
|
Serial.println(F("SPIFS: Failed to write to file"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Close file
|
||||||
|
configFile.close();
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool nvMemory::loadConfig(TSettings* Settings)
|
||||||
|
{
|
||||||
|
// Uncomment if we need to format filesystem
|
||||||
|
// SPIFFS.format();
|
||||||
|
|
||||||
|
// Load existing configuration file
|
||||||
|
// Read configuration from FS json
|
||||||
|
|
||||||
|
if (init())
|
||||||
|
{
|
||||||
|
if (SPIFFS.exists(JSON_CONFIG_FILE))
|
||||||
|
{
|
||||||
|
// The file exists, reading and loading
|
||||||
|
Serial.println("SPIFS: Reading config file");
|
||||||
|
File configFile = SPIFFS.open(JSON_CONFIG_FILE, "r");
|
||||||
|
if (configFile)
|
||||||
|
{
|
||||||
|
Serial.println("SPIFS: Opened configuration file");
|
||||||
|
StaticJsonDocument<512> json;
|
||||||
|
DeserializationError error = deserializeJson(json, configFile);
|
||||||
|
configFile.close();
|
||||||
|
serializeJsonPretty(json, Serial);
|
||||||
|
Serial.print('\n');
|
||||||
|
if (!error)
|
||||||
|
{
|
||||||
|
Serial.println("SPIFS: Parsing JSON");
|
||||||
|
strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress);
|
||||||
|
strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet);
|
||||||
|
if (json.containsKey(JSON_KEY_POOLPORT))
|
||||||
|
Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>();
|
||||||
|
if (json.containsKey(JSON_KEY_TIMEZONE))
|
||||||
|
Settings->Timezone = json[JSON_KEY_TIMEZONE].as<int>();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Error loading JSON data
|
||||||
|
Serial.println("SPIFS: Failed to load json config");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool nvMemory::deleteConfig()
|
||||||
|
{
|
||||||
|
Serial.println("SPIFS: Erasing config file..");
|
||||||
|
return SPIFFS.remove(JSON_CONFIG_FILE); //Borramos fichero
|
||||||
|
}
|
||||||
|
|
||||||
|
bool nvMemory::init()
|
||||||
|
{
|
||||||
|
if (!Initialized_)
|
||||||
|
{
|
||||||
|
Serial.println("SPIFS: Mounting File System...");
|
||||||
|
// May need to make it begin(true) first time you are using SPIFFS
|
||||||
|
Initialized_ = SPIFFS.begin(false) || SPIFFS.begin(true);
|
||||||
|
Initialized_ ? Serial.println("SPIFS: Mounted") : Serial.println("SPIFS: Mounting failed.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("SPIFS: Already Mounted");
|
||||||
|
}
|
||||||
|
return Initialized_;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
#error We need some kind of permanent storage here!
|
||||||
|
|
||||||
|
nvMemory::nvMemory() {}
|
||||||
|
|
||||||
|
nvMemory::~nvMemory() {}
|
||||||
|
|
||||||
|
bool nvMemory::saveConfig(TSettings* Settings) { return false; }
|
||||||
|
|
||||||
|
bool nvMemory::loadConfig(TSettings* Settings) { return false; }
|
||||||
|
|
||||||
|
bool nvMemory::deleteConfig() { return false; }
|
||||||
|
|
||||||
|
bool nvMemory::init() { return false; }
|
||||||
|
|
||||||
|
|
||||||
|
#endif //BUILD_SPIFFS
|
||||||
|
|
||||||
|
#endif // _NVMEMORY_H_
|
@ -9,7 +9,7 @@
|
|||||||
#include "monitor.h"
|
#include "monitor.h"
|
||||||
#include "drivers/display.h"
|
#include "drivers/display.h"
|
||||||
#include "drivers/storage/SDCard.h"
|
#include "drivers/storage/SDCard.h"
|
||||||
#include "drivers/storage/SPIStorage.h"
|
#include "drivers/storage/nvMemory.h"
|
||||||
#include "drivers/storage/storage.h"
|
#include "drivers/storage/storage.h"
|
||||||
|
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ TSettings Settings;
|
|||||||
WiFiManager wm;
|
WiFiManager wm;
|
||||||
extern monitor_data mMonitor;
|
extern monitor_data mMonitor;
|
||||||
|
|
||||||
SPIStorage SPIFS;
|
nvMemory nvMem;
|
||||||
|
|
||||||
void saveConfigCallback()
|
void saveConfigCallback()
|
||||||
// Callback notifying us of the need to save configuration
|
// Callback notifying us of the need to save configuration
|
||||||
@ -49,7 +49,7 @@ void configModeCallback(WiFiManager* myWiFiManager)
|
|||||||
void reset_configurations()
|
void reset_configurations()
|
||||||
{
|
{
|
||||||
Serial.println("Erasing Config, restarting");
|
Serial.println("Erasing Config, restarting");
|
||||||
SPIFS.deleteConfigFile();
|
nvMem.deleteConfig();
|
||||||
wm.resetSettings();
|
wm.resetSettings();
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
@ -84,14 +84,14 @@ void init_WifiManager()
|
|||||||
// Explicitly set WiFi mode
|
// Explicitly set WiFi mode
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
if (!SPIFS.loadConfigFile(&Settings))
|
if (!nvMem.loadConfig(&Settings))
|
||||||
{
|
{
|
||||||
//No config file on internal flash.
|
//No config file on internal flash.
|
||||||
SDCard sdc;
|
SDCard SDCrd;
|
||||||
if (!sdc.loadConfigFile(&Settings))
|
if (!SDCrd.loadConfigFile(&Settings))
|
||||||
{
|
{
|
||||||
//No config file on SD card.
|
//No config file on SD card.
|
||||||
sdc.SD2SPIStorage(&SPIFS); // reboot on success.
|
SDCrd.SD2nvMemory(&nvMem); // reboot on success.
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -166,7 +166,7 @@ void init_WifiManager()
|
|||||||
strncpy(Settings.BtcWallet, addr_text_box.getValue(), sizeof(Settings.BtcWallet));
|
strncpy(Settings.BtcWallet, addr_text_box.getValue(), sizeof(Settings.BtcWallet));
|
||||||
Settings.Timezone = atoi(time_text_box_num.getValue());
|
Settings.Timezone = atoi(time_text_box_num.getValue());
|
||||||
|
|
||||||
SPIFS.saveConfigFile(&Settings);
|
nvMem.saveConfig(&Settings);
|
||||||
delay(3000);
|
delay(3000);
|
||||||
//reset and try again, or maybe put it to deep sleep
|
//reset and try again, or maybe put it to deep sleep
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
@ -225,7 +225,7 @@ void init_WifiManager()
|
|||||||
// Save the custom parameters to FS
|
// Save the custom parameters to FS
|
||||||
if (shouldSaveConfig)
|
if (shouldSaveConfig)
|
||||||
{
|
{
|
||||||
SPIFS.saveConfigFile(&Settings);
|
nvMem.saveConfig(&Settings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user