forked from jnsbyr/esp8266-intexsbh20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurationFile.cpp
117 lines (107 loc) · 2.99 KB
/
ConfigurationFile.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* project: generic
*
* file: ConfigurationFile.cpp
*
* encoding: UTF-8
* created: 23rd March 2021
*
* Copyright (C) 2021 Jens B.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "ConfigurationFile.h"
#include <LittleFS.h>
#include <stdexcept>
/**
* load JSON config file into memory
*
* @param fileName name of file
* @return true on success
*/
bool ConfigurationFile::load(const char* fileName)
{
bool success = true;
if (LittleFS.begin())
{
try
{
// open wifiConfig file
File configFile = LittleFS.open(fileName, "r");
if (!configFile)
{
snprintf_P(exceptionMessage, EXCEPTION_MESSAGE_SIZE, PSTR("config file '%s' not found"), fileName);
throw std::runtime_error(exceptionMessage);
}
size_t fileSize = configFile.size();
if (fileSize > configDoc.capacity())
{
snprintf_P(exceptionMessage, EXCEPTION_MESSAGE_SIZE, PSTR("max. config file size of %u bytes exceeded (has %u bytes)"), configDoc.capacity(), fileSize);
throw std::runtime_error(exceptionMessage);
}
// parse buffer as JSON object
DeserializationError error = deserializeJson(configDoc, configFile);
if (error)
{
snprintf_P(exceptionMessage, EXCEPTION_MESSAGE_SIZE, PSTR("error parsing config file: %s"), error.f_str());
throw std::runtime_error(exceptionMessage);
}
// close config file
configFile.close();
Serial.printf_P(PSTR("config file loaded successfully (has %d entries)\n"), configDoc.size());
}
catch (std::runtime_error re)
{
Serial.println(re.what());
success = false;
}
}
else
{
Serial.printf_P(PSTR("error mounting file system, unable to open config file\n"));
success = false;
}
return success;
}
/**
* try if config tag exists
* @param tag
* @return true if config tag exists
*/
bool ConfigurationFile::exists(const char* tag) const
{
return configDoc.containsKey(tag);
}
/**
* try to find config tag and return its value
*
* @param tag config tag
* @return config value for given tag, might be empty
*
* @throws std::runtime_error if config tag is unknown
*/
const char* ConfigurationFile::get(const char* tag)
{
if (configDoc.containsKey(tag))
{
return configDoc[tag];
}
else
{
snprintf_P(exceptionMessage, EXCEPTION_MESSAGE_SIZE, PSTR("required entry '%s' not found in config file"), tag);
throw std::runtime_error(exceptionMessage);
}
}