From f21ff7d1e92b5caafeef78ebedb323e6e85633dc Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 7 Apr 2023 18:43:42 +0200 Subject: [PATCH] add migration for obsolete gpio setting --- Gpio.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++- Gpio.h | 2 ++ PreferencesKeys.h | 4 ++-- WebCfgServer.cpp | 6 ------ main.cpp | 1 + 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Gpio.cpp b/Gpio.cpp index 3c9c1af..df2099d 100644 --- a/Gpio.cpp +++ b/Gpio.cpp @@ -4,6 +4,7 @@ #include "Pins.h" #include "Logger.h" #include "PreferencesKeys.h" +#include "RestartReason.h" Gpio* Gpio::_inst = nullptr; unsigned long Gpio::_debounceTs = 0; @@ -15,6 +16,11 @@ Gpio::Gpio(Preferences* preferences) _inst = this; loadPinConfiguration(); + if(_preferences->getBool(preference_gpio_locking_enabled)) + { + migrateObsoleteSetting(); + } + _inst->init(); } @@ -108,7 +114,7 @@ void Gpio::loadPinConfiguration() void Gpio::savePinConfiguration(const std::vector &pinConfiguration) { - int8_t serialized[pinConfiguration.size() * 2]; + int8_t serialized[std::max(pinConfiguration.size() * 2, _preferences->getBytesLength(preference_gpio_configuration))]; memset(serialized, 0, sizeof(serialized)); int len = pinConfiguration.size(); @@ -123,6 +129,13 @@ void Gpio::savePinConfiguration(const std::vector &pinConfiguration) } } + for(int8_t v : serialized) + { + Serial.print((int)v); + Serial.print(" "); + } + Serial.println(); + _preferences->putBytes(preference_gpio_configuration, serialized, sizeof(serialized)); } @@ -260,3 +273,35 @@ void Gpio::setPinOutput(const uint8_t& pin, const uint8_t& state) { digitalWrite(pin, state); } + +#define TRIGGER_LOCK_PIN 32 +#define TRIGGER_UNLOCK_PIN 33 +#define TRIGGER_UNLATCH_PIN 27 + +void Gpio::migrateObsoleteSetting() +{ + _pinConfiguration.clear(); + + PinEntry entry1; + entry1.pin = 27; + entry1.role = PinRole::InputUnlatch; + + PinEntry entry2; + entry2.pin = 32; + entry2.role = PinRole::InputLock; + + PinEntry entry3; + entry3.pin = 33; + entry3.role = PinRole::InputUnlock; + + _pinConfiguration.push_back(entry1); + _pinConfiguration.push_back(entry2); + _pinConfiguration.push_back(entry3); + + savePinConfiguration(_pinConfiguration); + + _preferences->remove(preference_gpio_locking_enabled); + Log->println("Migrated gpio control setting"); + delay(200); + restartEsp(RestartReason::GpioConfigurationUpdated); +} diff --git a/Gpio.h b/Gpio.h index 5b0e115..b67eed8 100644 --- a/Gpio.h +++ b/Gpio.h @@ -45,6 +45,8 @@ public: Gpio(Preferences* preferences); static void init(); + void migrateObsoleteSetting(); + void addCallback(std::function callback); void loadPinConfiguration(); diff --git a/PreferencesKeys.h b/PreferencesKeys.h index be0caee..ca6c3aa 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -69,7 +69,7 @@ private: preference_query_interval_configuration, preference_query_interval_battery, preference_query_interval_keypad, preference_keypad_control_enabled, preference_register_as_app, preference_command_nr_of_retries, preference_command_retry_delay, preference_cred_user, preference_cred_password, preference_publish_authdata, - preference_gpio_locking_enabled, preference_publish_debug_info, preference_presence_detection_timeout, + preference_publish_debug_info, preference_presence_detection_timeout, preference_has_mac_saved, preference_has_mac_byte_0, preference_has_mac_byte_1, preference_has_mac_byte_2, }; std::vector _redact = @@ -82,7 +82,7 @@ private: { preference_started_before, preference_mqtt_log_enabled, preference_lock_enabled, preference_opener_enabled, preference_restart_on_disconnect, preference_keypad_control_enabled, preference_register_as_app, preference_ip_dhcp_enabled, - preference_publish_authdata, preference_gpio_locking_enabled, preference_has_mac_saved, preference_publish_debug_info + preference_publish_authdata, preference_has_mac_saved, preference_publish_debug_info }; const bool isRedacted(const char* key) const diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 9b96f41..044f7c9 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -451,11 +451,6 @@ bool WebCfgServer::processArgs(String& message) _preferences->putBool(preference_publish_authdata, (value == "1")); configChanged = true; } - else if(key == "GPLCK") - { - _preferences->putBool(preference_gpio_locking_enabled, (value == "1")); - configChanged = true; - } else if(key == "REGAPP") { _preferences->putBool(preference_register_as_app, (value == "1")); @@ -826,7 +821,6 @@ void WebCfgServer::buildNukiConfigHtml(String &response) printInputField(response, "NRTRY", "Number of retries if command failed", _preferences->getInt(preference_command_nr_of_retries), 10); printInputField(response, "TRYDLY", "Delay between retries (milliseconds)", _preferences->getInt(preference_command_retry_delay), 10); printCheckBox(response, "PUBAUTH", "Publish auth data (May reduce battery life)", _preferences->getBool(preference_publish_authdata)); - printCheckBox(response, "GPLCK", "Enable control via GPIO", _preferences->getBool(preference_gpio_locking_enabled)); printInputField(response, "PRDTMO", "Presence detection timeout (seconds; -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10); printInputField(response, "RSBC", "Restart if bluetooth beacons not received (seconds; -1 to disable)", _preferences->getInt(preference_restart_ble_beacon_lost), 10); response.concat(""); diff --git a/main.cpp b/main.cpp index 15357b7..e88522e 100644 --- a/main.cpp +++ b/main.cpp @@ -170,6 +170,7 @@ void setup() Log->print(F("NUKI Hub version ")); Log->println(NUKI_HUB_VERSION); bool firstStart = initPreferences(); + initializeRestartReason(); CharBuffer::initialize();