This repository has been archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathConfiguration.hpp
149 lines (118 loc) · 3.7 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Basecamp - ESP32 library to simplify the basics of IoT projects
Written by Merlin Schumacher ([email protected]) 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 {
deviceName,
accessPointSecret,
wifiConfigured,
wifiEssid,
wifiPassword,
mqttActive,
mqttHost,
mqttPort,
mqttUser,
mqttPass,
otaActive,
otaPass,
};
// 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::deviceName:
return "DeviceName";
case ConfigurationKey::accessPointSecret:
return "APSecret";
case ConfigurationKey::wifiConfigured:
return "WifiConfigured";
case ConfigurationKey::wifiEssid:
return "WifiEssid";
case ConfigurationKey::wifiPassword:
return "WifiPassword";
case ConfigurationKey::mqttActive:
return "MQTTActive";
case ConfigurationKey::mqttHost:
return "MQTTHost";
case ConfigurationKey::mqttPort:
return "MQTTPort";
case ConfigurationKey::mqttUser:
return "MQTTUser";
case ConfigurationKey::mqttPass:
return "MQTTPass";
case ConfigurationKey::otaActive:
return "OTAActive";
case ConfigurationKey::otaPass:
return "OTAPass";
}
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