Skip to content

Commit

Permalink
Make ConfigurableResult an enum class and rename the values
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Jul 25, 2024
1 parent 1b53c3d commit 8549f60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
34 changes: 20 additions & 14 deletions src/sensesp/net/web/config_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ void add_config_get_handler(HTTPServer* server) {
url_tail = String(path_cstr);

ESP_LOGV("ConfigHandler", "URL parts: %s, %s", path.c_str(),
query.c_str()
);
query.c_str());

if (path.length() == 0) {
// return a list of all Configurable objects
Expand All @@ -78,20 +77,28 @@ void add_config_get_handler(HTTPServer* server) {
// - schema, optional: the configuration schema
// - description, optional: the configuration description

ConfigurableResult status = ConfigurableResult::kConfigOk;
ConfigurableResult status = ConfigurableResult::kOk;

if (confable->is_async()) {
if (query == "result") {
if (query.startsWith("poll_get_result")) {
// Poll the status of a previous async GET (get) request
status = confable->poll_get_result(config);
if (status == ConfigurableResult::kPending) {
// Set result code 202
httpd_resp_set_status(req, "202 Accepted");
}
} else
if (query.startsWith("poll_put_result")) {
// Poll the status of a previous async PUT (set) request
status = confable->poll_set_result();
if (status == ConfigurableResult::kConfigPending) {
if (status == ConfigurableResult::kPending) {
// Set result code 202
httpd_resp_set_status(req, "202 Accepted");
}
} else {
// Initiate an async GET (get) request
status = confable->async_get_configuration();
if (status == ConfigurableResult::kConfigOk) {
if (status == ConfigurableResult::kOk) {
// Return the resulting config object immediately
confable->poll_get_result(config);
} else {
Expand All @@ -104,21 +111,22 @@ void add_config_get_handler(HTTPServer* server) {
doc["status"] = poll_status_strings[static_cast<int>(status)];
} else {
// Synchronous GET (get) request
if (query == "result") {
if (query.startsWith("poll")) {
// Polling is not supported for synchronous requests
httpd_resp_send_err(
req, HTTPD_400_BAD_REQUEST,
"Polling is not supported for synchronous requests");
}

doc["status"] = poll_status_strings[static_cast<int>(
ConfigurableResult::kConfigOk)];
confable->get_configuration(config);
doc["status"] =
poll_status_strings[static_cast<int>(ConfigurableResult::kOk)];

doc["schema"] = serialized(confable->get_config_schema());
doc["description"] = confable->get_description();
}

confable->get_configuration(config);

String response;
serializeJson(doc, response);
ESP_LOGV("ConfigHandler", "Response: %s", response.c_str());
Expand Down Expand Up @@ -185,16 +193,14 @@ void add_config_put_handler(HTTPServer* server) {
// Initiate an async PUT (set) request
ConfigurableResult result =
confable->async_set_configuration(doc.as<JsonObject>());
if (result == ConfigurableResult::kConfigError) {
if (result == ConfigurableResult::kError) {
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
"Error setting configuration");
return ESP_FAIL;
}
confable->save_configuration();
String result_string = poll_status_strings[static_cast<int>(result)];
String poll_url = "/api/config" + url_tail + "?result";
response = "{\"status\":\"" + result_string + "\",\"poll\":\"" +
poll_url + "\"}";
response = "{\"status\":\"" + result_string + "\"}";
// Set result code 202
httpd_resp_set_status(req, "202 Accepted");
} else {
Expand Down
18 changes: 9 additions & 9 deletions src/sensesp/system/configurable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

#include "Arduino.h"

enum ConfigurableResult {
kConfigError,
kConfigOk,
kConfigPending,
enum class ConfigurableResult {
kError,
kOk,
kPending,
};

namespace sensesp {
Expand Down Expand Up @@ -74,7 +74,7 @@ class Configurable {
*/
virtual ConfigurableResult async_get_configuration() {
ESP_LOGE("Configurable", "async_get_configuration not implemented");
return ConfigurableResult::kConfigError;
return ConfigurableResult::kError;
}

/**
Expand All @@ -83,9 +83,9 @@ class Configurable {
* Override to implement support for polling.
*
*/
virtual ConfigurableResult poll_get_result(JsonObject& config_object) {
virtual ConfigurableResult poll_get_result(JsonObject& config) {
ESP_LOGE("Configurable", "poll_get_result not implemented");
return ConfigurableResult::kConfigError;
return ConfigurableResult::kError;
}

/**
Expand All @@ -104,7 +104,7 @@ class Configurable {
*/
virtual ConfigurableResult async_set_configuration(const JsonObject& config) {
ESP_LOGE("Configurable", "async_set_configuration not implemented");
return ConfigurableResult::kConfigError;
return ConfigurableResult::kError;
}

/**
Expand All @@ -115,7 +115,7 @@ class Configurable {
*/
virtual ConfigurableResult poll_set_result() {
ESP_LOGE("Configurable", "poll_set_result not implemented");
return ConfigurableResult::kConfigError;
return ConfigurableResult::kError;
}

/**
Expand Down

0 comments on commit 8549f60

Please sign in to comment.