Skip to content

Commit

Permalink
more fixes - Refactor MQTT subscriptions #173
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Nov 2, 2021
1 parent 131b936 commit e1419ed
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
5 changes: 4 additions & 1 deletion pio_local.ini_example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ upload_flags =
--auth=ems-esp-neo
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
; or from Visual Studio Code do PIO -> Project Tasks -> debug -> General -> Upload and Monitor
Expand Down
20 changes: 15 additions & 5 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,20 @@ 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
// parse_command_string returns the extracted command
// exit if we don't have a command
command_p = parse_command_string(command_p, id_n);
if (command_p == nullptr) {
output.clear();
output["message"] = "missing or bad command";
return CommandRet::NOT_FOUND;
// 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["message"] = "missing or bad command";
return CommandRet::NOT_FOUND;
}
}

// if we don't have an id/hc/wwc try and get it from the JSON input
Expand Down Expand Up @@ -237,20 +245,22 @@ 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 return_code = CommandRet::OK;

std::string dname = EMSdevice::device_type_2_device_name(device_type);

// see if there is a command registered
auto cf = find_command(device_type, cmd);

// 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)
if ((device_type != EMSdevice::DeviceType::SYSTEM) && (!value || !strlen(value))) {
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
}
}

if (cf) {
// we have a matching command
std::string dname = EMSdevice::device_type_2_device_name(device_type);
if ((value == nullptr) || !strlen(value)) {
LOG_INFO(F("Calling %s command '%s'"), dname.c_str(), cmd);
} else if (id == -1) {
Expand Down
2 changes: 1 addition & 1 deletion src/emsdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class EMSdevice {
const std::string get_value_uom(const char * key);
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);
void generate_values_json_web(JsonObject & output);

Expand Down
4 changes: 2 additions & 2 deletions src/emsesp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,9 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, std::
F_(info_cmd));
Command::add(
device_type,
F("list"),
F("values"),
[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,
CommandFlag::HIDDEN); // this command is hidden
Expand Down
33 changes: 31 additions & 2 deletions src/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
AsyncWebServerRequest request2;
request2.method(HTTP_GET);

// request2.url("/api/thermostat/seltemp");
// request2.url("/api/thermostat/seltemp");
// EMSESP::webAPIService.webAPIService_get(&request2);
// return;

Expand All @@ -508,6 +508,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
return;
*/

/*
request2.url("/api/thermostat"); // check if defaults to info
EMSESP::webAPIService.webAPIService_get(&request2);
request2.url("/api/thermostat/info");
Expand All @@ -519,6 +520,33 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
request2.url("/api/thermostat/mode");
EMSESP::webAPIService.webAPIService_get(&request2);
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;
Expand Down Expand Up @@ -595,7 +623,6 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
Mqtt::base("home/cellar/heating");
EMSESP::mqtt_.incoming("home/cellar/heating/thermostat/mode"); // empty payload, sends reponse


#if defined(EMSESP_STANDALONE)
// Web API TESTS
AsyncWebServerRequest request;
Expand Down Expand Up @@ -673,6 +700,8 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd) {
request.url("/rest/writeValue");
EMSESP::webDataService.write_value(&request, json);

emsesp::EMSESP::logger().notice("*");

// should fail
char data8[] = "{}";
deserializeJson(doc, data8);
Expand Down
9 changes: 2 additions & 7 deletions src/web/WebAPIService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,8 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject & input) {
auto method = request->method();
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) {
Authentication authentication = _securityManager->authenticateRequest(request);
authenticated = settings.notoken_api | AuthenticationPredicates::IS_ADMIN(authentication);
Expand Down

0 comments on commit e1419ed

Please sign in to comment.