Remove install-specific names from config and API docs
This commit is contained in:
@@ -587,6 +587,65 @@ void handleFactoryResetConfig() {
|
||||
sendConfigJson();
|
||||
}
|
||||
|
||||
|
||||
void handleGenericRelayRoute() {
|
||||
String uri = server.uri();
|
||||
|
||||
// Expected format:
|
||||
// /relay/relay_1/on
|
||||
// /relay/relay_1/off
|
||||
// /relay/relay_2/on
|
||||
// /relay/relay_2/off
|
||||
|
||||
String prefix = "/relay/";
|
||||
if (!uri.startsWith(prefix)) {
|
||||
server.send(404, "application/json", "{\"ok\":false,\"error\":\"invalid_relay_route\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
String rest = uri.substring(prefix.length());
|
||||
int slash = rest.indexOf('/');
|
||||
|
||||
if (slash < 0) {
|
||||
server.send(400, "application/json", "{\"ok\":false,\"error\":\"missing_relay_action\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
String relayId = rest.substring(0, slash);
|
||||
String action = rest.substring(slash + 1);
|
||||
|
||||
bool enabled;
|
||||
|
||||
if (action == "on") {
|
||||
enabled = true;
|
||||
} else if (action == "off") {
|
||||
enabled = false;
|
||||
} else {
|
||||
server.send(400, "application/json", "{\"ok\":false,\"error\":\"invalid_relay_action\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (relayId == "relay_1") {
|
||||
relays.starlink = enabled;
|
||||
} else if (relayId == "relay_2") {
|
||||
relays.fridge = enabled;
|
||||
} else {
|
||||
server.send(404, "application/json", "{\"ok\":false,\"error\":\"unknown_relay\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
updateRelayOutputs();
|
||||
|
||||
DynamicJsonDocument doc(512);
|
||||
doc["ok"] = true;
|
||||
doc["id"] = relayId;
|
||||
doc["state"] = enabled;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
server.send(200, "application/json", output);
|
||||
}
|
||||
|
||||
void handleStatus() {
|
||||
DynamicJsonDocument doc(2048);
|
||||
buildStatusDocument(doc);
|
||||
@@ -669,6 +728,11 @@ void setup() {
|
||||
}
|
||||
|
||||
server.on("/status", handleStatus);
|
||||
server.on("/relay/relay_1/on", HTTP_GET, handleGenericRelayRoute);
|
||||
server.on("/relay/relay_1/off", HTTP_GET, handleGenericRelayRoute);
|
||||
server.on("/relay/relay_2/on", HTTP_GET, handleGenericRelayRoute);
|
||||
server.on("/relay/relay_2/off", HTTP_GET, handleGenericRelayRoute);
|
||||
|
||||
|
||||
server.on("/config", HTTP_GET, handleGetConfig);
|
||||
server.on("/config/relay", HTTP_POST, handleUpdateRelayConfig);
|
||||
|
||||
Reference in New Issue
Block a user