Add relay enable toggles to WebUI
This commit is contained in:
@@ -1249,11 +1249,20 @@ function renderConfigControls(data){
|
||||
<div class="configrow">
|
||||
<label>${r.id}</label>
|
||||
<input id="relayName${i}" placeholder="Relay name" oninput="markConfigFieldTouched('relayName${i}')">
|
||||
<label>${r.enabled?"Enabled":"Disabled"}</label>
|
||||
<label class="tempCheck">
|
||||
<input id="relayEnabled${i}" type="checkbox" onchange="markConfigFieldTouched('relayEnabled${i}')">
|
||||
Enabled
|
||||
</label>
|
||||
</div>`).join("");
|
||||
}
|
||||
|
||||
relays.forEach((r,i)=>setConfigValue("relayName"+i,r.name||""));
|
||||
relays.forEach((r,i)=>{
|
||||
setConfigValue("relayName"+i,r.name||"");
|
||||
const enabled=$("relayEnabled"+i);
|
||||
if(enabled && !configTouched["relayEnabled"+i] && document.activeElement!==enabled){
|
||||
enabled.checked=!!r.enabled;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setConfigValue("tempEnabledCount", temps.filter(t=>t.enabled).length);
|
||||
@@ -1359,8 +1368,8 @@ async function saveRelayConfig(){
|
||||
const relays=data.config?.relays||[];
|
||||
|
||||
for(let i=0;i<relays.length;i++){
|
||||
const name=$("relayName"+i)?.value.trim();
|
||||
if(!name) continue;
|
||||
const name=$("relayName"+i)?.value.trim() || relays[i].name || relays[i].id;
|
||||
const enabled=$("relayEnabled"+i)?.checked ?? relays[i].enabled;
|
||||
|
||||
await fetch(api("/config/relay"),{
|
||||
method:"POST",
|
||||
@@ -1368,14 +1377,14 @@ async function saveRelayConfig(){
|
||||
body:JSON.stringify({
|
||||
id:relays[i].id,
|
||||
name:name,
|
||||
enabled:relays[i].enabled
|
||||
enabled:enabled
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
await fetch(api("/config/save"),{method:"POST"});
|
||||
await load();
|
||||
alert("Relay names saved");
|
||||
alert("Relay settings saved");
|
||||
}
|
||||
|
||||
async function saveTempConfig(){
|
||||
@@ -2643,9 +2652,19 @@ void sendError(Stream& output, const char* message) {
|
||||
|
||||
int findRelayConfigIndex(const String& id);
|
||||
|
||||
bool relayExistsAndAvailable(const String& relayId) {
|
||||
int index = findRelayConfigIndex(relayId);
|
||||
return index >= 0 && appConfig.relays[index].available;
|
||||
}
|
||||
|
||||
bool relayIsEnabled(const String& relayId) {
|
||||
int index = findRelayConfigIndex(relayId);
|
||||
return index >= 0 && appConfig.relays[index].enabled;
|
||||
}
|
||||
|
||||
bool setRelayById(const String& relayId, bool enabled) {
|
||||
int index = findRelayConfigIndex(relayId);
|
||||
if (index < 0 || !appConfig.relays[index].available) {
|
||||
if (index < 0 || !appConfig.relays[index].available || !appConfig.relays[index].enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -3651,7 +3670,13 @@ void handleUpdateRelayConfig() {
|
||||
}
|
||||
|
||||
if (doc["name"].is<String>()) appConfig.relays[index].name = doc["name"].as<String>();
|
||||
if (doc["enabled"].is<bool>()) appConfig.relays[index].enabled = doc["enabled"].as<bool>();
|
||||
if (doc["enabled"].is<bool>()) {
|
||||
appConfig.relays[index].enabled = doc["enabled"].as<bool>();
|
||||
if (!appConfig.relays[index].enabled) {
|
||||
relays.state[index] = false;
|
||||
updateRelayOutputs();
|
||||
}
|
||||
}
|
||||
|
||||
sendOkConfig();
|
||||
}
|
||||
@@ -3994,10 +4019,18 @@ void handleSetRelayPost() {
|
||||
bool state = doc["state"] | false;
|
||||
|
||||
unsigned long applyStartMs = millis();
|
||||
if (!setRelayById(relayId, state)) {
|
||||
if (!relayExistsAndAvailable(relayId)) {
|
||||
server.send(404, "application/json", "{\"ok\":false,\"error\":\"unknown_relay\"}");
|
||||
return;
|
||||
}
|
||||
if (!relayIsEnabled(relayId)) {
|
||||
server.send(409, "application/json", "{\"ok\":false,\"error\":\"relay_disabled\"}");
|
||||
return;
|
||||
}
|
||||
if (!setRelayById(relayId, state)) {
|
||||
server.send(409, "application/json", "{\"ok\":false,\"error\":\"relay_disabled\"}");
|
||||
return;
|
||||
}
|
||||
unsigned long appliedMs = millis() - applyStartMs;
|
||||
|
||||
DynamicJsonDocument response(512);
|
||||
@@ -4052,10 +4085,18 @@ void handleGenericRelayRoute() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!setRelayById(relayId, enabled)) {
|
||||
if (!relayExistsAndAvailable(relayId)) {
|
||||
server.send(404, "application/json", "{\"ok\":false,\"error\":\"unknown_relay\"}");
|
||||
return;
|
||||
}
|
||||
if (!relayIsEnabled(relayId)) {
|
||||
server.send(409, "application/json", "{\"ok\":false,\"error\":\"relay_disabled\"}");
|
||||
return;
|
||||
}
|
||||
if (!setRelayById(relayId, enabled)) {
|
||||
server.send(409, "application/json", "{\"ok\":false,\"error\":\"relay_disabled\"}");
|
||||
return;
|
||||
}
|
||||
|
||||
DynamicJsonDocument doc(512);
|
||||
doc["ok"] = true;
|
||||
|
||||
Reference in New Issue
Block a user