-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
68 lines (64 loc) · 2.27 KB
/
Config.cpp
File metadata and controls
68 lines (64 loc) · 2.27 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
/*
* WaterTankController project
* Copyright (c) 2026 Fedir Vilhota <fredy31415@gmail.com>
* This software is released under the MIT License.
* See the LICENSE file in the project root for full license information.
*/
#include "Config.h"
bool Config::load(const char* filename) {
File configFile = LittleFS.open(filename, "r");
if (!configFile) {
// Якщо файл не знайдено — лишаємо дефолтні значення
return false;
}
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, configFile);
configFile.close();
if (error) {
// Якщо не вдалося розпарсити — лишаємо дефолт
return false;
}
// Зчитування масиву режимів, якщо є
if (doc.containsKey("relayControlModes")) {
JsonArray arr = doc["relayControlModes"].as<JsonArray>();
for (uint8_t i = 0; i < NUM_RELAYS; ++i) {
relayControlModes[i] = arr[i] | 0;
}
}
// Зчитування кількості зон, якщо є
if (doc.containsKey("numZones")) {
numZones = doc["numZones"].as<int>();
// Гарантуємо межі (наприклад, 1..10)
if (numZones < 1) numZones = 1;
if (numZones > 10) numZones = 10;
}
return true;
}
bool Config::save(const char* filename) {
StaticJsonDocument<512> doc;
// Зберігаємо масив режимів у поле relayControlModes
JsonArray arr = doc.createNestedArray("relayControlModes");
for (uint8_t i = 0; i < NUM_RELAYS; ++i) {
arr.add(relayControlModes[i]);
}
// Зберігаємо кількість зон
doc["numZones"] = numZones;
File configFile = LittleFS.open(filename, "w");
if (!configFile) {
Serial.println(F("Failed to open config.json for writing"));
return false;
}
serializeJsonPretty(doc, configFile);
configFile.close();
return true;
}
void Config::setRelayControlMode(int relayNum, int mode) {
if (relayNum >= 1 && relayNum <= NUM_RELAYS) {
relayControlModes[relayNum-1] = mode;
}
}
void Config::setNumZones(int zones) {
if (zones < 1) zones = 1;
if (zones > 10) zones = 10;
numZones = zones;
}