From 483622f4307a2b5176799d417328e585b95091c7 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Thu, 16 Nov 2023 00:26:23 +0100 Subject: [PATCH] Same hostname generation and customization as V3 --- data/src/index.html | 7 +++++++ data/src/js/script.ts | 1 + src/lib/functions.cpp | 4 ++-- src/lib/functions.hpp | 1 + src/lib/utils.cpp | 11 +++++++++++ src/lib/utils.hpp | 3 ++- src/lib/webserver.cpp | 17 +++++++++++++++-- 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/data/src/index.html b/data/src/index.html index a601d7e..0c8b980 100644 --- a/data/src/index.html +++ b/data/src/index.html @@ -191,6 +191,13 @@ +
+ +
+ +
A restart is required to apply new hostname prefix.
+
+
diff --git a/data/src/js/script.ts b/data/src/js/script.ts index 1125f9b..0952e77 100644 --- a/data/src/js/script.ts +++ b/data/src/js/script.ts @@ -76,6 +76,7 @@ fetch('/api/settings', { document.getElementById('fullRefreshMin').value = jsonData.fullRefreshMin; document.getElementById('wpTimeout').value = jsonData.wpTimeout; document.getElementById('mempoolInstance').value = jsonData.mempoolInstance; + document.getElementById('hostnamePrefix').value = jsonData.hostnamePrefix; if (jsonData.gitRev) document.getElementById('gitRev').innerHTML = "Version: " + jsonData.gitRev; diff --git a/src/lib/functions.cpp b/src/lib/functions.cpp index 0e6cd4e..78d2d33 100644 --- a/src/lib/functions.cpp +++ b/src/lib/functions.cpp @@ -36,8 +36,8 @@ void setupSoftAP() { byte mac[6]; WiFi.macAddress(mac); - softAP_SSID = String("BTClock" + String(mac[5], 16) + String(mac[1], 16)); - WiFi.setHostname(softAP_SSID.c_str()); + softAP_SSID = getMyHostname().c_str(); + WiFi.setHostname(getMyHostname().c_str()); softAP_password = base64::encode(String(mac[2], 16) + String(mac[4], 16) + String(mac[5], 16) + String(mac[1], 16)).substring(2, 10); } diff --git a/src/lib/functions.hpp b/src/lib/functions.hpp index 95177b4..d4d939d 100644 --- a/src/lib/functions.hpp +++ b/src/lib/functions.hpp @@ -5,6 +5,7 @@ //#include #include "config.h" +#include "utils.hpp" #include "shared.hpp" #include "Adafruit_GFX.h" #include "lib/epd.hpp" diff --git a/src/lib/utils.cpp b/src/lib/utils.cpp index 245d4b5..480ca6a 100644 --- a/src/lib/utils.cpp +++ b/src/lib/utils.cpp @@ -41,3 +41,14 @@ std::string formatNumberWithSuffix(int64_t num) { return std::to_string(num); } } + +String getMyHostname() { + uint8_t mac[6]; + //WiFi.macAddress(mac); + esp_efuse_mac_get_default(mac); + char hostname[15]; + String hostnamePrefix = preferences.getString("hostnamePrefix", "btclock"); + snprintf(hostname, sizeof(hostname), "%s-%02x%02x%02x", + hostnamePrefix, mac[3], mac[4], mac[5]); + return hostname; +} \ No newline at end of file diff --git a/src/lib/utils.hpp b/src/lib/utils.hpp index c0ffac7..32f2a6d 100644 --- a/src/lib/utils.hpp +++ b/src/lib/utils.hpp @@ -3,4 +3,5 @@ #include "shared.hpp" double getSupplyAtBlock(uint blockNr); -std::string formatNumberWithSuffix(int64_t num); \ No newline at end of file +std::string formatNumberWithSuffix(int64_t num); +String getMyHostname(); diff --git a/src/lib/webserver.cpp b/src/lib/webserver.cpp index 4a3b4f2..0f96454 100644 --- a/src/lib/webserver.cpp +++ b/src/lib/webserver.cpp @@ -52,7 +52,7 @@ void setupWebserver() // Start server server.begin(); - if (!MDNS.begin(HOSTNAME)) + if (!MDNS.begin(getMyHostname())) { Serial.println(F("Error setting up MDNS responder!")); while (1) @@ -61,6 +61,9 @@ void setupWebserver() } } MDNS.addService("http", "tcp", 80); + MDNS.addServiceTxt("http", "tcp", "model", "BTClock"); + MDNS.addServiceTxt("http", "tcp", "version", "2.0"); + MDNS.addServiceTxt("http", "tcp", "rev", GIT_REV); Serial.println(F("Webserver should be running")); } @@ -184,7 +187,9 @@ void onApiSettingsGet(AsyncWebServerRequest *request) root["rpcUser"] = preferences.getString("rpcUser", BITCOIND_RPC_USER); root["rpcHost"] = preferences.getString("rpcHost", BITCOIND_HOST); root["mempoolInstance"] = preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE); - + root["hostnamePrefix"] = preferences.getString("hostnamePrefix", "btclock"); + root["hostname"] = getMyHostname(); + #ifdef IS_BW root["epdColors"] = 2; #else @@ -290,6 +295,14 @@ void onApiSettingsPost(AsyncWebServerRequest *request) settingsChanged = true; } + if (request->hasParam("hostnamePrefix", true)) + { + AsyncWebParameter *hostnamePrefix = request->getParam("hostnamePrefix", true); + + preferences.putString("hostnamePrefix", hostnamePrefix->value().c_str()); + settingsChanged = true; + } + if (request->hasParam("ledBrightness", true)) { AsyncWebParameter *ledBrightness = request->getParam("ledBrightness", true);