-
Notifications
You must be signed in to change notification settings - Fork 2
/
WifiConfig.h
77 lines (60 loc) · 1.53 KB
/
WifiConfig.h
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
#ifndef PM25_NODEMCU_WIFICONFIG_H
#define PM25_NODEMCU_WIFICONFIG_H
#include <vector>
class WifiCreds
{
public:
String ssid();
String pass();
WifiCreds(String ssid, String pass);
private:
String NetworkSSID;
String NetworkPASS;
};
String WifiCreds::ssid() {
return NetworkSSID;
}
String WifiCreds::pass() {
return NetworkPASS;
}
WifiCreds::WifiCreds(String ssid, String pass) {
this->NetworkSSID = ssid;
this->NetworkPASS = pass;
}
class WifiConfig : public JsonConfig
{
public:
std::vector<WifiCreds> creds;
WifiConfig();
private:
void parseConfig(String conf);
};
WifiConfig::WifiConfig() {
String conf = this->readConfigFile(WIFI_CREDENTIALS);
if (conf.length() > 0) {
this->parseConfig(conf);
} else {
return;
}
}
void WifiConfig::parseConfig(String conf) {
DynamicJsonBuffer jsonBuffer;
JsonArray& confRoot = jsonBuffer.parseArray(conf.c_str());
// DEBUG_SERIAL("WifiConfig: JSON: " + conf);
if (!confRoot.success()) {
DEBUG_SERIAL("WifiConfig: Parsing failure");
return;
}
for (int i = 0; i < confRoot.size(); i++) {
JsonObject& net = confRoot[i];
if (!net.containsKey("SSID") || !net.containsKey("PASS")) {
DEBUG_SERIAL("WifiConfig: Missing SSID and/or PASS entry at " + String(i));
return;
}
WifiCreds _creds(net.get<String>("SSID"), net.get<String>("PASS"));
this->creds.push_back(_creds);
// DEBUG_SERIAL("WifiConfig: added " + _creds.ssid() + " as " + String(i));
}
return;
}
#endif // PM25_NODEMCU_WIFICONFIG_H