-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconfig.cpp
More file actions
124 lines (103 loc) · 5.29 KB
/
config.cpp
File metadata and controls
124 lines (103 loc) · 5.29 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
#include "config.h"
#include "config_storage.h"
// =============================================================================
// DYNAMIC CONFIGURATION IMPLEMENTATION
// =============================================================================
// Configuration is now loaded from persistent storage via ConfigStorage
// These variables will be updated from stored values at runtime
// WiFi Configuration - Will be loaded from storage
const char* WIFI_SSID = nullptr;
const char* WIFI_PASSWORD = nullptr;
// MQTT Configuration - Will be loaded from storage
const char* MQTT_SERVER = nullptr;
int MQTT_PORT = 1883;
const char* MQTT_USER = nullptr;
const char* MQTT_PASSWORD = nullptr;
const char* MQTT_CLIENT_ID = nullptr;
// Image Configuration - Will be loaded from storage
const char* IMAGE_URL = nullptr;
// =============================================================================
// DEFAULT IMAGE SOURCES CONFIGURATION
// =============================================================================
// Configure your default image sources here. These will be loaded on first boot
// or when the configuration is reset to defaults.
const char* DEFAULT_IMAGE_SOURCES[] = {
"https://i.imgur.com/EsstNmc.jpeg", // Default source 1
"https://i.imgur.com/EtW1eaT.jpeg", // Default source 2
"https://i.imgur.com/k23xBF5.jpeg", // Default source 3
"https://i.imgur.com/BysRDbf.jpeg", // Default source 4
"http://allskypi5.lan/current/resized/image.jpg" // Default source 5
// Add more image URLs here as needed (up to MAX_IMAGE_SOURCES = 10)
};
const int DEFAULT_IMAGE_SOURCE_COUNT = sizeof(DEFAULT_IMAGE_SOURCES) / sizeof(DEFAULT_IMAGE_SOURCES[0]);
const bool DEFAULT_CYCLING_ENABLED = true; // Enable cycling by default
const bool DEFAULT_RANDOM_ORDER = false; // Use sequential order by default
// Internal string storage for dynamic configuration
String wifiSSIDStr, wifiPasswordStr;
String mqttServerStr, mqttUserStr, mqttPasswordStr, mqttClientIDStr;
String imageURLStr;
// =============================================================================
// CONFIGURATION INITIALIZATION
// =============================================================================
void initializeConfiguration() {
// Initialize configuration storage
configStorage.begin();
// Check if this is first boot or configuration needs initialization
if (!configStorage.hasStoredConfig()) {
Serial.println("First boot detected - initializing with default image sources");
// Clear any existing image sources
configStorage.clearImageSources();
// Load default image sources from config
for (int i = 0; i < DEFAULT_IMAGE_SOURCE_COUNT; i++) {
configStorage.addImageSource(String(DEFAULT_IMAGE_SOURCES[i]));
Serial.printf("Added default image source %d: %s\n", i+1, DEFAULT_IMAGE_SOURCES[i]);
}
// Set default cycling configuration
configStorage.setCyclingEnabled(DEFAULT_CYCLING_ENABLED);
configStorage.setRandomOrder(DEFAULT_RANDOM_ORDER);
configStorage.setCycleInterval(DEFAULT_CYCLE_INTERVAL);
// Save the default configuration
configStorage.saveConfig();
Serial.printf("Initialized %d default image sources with cycling %s\n",
DEFAULT_IMAGE_SOURCE_COUNT,
DEFAULT_CYCLING_ENABLED ? "enabled" : "disabled");
}
// Load values from storage and update global variables
wifiSSIDStr = configStorage.getWiFiSSID();
wifiPasswordStr = configStorage.getWiFiPassword();
mqttServerStr = configStorage.getMQTTServer();
MQTT_PORT = configStorage.getMQTTPort();
mqttUserStr = configStorage.getMQTTUser();
mqttPasswordStr = configStorage.getMQTTPassword();
mqttClientIDStr = configStorage.getMQTTClientID();
imageURLStr = configStorage.getImageURL();
// Update const char* pointers to point to string data
WIFI_SSID = wifiSSIDStr.c_str();
WIFI_PASSWORD = wifiPasswordStr.c_str();
MQTT_SERVER = mqttServerStr.c_str();
MQTT_USER = mqttUserStr.c_str();
MQTT_PASSWORD = mqttPasswordStr.c_str();
MQTT_CLIENT_ID = mqttClientIDStr.c_str();
IMAGE_URL = imageURLStr.c_str();
Serial.println("Configuration loaded from persistent storage");
if (configStorage.hasStoredConfig()) {
Serial.println("Using stored configuration");
} else {
Serial.println("Using default configuration (first boot)");
}
// Print current image source configuration
int sourceCount = configStorage.getImageSourceCount();
Serial.printf("Current image sources: %d configured\n", sourceCount);
for (int i = 0; i < sourceCount; i++) {
Serial.printf(" [%d] %s\n", i+1, configStorage.getImageSource(i).c_str());
}
Serial.printf("Cycling: %s, Random: %s, Interval: %lu ms\n",
configStorage.getCyclingEnabled() ? "enabled" : "disabled",
configStorage.getRandomOrder() ? "enabled" : "disabled",
configStorage.getCycleInterval());
}
void reloadConfiguration() {
// Reload configuration from storage (useful after web config changes)
initializeConfiguration();
Serial.println("Configuration reloaded from storage");
}