Skip to content

Commit

Permalink
fix handling on non json payloads - #173
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Nov 13, 2021
1 parent 09addcb commit 805cef6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
char data_str[10];
return_code = Command::call(device_type, command_p, Helpers::render_value(data_str, (float)data.as<float>(), 2), is_admin, id_n, output);
} else if (data.isNull()) {
return_code = Command::call(device_type, command_p, "", is_admin, id_n, output); // empty
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
}
Expand Down Expand Up @@ -246,6 +246,7 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
// check if its a call to and end-point to a device, i.e. has no value
// except for system commands as this is a special device without any queryable entities (device values)
// exclude SYSTEM and DALLASSENSOR

if ((device_type >= EMSdevice::DeviceType::BOILER) && (!value || !strlen(value))) {
if (!cf || !cf->cmdfunction_json_) {
#if defined(EMSESP_DEBUG)
Expand Down
19 changes: 11 additions & 8 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,24 @@ void Mqtt::on_message(const char * topic, const char * payload, size_t len) {
}
}

StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> input_doc;
StaticJsonDocument<EMSESP_JSON_SIZE_LARGE_DYN> output_doc;
JsonObject input, output;

// convert payload into a json doc, if it's not empty
// if the payload is a single parameter (not JSON) create a JSON with the key 'value'
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> input;
if (len != 0) {
DeserializationError error = deserializeJson(input, message);
if (error == DeserializationError::Code::InvalidInput) {
input.clear(); // this is important to clear first
input["value"] = (const char *)message; // always a string
DeserializationError error = deserializeJson(input_doc, message);
if (!input_doc.containsKey("value") || error) {
input_doc.clear();
input_doc["value"] = (const char *)message; // always a string
}
}

// parse and call the command
StaticJsonDocument<EMSESP_JSON_SIZE_LARGE_DYN> output_doc;
JsonObject output = output_doc.to<JsonObject>();
uint8_t return_code = Command::process(topic, true, input.as<JsonObject>(), output); // mqtt is always authenticated
input = input_doc.as<JsonObject>();
output = output_doc.to<JsonObject>();
uint8_t return_code = Command::process(topic, true, input, output); // mqtt is always authenticated

if (return_code != CommandRet::OK) {
char error[100];
Expand Down
3 changes: 3 additions & 0 deletions src/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
EMSESP::mqtt_.incoming("ems-esp/system/publish");
EMSESP::mqtt_.incoming("ems-esp/thermostat/seltemp"); // empty payload, sends reponse

EMSESP::mqtt_.incoming("ems-esp/boiler/wwseltemp", "59");
EMSESP::mqtt_.incoming("ems-esp/boiler/wwseltemp");

// MQTT bad tests
EMSESP::mqtt_.incoming("ems-esp/thermostate/mode", "auto"); // unknown device
EMSESP::mqtt_.incoming("ems-esp/thermostat/modee", "auto"); // unknown command
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.3.0b4"
#define EMSESP_APP_VERSION "3.3.0b5"

0 comments on commit 805cef6

Please sign in to comment.