Support upgrade/downgrade between versions #832

This commit is contained in:
Proddy
2022-12-25 16:47:17 +01:00
parent 8000497302
commit 9b3b7fc8ff
6 changed files with 83 additions and 90 deletions

View File

@@ -41,7 +41,6 @@ class FSPersistence {
Serial.println(); Serial.println();
#endif #endif
#endif #endif
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater); _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
settingsFile.close(); settingsFile.close();
return; return;
@@ -96,6 +95,7 @@ class FSPersistence {
// debug added by Proddy // debug added by Proddy
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
#if defined(EMSESP_USE_SERIAL) #if defined(EMSESP_USE_SERIAL)
Serial.println();
Serial.printf("Writing to file: %s: ", _filePath); Serial.printf("Writing to file: %s: ", _filePath);
serializeJson(jsonDocument, Serial); serializeJson(jsonDocument, Serial);
Serial.println(); Serial.println();

View File

@@ -192,6 +192,7 @@ void Mqtt::loop() {
// print MQTT log and other stuff to console // print MQTT log and other stuff to console
void Mqtt::show_mqtt(uuid::console::Shell & shell) { void Mqtt::show_mqtt(uuid::console::Shell & shell) {
shell.printfln("MQTT is %s", connected() ? F_(connected) : F_(disconnected)); shell.printfln("MQTT is %s", connected() ? F_(connected) : F_(disconnected));
shell.printfln("MQTT Entity ID format is %d", entity_format_);
shell.printfln("MQTT publish errors: %lu", mqtt_publish_fails_); shell.printfln("MQTT publish errors: %lu", mqtt_publish_fails_);
shell.println(); shell.println();

View File

@@ -220,7 +220,7 @@ void System::wifi_reconnect() {
void System::format(uuid::console::Shell & shell) { void System::format(uuid::console::Shell & shell) {
auto msg = ("Formatting file system. This will reset all settings to their defaults"); auto msg = ("Formatting file system. This will reset all settings to their defaults");
shell.logger().warning(msg); shell.logger().warning(msg);
shell.flush(); // shell.flush();
EMSuart::stop(); EMSuart::stop();
@@ -1015,78 +1015,70 @@ bool System::check_restore() {
} }
// handle upgrades from previous versions // handle upgrades from previous versions
// this function will not be called on a clean install, with no settings files yet created
// returns true if we need a reboot // returns true if we need a reboot
bool System::check_upgrade(bool factory_settings) { bool System::check_upgrade(bool factory_settings) {
std::string settingsVersion{}; bool missing_version = true;
std::string settingsVersion{EMSESP_APP_VERSION}; // default setting version
// fetch current version from file if (!factory_settings) {
EMSESP::webSettingsService.read([&](WebSettings & settings) { settingsVersion = settings.version; }); // fetch current version from settings file
EMSESP::webSettingsService.read([&](WebSettings & settings) { settingsVersion = settings.version.c_str(); });
if (settingsVersion.empty() || (settingsVersion.length() < 5) || factory_settings) { // see if we're missing a version, will be < 3.5.0b13 from Dec 23 2022
LOG_DEBUG("No prior version found, preparing fresh install of v%s", EMSESP_APP_VERSION); missing_version = (settingsVersion.empty() || (settingsVersion.length() < 5));
settingsVersion = "0.0.0"; if (missing_version) {
#ifdef EMSESP_DEBUG
// check if its before 3.5.0b12 when we didn't store the version LOG_DEBUG("No version information found (%s)", settingsVersion.c_str());
// by checking ... #endif
settingsVersion = "3.4.4"; // this was the last stable version
}
} }
version::Semver200_version settings_version(settingsVersion); version::Semver200_version settings_version(settingsVersion);
#ifdef EMSESP_DEBUG
LOG_NOTICE("Settings version: string %s, major %d, minor %d, patch %d, pre-release %s, build %s",
settingsVersion.c_str(),
settings_version.major(),
settings_version.minor(),
settings_version.patch(),
settings_version.build().c_str(),
settings_version.prerelease().c_str());
#endif
version::Semver200_version this_version(EMSESP_APP_VERSION); if (!missing_version) {
#ifdef EMSESP_DEBUG LOG_INFO("Current version from settings is %d.%d.%d-%s",
LOG_NOTICE("This version: string %s, major %d, minor %d, patch %d, pre-release %s, build %s", settings_version.major(),
EMSESP_APP_VERSION, settings_version.minor(),
this_version.major(), settings_version.patch(),
this_version.minor(), settings_version.prerelease().c_str());
this_version.patch(), }
this_version.prerelease().c_str(),
this_version.build().c_str());
#endif
// save the new version to the settings // always save the new version to the settings
// TODO put back in after testing!
/*
EMSESP::webSettingsService.update( EMSESP::webSettingsService.update(
[&](WebSettings & settings) { [&](WebSettings & settings) {
settings.version = EMSESP_APP_VERSION; settings.version = EMSESP_APP_VERSION;
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
}, },
"local"); "local");
*/
if (factory_settings) {
return false; // fresh install, do nothing
}
version::Semver200_version this_version(EMSESP_APP_VERSION);
// compare versions // compare versions
bool reboot_required = false; bool reboot_required = false;
if (this_version > settings_version) { if (this_version > settings_version) {
LOG_NOTICE("Upgrading from version %d.%d.%d-%s to version %d.%d.%d-%s", LOG_NOTICE("Upgrading to version %d.%d.%d-%s", this_version.major(), this_version.minor(), this_version.patch(), this_version.prerelease().c_str());
settings_version.major(),
settings_version.minor(), // if we're coming from 3.4.4 or 3.5.0b14 then we need to apply new settings
settings_version.patch(), if (missing_version) {
settings_version.prerelease().c_str(), #ifdef EMSESP_DEBUG
this_version.major(), LOG_DEBUG("Setting MQTT ID Entity to v3.4 format");
this_version.minor(), #endif
this_version.patch(), EMSESP::esp8266React.getMqttSettingsService()->update(
this_version.prerelease().c_str()); [&](MqttSettings & mqttSettings) {
mqttSettings.entity_format = 0; // use old Entity ID format from v3.4
return StateUpdateResult::CHANGED;
},
"local");
}
} else if (this_version < settings_version) { } else if (this_version < settings_version) {
LOG_NOTICE("Downgrading from version %d.%d.%d-%s to version %d.%d.%d-%s", LOG_NOTICE("Downgrading to version %d.%d.%d-%s", this_version.major(), this_version.minor(), this_version.patch(), this_version.prerelease().c_str());
settings_version.major(),
settings_version.minor(),
settings_version.patch(),
settings_version.prerelease().c_str(),
this_version.major(),
this_version.minor(),
this_version.patch(),
this_version.prerelease().c_str());
} else { } else {
// same version, do nothing // same version, do nothing
return false; return false;

View File

@@ -295,7 +295,7 @@ class System {
uint8_t bool_format_; uint8_t bool_format_;
uint8_t enum_format_; uint8_t enum_format_;
bool readonly_mode_; bool readonly_mode_;
std::string version_; String version_;
// ethernet // ethernet
uint8_t phy_type_; uint8_t phy_type_;

View File

@@ -83,7 +83,7 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) {
StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings) { StateUpdateResult WebSettings::update(JsonObject & root, WebSettings & settings) {
// load the version of the settings // load the version of the settings
// will be picked up in System::check_upgrade() // will be picked up in System::check_upgrade()
settings.version = root["version"] || EMSESP_DEFAULT_VERSION; settings.version = root["version"] | EMSESP_DEFAULT_VERSION;
// load default GPIO configuration based on board profile // load default GPIO configuration based on board profile
std::vector<int8_t> data; // // led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode std::vector<int8_t> data; // // led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode

View File

@@ -29,41 +29,41 @@ namespace emsesp {
class WebSettings { class WebSettings {
public: public:
std::string version; String version;
String locale; String locale;
uint8_t tx_mode; uint8_t tx_mode;
uint8_t ems_bus_id; uint8_t ems_bus_id;
bool shower_timer; bool shower_timer;
bool shower_alert; bool shower_alert;
uint8_t shower_alert_trigger; uint8_t shower_alert_trigger;
uint8_t shower_alert_coldshot; uint8_t shower_alert_coldshot;
bool syslog_enabled; bool syslog_enabled;
int8_t syslog_level; // uuid::log::Level int8_t syslog_level; // uuid::log::Level
uint32_t syslog_mark_interval; uint32_t syslog_mark_interval;
String syslog_host; String syslog_host;
uint16_t syslog_port; uint16_t syslog_port;
bool trace_raw; bool trace_raw;
uint8_t rx_gpio; uint8_t rx_gpio;
uint8_t tx_gpio; uint8_t tx_gpio;
uint8_t dallas_gpio; uint8_t dallas_gpio;
bool dallas_parasite; bool dallas_parasite;
uint8_t led_gpio; uint8_t led_gpio;
bool hide_led; bool hide_led;
bool low_clock; bool low_clock;
bool telnet_enabled; bool telnet_enabled;
bool notoken_api; bool notoken_api;
bool readonly_mode; bool readonly_mode;
bool analog_enabled; bool analog_enabled;
uint8_t pbutton_gpio; uint8_t pbutton_gpio;
uint8_t solar_maxflow; uint8_t solar_maxflow;
String board_profile; String board_profile;
uint8_t bool_format; uint8_t bool_format;
uint8_t bool_dashboard; uint8_t bool_dashboard;
uint8_t enum_format; uint8_t enum_format;
int8_t weblog_level; int8_t weblog_level;
uint8_t weblog_buffer; uint8_t weblog_buffer;
bool weblog_compact; bool weblog_compact;
bool fahrenheit; bool fahrenheit;
uint8_t phy_type; uint8_t phy_type;
int8_t eth_power; // -1 means disabled int8_t eth_power; // -1 means disabled