add scheduler api commands/info/value

This commit is contained in:
MichaelDvP
2023-03-06 16:26:52 +01:00
parent db1a335509
commit c9dbf282f6
4 changed files with 93 additions and 10 deletions

View File

@@ -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
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()) {

View File

@@ -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];

View File

@@ -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,8 +281,10 @@ void WebSchedulerService::publish(const bool force) {
}
}
}
if (doc.size() > 0) {
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
}
}
bool WebSchedulerService::has_commands() {
EMSESP::webSchedulerService.read([&](WebScheduler & webScheduler) { scheduleItems = &webScheduler.scheduleItems; });

View File

@@ -58,6 +58,7 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
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);