From 227cbd536f66ca3f7231bf5db7aea92d57ef7c17 Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Tue, 9 Jul 2024 20:01:11 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=84=20Formats=20StatefulService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp32/lib/ESP32-sveltekit/SettingValue.cpp | 77 ++++++++++------------ esp32/lib/ESP32-sveltekit/SettingValue.h | 5 +- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/esp32/lib/ESP32-sveltekit/SettingValue.cpp b/esp32/lib/ESP32-sveltekit/SettingValue.cpp index 9ebba8e..122def2 100644 --- a/esp32/lib/ESP32-sveltekit/SettingValue.cpp +++ b/esp32/lib/ESP32-sveltekit/SettingValue.cpp @@ -14,54 +14,45 @@ #include -namespace SettingValue -{ - const String PLATFORM = "esp32"; +namespace SettingValue { +const String PLATFORM = "esp32"; - /** - * Returns a new string after replacing each instance of the pattern with a value generated by calling the provided - * callback. - */ - String replaceEach(String value, String pattern, String (*generateReplacement)()) - { - while (true) - { - int index = value.indexOf(pattern); - if (index == -1) - { - break; - } - value = value.substring(0, index) + generateReplacement() + value.substring(index + pattern.length()); +/** + * Returns a new string after replacing each instance of the pattern with a value generated by calling the provided + * callback. + */ +String replaceEach(String value, String pattern, String (*generateReplacement)()) { + while (true) { + int index = value.indexOf(pattern); + if (index == -1) { + break; } - return value; + value = value.substring(0, index) + generateReplacement() + value.substring(index + pattern.length()); } + return value; +} - /** - * Generates a random number, encoded as a hex string. - */ - String getRandom() - { - return String(random(2147483647), HEX); - } +/** + * Generates a random number, encoded as a hex string. + */ +String getRandom() { return String(random(2147483647), HEX); } - /** - * Uses the station's MAC address to create a unique id for each device. - */ - String getUniqueId() - { - uint8_t mac[6]; - esp_read_mac(mac, ESP_MAC_WIFI_STA); - char macStr[13] = {0}; - sprintf(macStr, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - return String(macStr); - } +/** + * Uses the station's MAC address to create a unique id for each device. + */ +String getUniqueId() { + uint8_t mac[6]; + esp_read_mac(mac, ESP_MAC_WIFI_STA); + char macStr[13] = {0}; + sprintf(macStr, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return String(macStr); +} - String format(String value) - { - value = replaceEach(value, "#{random}", getRandom); - value.replace("#{unique_id}", getUniqueId()); - value.replace("#{platform}", PLATFORM); - return value; - } +String format(String value) { + value = replaceEach(value, "#{random}", getRandom); + value.replace("#{unique_id}", getUniqueId()); + value.replace("#{platform}", PLATFORM); + return value; +} }; // end namespace SettingValue diff --git a/esp32/lib/ESP32-sveltekit/SettingValue.h b/esp32/lib/ESP32-sveltekit/SettingValue.h index d0aee40..93dc23a 100644 --- a/esp32/lib/ESP32-sveltekit/SettingValue.h +++ b/esp32/lib/ESP32-sveltekit/SettingValue.h @@ -17,9 +17,8 @@ #include -namespace SettingValue -{ - String format(String value); +namespace SettingValue { +String format(String value); }; #endif // end SettingValue