Merge pull request #189 from proddy:dev

fix MQTT non-JSON payloads
This commit is contained in:
Proddy
2021-11-14 12:56:20 +01:00
committed by GitHub
5 changed files with 19 additions and 14 deletions

View File

@@ -8,7 +8,7 @@
my_build_flags = my_build_flags =
; 5=verbose, 4=debug, 3=info ; 5=verbose, 4=debug, 3=info
debug_flags = DCORE_DEBUG_LEVEL=5 debug_flags = -DCORE_DEBUG_LEVEL=5
[env:esp32] [env:esp32]
; if using OTA enter your details below ; if using OTA enter your details below
@@ -19,9 +19,7 @@ upload_flags =
upload_port = ems-esp.local upload_port = ems-esp.local
; use this when you don't want to re-build the WebUI ; use this when you don't want to re-build the WebUI
extra_scripts = extra_scripts = scripts/rename_fw.py
scripts/rename_fw.py
scripts/upload_fw.py
; pio run -e debug ; pio run -e debug
; or from Visual Studio Code do PIO -> Project Tasks -> debug -> General -> Upload and Monitor ; or from Visual Studio Code do PIO -> Project Tasks -> debug -> General -> Upload and Monitor

View File

@@ -151,7 +151,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
char data_str[10]; 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); 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()) { } 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 { } else {
return message(CommandRet::ERROR, "cannot parse command", output); // can't process return message(CommandRet::ERROR, "cannot parse command", output); // can't process
} }
@@ -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 // 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) // except for system commands as this is a special device without any queryable entities (device values)
// exclude SYSTEM and DALLASSENSOR // exclude SYSTEM and DALLASSENSOR
if ((device_type >= EMSdevice::DeviceType::BOILER) && (!value || !strlen(value))) { if ((device_type >= EMSdevice::DeviceType::BOILER) && (!value || !strlen(value))) {
if (!cf || !cf->cmdfunction_json_) { if (!cf || !cf->cmdfunction_json_) {
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)

View File

@@ -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 // 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' // 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) { if (len != 0) {
DeserializationError error = deserializeJson(input, message); DeserializationError error = deserializeJson(input_doc, message);
if (error == DeserializationError::Code::InvalidInput) { if (!input_doc.containsKey("value") || error) {
input.clear(); // this is important to clear first input_doc.clear();
input["value"] = (const char *)message; // always a string input_doc["value"] = (const char *)message; // always a string
} }
} }
// parse and call the command // parse and call the command
StaticJsonDocument<EMSESP_JSON_SIZE_LARGE_DYN> output_doc; input = input_doc.as<JsonObject>();
JsonObject output = output_doc.to<JsonObject>(); output = output_doc.to<JsonObject>();
uint8_t return_code = Command::process(topic, true, input.as<JsonObject>(), output); // mqtt is always authenticated uint8_t return_code = Command::process(topic, true, input, output); // mqtt is always authenticated
if (return_code != CommandRet::OK) { if (return_code != CommandRet::OK) {
char error[100]; char error[100];

View File

@@ -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/system/publish");
EMSESP::mqtt_.incoming("ems-esp/thermostat/seltemp"); // empty payload, sends reponse 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 // MQTT bad tests
EMSESP::mqtt_.incoming("ems-esp/thermostate/mode", "auto"); // unknown device EMSESP::mqtt_.incoming("ems-esp/thermostate/mode", "auto"); // unknown device
EMSESP::mqtt_.incoming("ems-esp/thermostat/modee", "auto"); // unknown command EMSESP::mqtt_.incoming("ems-esp/thermostat/modee", "auto"); // unknown command

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.3.0b4" #define EMSESP_APP_VERSION "3.3.0b5"