-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
70 lines (53 loc) · 1.48 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
#include "config.h"
_Config Config;
void _Config::write(){
DynamicJsonBuffer json_buf;
JsonObject &cfg = json_buf.createObject();
cfg["ssid"] = ssid.c_str();
cfg["password"] = password.c_str();
cfg["host"] = host.c_str();
cfg["token"] = token.c_str();
cfg["port"] = port;
std::ofstream cfg_file("/home/root/config.json");
String result;
cfg.printTo(result);
cfg_file << result.c_str();
cfg_file.close();
}
bool _Config::read_from_file(){
std::ifstream cfg_file("/home/root/config.json");
std::string cfg_content;
std::getline(cfg_file, cfg_content);
cfg_file.close();
return read(cfg_content);
}
bool _Config::read(std::string content){
DynamicJsonBuffer json_buf;
JsonObject &cfg = json_buf.parseObject(content.c_str());
if (!cfg.success()){
Serial.println("[_Config::read] oops, can't read config file");
return false;
}
char buf[10240];
strcpy(buf, cfg["ssid"]);
ssid.assign(buf);
strcpy(buf, cfg["password"]);
password.assign(buf);
strcpy(buf, cfg["host"]);
host.assign(buf);
strcpy(buf, cfg["token"]);
token.assign(buf);
port = cfg["port"];
Serial.println("[Config::read] read config: ");
Serial.print("[Config::read] SSID: ");
Serial.println(ssid.c_str());
Serial.print("[Config::read] Password: ");
Serial.println(password.c_str());
Serial.print("[Config::read] Host: ");
Serial.println(host.c_str());
Serial.print("[Config::read] Token: ");
Serial.println(token.c_str());
Serial.print("[Config::read] Port: ");
Serial.println(port);
return true;
}