From c9dbf282f6fba0a933effd8b8ec1120333f2ff6a Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Mon, 6 Mar 2023 16:26:52 +0100 Subject: [PATCH] add scheduler api commands/info/value --- src/command.cpp | 19 ++++++--- src/emsesp.cpp | 11 +++-- src/web/WebSchedulerService.cpp | 72 ++++++++++++++++++++++++++++++++- src/web/WebSchedulerService.h | 1 + 4 files changed, 93 insertions(+), 10 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index c997fddb1..1649b2fcd 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -565,6 +565,9 @@ bool Command::device_has_commands(const uint8_t device_type) { void Command::show_devices(uuid::console::Shell & shell) { shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SYSTEM)); + if (EMSESP::webSchedulerService.has_commands()) { + shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER)); + } if (EMSESP::dallassensor_.have_sensors()) { shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::DALLASSENSOR)); } @@ -596,11 +599,17 @@ void Command::show_all(uuid::console::Shell & shell) { show(shell, EMSdevice::DeviceType::SYSTEM, true); // show scheduler - shell.print(COLOR_BOLD_ON); - shell.print(COLOR_YELLOW); - shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER)); - shell.print(COLOR_RESET); - show(shell, EMSdevice::DeviceType::SCHEDULER, true); + if (EMSESP::webSchedulerService.has_commands()) { + shell.print(COLOR_BOLD_ON); + shell.print(COLOR_YELLOW); + shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER)); + shell.println(COLOR_RESET); + shell.printf(" info: %slists all values %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED); + shell.println(COLOR_RESET); + shell.printf(" commands: %slists all commands %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_RED); + shell.print(COLOR_RESET); + show(shell, EMSdevice::DeviceType::SCHEDULER, true); + } // show sensors if (EMSESP::dallassensor_.have_sensors()) { diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 2272db1e6..f6bf067b9 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -653,14 +653,17 @@ bool EMSESP::get_device_value_info(JsonObject & root, const char * cmd, const in // specific for the dallassensor if (devicetype == DeviceType::DALLASSENSOR) { - EMSESP::dallassensor_.get_value_info(root, cmd, id); - return true; + return EMSESP::dallassensor_.get_value_info(root, cmd, id); } // analog sensor if (devicetype == DeviceType::ANALOGSENSOR) { - EMSESP::analogsensor_.get_value_info(root, cmd, id); - return true; + return EMSESP::analogsensor_.get_value_info(root, cmd, id); + } + + // scheduler + if (devicetype == DeviceType::SCHEDULER) { + return EMSESP::webSchedulerService.get_value_info(root, cmd); } char error[100]; diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 201ffcaff..8a9cfe5ae 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -128,6 +128,74 @@ bool WebSchedulerService::command_setvalue(const char * value, const std::string return false; } +// process json output for info/commands and value_info +bool WebSchedulerService::get_value_info(JsonObject & output, const char * cmd) { + EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; }); + if (scheduleItems->size() == 0) { + return false; + } + if (Helpers::toLower(cmd) == "commands") { + output["info"] = "lists all values"; + output["commands"] = "lists all commands"; + for (const ScheduleItem & scheduleItem : *scheduleItems) { + if (!scheduleItem.name.empty()) { + output[scheduleItem.name] = "activate schedule"; + } + } + return true; + } + if (strlen(cmd) == 0 || Helpers::toLower(cmd) == "values" || Helpers::toLower(cmd) == "info") { + // list all names + for (const ScheduleItem & scheduleItem : *scheduleItems) { + if (!scheduleItem.name.empty()) { + if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) { + output[scheduleItem.name] = scheduleItem.active; + } else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) { + output[scheduleItem.name] = scheduleItem.active ? 1 : 0; + } else { + char result[12]; + output[scheduleItem.name] = Helpers::render_boolean(result, scheduleItem.active); + } + } + } + return (output.size() > 0); + } + char command_s[30]; + strlcpy(command_s, cmd, sizeof(command_s)); + char * attribute_s = nullptr; + + // check specific attribute to fetch instead of the complete record + char * breakp = strchr(command_s, '/'); + if (breakp) { + *breakp = '\0'; + attribute_s = breakp + 1; + } + JsonVariant data; + for (const ScheduleItem & scheduleItem : *scheduleItems) { + if (Helpers::toLower(scheduleItem.name) == Helpers::toLower(command_s)) { + if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) { + output[scheduleItem.name] = scheduleItem.active; + } else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) { + output[scheduleItem.name] = scheduleItem.active ? 1 : 0; + } else { + char result[12]; + output[scheduleItem.name] = Helpers::render_boolean(result, scheduleItem.active); + } + data = output[scheduleItem.name]; + } + } + if (attribute_s && !strcmp(attribute_s, "value")) { + output.clear(); + output["api_data"] = data; + } + if (output.size()) { + return true; + } + output["message"] = "unknown command"; + return false; +} + +// publish single value void WebSchedulerService::publish_single(const char * name, const bool state) { if (!Mqtt::publish_single() || name == nullptr || name[0] == '\0') { return; @@ -213,7 +281,9 @@ void WebSchedulerService::publish(const bool force) { } } } - Mqtt::queue_publish("scheduler_data", doc.as()); + if (doc.size() > 0) { + Mqtt::queue_publish("scheduler_data", doc.as()); + } } bool WebSchedulerService::has_commands() { diff --git a/src/web/WebSchedulerService.h b/src/web/WebSchedulerService.h index 52341d8e4..e723185e4 100644 --- a/src/web/WebSchedulerService.h +++ b/src/web/WebSchedulerService.h @@ -58,6 +58,7 @@ class WebSchedulerService : public StatefulService { void publish(const bool force = false); bool has_commands(); bool command_setvalue(const char * value, const std::string name); + bool get_value_info(JsonObject & output, const char * cmd); private: bool command(const char * cmd, const char * data);