more fixes - Refactor MQTT subscriptions #173

This commit is contained in:
proddy
2021-11-02 18:15:57 +01:00
parent 131b936a69
commit e1419edb15
6 changed files with 55 additions and 18 deletions

View File

@@ -18,7 +18,10 @@ upload_flags =
--auth=ems-esp-neo --auth=ems-esp-neo
upload_port = ems-esp.local upload_port = ems-esp.local
extra_scripts = scripts/rename_fw.py ; don't build WebUI just firmware ; use this when you don't want to re-build the WebUI
extra_scripts =
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

@@ -106,13 +106,21 @@ uint8_t Command::process(const char * path, const bool authenticated, const Json
// some commands may be prefixed with hc. or wwc. so extract these if they exist // some commands may be prefixed with hc. or wwc. so extract these if they exist
// parse_command_string returns the extracted command // parse_command_string returns the extracted command
// exit if we don't have a command
command_p = parse_command_string(command_p, id_n); command_p = parse_command_string(command_p, id_n);
if (command_p == nullptr) { if (command_p == nullptr) {
// default to info or values, only for device endpoints like api/system or api/boiler
if (num_paths < 3) {
if (device_type == EMSdevice::DeviceType::SYSTEM) {
command_p = "info";
} else {
command_p = "values";
}
} else {
output.clear(); output.clear();
output["message"] = "missing or bad command"; output["message"] = "missing or bad command";
return CommandRet::NOT_FOUND; return CommandRet::NOT_FOUND;
} }
}
// if we don't have an id/hc/wwc try and get it from the JSON input // if we don't have an id/hc/wwc try and get it from the JSON input
// it's allowed to have no id, and then keep the default to -1 // it's allowed to have no id, and then keep the default to -1
@@ -237,6 +245,8 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * value, bool authenticated, const int8_t id, JsonObject & output) { uint8_t Command::call(const uint8_t device_type, const char * cmd, const char * value, bool authenticated, const int8_t id, JsonObject & output) {
uint8_t return_code = CommandRet::OK; uint8_t return_code = CommandRet::OK;
std::string dname = EMSdevice::device_type_2_device_name(device_type);
// see if there is a command registered // see if there is a command registered
auto cf = find_command(device_type, cmd); auto cf = find_command(device_type, cmd);
@@ -244,13 +254,13 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
// 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)
if ((device_type != EMSdevice::DeviceType::SYSTEM) && (!value || !strlen(value))) { if ((device_type != EMSdevice::DeviceType::SYSTEM) && (!value || !strlen(value))) {
if (!cf || !cf->cmdfunction_json_) { if (!cf || !cf->cmdfunction_json_) {
LOG_INFO(F("Calling %s command '%s' to retrieve values"), dname.c_str(), cmd);
return EMSESP::get_device_value_info(output, cmd, id, device_type) ? CommandRet::OK : CommandRet::ERROR; // entity = cmd return EMSESP::get_device_value_info(output, cmd, id, device_type) ? CommandRet::OK : CommandRet::ERROR; // entity = cmd
} }
} }
if (cf) { if (cf) {
// we have a matching command // we have a matching command
std::string dname = EMSdevice::device_type_2_device_name(device_type);
if ((value == nullptr) || !strlen(value)) { if ((value == nullptr) || !strlen(value)) {
LOG_INFO(F("Calling %s command '%s'"), dname.c_str(), cmd); LOG_INFO(F("Calling %s command '%s'"), dname.c_str(), cmd);
} else if (id == -1) { } else if (id == -1) {

View File

@@ -248,7 +248,7 @@ class EMSdevice {
const std::string get_value_uom(const char * key); const std::string get_value_uom(const char * key);
bool get_value_info(JsonObject & root, const char * cmd, const int8_t id); bool get_value_info(JsonObject & root, const char * cmd, const int8_t id);
enum OUTPUT_TARGET : uint8_t { API_VERBOSE, API_SHORT, MQTT }; enum OUTPUT_TARGET : uint8_t { API_VERBOSE, API_SHORTNAMES, MQTT };
bool generate_values_json(JsonObject & output, const uint8_t tag_filter, const bool nested, const uint8_t output_target); bool generate_values_json(JsonObject & output, const uint8_t tag_filter, const bool nested, const uint8_t output_target);
void generate_values_json_web(JsonObject & output); void generate_values_json_web(JsonObject & output);

View File

@@ -998,9 +998,9 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, std::
F_(info_cmd)); F_(info_cmd));
Command::add( Command::add(
device_type, device_type,
F("list"), F("values"),
[device_type](const char * value, const int8_t id, JsonObject & output) { [device_type](const char * value, const int8_t id, JsonObject & output) {
return command_info(device_type, output, id, EMSdevice::OUTPUT_TARGET::API_SHORT); // HIDDEN command showing short names, used in e.g. /api/boiler return command_info(device_type, output, id, EMSdevice::OUTPUT_TARGET::API_SHORTNAMES); // HIDDEN command showing short names, used in e.g. /api/boiler
}, },
nullptr, nullptr,
CommandFlag::HIDDEN); // this command is hidden CommandFlag::HIDDEN); // this command is hidden

View File

@@ -508,6 +508,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
return; return;
*/ */
/*
request2.url("/api/thermostat"); // check if defaults to info request2.url("/api/thermostat"); // check if defaults to info
EMSESP::webAPIService.webAPIService_get(&request2); EMSESP::webAPIService.webAPIService_get(&request2);
request2.url("/api/thermostat/info"); request2.url("/api/thermostat/info");
@@ -519,6 +520,33 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
request2.url("/api/thermostat/mode"); request2.url("/api/thermostat/mode");
EMSESP::webAPIService.webAPIService_get(&request2); EMSESP::webAPIService.webAPIService_get(&request2);
return; return;
*/
request2.url("/api/system"); // check if defaults to info
EMSESP::webAPIService.webAPIService_get(&request2);
emsesp::EMSESP::logger().notice("*");
request2.url("/api/system/info");
EMSESP::webAPIService.webAPIService_get(&request2);
emsesp::EMSESP::logger().notice("*");
request2.url("/api/thermostat"); // check if defaults to values
EMSESP::webAPIService.webAPIService_get(&request2);
emsesp::EMSESP::logger().notice("*");
request2.url("/api/thermostat/info");
EMSESP::webAPIService.webAPIService_get(&request2);
emsesp::EMSESP::logger().notice("*");
request2.url("/api/thermostat/seltemp");
EMSESP::webAPIService.webAPIService_get(&request2);
emsesp::EMSESP::logger().notice("****");
request2.url("/api/dallassensor/fdfd");
EMSESP::webAPIService.webAPIService_get(&request2);
request2.url("/api/dallassensor/info");
EMSESP::webAPIService.webAPIService_get(&request2);
return;
/* /*
AsyncWebServerRequest request2; AsyncWebServerRequest request2;
@@ -595,7 +623,6 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
Mqtt::base("home/cellar/heating"); Mqtt::base("home/cellar/heating");
EMSESP::mqtt_.incoming("home/cellar/heating/thermostat/mode"); // empty payload, sends reponse EMSESP::mqtt_.incoming("home/cellar/heating/thermostat/mode"); // empty payload, sends reponse
#if defined(EMSESP_STANDALONE) #if defined(EMSESP_STANDALONE)
// Web API TESTS // Web API TESTS
AsyncWebServerRequest request; AsyncWebServerRequest request;
@@ -673,6 +700,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
request.url("/rest/writeValue"); request.url("/rest/writeValue");
EMSESP::webDataService.write_value(&request, json); EMSESP::webDataService.write_value(&request, json);
emsesp::EMSESP::logger().notice("*");
// should fail // should fail
char data8[] = "{}"; char data8[] = "{}";
deserializeJson(doc, data8); deserializeJson(doc, data8);

View File

@@ -62,13 +62,8 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject & input) {
auto method = request->method(); auto method = request->method();
bool authenticated = false; bool authenticated = false;
if (method == HTTP_GET) {
// special case if there is no command, then default to 'list' which is like info but showing short names
if (!input.size()) {
input["cmd"] = "list";
}
} else {
// if its a POST then check authentication // if its a POST then check authentication
if (method != HTTP_GET) {
EMSESP::webSettingsService.read([&](WebSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
Authentication authentication = _securityManager->authenticateRequest(request); Authentication authentication = _securityManager->authenticateRequest(request);
authenticated = settings.notoken_api | AuthenticationPredicates::IS_ADMIN(authentication); authenticated = settings.notoken_api | AuthenticationPredicates::IS_ADMIN(authentication);