mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
support single scheduler item updates for Dashboard
This commit is contained in:
@@ -49,10 +49,10 @@ void WebSchedulerService::begin() {
|
|||||||
// and also calls when the Scheduler web page is refreshed
|
// and also calls when the Scheduler web page is refreshed
|
||||||
void WebScheduler::read(WebScheduler & webScheduler, JsonObject root) {
|
void WebScheduler::read(WebScheduler & webScheduler, JsonObject root) {
|
||||||
JsonArray schedule = root["schedule"].to<JsonArray>();
|
JsonArray schedule = root["schedule"].to<JsonArray>();
|
||||||
uint8_t counter = 0;
|
uint8_t counter = 1;
|
||||||
for (const ScheduleItem & scheduleItem : webScheduler.scheduleItems) {
|
for (const ScheduleItem & scheduleItem : webScheduler.scheduleItems) {
|
||||||
JsonObject si = schedule.add<JsonObject>();
|
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["flags"] = scheduleItem.flags;
|
||||||
si["active"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.active : false;
|
si["active"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.active : false;
|
||||||
si["time"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.time : "";
|
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
|
// call on initialization and also when the Schedule web page is saved
|
||||||
// this loads the data into the internal class
|
// this loads the data into the internal class
|
||||||
StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webScheduler) {
|
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
|
// reset the list
|
||||||
Command::erase_device_commands(EMSdevice::DeviceType::SCHEDULER);
|
Command::erase_device_commands(EMSdevice::DeviceType::SCHEDULER);
|
||||||
webScheduler.scheduleItems.clear();
|
webScheduler.scheduleItems.clear();
|
||||||
EMSESP::webSchedulerService.ha_reset();
|
EMSESP::webSchedulerService.ha_reset();
|
||||||
|
|
||||||
// build up the list of schedule items
|
// build up the list of schedule items
|
||||||
if (root["schedule"].is<JsonArray>()) {
|
|
||||||
for (const JsonObject schedule : root["schedule"].as<JsonArray>()) {
|
for (const JsonObject schedule : root["schedule"].as<JsonArray>()) {
|
||||||
// create each schedule item, overwriting any previous settings
|
// create each schedule item, overwriting any previous settings
|
||||||
// ignore the id (as this is only used in the web for table rendering)
|
// 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);
|
CommandFlag::ADMIN_ONLY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
EMSESP::webSchedulerService.publish(true);
|
EMSESP::webSchedulerService.publish(true);
|
||||||
|
|
||||||
@@ -551,6 +572,19 @@ void WebSchedulerService::test() {
|
|||||||
si.elapsed_min = 0;
|
si.elapsed_min = 0;
|
||||||
si.retry_cnt = 0xFF; // no startup retries
|
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);
|
webScheduler.scheduleItems.push_back(si);
|
||||||
already_added = true;
|
already_added = true;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user