migrate from binary config to json config

This commit is contained in:
sharandac
2020-08-11 14:32:27 +02:00
parent 77703b42b0
commit 33fe453f5e
24 changed files with 557 additions and 226 deletions

View File

@@ -27,6 +27,7 @@
#include "powermgm.h"
#include "wifictl.h"
#include "json_config_psram_allocator.h"
#include "gui/statusbar.h"
#include "webserver/webserver.h"
@@ -67,8 +68,7 @@ void wifictl_setup( void ) {
wifictl_networklist[ entry ].password[ 0 ] = '\0';
}
// load network list from spiff
wifictl_load_network();
// load config from spiff
wifictl_load_config();
// register WiFi events
@@ -185,36 +185,91 @@ void wifictl_setup( void ) {
*
*/
void wifictl_save_config( void ) {
fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_WRITE );
if ( SPIFFS.exists( WIFICTL_CONFIG_FILE ) ) {
SPIFFS.remove( WIFICTL_CONFIG_FILE );
log_i("remove old binary wificfg config");
}
if ( SPIFFS.exists( WIFICTL_LIST_FILE ) ) {
SPIFFS.remove( WIFICTL_LIST_FILE );
log_i("remove old binary wifilist config");
}
if ( !file ) {
log_e("Can't save file: %s", WIFICTL_CONFIG_FILE );
}
else {
file.write( (uint8_t *)&wifictl_config, sizeof( wifictl_config ) );
fs::File file = SPIFFS.open( WIFICTL_JSON_CONFIG_FILE, FILE_WRITE );
if (!file) {
log_e("Can't open file: %s!", WIFICTL_JSON_CONFIG_FILE );
}
else {
SpiRamJsonDocument doc( 10000 );
doc["autoon"] = wifictl_config.autoon;
doc["webserver"] = wifictl_config.webserver;
for ( int i = 0 ; i < NETWORKLIST_ENTRYS ; i++ ) {
doc["networklist"][ i ]["ssid"] = wifictl_networklist[ i ].ssid;
doc["networklist"][ i ]["psk"] = wifictl_networklist[ i ].password;
}
if ( serializeJsonPretty( doc, file ) == 0) {
log_e("Failed to write config file");
}
doc.clear();
}
file.close();
}
}
/*
*
*/
void wifictl_load_config( void ) {
fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_READ );
if ( SPIFFS.exists( WIFICTL_JSON_CONFIG_FILE ) ) {
fs::File file = SPIFFS.open( WIFICTL_JSON_CONFIG_FILE, FILE_READ );
if (!file) {
log_e("Can't open file: %s!", WIFICTL_JSON_CONFIG_FILE );
}
else {
int filesize = file.size();
SpiRamJsonDocument doc( filesize * 2 );
if (!file) {
log_e("Can't open file: %s", WIFICTL_CONFIG_FILE );
}
else {
int filesize = file.size();
if ( filesize > sizeof( wifictl_config ) ) {
log_e("Failed to read configfile. Wrong filesize!" );
DeserializationError error = deserializeJson( doc, file );
if ( error ) {
log_e("update check deserializeJson() failed: %s", error.c_str() );
}
else {
wifictl_config.autoon = doc["autoon"].as<bool>();
wifictl_config.webserver = doc["webserver"].as<bool>();
for ( int i = 0 ; i < NETWORKLIST_ENTRYS ; i++ ) {
strlcpy( wifictl_networklist[ i ].ssid , doc["networklist"][ i ]["ssid"], sizeof( wifictl_networklist[ i ].ssid ) );
strlcpy( wifictl_networklist[ i ].password, doc["networklist"][ i ]["psk"], sizeof( wifictl_networklist[ i ].password ) );
}
}
doc.clear();
}
file.close();
}
else {
file.read( (uint8_t *)&wifictl_config, filesize );
log_i("no json config exists, read from binary");
wifictl_load_network();
fs::File file = SPIFFS.open( WIFICTL_CONFIG_FILE, FILE_READ );
if (!file) {
log_e("Can't open file: %s!", WIFICTL_CONFIG_FILE );
}
else {
int filesize = file.size();
if ( filesize > sizeof( wifictl_config ) ) {
log_e("Failed to read configfile. Wrong filesize!" );
}
else {
file.read( (uint8_t *)&wifictl_config, filesize );
file.close();
wifictl_save_config();
return;
}
file.close();
}
}
file.close();
}
}
bool wifictl_get_autoon( void ) {
@@ -234,20 +289,6 @@ void wifictl_set_webserver( bool webserver ) {
wifictl_config.webserver = webserver;
wifictl_save_config();
}
/*
*
*/
void wifictl_save_network( void ) {
fs::File file = SPIFFS.open( WIFICTL_LIST_FILE, FILE_WRITE );
if ( !file ) {
log_e("Can't save file: %s", WIFICTL_LIST_FILE );
}
else {
file.write( (uint8_t *)wifictl_networklist, sizeof( wifictl_networklist ) );
file.close();
}
}
/*
*
@@ -296,7 +337,7 @@ bool wifictl_delete_network( const char *ssid ) {
if( !strcmp( ssid, wifictl_networklist[ entry ].ssid ) ) {
wifictl_networklist[ entry ].ssid[ 0 ] = '\0';
wifictl_networklist[ entry ].password[ 0 ] = '\0';
wifictl_save_network();
wifictl_save_config();
return( true );
}
}
@@ -314,7 +355,7 @@ bool wifictl_insert_network( const char *ssid, const char *password ) {
for( int entry = 0 ; entry < NETWORKLIST_ENTRYS; entry++ ) {
if( !strcmp( ssid, wifictl_networklist[ entry ].ssid ) ) {
strncpy( wifictl_networklist[ entry ].password, password, sizeof( wifictl_networklist[ entry ].password ) );
wifictl_save_network();
wifictl_save_config();
WiFi.scanNetworks();
powermgm_set_event( POWERMGM_WIFI_SCAN );
return( true );
@@ -325,7 +366,7 @@ bool wifictl_insert_network( const char *ssid, const char *password ) {
if( strlen( wifictl_networklist[ entry ].ssid ) == 0 ) {
strncpy( wifictl_networklist[ entry ].ssid, ssid, sizeof( wifictl_networklist[ entry ].ssid ) );
strncpy( wifictl_networklist[ entry ].password, password, sizeof( wifictl_networklist[ entry ].password ) );
wifictl_save_network();
wifictl_save_config();
WiFi.scanNetworks();
powermgm_set_event( POWERMGM_WIFI_SCAN );
return( true );