config set from the web

master
Kenneth Barbour 2020-06-10 22:24:38 -04:00
parent c9ba9f6bb9
commit 9ed4f3f07c
3 changed files with 63 additions and 9 deletions

View File

@ -8,6 +8,8 @@ const uint8_t CONFIG_SIG[2] = {0xAB, 0xCD};
const uint8_t CONFIG_VERSION = 1;
uint8_t CONFIG_EEPROM_ADDR = 0;
const uint8_t CONFIG_FLAG_HTTP = 1 << 0;
void cUWeatherConfig::begin()
{
if (!load()) {
@ -66,12 +68,13 @@ size_t cUWeatherConfig::print(Stream& out)
size_t len = 0;
sprintf((char*)buff,
"{\"station_id\": \"%s\", \"mdns_hostname\": \"%s\", \"ntp_host\": \"%s\", \"mqtt_host\": \"%s\", \"enable_http\": %s }",
"{\"station_id\": \"%s\", \"mdns_hostname\": \"%s\", \"ntp_host\": \"%s\", \"mqtt_host\": \"%s\", \"mqtt_prefix\": \"%s\", \"enable_http\": %s }",
(char*) _data.station_id,
(char*) _data.mdns_hostname,
(char*) _data.ntp_host,
(char*) _data.mqtt_host,
(_data.flags && 1) ?"true":"false"
(char*) _data.mqtt_prefix,
(_data.flags & CONFIG_FLAG_HTTP) ?"true":"false"
);
len += out.print((char*) buff);
return len;
@ -105,3 +108,27 @@ bool cUWeatherConfig::setMQTTHost(const char* buff)
return true;
}
bool cUWeatherConfig::setMQTTPrefix(const char* buff)
{
if (strlen(buff) >= 32) return false;
strncpy((char*)_data.mqtt_prefix, buff, 32);
return true;
}
bool cUWeatherConfig::enableHttp(bool enable)
{
if (enable) {
Serial.println("Enable HTTP");
_data.flags |= CONFIG_FLAG_HTTP;
} else {
Serial.println("Disable HTTP");
_data.flags &= ~(CONFIG_FLAG_HTTP);
}
return true;
}
bool cUWeatherConfig::isHttpEnabled() {
return (_data.flags & CONFIG_FLAG_HTTP);
}

View File

@ -10,6 +10,7 @@ typedef struct uweather_config_t {
uint8_t mdns_hostname[16];
uint8_t ntp_host[32];
uint8_t mqtt_host[32];
uint8_t mqtt_prefix[32];
uint8_t flags;
};
@ -31,6 +32,7 @@ public:
bool setMDNSHostname(const char*);
bool setNTPHost(const char*);
bool setMQTTHost(const char*);
bool setMQTTPrefix(const char*);
};
extern cUWeatherConfig Config;

View File

@ -136,15 +136,40 @@ void handle_config(HttpRequest& req, HttpResponse& res)
void handle_config_set(HttpRequest& req, HttpResponse& res)
{
if (req.getMessageLength() > 128)
return client_error("Message is too long", res);
const char* key = req.getUrl()+8;
Serial.println(req.getMessage());
if (strcmp("station_id", key)) {
Serial.println("Saving station id");
// strncpy(config.station_id, req.getMessage(), 15);
//config_save();
char value[32]; // max size of config param
// value may have quotes - remove them
const char* val_start = req.getMessage();
size_t val_len = strlen(val_start);
if (val_len > 32)
return client_error("Message is too long", res);
if (val_start[0] == '"') {
val_len -= 2;
val_start++;
}
strncpy(value, val_start, val_len);
value[val_len] = '\0';
bool result = false;
if (strcmp("station_id", key) == 0) {
result = Config.setStationId(value);
} else if (strcmp("mdns_hostname", key) == 0) {
result = Config.setMDNSHostname(value);
} else if (strcmp("ntp_host", key) == 0) {
result = Config.setNTPHost(value);
} else if (strcmp("mqtt_host", key) == 0) {
result = Config.setMQTTHost(value);
} 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));
}
if (!result)
return client_error("Unable to set parameter", res);
content.print("What was I supposed to return, again?");
}
/**