forked from Dynatrace/ufo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cpp
96 lines (76 loc) · 2.65 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
92
93
94
95
#include "config.h"
#include <FS.h>
#include <ArduinoJson.h>
// Storing JSON configuration file in flash file system
// Uses ArduinoJson library by Benoit Blanchon.
// https://github.com/bblanchon/ArduinoJson
// note: wifi credentials are stored in ESP8266 special area flash memory and NOT in this config
#define MAX_CONFIGFILESIZE 512
#define CONFIG_FILE F("/config.json")
Config::Config(bool debug){
mDebug = debug;
mChanged = true;
}
bool Config::Read() {
File configFile = SPIFFS.open(CONFIG_FILE, "r");
if (!configFile) {
if (mDebug) Serial.println(F("Failed to open config file"));
return false;
}
size_t size = configFile.size();
if (size > (MAX_CONFIGFILESIZE)) {
if (mDebug) Serial.println(F("Config file size is too large"));
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
StaticJsonBuffer < MAX_CONFIGFILESIZE + 1 > jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success()) {
if (mDebug) Serial.println(F("Failed to parse config file"));
return false;
}
enabled = json[F("dynatrace-enabled")];
dynatraceEnvironmentID = (const char*)json[F("dynatrace-environmentid")];
dynatraceApiKey = (const char*)json[F("dynatrace-apikey")];
pollingIntervalS = json[F("dynatrace-interval")];
if (pollingIntervalS < 30){
pollingIntervalS = 30;
}
return true;
}
// note that writing to the SPIFFS wears the flash memory; so make sure to only use it when saving is really required.
bool Config::Write() {
StaticJsonBuffer < MAX_CONFIGFILESIZE + 1 > jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json[F("dynatrace-enabled")] = enabled;
json[F("dynatrace-environmentid")] = dynatraceEnvironmentID;
json[F("dynatrace-apikey")] = dynatraceApiKey;
json[F("dynatrace-interval")] = pollingIntervalS;
File configFile = SPIFFS.open(CONFIG_FILE, "w");
if (!configFile) {
if (mDebug) Serial.println(F("Failed to open config file for writing"));
return false;
}
json.printTo(configFile);
if (mDebug) json.printTo(Serial);
return true;
}
bool Config::Delete() {
SPIFFS.begin(); // just make sure its already open
SPIFFS.remove(CONFIG_FILE);
}
bool Config::Enabled(){
return enabled && (dynatraceEnvironmentID.length() > 0) && (dynatraceApiKey.length() > 0);
}
bool Config::Changed(){
if (mChanged){
mChanged = false;
return true;
}
return false;
}