support single scheduler item updates for Dashboard

This commit is contained in:
proddy
2024-10-10 21:26:22 +01:00
parent 37ac684d24
commit 31cfdc6604

View File

@@ -49,10 +49,10 @@ void WebSchedulerService::begin() {
// and also calls when the Scheduler web page is refreshed
void WebScheduler::read(WebScheduler & webScheduler, JsonObject root) {
JsonArray schedule = root["schedule"].to<JsonArray>();
uint8_t counter = 0;
uint8_t counter = 1;
for (const ScheduleItem & scheduleItem : webScheduler.scheduleItems) {
JsonObject si = schedule.add<JsonObject>();
si["id"] = counter++; // id is only used to render the table and must be unique
si["id"] = counter++; // id is only used to render the table and must be unique. 0 is for Dashboard
si["flags"] = scheduleItem.flags;
si["active"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.active : false;
si["time"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.time : "";
@@ -65,13 +65,35 @@ void WebScheduler::read(WebScheduler & webScheduler, JsonObject root) {
// call on initialization and also when the Schedule web page is saved
// this loads the data into the internal class
StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webScheduler) {
// check if we're only toggling it on/off via the Dashboard
// if it is a single schedule object and id is 0
if (root["schedule"].is<JsonObject>()) {
JsonObject si = root["schedule"];
if (si["id"] == 0) {
// find the item, matching the name
for (ScheduleItem & scheduleItem : webScheduler.scheduleItems) {
std::string name = si["name"].as<std::string>();
if (scheduleItem.name == name) {
scheduleItem.active = si["active"];
EMSESP::webSchedulerService.publish(true);
return StateUpdateResult::CHANGED;
}
}
return StateUpdateResult::UNCHANGED;
}
}
// otherwise we're updating and saving the whole list again
if (!root["schedule"].is<JsonArray>()) {
return StateUpdateResult::ERROR; // invalid format
}
// reset the list
Command::erase_device_commands(EMSdevice::DeviceType::SCHEDULER);
webScheduler.scheduleItems.clear();
EMSESP::webSchedulerService.ha_reset();
// build up the list of schedule items
if (root["schedule"].is<JsonArray>()) {
for (const JsonObject schedule : root["schedule"].as<JsonArray>()) {
// create each schedule item, overwriting any previous settings
// ignore the id (as this is only used in the web for table rendering)
@@ -99,7 +121,6 @@ StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webSchedu
CommandFlag::ADMIN_ONLY);
}
}
}
EMSESP::webSchedulerService.publish(true);
@@ -551,6 +572,19 @@ void WebSchedulerService::test() {
si.elapsed_min = 0;
si.retry_cnt = 0xFF; // no startup retries
webScheduler.scheduleItems.push_back(si);
// test 2
si = ScheduleItem();
si.active = false;
si.flags = 1;
si.time = "13:00";
si.cmd = "system/message";
si.value = "20";
si.name = ""; // to make sure its excluded from Dashboard
si.elapsed_min = 0;
si.retry_cnt = 0xFF; // no startup retries
webScheduler.scheduleItems.push_back(si);
already_added = true;