Skip to content

Commit

Permalink
Support upgrade/downgrade between versions #832
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Dec 25, 2022
1 parent 8000497 commit 9b3b7fc
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 90 deletions.
2 changes: 1 addition & 1 deletion lib/framework/FSPersistence.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class FSPersistence {
Serial.println();
#endif
#endif

_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
settingsFile.close();
return;
Expand Down Expand Up @@ -96,6 +95,7 @@ class FSPersistence {
// debug added by Proddy
#if defined(EMSESP_DEBUG)
#if defined(EMSESP_USE_SERIAL)
Serial.println();
Serial.printf("Writing to file: %s: ", _filePath);
serializeJson(jsonDocument, Serial);
Serial.println();
Expand Down
1 change: 1 addition & 0 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ void Mqtt::loop() {
// print MQTT log and other stuff to console
void Mqtt::show_mqtt(uuid::console::Shell & shell) {
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.println();
Expand Down
96 changes: 44 additions & 52 deletions src/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void System::wifi_reconnect() {
void System::format(uuid::console::Shell & shell) {
auto msg = ("Formatting file system. This will reset all settings to their defaults");
shell.logger().warning(msg);
shell.flush();
// shell.flush();

EMSuart::stop();

Expand Down Expand Up @@ -1015,78 +1015,70 @@ bool System::check_restore() {
}

// 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
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
EMSESP::webSettingsService.read([&](WebSettings & settings) { settingsVersion = settings.version; });
if (!factory_settings) {
// fetch current version from settings file
EMSESP::webSettingsService.read([&](WebSettings & settings) { settingsVersion = settings.version.c_str(); });

if (settingsVersion.empty() || (settingsVersion.length() < 5) || factory_settings) {
LOG_DEBUG("No prior version found, preparing fresh install of v%s", EMSESP_APP_VERSION);
settingsVersion = "0.0.0";

// check if its before 3.5.0b12 when we didn't store the version
// by checking ...
}

version::Semver200_version settings_version(settingsVersion);
// see if we're missing a version, will be < 3.5.0b13 from Dec 23 2022
missing_version = (settingsVersion.empty() || (settingsVersion.length() < 5));
if (missing_version) {
#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());
LOG_DEBUG("No version information found (%s)", settingsVersion.c_str());
#endif
settingsVersion = "3.4.4"; // this was the last stable version
}
}

version::Semver200_version this_version(EMSESP_APP_VERSION);
#ifdef EMSESP_DEBUG
LOG_NOTICE("This version: string %s, major %d, minor %d, patch %d, pre-release %s, build %s",
EMSESP_APP_VERSION,
this_version.major(),
this_version.minor(),
this_version.patch(),
this_version.prerelease().c_str(),
this_version.build().c_str());
#endif
version::Semver200_version settings_version(settingsVersion);

// save the new version to the settings
if (!missing_version) {
LOG_INFO("Current version from settings is %d.%d.%d-%s",
settings_version.major(),
settings_version.minor(),
settings_version.patch(),
settings_version.prerelease().c_str());
}

// TODO put back in after testing!
/*
// always save the new version to the settings
EMSESP::webSettingsService.update(
[&](WebSettings & settings) {
settings.version = EMSESP_APP_VERSION;
return StateUpdateResult::CHANGED;
},
"local");
*/

if (factory_settings) {
return false; // fresh install, do nothing
}

version::Semver200_version this_version(EMSESP_APP_VERSION);

// compare versions
bool reboot_required = false;
if (this_version > settings_version) {
LOG_NOTICE("Upgrading from version %d.%d.%d-%s to version %d.%d.%d-%s",
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());
LOG_NOTICE("Upgrading to version %d.%d.%d-%s", this_version.major(), this_version.minor(), this_version.patch(), this_version.prerelease().c_str());

// if we're coming from 3.4.4 or 3.5.0b14 then we need to apply new settings
if (missing_version) {
#ifdef EMSESP_DEBUG
LOG_DEBUG("Setting MQTT ID Entity to v3.4 format");
#endif
EMSESP::esp8266React.getMqttSettingsService()->update(
[&](MqttSettings & mqttSettings) {
mqttSettings.entity_format = 0; // use old Entity ID format from v3.4
return StateUpdateResult::CHANGED;
},
"local");
}

} else if (this_version < settings_version) {
LOG_NOTICE("Downgrading from version %d.%d.%d-%s to version %d.%d.%d-%s",
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());
LOG_NOTICE("Downgrading to version %d.%d.%d-%s", this_version.major(), this_version.minor(), this_version.patch(), this_version.prerelease().c_str());
} else {
// same version, do nothing
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class System {
uint8_t bool_format_;
uint8_t enum_format_;
bool readonly_mode_;
std::string version_;
String version_;

// ethernet
uint8_t phy_type_;
Expand Down
2 changes: 1 addition & 1 deletion src/web/WebSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void WebSettings::read(WebSettings & settings, JsonObject & root) {
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"] || EMSESP_DEFAULT_VERSION;
settings.version = root["version"] | EMSESP_DEFAULT_VERSION;

// 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
Expand Down
70 changes: 35 additions & 35 deletions src/web/WebSettingsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,41 @@ namespace emsesp {

class WebSettings {
public:
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;
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
Expand Down

0 comments on commit 9b3b7fc

Please sign in to comment.