intermediate: config handler implementation
This commit is contained in:
parent
dad454745a
commit
ec67767ca6
|
@ -0,0 +1,46 @@
|
|||
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into executable file.
|
||||
|
||||
The source code of each library should be placed in a an own separate directory
|
||||
("lib/your_library_name/[here are source files]").
|
||||
|
||||
For example, see a structure of the following two libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
and a contents of `src/main.c`:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
|
@ -0,0 +1,88 @@
|
|||
#include "GWConfig.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define B(v) (v?"true":"false")
|
||||
|
||||
bool isTrue(const char * value){
|
||||
return (strcasecmp(value,"true") == 0);
|
||||
}
|
||||
|
||||
class DummyConfig : public ConfigItem{
|
||||
public:
|
||||
DummyConfig():ConfigItem("dummy",""){}
|
||||
virtual void fromString(const String v){
|
||||
};
|
||||
virtual void reset(){
|
||||
}
|
||||
};
|
||||
|
||||
DummyConfig dummyConfig;
|
||||
String ConfigHandler::toString() const{
|
||||
String rt;
|
||||
rt+="Config: ";
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
rt+=configs[i]->getName();
|
||||
rt+="=";
|
||||
rt+=configs[i]->asString();
|
||||
rt+=", ";
|
||||
}
|
||||
return rt;
|
||||
}
|
||||
|
||||
String ConfigHandler::toJson() const{
|
||||
String rt;
|
||||
DynamicJsonDocument jdoc(50);
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
jdoc[configs[i]->getName()]=configs[i]->asString();
|
||||
}
|
||||
serializeJson(jdoc,rt);
|
||||
return rt;
|
||||
}
|
||||
ConfigItem * ConfigHandler::findConfig(const String name, bool dummy){
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
if (configs[i]->getName() == name) return configs[i];
|
||||
}
|
||||
if (!dummy) return NULL;
|
||||
return &dummyConfig;
|
||||
}
|
||||
|
||||
#define PREF_NAME "gwprefs"
|
||||
ConfigHandler::ConfigHandler(){
|
||||
|
||||
}
|
||||
bool ConfigHandler::loadConfig(){
|
||||
prefs.begin(PREF_NAME,true);
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
String v=prefs.getString(configs[i]->getName().c_str(),configs[i]->getDefault());
|
||||
}
|
||||
prefs.end();
|
||||
return true;
|
||||
}
|
||||
bool ConfigHandler::saveConfig(){
|
||||
prefs.begin(PREF_NAME,false);
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
prefs.putString(configs[i]->getName().c_str(),configs[i]->asString());
|
||||
}
|
||||
prefs.end();
|
||||
return true;
|
||||
}
|
||||
bool ConfigHandler::updateValue(const char *name, const char * value){
|
||||
return false;
|
||||
}
|
||||
bool ConfigHandler::reset(bool save){
|
||||
for (int i=0;i<getNumConfig();i++){
|
||||
configs[i]->reset();
|
||||
}
|
||||
if (!save) return true;
|
||||
return saveConfig();
|
||||
}
|
||||
String ConfigHandler::getString(const String name){
|
||||
ConfigItem *i=findConfig(name);
|
||||
if (!i) return String();
|
||||
return i->asString();
|
||||
}
|
||||
bool ConfigHandler::getBool(const String name){
|
||||
ConfigItem *i=findConfig(name);
|
||||
if (!i) return false;
|
||||
return i->asBoolean();
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
#ifndef _GWCONFIG_H
|
||||
#define _GWCONFIG_H
|
||||
#include <Arduino.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
class ConfigItem{
|
||||
private:
|
||||
String name;
|
||||
String initialValue;
|
||||
String value;
|
||||
public:
|
||||
ConfigItem(const String &name, const String initialValue){
|
||||
this->name=name;
|
||||
this->initialValue=initialValue;
|
||||
this->value=initialValue;
|
||||
}
|
||||
ConfigItem(const String &name, bool initalValue):
|
||||
ConfigItem(name,initalValue?String("true"):String("false")){};
|
||||
String asString() const{
|
||||
return value;
|
||||
}
|
||||
virtual void fromString(const String v){
|
||||
value=v;
|
||||
};
|
||||
bool asBoolean() const{
|
||||
return strcasecmp(value.c_str(),"true") == 0;
|
||||
}
|
||||
String getName() const{
|
||||
return name;
|
||||
}
|
||||
virtual void reset(){
|
||||
value=initialValue;
|
||||
}
|
||||
bool changed() const{
|
||||
return value != initialValue;
|
||||
}
|
||||
String getDefault() const {
|
||||
return initialValue;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class ConfigHandler{
|
||||
private:
|
||||
Preferences prefs;
|
||||
public:
|
||||
public:
|
||||
const String sendUsb="sendUsb";
|
||||
const String receiveUsb="receiveUsb";
|
||||
const String wifiClient="wifiClient";
|
||||
const String wifiPass="wifiPass";
|
||||
const String wifiSSID="wifiSSID";
|
||||
ConfigHandler();
|
||||
bool loadConfig();
|
||||
bool saveConfig();
|
||||
bool updateValue(const char *name, const char * value);
|
||||
bool reset(bool save);
|
||||
String toString() const;
|
||||
String toJson() const;
|
||||
String getString(const String name);
|
||||
bool getBool(const String name);
|
||||
ConfigItem * findConfig(const String name, bool dummy=false);
|
||||
private:
|
||||
ConfigItem* configs[5]={
|
||||
new ConfigItem(sendUsb,true),
|
||||
new ConfigItem (receiveUsb,false),
|
||||
new ConfigItem (wifiClient,false),
|
||||
new ConfigItem (wifiSSID,""),
|
||||
new ConfigItem (wifiPass,"")
|
||||
};
|
||||
int getNumConfig() const{
|
||||
return sizeof(configs)/sizeof(ConfigItem*);
|
||||
}
|
||||
};
|
||||
#endif
|
16
src/main.cpp
16
src/main.cpp
|
@ -37,6 +37,8 @@
|
|||
#include "List.h"
|
||||
#include "BoatData.h"
|
||||
|
||||
#include "GWConfig.h"
|
||||
|
||||
|
||||
|
||||
#define ENABLE_DEBUG_LOG 0 // Debug log, set to 1 to enable AIS forward on USB-Serial / 2 for ADC voltage to support calibration
|
||||
|
@ -49,6 +51,7 @@
|
|||
|
||||
#define WLAN_CLIENT 0 // Set to 1 to enable client network. 0 to act as AP only
|
||||
|
||||
ConfigHandler config;
|
||||
|
||||
// Wifi cofiguration Client and Access Point
|
||||
const char *AP_ssid = "MyESP32"; // ESP32 as AP
|
||||
|
@ -177,12 +180,20 @@ void js_status(){
|
|||
webserver.send(200,F("application/json"),buf);
|
||||
}
|
||||
|
||||
void js_config(){
|
||||
webserver.send(200,F("application/json"),config.toJson());
|
||||
}
|
||||
|
||||
void web_config(){
|
||||
}
|
||||
|
||||
void handleNotFound()
|
||||
{
|
||||
webserver.send(404, "text/plain", "File Not Found\n\n");
|
||||
}
|
||||
|
||||
|
||||
ConfigItem *sendUsb=NULL;
|
||||
|
||||
void setup() {
|
||||
|
||||
|
@ -195,6 +206,9 @@ void setup() {
|
|||
// Init USB serial port
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting...");
|
||||
config.loadConfig();
|
||||
Serial.println(config.toString());
|
||||
sendUsb=config.findConfig(config.sendUsb,true);
|
||||
// Init AIS serial port 2
|
||||
//Serial2.begin(baudrate, rs_config);
|
||||
//NMEA0183.Begin(&Serial2, 3, baudrate);
|
||||
|
@ -330,7 +344,7 @@ void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg) {
|
|||
char buf[MAX_NMEA0183_MESSAGE_SIZE];
|
||||
if ( !NMEA0183Msg.GetMessage(buf, MAX_NMEA0183_MESSAGE_SIZE) ) return;
|
||||
SendBufToClients(buf);
|
||||
if (NMEA_TO_SERIAL){
|
||||
if (sendUsb->asBoolean()){
|
||||
Serial.println(buf);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue