-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
91 lines (83 loc) · 2.24 KB
/
config.cpp
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
#include "ArduinoNvs.h"
#include "config.h"
#include "logging.h"
Config *Config::_instance = nullptr;
Config::Config() :
mode(0),
version(0),
name(""),
ssid(""),
passphrase(""),
mqttHost(""),
mqttPort(1883),
syslogHost(""),
syslogPort(514),
updateUrl(""),
rainSensor(false),
batterySensor(false),
syslogEnabled(false)
{
NVS.begin();
}
void Config::load()
{
mode = NVS.getInt("X");
if (mode != CONFIG_MODE_UNSET)
{
version = NVS.getInt("v");
name = NVS.getString("n");
ssid = NVS.getString("ss");
passphrase = NVS.getString("pp");
mqttHost = NVS.getString("mh");
mqttPort = NVS.getInt("mp");
syslogHost = NVS.getString("sh");
syslogPort = NVS.getInt("sp");
updateUrl = NVS.getString("uu");
rainSensor = NVS.getInt("rs") != 0;
batterySensor = NVS.getInt("bs") != 0;
logfmt("Version: %d\n", version);
logfmt("name: %s\n", name.c_str());
logfmt("ssid: %s\n", ssid.c_str());
logfmt("passphrase: %s\n", passphrase.c_str());
logfmt("mqttHost: %s\n", mqttHost.c_str());
logfmt("mqttPort: %d\n", mqttPort);
logfmt("syslogHost: %s\n", syslogHost.c_str());
logfmt("syslogPort: %d\n", syslogPort);
logfmt("updateUrl: %s\n", updateUrl.c_str());
logfmt("rainSensor: %s\n", rainSensor ? "true" : "false");
logfmt("batterySensor: %s\n", batterySensor ? "true" : "false");
if (!syslogHost.isEmpty())
{
syslogEnabled = true;
}
}
}
void Config::save()
{
NVS.setInt("X", CONFIG_MODE_SET);
NVS.setInt("v", version);
NVS.setString("n", name);
NVS.setString("ss", ssid);
NVS.setString("pp", passphrase);
NVS.setString("mh", mqttHost);
NVS.setInt("mp", mqttPort);
NVS.setString("sh", syslogHost);
NVS.setInt("sp", syslogPort);
NVS.setString("uu", updateUrl);
NVS.setInt("rs", rainSensor ? 1 : 0);
NVS.setInt("bs", batterySensor ? 1 : 0);
NVS.commit();
}
void Config::reconfigure()
{
NVS.setInt("X", CONFIG_MODE_RESET);
NVS.commit();
}
Config *Config::instance()
{
if (_instance == nullptr)
{
_instance = new Config();
}
return _instance;
}