forked from ct-Open-Source/Basecamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfiguration.hpp
106 lines (86 loc) · 2.93 KB
/
Configuration.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Basecamp - ESP32 library to simplify the basics of IoT projects
Written by Merlin Schumacher (mls@ct.de) for c't magazin für computer technik (https://www.ct.de)
Licensed under GPLv3. See LICENSE for details.
*/
#ifndef Configuration_h
#define Configuration_h
#include "debug.hpp"
#include <sstream>
#include <list>
#include <map>
#include <ArduinoJson.h>
#include <SPIFFS.h>
// TODO: Extend with all known keys
enum class ConfigurationKey {
accessPointSecret,
};
// TODO: Extend with all known keys
static const String getKeyName(ConfigurationKey key)
{
// This automatically will break the compiler if a known key has been forgotten
// (if the warnings are turned on exactly...)
switch (key)
{
case ConfigurationKey::accessPointSecret:
return "APSecret";
break;
}
return "";
}
class Configuration {
public:
// Default constructor: Memory-only configuration (NO EEPROM read/writes
Configuration();
// Constructor with filename: Can be read from and written to EEPROM
explicit Configuration(String filename);
~Configuration() = default;
// Switched configuration to memory-only and empties filename
void setMemOnly();
// Sets new filename and removes memory-only tag
void setFileName(const String& filename);
// Returns memory-only state of configuration
bool isMemOnly() {return _memOnlyConfig;}
const String& getKey(ConfigurationKey configKey) const;
// Both functions return true on successful load or save. Return false on any failure. Also return false for memory-only configurations.
bool load();
bool save();
void dump();
// Returns true if the key 'key' exists
bool keyExists(const String& key) const;
// Returns true if the key 'key' exists
bool keyExists(ConfigurationKey key) const;
// Returns true if the key 'key' exists and is not empty
bool isKeySet(ConfigurationKey key) const;
// Reset the whole configuration
void reset();
// Reset everything except the AP secret
void resetExcept(const std::list<ConfigurationKey> &keysToPreserve);
// FIXME: Get rid of every direct access ("name") set() and get()
// to minimize the rist of unknown-key usage. Move to private.
void set(String key, String value);
// FIXME: use this instead
void set(ConfigurationKey key, String value);
// FIXME: Get rid of every direct access ("name") set() and get()
// to minimize the rist of unknown-key usage. Move to private.
const String& get(String key) const;
// FIXME: use this instead
const String& get(ConfigurationKey key) const;
char* getCString(String key);
struct cmp_str
{
bool operator()(const String &a, const String &b) const
{
return strcmp(a.c_str(), b.c_str()) < 0;
}
};
std::map<String, String, cmp_str> configuration;
private:
static void CheckConfigStatus(void *);
String _jsonFile;
bool _configurationTainted = false;
String noResult_ = {};
// Set to true if configuration is memory-only
bool _memOnlyConfig;
};
#endif