77
88#define APB_PREFS_VERSION 1
99#define APB_KEY_VERSION " version"
10- #define APB_KEY_AP_ESSID " ap_essid"
11- #define APB_KEY_AP_PSK " ap_psk"
12-
13- #define APB_KEY_STATION_X_ESSID " station_%d_essid"
14- #define APB_KEY_STATION_X_PSK " station_%d_psk"
1510
1611#define APB_KEY_STATUS_LED_DUTY " status_led_duty"
1712
18-
1913#define LOG_SCOPE " APB::Configuration - "
2014
2115using namespace std ::placeholders;
2216
2317APB::Settings &APB::Settings::Instance = *new APB::Settings();
2418
25- namespace {
26- Preferences prefs;
27- }
28-
29- bool APB::Settings::WiFiStation::empty () const {
30- return strlen (essid) == 0 ;
31- }
32-
33- bool APB::Settings::WiFiStation::open () const {
34- return strlen (psk) == 0 ;
35- }
3619
37- APB::Settings::Settings () {
20+ APB::Settings::Settings () :
21+ #ifdef APB_DEFAULT_HOSTNAME
22+ wifiSettings{prefs, LittleFS, APB_DEFAULT_HOSTNAME, false }
23+ #else
24+ wifiSettings{prefs, LittleFS, " AstroPowerBox-" , true }
25+ #endif
26+ {
3827
3928}
4029
@@ -46,11 +35,6 @@ void APB::Settings::setup() {
4635 Log.infoln (LOG_SCOPE " Setup finished" );
4736}
4837
49- void runOnFormatKey (const char *format, uint16_t index, std::function<void (const char *)> apply) {
50- char key[256 ];
51- sprintf (key, format, index);
52- apply (key);
53- }
5438
5539void APB::Settings::load () {
5640 Log.traceln (LOG_SCOPE " Loading APB Settings" );
@@ -59,102 +43,21 @@ void APB::Settings::load() {
5943 loadDefaults ();
6044 return ;
6145 }
62- size_t apSSIDChars = prefs.getString (APB_KEY_AP_ESSID, _apConfiguration.essid , APB_MAX_ESSID_PSK_SIZE);
63- Log.traceln (LOG_SCOPE " %s characters: %d" , APB_KEY_AP_ESSID, apSSIDChars);
64- if (apSSIDChars > 0 && _apConfiguration ) {
65- prefs.getString (APB_KEY_AP_PSK, _apConfiguration.psk , APB_MAX_ESSID_PSK_SIZE);
66- Log.traceln (LOG_SCOPE " Loaded AP Settings: essid=`%s`, psk=`%s`" , _apConfiguration.essid , _apConfiguration.psk );
67- } else {
68- loadDefaults ();
69- }
70- for (uint8_t i=0 ; i<APB_MAX_STATIONS; i++) {
71- runOnFormatKey (APB_KEY_STATION_X_ESSID, i, [this , i](const char *key) { prefs.getString (key, _stations[i].essid , APB_MAX_ESSID_PSK_SIZE); });
72- runOnFormatKey (APB_KEY_STATION_X_PSK, i, [this , i](const char *key) { prefs.getString (key, _stations[i].psk , APB_MAX_ESSID_PSK_SIZE); });
73- Log.traceln (LOG_SCOPE " Station %d: essid=`%s`, psk=`%s`" , i, _stations[i].essid , _stations[i].psk );
74- }
75-
76- if (std::none_of (_stations.begin (), _stations.end (), std::bind (&WiFiStation::valid, _1))) {
77- loadDefaultStations ();
78- }
7946 _statusLedDuty = prefs.getFloat (APB_KEY_STATUS_LED_DUTY, 1 );
47+ wifiSettings.load ();
8048 Log.infoln (LOG_SCOPE " Preferences loaded" );
8149}
8250
8351void APB::Settings::loadDefaults () {
84- #ifdef APB_DEFAULT_HOSTNAME
85- sprintf (_apConfiguration.essid , APB_DEFAULT_HOSTNAME);
86- #else
87- String mac = WiFi.macAddress ();
88- Log.traceln (LOG_SCOPE " Found mac address: `%s`" , mac.c_str ());
89- mac.replace (F (" :" ), F (" " ));
90- mac = mac.substring (6 );
91- sprintf (_apConfiguration.essid , " AstroPowerBox-%s" , mac.c_str ());
92- #endif
93- memset (_apConfiguration.psk , 0 , APB_MAX_ESSID_PSK_SIZE);
94- Log.traceln (LOG_SCOPE " Using default ESSID: `%s`" , _apConfiguration.essid );
95- loadDefaultStations ();
52+ wifiSettings.loadDefaults ();
9653}
9754
98- void APB::Settings::loadDefaultStations () {
99- if (LittleFS.exists (" /wifi.json" )) {
100- fs::File wifiJson = LittleFS.open (" /wifi.json" );
101- JsonDocument doc;
102- DeserializationError error = deserializeJson (doc, wifiJson);
103- if (error) {
104- Log.warningln (F (" Failed to read file \" /wifi.json\" , using empty wifi configuration" ));
105- wifiJson.close ();
106- return ;
107- }
108- wifiJson.close ();
109- JsonArray stations = doc.as <JsonArray>();
110- Log.infoln (" Found valid /wifi.json settings file, loading default wifi settings with %d stations" , stations.size ());
111- for (int i=0 ; i<stations.size () && i<APB_MAX_STATIONS; i++) {
112- String ssid = stations[i][" ssid" ];
113- String psk = stations[i][" psk" ];
114- Log.infoln (" Adding station: %s" , ssid);
115- _stations[i] = WiFiStation{};
116- strcpy (_stations[i].essid , ssid.c_str ());
117- strcpy (_stations[i].psk , psk.c_str ());
118-
119- }
120- save ();
121- }
122- }
55+
12356
12457void APB::Settings::save () {
12558 Log.traceln (LOG_SCOPE " Saving APB Settings" );
12659 prefs.putUShort (APB_KEY_VERSION, APB_PREFS_VERSION);
127- prefs.putString (APB_KEY_AP_ESSID, _apConfiguration.essid );
128- prefs.putString (APB_KEY_AP_PSK, _apConfiguration.psk );
129-
130- for (uint8_t i=0 ; i<APB_MAX_STATIONS; i++) {
131- runOnFormatKey (APB_KEY_STATION_X_ESSID, i, [this , i](const char *key) { prefs.putString (key, _stations[i].essid ); });
132- runOnFormatKey (APB_KEY_STATION_X_PSK, i, [this , i](const char *key) { prefs.putString (key, _stations[i].psk ); });
133- }
60+ wifiSettings.save ();
13461 prefs.putFloat (APB_KEY_STATUS_LED_DUTY, _statusLedDuty);
13562 Log.infoln (LOG_SCOPE " Preferences saved" );
13663}
137-
138- void APB::Settings::setAPConfiguration (const char *essid, const char *psk) {
139- strcpy (_apConfiguration.essid , essid);
140- strcpy (_apConfiguration.psk , psk);
141- }
142-
143- void APB::Settings::setStationConfiguration (uint8_t index, const char *essid, const char *psk) {
144- strcpy (_stations[index].essid , essid);
145- strcpy (_stations[index].psk , psk);
146- }
147-
148- bool APB::Settings::hasStation (const String &essid) const {
149- return std::any_of (
150- std::begin (_stations),
151- std::end (_stations),
152- [&essid](const WiFiStation &station){
153- return essid == station.essid ;
154- }
155- );
156- }
157-
158- bool APB::Settings::hasValidStations () const {
159- return std::any_of (_stations.begin (), _stations.end (), std::bind (&APB::Settings::WiFiStation::valid, _1));
160- }
0 commit comments