Skip to content

Commit

Permalink
allow id for attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed May 6, 2022
1 parent d075ee3 commit f4aa4ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
35 changes: 29 additions & 6 deletions src/command.cpp
Expand Up @@ -90,11 +90,16 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
const char * command_p = nullptr;
if (num_paths == 2) {
command_p = p.paths()[1].c_str();
} else if (num_paths >= 3) {
} else if (num_paths == 3) {
// concatenate the path into one string as it could be in the format 'hc/XXX'
char command[50];
snprintf(command, sizeof(command), "%s/%s", p.paths()[1].c_str(), p.paths()[2].c_str());
command_p = command;
} else if (num_paths > 3) {
// concatenate the path into one string as it could be in the format 'hc/XXX'
char command[50];
snprintf(command, sizeof(command), "%s/%s/%s", p.paths()[1].c_str(), p.paths()[2].c_str(), p.paths()[3].c_str());
command_p = command;
} else {
// take it from the JSON
if (input.containsKey("entity")) {
Expand All @@ -110,7 +115,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
if (command_p == nullptr) {
// handle dead endpoints like api/system or api/boiler
// default to 'info' for SYSTEM, DALLASENSOR and ANALOGSENSOR, the other devices to 'values' for shortname version
if (num_paths < 3) {
if (num_paths < (id_n > 0 ? 4 : 3)) {
if (device_type < EMSdevice::DeviceType::BOILER) {
command_p = "info";
} else {
Expand Down Expand Up @@ -187,7 +192,7 @@ const char * Command::parse_command_string(const char * command, int8_t & id) {
if (command == nullptr) {
return nullptr;
}

/*
// make a copy of the string command for parsing
char command_s[30];
strlcpy(command_s, command, sizeof(command_s));
Expand All @@ -214,11 +219,29 @@ const char * Command::parse_command_string(const char * command, int8_t & id) {
} else if (!strncmp(command, "wwc", 3) && start_pos == 5) {
id = command[start_pos - 2] - '0' + 8; // wwc1 has id 9
} else {
id = 0; // special case for extracting the attributes
// id = 0; // special case for extracting the attributes
return command;
}

return (command + start_pos);
*/
if (!strncmp(command, "hc", 2) && strlen(command) >= 3) {
id = command[2] - '0';
command += 3;
} else if (!strncmp(command, "wwc", 3) && strlen(command) >= 4) {
if (command[3] == '1' && command[4] == '0') {
id = 19; // wwc10
command += 5;
} else {
id = command[3] - '0' + 8; // wwc1 has id 9
command += 4;
}
}
if (command[0] == '/' || command[0] == '.' || command[0] == '_') {
command++;
}
if (command[0] == '\0') {
return nullptr;
}
return command;
}

// calls a command directly
Expand Down
19 changes: 9 additions & 10 deletions src/emsdevice.cpp
Expand Up @@ -920,14 +920,13 @@ bool EMSdevice::get_value_info(JsonObject & output, const char * cmd, const int8
// make a copy of the string command for parsing
char command_s[30];
strlcpy(command_s, cmd, sizeof(command_s));
char * attribute_s = command_s;

// if id=0 then we have a specific attribute to fetch instead of the complete record
if (id == 0) {
char * p = command_s;
char * breakp = strchr(p, '/');
if (breakp) {
*breakp = '\0';
char * attribute_s = nullptr;

// check specific attribute to fetch instead of the complete record
char * breakp = strchr(command_s, '/');
if (breakp) {
*breakp = '\0';
if (strlen(breakp + 1)) {
attribute_s = breakp + 1;
}
}
Expand Down Expand Up @@ -1073,8 +1072,8 @@ bool EMSdevice::get_value_info(JsonObject & output, const char * cmd, const int8
json[value] = "not set";
}

// if id is 0 then we're filtering on an attribute, go find it
if (id == 0) {
// if we're filtering on an attribute, go find it
if (attribute_s) {
#if defined(EMSESP_DEBUG)
EMSESP::logger().debug(F("[DEBUG] Attribute '%s'"), attribute_s);
#endif
Expand Down

0 comments on commit f4aa4ed

Please sign in to comment.