🪄 Formats WiFiSettingsService
This commit is contained in:
@@ -54,8 +54,7 @@
|
||||
#define EVENT_WIFI_SETTINGS "WiFiSettings"
|
||||
|
||||
// Struct defining the wifi settings
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
String ssid;
|
||||
String password;
|
||||
bool staticIPConfig;
|
||||
@@ -67,16 +66,14 @@ typedef struct
|
||||
bool available;
|
||||
} wifi_settings_t;
|
||||
|
||||
class WiFiSettings
|
||||
{
|
||||
public:
|
||||
class WiFiSettings {
|
||||
public:
|
||||
// core wifi configuration
|
||||
String hostname;
|
||||
bool priorityBySignalStrength;
|
||||
std::vector<wifi_settings_t> wifiSettings;
|
||||
|
||||
static void read(WiFiSettings &settings, JsonObject &root)
|
||||
{
|
||||
static void read(WiFiSettings &settings, JsonObject &root) {
|
||||
root["hostname"] = settings.hostname;
|
||||
root["priority_RSSI"] = settings.priorityBySignalStrength;
|
||||
|
||||
@@ -84,8 +81,7 @@ public:
|
||||
JsonArray wifiNetworks = root["wifi_networks"].to<JsonArray>();
|
||||
|
||||
// iterate over the wifiSettings
|
||||
for (auto &wifi : settings.wifiSettings)
|
||||
{
|
||||
for (auto &wifi : settings.wifiSettings) {
|
||||
// create JSON object for each wifi network
|
||||
JsonObject wifiNetwork = wifiNetworks.add<JsonObject>();
|
||||
|
||||
@@ -105,8 +101,7 @@ public:
|
||||
ESP_LOGV("WiFiSettings", "WiFi Settings read");
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, WiFiSettings &settings)
|
||||
{
|
||||
static StateUpdateResult update(JsonObject &root, WiFiSettings &settings) {
|
||||
settings.hostname = root["hostname"] | SettingValue::format(FACTORY_WIFI_HOSTNAME);
|
||||
settings.priorityBySignalStrength = root["priority_RSSI"] | true;
|
||||
|
||||
@@ -114,15 +109,12 @@ public:
|
||||
|
||||
// create JSON array from root
|
||||
JsonArray wifiNetworks = root["wifi_networks"];
|
||||
if (root["wifi_networks"].is<JsonArray>())
|
||||
{
|
||||
if (root["wifi_networks"].is<JsonArray>()) {
|
||||
// iterate over the wifiSettings
|
||||
int i = 0;
|
||||
for (auto wifiNetwork : wifiNetworks)
|
||||
{
|
||||
for (auto wifiNetwork : wifiNetworks) {
|
||||
// max 5 wifi networks
|
||||
if (i++ >= 5)
|
||||
{
|
||||
if (i++ >= 5) {
|
||||
ESP_LOGE("WiFiSettings", "Too many wifi networks");
|
||||
break;
|
||||
}
|
||||
@@ -131,12 +123,10 @@ public:
|
||||
JsonObject wifi = wifiNetwork.as<JsonObject>();
|
||||
|
||||
// Check if SSID length is between 1 and 31 characters and password between 0 and 64 characters
|
||||
if (wifi["ssid"].as<String>().length() < 1 || wifi["ssid"].as<String>().length() > 31 || wifi["password"].as<String>().length() > 64)
|
||||
{
|
||||
if (wifi["ssid"].as<String>().length() < 1 || wifi["ssid"].as<String>().length() > 31 ||
|
||||
wifi["password"].as<String>().length() > 64) {
|
||||
ESP_LOGE("WiFiSettings", "SSID or password length is invalid");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// add the ssid and password to the JSON object
|
||||
wifi_settings_t wifiSettings;
|
||||
|
||||
@@ -152,8 +142,7 @@ public:
|
||||
JsonUtils::readIP(wifi, "dns_ip_2", wifiSettings.dnsIP2);
|
||||
|
||||
// Swap around the dns servers if 2 is populated but 1 is not
|
||||
if (IPUtils::isNotSet(wifiSettings.dnsIP1) && IPUtils::isSet(wifiSettings.dnsIP2))
|
||||
{
|
||||
if (IPUtils::isNotSet(wifiSettings.dnsIP1) && IPUtils::isSet(wifiSettings.dnsIP2)) {
|
||||
wifiSettings.dnsIP1 = wifiSettings.dnsIP2;
|
||||
wifiSettings.dnsIP2 = INADDR_NONE;
|
||||
}
|
||||
@@ -161,9 +150,9 @@ public:
|
||||
// Turning off static ip config if we don't meet the minimum requirements
|
||||
// of ipAddress, gateway and subnet. This may change to static ip only
|
||||
// as sensible defaults can be assumed for gateway and subnet
|
||||
if (wifiSettings.staticIPConfig && (IPUtils::isNotSet(wifiSettings.localIP) || IPUtils::isNotSet(wifiSettings.gatewayIP) ||
|
||||
IPUtils::isNotSet(wifiSettings.subnetMask)))
|
||||
{
|
||||
if (wifiSettings.staticIPConfig &&
|
||||
(IPUtils::isNotSet(wifiSettings.localIP) || IPUtils::isNotSet(wifiSettings.gatewayIP) ||
|
||||
IPUtils::isNotSet(wifiSettings.subnetMask))) {
|
||||
wifiSettings.staticIPConfig = false;
|
||||
}
|
||||
|
||||
@@ -175,13 +164,10 @@ public:
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// populate with factory defaults if they are present
|
||||
if (String(FACTORY_WIFI_SSID).length() > 0)
|
||||
{
|
||||
settings.wifiSettings.push_back(wifi_settings_t{
|
||||
if (String(FACTORY_WIFI_SSID).length() > 0) {
|
||||
settings.wifiSettings.push_back(wifi_settings_t {
|
||||
.ssid = FACTORY_WIFI_SSID,
|
||||
.password = FACTORY_WIFI_PASSWORD,
|
||||
.staticIPConfig = false,
|
||||
@@ -200,9 +186,8 @@ public:
|
||||
};
|
||||
};
|
||||
|
||||
class WiFiSettingsService : public StatefulService<WiFiSettings>
|
||||
{
|
||||
public:
|
||||
class WiFiSettingsService : public StatefulService<WiFiSettings> {
|
||||
public:
|
||||
WiFiSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager, EventSocket *socket);
|
||||
|
||||
void initWiFi();
|
||||
@@ -210,7 +195,7 @@ public:
|
||||
void loop();
|
||||
String getHostname();
|
||||
|
||||
private:
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
SecurityManager *_securityManager;
|
||||
HttpEndpoint<WiFiSettings> _httpEndpoint;
|
||||
|
||||
Reference in New Issue
Block a user