-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathConfig.cpp
More file actions
127 lines (101 loc) · 2.51 KB
/
Config.cpp
File metadata and controls
127 lines (101 loc) · 2.51 KB
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
/*
* Config.cpp
*
* Created on: Jun 10, 2018
* Author: chris.l
*/
#include "Config.h"
#include "FS.h"
#include "Log.h"
#include "ArduinoJson.h"
static const char* TAG = "Config";
static const char* CONFIG_FILE = "/Config.json";
Config::Config() : _syslog_host(), _syslog_port(0)
{
}
Config::~Config()
{
}
bool Config::begin()
{
dlog.info(TAG, "begin: Mounting SPIFFS");
if (!SPIFFS.begin())
{
dlog.warning(TAG, "begin: Formatting SPIFFS!!!!!");
if (SPIFFS.format())
{
dlog.error(TAG, "begin: SPIFFS format failed!");
return false;
}
dlog.info(TAG, "begin: mounting SPIFFS after format!");
if (!SPIFFS.begin())
{
dlog.error(TAG, "begin: SPIFFS mount failed!!!!");
return false;
}
}
dlog.info(TAG, "begin scanning SPIFFS files");
Dir dir = SPIFFS.openDir("");
while (dir.next())
{
File f = dir.openFile("r");
dlog.info(TAG, "begin: file: '%s' size %d", dir.fileName().c_str(), f.size());
}
return true;
}
bool Config::load()
{
dlog.info(TAG, "load: file: '%s'", CONFIG_FILE);
if (!SPIFFS.exists(CONFIG_FILE))
{
dlog.warning(TAG, "load: config file does not exist!");
return false;
}
File f = SPIFFS.open(CONFIG_FILE, "r");
if (!f)
{
dlog.error(TAG, "load: failed to open config file!");
return false;
}
StaticJsonBuffer<512> buffer;
dlog.debug(TAG, "load: parsing file contents");
JsonObject& root = buffer.parseObject(f);
f.close();
if (!root.success())
{
dlog.error(TAG, "load: fails to parse file!");
return false;
}
strlcpy(_syslog_host, root["syslogHost"]|"", sizeof(_syslog_host));
_syslog_port = root["syslogPort"] | 0;
dlog.info(TAG, "load: config loaded!");
return true;
}
void Config::save()
{
dlog.info(TAG, "save: file: '%s'", CONFIG_FILE);
File f = SPIFFS.open(CONFIG_FILE, "w");
StaticJsonBuffer<512> buffer;
JsonObject& root = buffer.createObject();
root["syslogHost"] = _syslog_host;
root["syslogPort"] = _syslog_port;
root.printTo(f);
f.close();
dlog.info(TAG, "save: config saved");
}
const char* Config::getSyslogHost()
{
return _syslog_host;
}
void Config::setSyslogHost(const char* host)
{
strlcpy(_syslog_host, host, sizeof(_syslog_host));
}
uint16_t Config::getSyslogPort()
{
return _syslog_port;
}
void Config::setSyslogPort(uint16_t port)
{
_syslog_port = port;
}