Same hostname generation and customization as V3

This commit is contained in:
Djuri Baars 2023-11-16 00:26:23 +01:00
parent 7d0d07e928
commit 483622f430
7 changed files with 39 additions and 5 deletions

View File

@ -191,6 +191,13 @@
<input type="text" name="mempoolInstance" id="mempoolInstance" class="form-control">
</div>
</div>
<div class="row">
<label for="hostnamePrefix" class="col-sm-6 col-form-label col-form-label-sm">Hostname prefix</label>
<div class="col-sm-6">
<input type="text" name="hostnamePrefix" id="hostnamePrefix" class="form-control">
<div class="form-text">A restart is required to apply new hostname prefix.</div>
</div>
</div>
<div class="row">
<div class=" col-sm-6">
<div class="form-check form-switch">

View File

@ -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;

View File

@ -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);
}

View File

@ -5,6 +5,7 @@
//#include <WiFiMulti.h>
#include "config.h"
#include "utils.hpp"
#include "shared.hpp"
#include "Adafruit_GFX.h"
#include "lib/epd.hpp"

View File

@ -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;
}

View File

@ -4,3 +4,4 @@
double getSupplyAtBlock(uint blockNr);
std::string formatNumberWithSuffix(int64_t num);
String getMyHostname();

View File

@ -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,6 +187,8 @@ 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;
@ -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);