🪄 Formats StatefulService
This commit is contained in:
@@ -14,21 +14,17 @@
|
||||
|
||||
#include <SettingValue.h>
|
||||
|
||||
namespace SettingValue
|
||||
{
|
||||
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)
|
||||
{
|
||||
String replaceEach(String value, String pattern, String (*generateReplacement)()) {
|
||||
while (true) {
|
||||
int index = value.indexOf(pattern);
|
||||
if (index == -1)
|
||||
{
|
||||
if (index == -1) {
|
||||
break;
|
||||
}
|
||||
value = value.substring(0, index) + generateReplacement() + value.substring(index + pattern.length());
|
||||
@@ -39,16 +35,12 @@ namespace SettingValue
|
||||
/**
|
||||
* Generates a random number, encoded as a hex string.
|
||||
*/
|
||||
String getRandom()
|
||||
{
|
||||
return String(random(2147483647), HEX);
|
||||
}
|
||||
String getRandom() { return String(random(2147483647), HEX); }
|
||||
|
||||
/**
|
||||
* Uses the station's MAC address to create a unique id for each device.
|
||||
*/
|
||||
String getUniqueId()
|
||||
{
|
||||
String getUniqueId() {
|
||||
uint8_t mac[6];
|
||||
esp_read_mac(mac, ESP_MAC_WIFI_STA);
|
||||
char macStr[13] = {0};
|
||||
@@ -56,8 +48,7 @@ namespace SettingValue
|
||||
return String(macStr);
|
||||
}
|
||||
|
||||
String format(String value)
|
||||
{
|
||||
String format(String value) {
|
||||
value = replaceEach(value, "#{random}", getRandom);
|
||||
value.replace("#{unique_id}", getUniqueId());
|
||||
value.replace("#{platform}", PLATFORM);
|
||||
|
||||
@@ -17,8 +17,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace SettingValue
|
||||
{
|
||||
namespace SettingValue {
|
||||
String format(String value);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user