From 62152825bd20d1c340d9cddb83263c4fbfc3b613 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 21 Jul 2024 12:59:23 +0200 Subject: [PATCH] cleanup --- src/command.cpp | 41 ++++++++++++++++++++++++++++------------- src/command.h | 6 +----- src/emsdevice.cpp | 2 +- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 1bb3f878c..d6d12fb88 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -35,7 +35,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec p.parse(path); if (!p.paths().size()) { - return message(CommandRet::ERROR, "invalid path", output); + return json_message(CommandRet::ERROR, "invalid path", output); } // check first if it's from API, if so strip the "api/" @@ -48,7 +48,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec strlcpy(new_path, path, sizeof(new_path)); p.parse(new_path + Mqtt::base().length() + 1); // re-parse the stripped path } else { - return message(CommandRet::ERROR, "unrecognized path", output); // error + return json_message(CommandRet::ERROR, "unrecognized path", output); // error } } @@ -56,7 +56,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec // if there is only a path (URL) and no body then error! size_t num_paths = p.paths().size(); if (!num_paths && !input.size()) { - return message(CommandRet::ERROR, "missing command in path", output); + return json_message(CommandRet::ERROR, "missing command in path", output); } std::string cmd_s; @@ -78,8 +78,11 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec // validate the device, make sure it exists uint8_t device_type = EMSdevice::device_name_2_device_type(device_s); if (!device_has_commands(device_type)) { - LOG_DEBUG("Command failed: unknown device '%s'", device_s); - return message(CommandRet::NOT_FOUND, "unknown device", output); + char err[100]; + snprintf(err, sizeof(err), "unknown device %s", device_s); + LOG_WARNING("Command failed: %s", err); + output["message"] = err; + return CommandRet::NOT_FOUND; } // the next value on the path should be the command or entity name @@ -114,7 +117,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec if (num_paths < (id_n > 0 ? 4 : 3)) { command_p = device_type == EMSdevice::DeviceType::SYSTEM ? F_(info) : F_(values); } else { - return message(CommandRet::NOT_FOUND, "missing or bad command", output); + return json_message(CommandRet::NOT_FOUND, "missing or bad command", output); } } @@ -199,7 +202,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec } else if (data.isNull()) { return_code = Command::call(device_type, command_p, "", is_admin, id_n, output); // empty, will do a query instead } else { - return message(CommandRet::ERROR, "cannot parse command", output); // can't process + return json_message(CommandRet::ERROR, "cannot parse command", output); // can't process } return return_code; } @@ -334,10 +337,12 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * // first see if there is a command registered and it's valid auto cf = find_command(device_type, device_id, cmd, flag); if (!cf) { - LOG_WARNING("Command failed: unknown command '%s'", cmd ? cmd : ""); + std::string err = std::string("unknown command ") + cmd; + LOG_WARNING("Command failed: %s", err.c_str()); + // if we don't alread have a message set, set it to invalid command if (!output["message"]) { - output["message"] = "unknown command"; + output["message"] = err; } return CommandRet::ERROR; } @@ -355,9 +360,9 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * auto description = Helpers::translated_word(cf->description_); char info_s[100]; if (strlen(description)) { - snprintf(info_s, sizeof(info_s), "'%s/%s' (%s)", dname, cmd, description); + snprintf(info_s, sizeof(info_s), "%s/%s (%s)", dname, cmd, description); } else { - snprintf(info_s, sizeof(info_s), "'%s/%s'", dname, cmd); + snprintf(info_s, sizeof(info_s), "%s/%s", dname, cmd); } // call the function based on command function type @@ -380,9 +385,9 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * if (return_code != CommandRet::OK) { char error[100]; if (single_command) { - snprintf(error, sizeof(error), "Command '%s' failed (%s)", cmd, return_code_string(return_code)); + snprintf(error, sizeof(error), "Command %s failed (%s)", cmd, return_code_string(return_code)); } else { - snprintf(error, sizeof(error), "Command '%s: %s' failed (%s)", cmd, value, return_code_string(return_code)); + snprintf(error, sizeof(error), "Command %s: %s failed (%s)", cmd, value, return_code_string(return_code)); } output.clear(); output["message"] = error; @@ -683,6 +688,16 @@ void Command::show_all(uuid::console::Shell & shell) { shell.println(); } +uint8_t Command::json_message(uint8_t error_code, const char * message, const JsonObject output) { + output.clear(); + output["message"] = message; + return error_code; +} + +// +// SUrlParser class +// + // Extract only the path component from the passed URI and normalized it // e.g. //one/two////three/// becomes /one/two/three std::string SUrlParser::path() { diff --git a/src/command.h b/src/command.h index 45827b644..564d43b21 100644 --- a/src/command.h +++ b/src/command.h @@ -143,11 +143,7 @@ class Command { static std::vector cmdfunctions_; // the list of commands - inline static uint8_t message(uint8_t error_code, const char * message, const JsonObject output) { - output.clear(); - output["message"] = message; - return error_code; - } + static uint8_t json_message(uint8_t error_code, const char * message, const JsonObject output); }; class SUrlParser { diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index df55e7a67..d2f255ef2 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1599,7 +1599,7 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t // if we're filtering on an attribute, go find it if (attribute_s) { #if defined(EMSESP_DEBUG) - EMSESP::logger().debug("[DEBUG] fetching single attribute '%s'", attribute_s); + EMSESP::logger().debug("[DEBUG] fetching single attribute %s", attribute_s); #endif if (json.containsKey(attribute_s)) { std::string data = json[attribute_s].as();