mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
add scheduler api commands/info/value
This commit is contained in:
@@ -565,6 +565,9 @@ bool Command::device_has_commands(const uint8_t device_type) {
|
|||||||
void Command::show_devices(uuid::console::Shell & shell) {
|
void Command::show_devices(uuid::console::Shell & shell) {
|
||||||
shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SYSTEM));
|
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()) {
|
if (EMSESP::dallassensor_.have_sensors()) {
|
||||||
shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::DALLASSENSOR));
|
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(shell, EMSdevice::DeviceType::SYSTEM, true);
|
||||||
|
|
||||||
// show scheduler
|
// show scheduler
|
||||||
shell.print(COLOR_BOLD_ON);
|
if (EMSESP::webSchedulerService.has_commands()) {
|
||||||
shell.print(COLOR_YELLOW);
|
shell.print(COLOR_BOLD_ON);
|
||||||
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER));
|
shell.print(COLOR_YELLOW);
|
||||||
shell.print(COLOR_RESET);
|
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER));
|
||||||
show(shell, EMSdevice::DeviceType::SCHEDULER, true);
|
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
|
// show sensors
|
||||||
if (EMSESP::dallassensor_.have_sensors()) {
|
if (EMSESP::dallassensor_.have_sensors()) {
|
||||||
|
|||||||
@@ -653,14 +653,17 @@ bool EMSESP::get_device_value_info(JsonObject & root, const char * cmd, const in
|
|||||||
|
|
||||||
// specific for the dallassensor
|
// specific for the dallassensor
|
||||||
if (devicetype == DeviceType::DALLASSENSOR) {
|
if (devicetype == DeviceType::DALLASSENSOR) {
|
||||||
EMSESP::dallassensor_.get_value_info(root, cmd, id);
|
return EMSESP::dallassensor_.get_value_info(root, cmd, id);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// analog sensor
|
// analog sensor
|
||||||
if (devicetype == DeviceType::ANALOGSENSOR) {
|
if (devicetype == DeviceType::ANALOGSENSOR) {
|
||||||
EMSESP::analogsensor_.get_value_info(root, cmd, id);
|
return EMSESP::analogsensor_.get_value_info(root, cmd, id);
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
// scheduler
|
||||||
|
if (devicetype == DeviceType::SCHEDULER) {
|
||||||
|
return EMSESP::webSchedulerService.get_value_info(root, cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
char error[100];
|
char error[100];
|
||||||
|
|||||||
@@ -128,6 +128,74 @@ bool WebSchedulerService::command_setvalue(const char * value, const std::string
|
|||||||
return false;
|
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) {
|
void WebSchedulerService::publish_single(const char * name, const bool state) {
|
||||||
if (!Mqtt::publish_single() || name == nullptr || name[0] == '\0') {
|
if (!Mqtt::publish_single() || name == nullptr || name[0] == '\0') {
|
||||||
return;
|
return;
|
||||||
@@ -213,7 +281,9 @@ void WebSchedulerService::publish(const bool force) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
|
if (doc.size() > 0) {
|
||||||
|
Mqtt::queue_publish("scheduler_data", doc.as<JsonObject>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebSchedulerService::has_commands() {
|
bool WebSchedulerService::has_commands() {
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
|
|||||||
void publish(const bool force = false);
|
void publish(const bool force = false);
|
||||||
bool has_commands();
|
bool has_commands();
|
||||||
bool command_setvalue(const char * value, const std::string name);
|
bool command_setvalue(const char * value, const std::string name);
|
||||||
|
bool get_value_info(JsonObject & output, const char * cmd);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool command(const char * cmd, const char * data);
|
bool command(const char * cmd, const char * data);
|
||||||
|
|||||||
Reference in New Issue
Block a user