feat: set labels in NodeContext/Info

This commit is contained in:
2025-08-29 20:21:11 +02:00
parent d3a9802ec9
commit fe045804cb
7 changed files with 61 additions and 42 deletions

View File

@@ -103,6 +103,11 @@ void ApiServer::onSystemStatusRequest(AsyncWebServerRequest *request) {
for (const auto& kv : it->second.labels) {
labelsObj[kv.first.c_str()] = kv.second;
}
} else if (!ctx.self.labels.empty()) {
JsonObject labelsObj = doc["labels"].to<JsonObject>();
for (const auto& kv : ctx.self.labels) {
labelsObj[kv.first.c_str()] = kv.second;
}
}
}
String json;
@@ -286,7 +291,7 @@ void ApiServer::onTaskControlRequest(AsyncWebServerRequest *request) {
// Add task details to response
statusDoc["taskDetails"] = JsonObject();
JsonObject taskDetails = statusDoc["taskDetails"];
JsonObject taskDetails = statusDoc["taskDetails"];
taskDetails["name"] = taskName;
taskDetails["enabled"] = taskManager.isTaskEnabled(taskName.c_str());
taskDetails["running"] = taskManager.isTaskRunning(taskName.c_str());
@@ -294,7 +299,7 @@ void ApiServer::onTaskControlRequest(AsyncWebServerRequest *request) {
// Add system context
taskDetails["system"] = JsonObject();
JsonObject systemInfo = taskDetails["system"];
JsonObject systemInfo = taskDetails["system"];
systemInfo["freeHeap"] = ESP.getFreeHeap();
systemInfo["uptime"] = millis();

View File

@@ -14,7 +14,6 @@ void NetworkManager::setHostnameFromMac() {
}
void NetworkManager::setupWiFi() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ctx.config.wifi_ssid.c_str(), ctx.config.wifi_password.c_str());
Serial.println("[WiFi] Connecting to AP...");
@@ -43,18 +42,26 @@ void NetworkManager::setupWiFi() {
Serial.println(ctx.hostname);
Serial.printf("[WiFi] UDP listening on port %d\n", ctx.config.udp_port);
// Register this node in the memberlist via event system
NodeInfo self;
self.hostname = ctx.hostname;
if(WiFi.isConnected()) {
self.ip = WiFi.localIP();
// Populate self NodeInfo
ctx.self.hostname = ctx.hostname;
if (WiFi.isConnected()) {
ctx.self.ip = WiFi.localIP();
} else {
// Fallback to AP IP if not connected
self.ip = WiFi.softAPIP();
ctx.self.ip = WiFi.softAPIP();
}
self.lastSeen = millis();
self.status = NodeInfo::ACTIVE;
// Initialize a default label for demonstration; users can modify at runtime
self.labels["hostname"] = ctx.hostname;
ctx.fire("node_discovered", &self);
ctx.self.lastSeen = millis();
ctx.self.status = NodeInfo::ACTIVE;
ctx.self.labels["hostname"] = ctx.hostname;
// Ensure member list has an entry for this node
auto &memberList = *ctx.memberList;
auto existing = memberList.find(ctx.hostname);
if (existing == memberList.end()) {
memberList[ctx.hostname] = ctx.self;
} else {
existing->second = ctx.self;
}
// Notify listeners that the node is (re)discovered
ctx.fire("node_discovered", &ctx.self);
}

View File

@@ -4,6 +4,16 @@ NodeContext::NodeContext() {
udp = new WiFiUDP();
memberList = new std::map<String, NodeInfo>();
hostname = "";
self.hostname = "";
self.ip = IPAddress();
self.lastSeen = 0;
self.status = NodeInfo::INACTIVE;
}
NodeContext::NodeContext(std::initializer_list<std::pair<String, String>> initialLabels) : NodeContext() {
for (const auto& kv : initialLabels) {
self.labels[kv.first] = kv.second;
}
}
NodeContext::~NodeContext() {