Skip to content

Commit b9adb8b

Browse files
committed
Refactor to use new field for alternative_networks
1 parent 5abd2ab commit b9adb8b

File tree

2 files changed

+31
-57
lines changed

2 files changed

+31
-57
lines changed

src/provisioning/littlefs/WipperSnapper_LittleFS.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,50 +68,37 @@ void WipperSnapper_LittleFS::parseSecrets() {
6868
error.c_str());
6969
}
7070

71-
if (doc.containsKey("network_type_wifi")) {
72-
if (doc["network_type_wifi"].is<JsonObject>()) {
71+
if (doc.containsKey("network_type_wifi")) {
72+
// set default network config
73+
convertFromJson(doc["network_type_wifi"], WS._config.network);
74+
75+
if (!doc["network_type_wifi"].containsKey("alternate_networks")) {
7376
WS_DEBUG_PRINTLN("Found single wifi network in secrets.json");
74-
// Parse network credentials from secrets
75-
convertFromJson(doc["network_type_wifi"], WS._config.network);
76-
} else if (doc["network_type_wifi"].is<JsonArray>()) {
77+
78+
} else if (doc["network_type_wifi"]["alternative_networks"].is<JsonArray>()) {
7779
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
78-
// Parse network credentials from secrets
79-
int8_t networkCount = doc["network_type_wifi"].size();
80+
// Parse network credentials from array in secrets
81+
JsonArray networks = doc["network_type_wifi"]["alternative_networks"];
82+
int8_t networkCount = networks.size();
8083
WS_DEBUG_PRINT("Network count: ");
8184
WS_DEBUG_PRINTLN(networkCount);
8285
if (networkCount == 0) {
8386
fsHalt("ERROR: No networks found in network_type_wifi in secrets.json!");
84-
} else
87+
}
8588
// check if over 5, warn user and take first five
8689
for (int i = 0; i < networkCount; i++) {
87-
if (i > 4) {
88-
WS_DEBUG_PRINT("WARNING: More than 5 networks in secrets.json, "
89-
"only the first 5 will be used. Not using ");
90-
WS_DEBUG_PRINTLN(doc["network_type_wifi"][i]["ssid"].as<const char*>());
90+
if (i >= 3) {
91+
WS_DEBUG_PRINT("WARNING: More than 3 networks in secrets.json, "
92+
"only the first 3 will be used. Not using ");
93+
WS_DEBUG_PRINTLN(networks[i]["ssid"].as<const char*>());
9194
break;
9295
}
93-
convertFromJson(doc["network_type_wifi"][i], WS._multiNetworks[i]);
96+
convertFromJson(networks[i], WS._multiNetworks[i]);
9497
WS_DEBUG_PRINT("Added SSID: ");
9598
WS_DEBUG_PRINTLN(WS._multiNetworks[i].ssid);
9699
WS_DEBUG_PRINT("PASS: ");
97100
WS_DEBUG_PRINTLN(WS._multiNetworks[i].pass);
98101
}
99-
100-
WS_DEBUG_PRINTLN("Parsing last network in array");
101-
convertFromJson(doc["network_type_wifi"][networkCount - 1],
102-
WS._config.network);
103-
WS_DEBUG_PRINTLN("Parsed last network in array");
104-
WS_DEBUG_PRINT("SSID: ");
105-
WS_DEBUG_PRINTLN(WS._config.network.ssid);
106-
WS_DEBUG_PRINT("PASS: ");
107-
WS_DEBUG_PRINTLN(WS._config.network.pass);
108-
WS_DEBUG_PRINTLN("removing network_type_wifi");
109-
doc.remove("network_type_wifi");
110-
WS_DEBUG_PRINTLN("removed network_type_wifi, creating new flat object");
111-
doc["network_type_wifi"].to<JsonObject>();
112-
WS_DEBUG_PRINTLN("created new object, setting network");
113-
doc["network_type_wifi"].set(WS._config.network);
114-
115102
} else {
116103
fsHalt("ERROR: Unrecognised value type for network_type_wifi in "
117104
"secrets.json!");

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -352,49 +352,36 @@ void Wippersnapper_FS::parseSecrets() {
352352
}
353353

354354
if (doc.containsKey("network_type_wifi")) {
355-
if (doc["network_type_wifi"].is<JsonObject>()) {
355+
// set default network config
356+
convertFromJson(doc["network_type_wifi"], WS._config.network);
357+
358+
if (!doc["network_type_wifi"].containsKey("alternate_networks")) {
356359
WS_DEBUG_PRINTLN("Found single wifi network in secrets.json");
357-
// Parse network credentials from secrets
358-
convertFromJson(doc["network_type_wifi"], WS._config.network);
359-
} else if (doc["network_type_wifi"].is<JsonArray>()) {
360+
361+
} else if (doc["network_type_wifi"]["alternative_networks"].is<JsonArray>()) {
360362
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
361-
// Parse network credentials from secrets
362-
int8_t networkCount = doc["network_type_wifi"].size();
363+
// Parse network credentials from array in secrets
364+
JsonArray networks = doc["network_type_wifi"]["alternative_networks"];
365+
int8_t networkCount = networks.size();
363366
WS_DEBUG_PRINT("Network count: ");
364367
WS_DEBUG_PRINTLN(networkCount);
365368
if (networkCount == 0) {
366369
fsHalt("ERROR: No networks found in network_type_wifi in secrets.json!");
367-
} else
370+
}
368371
// check if over 5, warn user and take first five
369372
for (int i = 0; i < networkCount; i++) {
370-
if (i > 4) {
371-
WS_DEBUG_PRINT("WARNING: More than 5 networks in secrets.json, "
372-
"only the first 5 will be used. Not using ");
373-
WS_DEBUG_PRINTLN(doc["network_type_wifi"][i]["ssid"].as<const char*>());
373+
if (i >= 3) {
374+
WS_DEBUG_PRINT("WARNING: More than 3 networks in secrets.json, "
375+
"only the first 3 will be used. Not using ");
376+
WS_DEBUG_PRINTLN(networks[i]["ssid"].as<const char*>());
374377
break;
375378
}
376-
convertFromJson(doc["network_type_wifi"][i], WS._multiNetworks[i]);
379+
convertFromJson(networks[i], WS._multiNetworks[i]);
377380
WS_DEBUG_PRINT("Added SSID: ");
378381
WS_DEBUG_PRINTLN(WS._multiNetworks[i].ssid);
379382
WS_DEBUG_PRINT("PASS: ");
380383
WS_DEBUG_PRINTLN(WS._multiNetworks[i].pass);
381384
}
382-
383-
WS_DEBUG_PRINTLN("Parsing last network in array");
384-
convertFromJson(doc["network_type_wifi"][networkCount - 1],
385-
WS._config.network);
386-
WS_DEBUG_PRINTLN("Parsed last network in array");
387-
WS_DEBUG_PRINT("SSID: ");
388-
WS_DEBUG_PRINTLN(WS._config.network.ssid);
389-
WS_DEBUG_PRINT("PASS: ");
390-
WS_DEBUG_PRINTLN(WS._config.network.pass);
391-
WS_DEBUG_PRINTLN("removing network_type_wifi");
392-
doc.remove("network_type_wifi");
393-
WS_DEBUG_PRINTLN("removed network_type_wifi, creating new flat object");
394-
doc["network_type_wifi"].to<JsonObject>();
395-
WS_DEBUG_PRINTLN("created new object, setting network");
396-
doc["network_type_wifi"].set(WS._config.network);
397-
398385
} else {
399386
fsHalt("ERROR: Unrecognised value type for network_type_wifi in "
400387
"secrets.json!");

0 commit comments

Comments
 (0)