diff --git a/src/command.cpp b/src/command.cpp index ce269c332..2a906fcdb 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -315,58 +315,56 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * } // check if we have a matching command - if (cf) { - // check permissions - if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !is_admin) { - output["message"] = "authentication failed"; - return CommandRet::NOT_ALLOWED; // command not allowed - } - - 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); - } else { - snprintf(info_s, sizeof(info_s), "'%s/%s'", dname, cmd); - } + if (!cf) { + // we didn't find the command, report error + LOG_DEBUG("Command failed: invalid command '%s'", cmd ? cmd : ""); + return message(CommandRet::NOT_FOUND, "invalid command", output); + } - std::string ro = EMSESP::system_.readonly_mode() ? "[readonly] " : ""; + // check permissions and abort if not authorized + if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !is_admin) { + output["message"] = "authentication failed"; + return CommandRet::NOT_ALLOWED; // command not allowed + } - if ((value == nullptr) || (strlen(value) == 0)) { - LOG_DEBUG(("%sCalling command %s"), ro.c_str(), info_s); + // build up the log string for reporting back + // We send the log message as Warning so it appears in the log (debug is only enabled when compiling with DEBUG) + std::string ro = EMSESP::system_.readonly_mode() ? "[readonly] " : ""; + 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); + } else { + snprintf(info_s, sizeof(info_s), "'%s/%s'", dname, cmd); + } + if ((value == nullptr) || (strlen(value) == 0)) { + LOG_WARNING(("%sCalling command %s"), ro.c_str(), info_s); + } else { + if (id > 0) { + LOG_WARNING(("%sCalling command %s with value %s and id %d on device 0x%02X"), ro.c_str(), info_s, value, id, device_id); } else { - if (id > 0) { - LOG_DEBUG(("%sCalling command %s with value %s and id %d on device 0x%02X"), ro.c_str(), info_s, value, id, device_id); - } else { - LOG_DEBUG(("%sCalling command %s with value %s"), ro.c_str(), info_s, value); - } - } - - // call the function based on type - if (cf->cmdfunction_json_) { - return_code = ((cf->cmdfunction_json_)(value, id, output)) ? CommandRet::OK : CommandRet::ERROR; - } - - // check if read-only. This also checks for valid tags (e.g. heating circuits) - if (cf->cmdfunction_) { - if (!single_command && EMSESP::cmd_is_readonly(device_type, device_id, cmd, id)) { - return_code = CommandRet::INVALID; // readonly or invalid hc - } else { - return_code = ((cf->cmdfunction_)(value, id)) ? CommandRet::OK : CommandRet::ERROR; - } + LOG_WARNING(("%sCalling command %s with value %s"), ro.c_str(), info_s, value); } + } - // report back - if (return_code != CommandRet::OK) { - return message(return_code, "callback function failed", output); + // call the function based on type, either with a json package or no parameters + if (cf->cmdfunction_json_) { + // JSON + return_code = ((cf->cmdfunction_json_)(value, id, output)) ? CommandRet::OK : CommandRet::ERROR; + } else if (cf->cmdfunction_) { + // Normal command + if (!single_command && EMSESP::cmd_is_readonly(device_type, device_id, cmd, id)) { + return_code = CommandRet::INVALID; // error on readonly or invalid hc + } else { + return_code = ((cf->cmdfunction_)(value, id)) ? CommandRet::OK : CommandRet::ERROR; } - - return return_code; } - // we didn't find the command and its not an endpoint, report error - LOG_DEBUG("Command failed: invalid command '%s'", cmd ? cmd : ""); - return message(CommandRet::NOT_FOUND, "invalid command", output); + // report back. If not OK show output from error, other return the HTTP code + if (return_code != CommandRet::OK) { + return message(return_code, "callback function failed", output); + } + return return_code; } // add a command to the list, which does not return json