This commit is contained in:
MichaelDvP
2024-07-26 18:30:14 +02:00
13 changed files with 87 additions and 72 deletions

View File

@@ -26,7 +26,7 @@ uuid::log::Logger Command::logger_{F_(command), uuid::log::Facility::DAEMON};
std::vector<Command::CmdFunction> Command::cmdfunctions_;
// takes a path and a json body, parses the data and calls the command
// takes a URI path and a json body, parses the data and calls the command
// the path is leading so if duplicate keys are in the input JSON it will be ignored
// the entry point will be either via the Web API (api/) or MQTT (<base>/)
// returns a return code and json output
@@ -34,22 +34,11 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
SUrlParser p; // parse URL for the path names
p.parse(path);
if (!p.paths().size()) {
return json_message(CommandRet::ERROR, "invalid path", output);
}
// check first if it's from API, if so strip the "api/"
if (p.paths().front() == "api") {
// check first if it's from API or MQTT, if so strip the "api/" or "<base>/" from the path
if (p.paths().size() && ((p.paths().front() == "api") || (p.paths().front() == Mqtt::base()))) {
p.paths().erase(p.paths().begin());
} else {
// not /api, so must be MQTT path. Check for base and remove it.
if (!strncmp(path, Mqtt::base().c_str(), Mqtt::base().length())) {
char new_path[Mqtt::MQTT_TOPIC_MAX_SIZE];
strlcpy(new_path, path, sizeof(new_path));
p.parse(new_path + Mqtt::base().length() + 1); // re-parse the stripped path
} else {
return json_message(CommandRet::ERROR, "unrecognized path", output); // error
}
return json_message(CommandRet::ERROR, "invalid path", output, path); // error
}
// re-calculate new path
@@ -701,15 +690,21 @@ void Command::show_all(uuid::console::Shell & shell) {
shell.println();
}
uint8_t Command::json_message(uint8_t error_code, const char * message, const JsonObject output) {
// creates a single json document with an error message
// object is optional
uint8_t Command::json_message(uint8_t error_code, const char * message, const JsonObject output, const char * object) {
output.clear();
output["message"] = message;
if (object) {
output["message"] = std::string(message) + " " + object;
} else {
output["message"] = message;
}
return error_code;
}
//
// SUrlParser class
//
// **************************
// **** SUrlParser class ****
// **************************
// Extract only the path component from the passed URI and normalized it
// e.g. //one/two////three/// becomes /one/two/three

View File

@@ -143,7 +143,7 @@ class Command {
static std::vector<CmdFunction> cmdfunctions_; // the list of commands
static uint8_t json_message(uint8_t error_code, const char * message, const JsonObject output);
static uint8_t json_message(uint8_t error_code, const char * message, JsonObject output, const char * object = nullptr);
};
class SUrlParser {

View File

@@ -580,7 +580,7 @@ static void setup_commands(std::shared_ptr<Commands> & commands) {
return;
} else if (!has_data) {
// show message if no data returned (e.g. for analogsensor, temperaturesensor, custom)
shell.println("Command executed. Check log for messages.");
shell.println("Command executed");
return;
} else {
return;

View File

@@ -438,7 +438,7 @@ ModbusMessage Modbus::handleWrite(const ModbusMessage & request) {
if (output.size()) {
snprintf(error,
sizeof(error),
"Modbus write command failed with error: %s (%s)",
"Modbus write command failed with error %s (%s)",
(const char *)output["message"],
Command::return_code_string(return_code));
} else {

View File

@@ -228,9 +228,9 @@ void Mqtt::on_message(const char * topic, const uint8_t * payload, size_t len) {
#if defined(EMSESP_DEBUG)
if (len) {
LOG_DEBUG("Received topic `%s` => payload `%s` (length %d)", topic, message, len);
LOG_DEBUG("Received topic %s => payload '%s' (length %d)", topic, message, len);
} else {
LOG_DEBUG("Received topic `%s`", topic);
LOG_DEBUG("Received topic %s", topic);
}
#endif
// remove HA topics if we don't use discovery

View File

@@ -1172,7 +1172,9 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
test("boiler");
test("thermostat");
EMSESP::mqtt_.incoming("ems-esp/boiler/wwseltemp", "59");
EMSESP::mqtt_.incoming("ems-esp/boiler/seltemp", "59");
EMSESP::mqtt_.incoming("badems-esp/boiler/seltemp", "59"); // should fail
ok = true;
}

View File

@@ -32,7 +32,6 @@ WebSettingsService::WebSettingsService(AsyncWebServer * server, FS * fs, Securit
}
void WebSettings::read(WebSettings & settings, JsonObject root) {
root["version"] = settings.version;
root["locale"] = settings.locale;
root["tx_mode"] = settings.tx_mode;
root["ems_bus_id"] = settings.ems_bus_id;

View File

@@ -24,9 +24,9 @@
namespace emsesp {
// /rest/HardwareStatus
// /rest/hardwareStatus
WebStatusService::WebStatusService(AsyncWebServer * server, SecurityManager * securityManager) {
server->on(HARDWARE_STATUS_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { HardwareStatus(request); });
server->on(HARDWARE_STATUS_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { hardwareStatus(request); });
server->on(SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { systemStatus(request); });
}
@@ -85,7 +85,7 @@ void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
request->send(response);
}
void WebStatusService::HardwareStatus(AsyncWebServerRequest * request) {
void WebStatusService::hardwareStatus(AsyncWebServerRequest * request) {
EMSESP::system_.refreshHeapMem(); // refresh free heap and max alloc heap
auto * response = new AsyncJsonResponse(false);
@@ -121,6 +121,7 @@ void WebStatusService::HardwareStatus(AsyncWebServerRequest * request) {
root["fs_free"] = EMSESP::system_.FStotal() - FSused;
root["free_caps"] = heap_caps_get_free_size(MALLOC_CAP_8BIT) / 1024; // includes heap and psram
root["psram"] = EMSESP::system_.PSram();
if (EMSESP::system_.PSram()) {
root["psram_size"] = EMSESP::system_.PSram();
root["free_psram"] = ESP.getFreePsram() / 1024;

View File

@@ -1,7 +1,7 @@
#ifndef WebStatusService_h
#define WebStatusService_h
#define HARDWARE_STATUS_SERVICE_PATH "/rest/HardwareStatus"
#define HARDWARE_STATUS_SERVICE_PATH "/rest/hardwareStatus"
#define SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus"
namespace emsesp {
@@ -12,7 +12,7 @@ class WebStatusService {
private:
void systemStatus(AsyncWebServerRequest * request);
void HardwareStatus(AsyncWebServerRequest * request);
void hardwareStatus(AsyncWebServerRequest * request);
};
} // namespace emsesp