Skip to content

Commit

Permalink
Add web page that allows configuration of the latitude, longitude and…
Browse files Browse the repository at this point in the history
… RGB LED type.
  • Loading branch information
bertrik committed Dec 13, 2024
1 parent 001cd17 commit dc4d15e
Show file tree
Hide file tree
Showing 8 changed files with 661 additions and 73 deletions.
108 changes: 108 additions & 0 deletions config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <Arduino.h>
#include <ArduinoJson.h>

#include "config.h"

static FS *_fs;
static StaticJsonDocument < 1024 > _jsonDoc;
static String _config_name;
static String _config_path;
static String _config_page;
static int _version = 0;

static String template_processor(const String & string)
{
// replace according to pattern "propName:propValue?replacement"
int i = string.indexOf("?");
if (i >= 0) {
String property = string.substring(0, i);
String replacement = string.substring(i + 1);
i = property.indexOf(":");
if (i >= 0) {
// split property in name and matching value
String propName = property.substring(0, i);
String propValue = property.substring(i + 1);
String actual = _jsonDoc[propName] | "";
if (actual == propValue) {
return replacement;
}
}
}
// try regular match
return _jsonDoc[string] | "";
}

static void handleGetConfig(AsyncWebServerRequest *request)
{
// read the config file in preparation of presentation in the web UI
config_load();

// serve the config page from flash
request->send(*_fs, _config_page, "text/html", false, template_processor);
}

static void handlePostConfig(AsyncWebServerRequest *request)
{
// put all parameters in the JSON document
for (size_t i = 0; i < request->params(); i++) {
const AsyncWebParameter *param = request->getParam(i);
String name = param->name();
String value = param->value();
_jsonDoc[name] = value;
}

// write the config file
config_save();

// redirect to page with values
request->redirect(_config_path);
}

void config_begin(FS & fs, String config_name)
{
_fs = &fs;
_config_name = config_name; // e.g. "config.json"
}

void config_serve(AsyncWebServer & server, const char *config_path, const char *config_page)
{
_config_path = config_path; // e.g. "/config"
_config_page = config_page; // e.g "/config.html"

// register ourselves with the server
server.on(config_path, HTTP_GET, handleGetConfig);
server.on(config_path, HTTP_POST, handlePostConfig);
}

bool config_load(void)
{
File file = _fs->open(_config_name, "r");
DeserializationError error = deserializeJson(_jsonDoc, file);
file.close();
return error == DeserializationError::Ok;
}

bool config_save(void)
{
File file = _fs->open(_config_name, "w");
size_t size = serializeJson(_jsonDoc, file);
file.close();
_version++;
return size > 0;
}

void config_set_value(String propName, String propValue)
{
_jsonDoc[propName] = propValue;
}

String config_get_value(String propName)
{
return _jsonDoc[propName] | "";
}

int config_get_version(void)
{
return _version;
}

16 changes: 16 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdbool.h>

#include <FS.h>
#include <ESPAsyncWebServer.h>

void config_begin(FS &fs, String config_name);

bool config_load(void);
bool config_save(void);

void config_serve(AsyncWebServer &server, const char *config_path, const char *config_page);

void config_set_value(String propName, String propValue);
String config_get_value(String propName);

int config_get_version(void);
Loading

0 comments on commit dc4d15e

Please sign in to comment.