added get_ip_or_hostname()

This commit is contained in:
proddy
2025-12-13 11:20:48 +01:00
parent 29037d19fb
commit c2e8a6c73d
2 changed files with 26 additions and 3 deletions

View File

@@ -1789,6 +1789,28 @@ std::string System::get_metrics_prometheus() {
return result;
}
// return IP or hostname of the EMS-ESP device
String System::get_ip_or_hostname() {
String result = "ems-esp";
EMSESP::esp32React.getNetworkSettingsService()->read([&](NetworkSettings & settings) {
if (settings.enableMDNS) {
if (EMSESP::system_.ethernet_connected()) {
result = ETH.getHostname();
} else if (WiFi.status() == WL_CONNECTED) {
result = WiFi.getHostname();
}
} else {
// no DNS, use the IP
if (EMSESP::system_.ethernet_connected()) {
result = ETH.localIP().toString();
} else if (WiFi.status() == WL_CONNECTED) {
result = WiFi.localIP().toString();
}
}
});
return result;
}
// export status information including the device information
// http://ems-esp/api/system/info
bool System::command_info(const char * value, const int8_t id, JsonObject output) {
@@ -2356,7 +2378,6 @@ String System::getBBQKeesGatewayDetails(uint8_t detail) {
// This is to avoid timeouts in callback functions, like calling from a web hook.
bool System::uploadFirmwareURL(const char * url) {
#ifndef EMSESP_STANDALONE
static String saved_url;
if (url && strlen(url) > 0) {

View File

@@ -92,8 +92,8 @@ class System {
static bool command_response(const char * value, const int8_t id, JsonObject output);
static bool command_service(const char * cmd, const char * value);
static bool get_value_info(JsonObject root, const char * cmd);
static void get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val);
static bool get_value_info(JsonObject root, const char * cmd);
static void get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val);
static std::string get_metrics_prometheus();
#if defined(EMSESP_TEST)
@@ -151,6 +151,8 @@ class System {
static bool readCommand(const char * data);
static String get_ip_or_hostname();
void dallas_gpio(uint8_t gpio) {
dallas_gpio_ = gpio;
}