mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
Support upgrade/downgrade between versions #832
This commit is contained in:
@@ -41,7 +41,6 @@ class FSPersistence {
|
||||
Serial.println();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||
settingsFile.close();
|
||||
return;
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 ...
|
||||
// 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_DEBUG("No version information found (%s)", settingsVersion.c_str());
|
||||
#endif
|
||||
settingsVersion = "3.4.4"; // this was the last stable version
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
|
||||
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.build().c_str(),
|
||||
settings_version.prerelease().c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
// save the new version to the settings
|
||||
|
||||
// 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;
|
||||
|
||||
@@ -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_;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace emsesp {
|
||||
|
||||
class WebSettings {
|
||||
public:
|
||||
std::string version;
|
||||
String version;
|
||||
String locale;
|
||||
uint8_t tx_mode;
|
||||
uint8_t ems_bus_id;
|
||||
|
||||
Reference in New Issue
Block a user