web config endpoints cleaned up; web headers include version info
parent
9ed4f3f07c
commit
492ced86c8
|
@ -25,7 +25,7 @@ void cUWeatherConfig::loadDefaults()
|
|||
_data.sig[0] = CONFIG_SIG[0];
|
||||
_data.sig[1] = CONFIG_SIG[1];
|
||||
_data.version = CONFIG_VERSION;
|
||||
_data.flags = 1; //TODO what does this mean;
|
||||
_data.flags = CONFIG_FLAG_HTTP;
|
||||
}
|
||||
|
||||
bool cUWeatherConfig::load()
|
||||
|
|
|
@ -10,3 +10,8 @@ size_t uweather_print_version_info(Stream& out)
|
|||
{
|
||||
return out.printf_P(PSTR("uWeather Firmware version %s\r\n"), UWEATHER_VERSION);
|
||||
}
|
||||
|
||||
const char* uweather_get_version_str()
|
||||
{
|
||||
return UWEATHER_VERSION;
|
||||
}
|
|
@ -4,5 +4,6 @@
|
|||
#include <Arduino.h>
|
||||
|
||||
size_t uweather_print_version_info(Stream&);
|
||||
const char* uweather_get_version_str();
|
||||
|
||||
#endif /* _UWEATHER_VERSION_H_ include guard */
|
||||
|
|
25
src/web.cpp
25
src/web.cpp
|
@ -5,6 +5,7 @@
|
|||
#include <FS.h>
|
||||
#include <QueryString.h>
|
||||
#include "config.h"
|
||||
#include "version.h"
|
||||
|
||||
/**
|
||||
* How long to wait after the OK to shutdown before no longer
|
||||
|
@ -33,6 +34,7 @@ void web_init(HttpRequest& req, HttpResponse& res)
|
|||
content.clear(); // clear content buffer
|
||||
res.content = &content; // response content buffer
|
||||
Serial.printf("[%s] %s ", req.getMethod(), req.getUrl());
|
||||
res.headers.set("X-UWeather-Version", uweather_get_version_str());
|
||||
}
|
||||
|
||||
// Terminate Connection
|
||||
|
@ -129,6 +131,9 @@ void handle_wifi_state(HttpRequest& req, HttpResponse& res)
|
|||
|
||||
void handle_config(HttpRequest& req, HttpResponse& res)
|
||||
{
|
||||
char version_buff[10];
|
||||
sprintf(version_buff, "version=%d;", Config._data.version);
|
||||
res.headers.set("X-UWeather-Config", version_buff);
|
||||
res.headers.set("Content-Type","application/json");
|
||||
Config.print(content);
|
||||
return;
|
||||
|
@ -152,6 +157,7 @@ void handle_config_set(HttpRequest& req, HttpResponse& res)
|
|||
value[val_len] = '\0';
|
||||
|
||||
bool result = false;
|
||||
bool is_str = true;
|
||||
if (strcmp("station_id", key) == 0) {
|
||||
result = Config.setStationId(value);
|
||||
} else if (strcmp("mdns_hostname", key) == 0) {
|
||||
|
@ -163,13 +169,28 @@ void handle_config_set(HttpRequest& req, HttpResponse& res)
|
|||
} else if (strcmp("mqtt_prefix", key) == 0) {
|
||||
result = Config.setMQTTPrefix(value);
|
||||
} else if (strcmp("enable_http", key) == 0) {
|
||||
result = Config.enableHttp((value[0] == '1' || strcmp(value, "true") == 0 || strcmp(value, "Enabled") == 0));
|
||||
is_str = false;
|
||||
if (value[0] == '1' || strcmp(value, "true") == 0 || strcmp(value, "Enabled") == 0) {
|
||||
strcpy(value, "true");
|
||||
result = Config.enableHttp(true);
|
||||
} else {
|
||||
strcpy(value, "false");
|
||||
result = Config.enableHttp(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (!result)
|
||||
return client_error("Unable to set parameter", res);
|
||||
|
||||
content.print("What was I supposed to return, again?");
|
||||
// return json object with property
|
||||
char buff[64];
|
||||
if (is_str) {
|
||||
sprintf(buff, "{\"%s\": \"%s\"}", key, value);
|
||||
} else {
|
||||
sprintf(buff, "{\"%s\": %s}", key, value);
|
||||
}
|
||||
res.headers.set("Content-Type", "application/json");
|
||||
content.print(buff);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue