This commit is contained in:
MichaelDvP
2024-01-29 10:20:15 +01:00
46 changed files with 1740 additions and 583 deletions

View File

@@ -304,6 +304,7 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
// check if its a call to an end-point of a device
// this is used to fetch the attributes of the device entity, or call a command directly
// for example info, values, commands, etc
bool single_command = (!value || !strlen(value));
if (single_command) {
// exception 1: anything that is from System
@@ -365,10 +366,10 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
// report back. If not OK show output from error, other return the HTTP code
if (return_code != CommandRet::OK) {
if (value == nullptr) {
if ((value == nullptr) || (strlen(value) == 0)) {
LOG_ERROR("Command '%s' failed with code: %d", cmd, return_code);
} else {
LOG_ERROR("Command '%s:%s' failed with code: %d", cmd, value, return_code);
LOG_ERROR("Command '%s/%s' failed with code: %d", cmd, value, return_code);
}
return message(return_code, "callback function failed", output);
}
@@ -443,7 +444,7 @@ bool Command::list(const uint8_t device_type, JsonObject output) {
return false;
}
// create a list of commands, sort them
// create a list of commands we have registered, and sort them
std::list<std::string> sorted_cmds;
for (const auto & cf : cmdfunctions_) {
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN)) {
@@ -452,6 +453,12 @@ bool Command::list(const uint8_t device_type, JsonObject output) {
}
sorted_cmds.sort();
// force add info and commands for those non-EMS devices
if (device_type == EMSdevice::DeviceType::TEMPERATURESENSOR) {
output[F_(info)] = Helpers::translated_word(FL_(info_cmd));
output[F_(commands)] = Helpers::translated_word(FL_(commands_cmd));
}
for (const auto & cl : sorted_cmds) {
for (const auto & cf : cmdfunctions_) {
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == std::string(cf.cmd_))) {
@@ -476,16 +483,26 @@ void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbo
return;
}
// create a list of commands, sort them
// create list of commands we have registered
std::list<std::string> sorted_cmds;
for (const auto & cf : cmdfunctions_) {
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN)) {
sorted_cmds.push_back((cf.cmd_));
}
}
sorted_cmds.sort();
// if not in verbose mode, just print them on a single line
// non EMS devices always have an info and commands command
bool show_info = (device_type == EMSdevice::DeviceType::TEMPERATURESENSOR || device_type == EMSdevice::DeviceType::ANALOGSENSOR
|| device_type == EMSdevice::DeviceType::SCHEDULER || device_type == EMSdevice::DeviceType::CUSTOM);
if (!verbose && show_info) {
sorted_cmds.push_back(F_(info));
sorted_cmds.push_back(F_(commands));
}
sorted_cmds.sort(); // sort them
// if not in verbose mode, just print them on a single line and exit
if (!verbose) {
for (const auto & cl : sorted_cmds) {
shell.print(cl);
@@ -496,7 +513,16 @@ void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbo
}
// verbose mode
shell.println();
shell.printfln("\n%s%s %s:%s", COLOR_BOLD_ON, COLOR_YELLOW, EMSdevice::device_type_2_device_name(device_type), COLOR_RESET);
// we hard code 'info' and 'commmands' commands so print them first
if (show_info) {
shell.printf(" info:\t\t\t\t%slists all values %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_GREEN);
shell.println(COLOR_RESET);
shell.printf(" commands:\t\t\t%slists all commands %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_GREEN);
shell.println(COLOR_RESET);
}
for (const auto & cl : sorted_cmds) {
// find and print the description
for (const auto & cf : cmdfunctions_) {
@@ -531,8 +557,6 @@ void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbo
}
shell.println();
}
shell.println();
}
// see if a device_type is active and has associated commands
@@ -600,67 +624,32 @@ void Command::show_devices(uuid::console::Shell & shell) {
shell.println();
}
// output list of all commands to console
// 'show commmands' : output list of all commands to console
// calls show with verbose mode set
void Command::show_all(uuid::console::Shell & shell) {
shell.printfln("Showing all available commands (%s*%s=authentication not required):", COLOR_BRIGHT_GREEN, COLOR_RESET);
// show system first
shell.print(COLOR_BOLD_ON);
shell.print(COLOR_YELLOW);
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SYSTEM));
shell.print(COLOR_RESET);
// show system ones first
show(shell, EMSdevice::DeviceType::SYSTEM, true);
// show Custom
shell.print(COLOR_BOLD_ON);
shell.print(COLOR_YELLOW);
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::CUSTOM));
shell.println(COLOR_RESET);
shell.printf(" info:\t\t\t\t%slists all values %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_GREEN);
shell.println(COLOR_RESET);
shell.printf(" commands:\t\t\t%slists all commands %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_GREEN);
shell.print(COLOR_RESET);
show(shell, EMSdevice::DeviceType::CUSTOM, 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.println(COLOR_RESET);
shell.printf(" info:\t\t\t\t%slists all values %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_GREEN);
shell.println(COLOR_RESET);
shell.printf(" commands:\t\t\t%slists all commands %s*", COLOR_BRIGHT_CYAN, COLOR_BRIGHT_GREEN);
shell.print(COLOR_RESET);
show(shell, EMSdevice::DeviceType::SCHEDULER, true);
// show sensors
// then sensors
if (EMSESP::sensor_enabled()) {
shell.print(COLOR_BOLD_ON);
shell.print(COLOR_YELLOW);
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::TEMPERATURESENSOR));
shell.print(COLOR_RESET);
show(shell, EMSdevice::DeviceType::TEMPERATURESENSOR, true);
}
if (EMSESP::analog_enabled()) {
shell.print(COLOR_BOLD_ON);
shell.print(COLOR_YELLOW);
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::ANALOGSENSOR));
shell.print(COLOR_RESET);
show(shell, EMSdevice::DeviceType::ANALOGSENSOR, true);
}
// do this in the order of factory classes to keep a consistent order when displaying
// now EMS devices, do this in the order of factory classes to keep a consistent order when displaying
for (const auto & device_class : EMSFactory::device_handlers()) {
if (Command::device_has_commands(device_class.first)) {
shell.print(COLOR_BOLD_ON);
shell.print(COLOR_YELLOW);
shell.printf(" %s: ", EMSdevice::device_type_2_device_name(device_class.first));
shell.print(COLOR_RESET);
show(shell, device_class.first, true);
}
}
shell.println();
}
// Extract only the path component from the passed URI and normalized it