From 2151905d467d14f60c82e5e7fc3f573c161af5fb Mon Sep 17 00:00:00 2001 From: Proddy Date: Fri, 23 Dec 2022 18:28:09 +0100 Subject: [PATCH] test upgrade --- lib_standalone/ESP8266React.h | 73 +++++++++++++++++----------------- lib_standalone/FSPersistence.h | 1 + src/emsesp.cpp | 15 +++++-- src/locale_common.h | 2 +- src/system.cpp | 52 ++++++++++++++++++++++-- src/system.h | 2 + src/web/WebSettingsService.cpp | 4 ++ src/web/WebSettingsService.h | 69 ++++++++++++++++---------------- 8 files changed, 139 insertions(+), 79 deletions(-) diff --git a/lib_standalone/ESP8266React.h b/lib_standalone/ESP8266React.h index b8c599862..e469cc380 100644 --- a/lib_standalone/ESP8266React.h +++ b/lib_standalone/ESP8266React.h @@ -26,42 +26,43 @@ class DummySettings { public: - String locale = "en"; - uint8_t tx_mode = 1; - uint8_t ems_bus_id = 0x0B; - bool syslog_enabled = false; - int8_t syslog_level = 3; // uuid::log::Level - uint32_t syslog_mark_interval = 0; - String syslog_host = "192.168.1.4"; - uint16_t syslog_port = 514; - bool shower_timer = true; - bool shower_alert = false; - uint8_t shower_alert_coldshot = 10; - uint8_t shower_alert_trigger = 7; - bool hide_led = false; - bool notoken_api = false; - bool readonly_mode = false; - uint8_t bool_format = 1; // using "on" and "off" - uint8_t bool_dashboard = 1; - uint8_t enum_format = 1; - bool nosleep = false; - bool fahrenheit = false; - bool bandwidth20 = false; - bool telnet_enabled = false; - String board_profile = "CUSTOM"; - bool trace_raw = false; - bool analog_enabled = true; // analog is enabled - int8_t weblog_level = 1; - uint8_t weblog_buffer = 50; - bool weblog_compact = true; - uint8_t rx_gpio = 0; - uint8_t tx_gpio = 0; - uint8_t dallas_gpio = 16; // to ensure its enabled - bool dallas_parasite = false; - uint8_t led_gpio = 0; - bool low_clock = false; - uint8_t pbutton_gpio = false; - uint8_t solar_maxflow = 30; + std::string version{"poerp"}; + String locale = "en"; + uint8_t tx_mode = 1; + uint8_t ems_bus_id = 0x0B; + bool syslog_enabled = false; + int8_t syslog_level = 3; // uuid::log::Level + uint32_t syslog_mark_interval = 0; + String syslog_host = "192.168.1.4"; + uint16_t syslog_port = 514; + bool shower_timer = true; + bool shower_alert = false; + uint8_t shower_alert_coldshot = 10; + uint8_t shower_alert_trigger = 7; + bool hide_led = false; + bool notoken_api = false; + bool readonly_mode = false; + uint8_t bool_format = 1; // using "on" and "off" + uint8_t bool_dashboard = 1; + uint8_t enum_format = 1; + bool nosleep = false; + bool fahrenheit = false; + bool bandwidth20 = false; + bool telnet_enabled = false; + String board_profile = "CUSTOM"; + bool trace_raw = false; + bool analog_enabled = true; // analog is enabled + int8_t weblog_level = 1; + uint8_t weblog_buffer = 50; + bool weblog_compact = true; + uint8_t rx_gpio = 0; + uint8_t tx_gpio = 0; + uint8_t dallas_gpio = 16; // to ensure its enabled + bool dallas_parasite = false; + uint8_t led_gpio = 0; + bool low_clock = false; + uint8_t pbutton_gpio = false; + uint8_t solar_maxflow = 30; // MQTT uint16_t publish_time = 10; diff --git a/lib_standalone/FSPersistence.h b/lib_standalone/FSPersistence.h index 345e9b8ae..a547b26f8 100644 --- a/lib_standalone/FSPersistence.h +++ b/lib_standalone/FSPersistence.h @@ -24,6 +24,7 @@ class FSPersistence { } void readFromFS() { + Serial.println(); Serial.print("Fake reading file "); Serial.println(_filePath); applyDefaults(); diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 5ee409283..31ed9f790 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -1293,13 +1293,20 @@ void EMSESP::start() { LOG_INFO("Last system reset reason Core0: %s, Core1: %s", system_.reset_reason(0).c_str(), system_.reset_reason(1).c_str()); - // do any system upgrades - if (system_.check_upgrade()) { - LOG_INFO("System needs a restart to apply new settings. Please wait."); + // see if we're restoring a settings file + if (system_.check_restore()) { + LOG_WARNING("System needs a restart to apply new settings. Please wait."); + system_.system_restart(); + }; + + webSettingsService.begin(); // load EMS-ESP Application settings... + + // do any system upgrades + if (system_.check_upgrade()) { + LOG_WARNING("System needs a restart to apply new settings. Please wait."); system_.system_restart(); }; - webSettingsService.begin(); // load EMS-ESP Application settings... system_.reload_settings(); // ... and store some of the settings locally webCustomizationService.begin(); // load the customizations diff --git a/src/locale_common.h b/src/locale_common.h index 4279345d6..20a0c9931 100644 --- a/src/locale_common.h +++ b/src/locale_common.h @@ -208,7 +208,7 @@ MAKE_PSTR(uom_ua, "µA") MAKE_PSTR(uom_lmin, "l/min") MAKE_PSTR(uom_kw, "kW") MAKE_PSTR(uom_w, "W") -MAKE_PSTR(uom_kb, "kB") +MAKE_PSTR(uom_kb, "KB") MAKE_PSTR(uom_dbm, "dBm") MAKE_PSTR(uom_fahrenheit, "°F") MAKE_PSTR(uom_mv, "mV") diff --git a/src/system.cpp b/src/system.cpp index bfba78c6e..469645388 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -290,6 +290,8 @@ void System::syslog_init() { // read some specific system settings to store locally for faster access void System::reload_settings() { EMSESP::webSettingsService.read([&](WebSettings & settings) { + version_ = settings.version; + pbutton_gpio_ = settings.pbutton_gpio; analog_enabled_ = settings.analog_enabled; low_clock_ = settings.low_clock; @@ -969,10 +971,8 @@ void System::show_system(uuid::console::Shell & shell) { #endif } -// handle upgrades from previous versions -// or managing an uploaded files to replace settings files -// returns true if we need a reboot -bool System::check_upgrade() { +// see if there is a restore of an older settings file that needs to be applied +bool System::check_restore() { bool reboot_required = false; #ifndef EMSESP_STANDALONE @@ -1012,6 +1012,50 @@ bool System::check_upgrade() { return reboot_required; } +// handle upgrades from previous versions +// returns true if we need a reboot +bool System::check_upgrade() { + std::string old_version; + + // TODO fix + + if (version_ != EMSESP_APP_VERSION) { + if (version_.empty()) { + LOG_DEBUG("No version, presuming fresh install. Setting to %s", EMSESP_APP_VERSION); + old_version = EMSESP_APP_VERSION; + } else { + LOG_DEBUG("Going from version %s to %s", version_, EMSESP_APP_VERSION); + old_version = version_; + } + // save the new version + version_ = EMSESP_APP_VERSION; + EMSESP::webSettingsService.update( + [&](WebSettings & settings) { + settings.version = EMSESP_APP_VERSION; + return StateUpdateResult::CHANGED; + }, + "local"); + } + + if (old_version == EMSESP_APP_VERSION) { + return false; // no upgrades or reboot needed. we're on the latest + } + + LOG_DEBUG("Doing upgrade..."); // TODO remove + + + // check variations between versions + // get major version + + // get minor version + + // get patch version (ignore alphanumerics) + + bool reboot_required = false; + + return reboot_required; +} + // list commands bool System::command_commands(const char * value, const int8_t id, JsonObject & output) { return Command::list(EMSdevice::DeviceType::SYSTEM, output); diff --git a/src/system.h b/src/system.h index 3f4169306..789de9018 100644 --- a/src/system.h +++ b/src/system.h @@ -75,6 +75,7 @@ class System { void wifi_tweak(); void syslog_init(); bool check_upgrade(); + bool check_restore(); bool heartbeat_json(JsonObject & output); void send_heartbeat(); void send_info_mqtt(const char * event_str, bool send_ntp = false); @@ -294,6 +295,7 @@ class System { uint8_t bool_format_; uint8_t enum_format_; bool readonly_mode_; + std::string version_; // ethernet uint8_t phy_type_; diff --git a/src/web/WebSettingsService.cpp b/src/web/WebSettingsService.cpp index b17963680..5923a0a97 100644 --- a/src/web/WebSettingsService.cpp +++ b/src/web/WebSettingsService.cpp @@ -80,6 +80,10 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) { // call on initialization and also when settings are updated via web or console StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings) { + // load the version of the settings + // will be picked up in System::check_upgrade() + settings.version = root["version"] || ""; + // load default GPIO configuration based on board profile std::vector data; // // led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode #if CONFIG_IDF_TARGET_ESP32C3 diff --git a/src/web/WebSettingsService.h b/src/web/WebSettingsService.h index a20e10c9a..2addbb4cb 100644 --- a/src/web/WebSettingsService.h +++ b/src/web/WebSettingsService.h @@ -29,40 +29,41 @@ namespace emsesp { class WebSettings { public: - String locale; - uint8_t tx_mode; - uint8_t ems_bus_id; - bool shower_timer; - bool shower_alert; - uint8_t shower_alert_trigger; - uint8_t shower_alert_coldshot; - bool syslog_enabled; - int8_t syslog_level; // uuid::log::Level - uint32_t syslog_mark_interval; - String syslog_host; - uint16_t syslog_port; - bool trace_raw; - uint8_t rx_gpio; - uint8_t tx_gpio; - uint8_t dallas_gpio; - bool dallas_parasite; - uint8_t led_gpio; - bool hide_led; - bool low_clock; - bool telnet_enabled; - bool notoken_api; - bool readonly_mode; - bool analog_enabled; - uint8_t pbutton_gpio; - uint8_t solar_maxflow; - String board_profile; - uint8_t bool_format; - uint8_t bool_dashboard; - uint8_t enum_format; - int8_t weblog_level; - uint8_t weblog_buffer; - bool weblog_compact; - bool fahrenheit; + std::string version; + String locale; + uint8_t tx_mode; + uint8_t ems_bus_id; + bool shower_timer; + bool shower_alert; + uint8_t shower_alert_trigger; + uint8_t shower_alert_coldshot; + bool syslog_enabled; + int8_t syslog_level; // uuid::log::Level + uint32_t syslog_mark_interval; + String syslog_host; + uint16_t syslog_port; + bool trace_raw; + uint8_t rx_gpio; + uint8_t tx_gpio; + uint8_t dallas_gpio; + bool dallas_parasite; + uint8_t led_gpio; + bool hide_led; + bool low_clock; + bool telnet_enabled; + bool notoken_api; + bool readonly_mode; + bool analog_enabled; + uint8_t pbutton_gpio; + uint8_t solar_maxflow; + String board_profile; + uint8_t bool_format; + uint8_t bool_dashboard; + uint8_t enum_format; + int8_t weblog_level; + uint8_t weblog_buffer; + bool weblog_compact; + bool fahrenheit; uint8_t phy_type; int8_t eth_power; // -1 means disabled