mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
log API call
This commit is contained in:
@@ -315,13 +315,21 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check if we have a matching command
|
// check if we have a matching command
|
||||||
if (cf) {
|
if (!cf) {
|
||||||
// check permissions
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check permissions and abort if not authorized
|
||||||
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !is_admin) {
|
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !is_admin) {
|
||||||
output["message"] = "authentication failed";
|
output["message"] = "authentication failed";
|
||||||
return CommandRet::NOT_ALLOWED; // command not allowed
|
return CommandRet::NOT_ALLOWED; // command not allowed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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_);
|
auto description = Helpers::translated_word(cf->description_);
|
||||||
char info_s[100];
|
char info_s[100];
|
||||||
if (strlen(description)) {
|
if (strlen(description)) {
|
||||||
@@ -329,46 +337,36 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
|||||||
} else {
|
} else {
|
||||||
snprintf(info_s, sizeof(info_s), "'%s/%s'", dname, cmd);
|
snprintf(info_s, sizeof(info_s), "'%s/%s'", dname, cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ro = EMSESP::system_.readonly_mode() ? "[readonly] " : "";
|
|
||||||
|
|
||||||
if ((value == nullptr) || (strlen(value) == 0)) {
|
if ((value == nullptr) || (strlen(value) == 0)) {
|
||||||
LOG_DEBUG(("%sCalling command %s"), ro.c_str(), info_s);
|
LOG_WARNING(("%sCalling command %s"), ro.c_str(), info_s);
|
||||||
} else {
|
} else {
|
||||||
if (id > 0) {
|
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);
|
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 {
|
} else {
|
||||||
LOG_DEBUG(("%sCalling command %s with value %s"), ro.c_str(), info_s, value);
|
LOG_WARNING(("%sCalling command %s with value %s"), ro.c_str(), info_s, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// call the function based on type
|
// call the function based on type, either with a json package or no parameters
|
||||||
if (cf->cmdfunction_json_) {
|
if (cf->cmdfunction_json_) {
|
||||||
|
// JSON
|
||||||
return_code = ((cf->cmdfunction_json_)(value, id, output)) ? CommandRet::OK : CommandRet::ERROR;
|
return_code = ((cf->cmdfunction_json_)(value, id, output)) ? CommandRet::OK : CommandRet::ERROR;
|
||||||
}
|
} else if (cf->cmdfunction_) {
|
||||||
|
// Normal command
|
||||||
// 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)) {
|
if (!single_command && EMSESP::cmd_is_readonly(device_type, device_id, cmd, id)) {
|
||||||
return_code = CommandRet::INVALID; // readonly or invalid hc
|
return_code = CommandRet::INVALID; // error on readonly or invalid hc
|
||||||
} else {
|
} else {
|
||||||
return_code = ((cf->cmdfunction_)(value, id)) ? CommandRet::OK : CommandRet::ERROR;
|
return_code = ((cf->cmdfunction_)(value, id)) ? CommandRet::OK : CommandRet::ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// report back
|
// report back. If not OK show output from error, other return the HTTP code
|
||||||
if (return_code != CommandRet::OK) {
|
if (return_code != CommandRet::OK) {
|
||||||
return message(return_code, "callback function failed", output);
|
return message(return_code, "callback function failed", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
return return_code;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add a command to the list, which does not return json
|
// add a command to the list, which does not return json
|
||||||
void Command::add(const uint8_t device_type, const uint8_t device_id, const char * cmd, const cmd_function_p cb, const char * const * description, uint8_t flags) {
|
void Command::add(const uint8_t device_type, const uint8_t device_id, const char * cmd, const cmd_function_p cb, const char * const * description, uint8_t flags) {
|
||||||
// if the command already exists for that device type don't add it
|
// if the command already exists for that device type don't add it
|
||||||
|
|||||||
Reference in New Issue
Block a user