🪕 Adds string utilities
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#include <IPAddress.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <JsonUtils.h>
|
||||
#include <SettingValue.h>
|
||||
#include <string_utilities.h>
|
||||
#include <IPUtils.h>
|
||||
#include <state_result.h>
|
||||
|
||||
@@ -98,7 +98,7 @@ class APSettings {
|
||||
case AP_MODE_NEVER: break;
|
||||
default: newSettings.provisionMode = AP_MODE_DISCONNECTED;
|
||||
}
|
||||
newSettings.ssid = root["ssid"] | SettingValue::format(FACTORY_AP_SSID);
|
||||
newSettings.ssid = root["ssid"] | format(FACTORY_AP_SSID);
|
||||
newSettings.password = root["password"] | FACTORY_AP_PASSWORD;
|
||||
newSettings.channel = root["channel"] | FACTORY_AP_CHANNEL;
|
||||
newSettings.ssidHidden = root["ssid_hidden"] | FACTORY_AP_SSID_HIDDEN;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
constexpr static const char* PLATFORM = "esp32";
|
||||
|
||||
static String getRandom() { return String(random(2147483647), HEX); }
|
||||
|
||||
static 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);
|
||||
}
|
||||
|
||||
static 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());
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static String format(String value) {
|
||||
value = replaceEach(value, "#{random}", getRandom);
|
||||
value.replace("#{unique_id}", getUniqueId());
|
||||
value.replace("#{platform}", PLATFORM);
|
||||
return value;
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <JsonUtils.h>
|
||||
#include <IPUtils.h>
|
||||
#include <SettingValue.h>
|
||||
#include <string_utilities.h>
|
||||
#include <state_result.h>
|
||||
|
||||
#ifndef FACTORY_WIFI_RSSI_THRESHOLD
|
||||
@@ -59,7 +59,7 @@ class WiFiSettings {
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, WiFiSettings &settings) {
|
||||
settings.hostname = root["hostname"] | SettingValue::format(FACTORY_WIFI_HOSTNAME);
|
||||
settings.hostname = root["hostname"] | format(FACTORY_WIFI_HOSTNAME);
|
||||
settings.priorityBySignalStrength = root["priority_RSSI"] | true;
|
||||
|
||||
settings.wifiSettings.clear();
|
||||
|
||||
Reference in New Issue
Block a user