♻️ Replaces JsonObject with JsonVariant
This commit is contained in:
@@ -75,7 +75,7 @@ class APSettings {
|
||||
localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
|
||||
}
|
||||
|
||||
static void read(APSettings &settings, JsonObject &root) {
|
||||
static void read(APSettings &settings, JsonVariant &root) {
|
||||
root["provision_mode"] = settings.provisionMode;
|
||||
root["ssid"] = settings.ssid;
|
||||
root["password"] = settings.password;
|
||||
@@ -87,7 +87,7 @@ class APSettings {
|
||||
root["subnet_mask"] = settings.subnetMask.toString();
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, APSettings &settings) {
|
||||
static StateUpdateResult update(JsonVariant &root, APSettings &settings) {
|
||||
APSettings newSettings = {};
|
||||
newSettings.provisionMode = root["provision_mode"] | FACTORY_AP_PROVISION_MODE;
|
||||
switch (settings.provisionMode) {
|
||||
|
||||
@@ -39,7 +39,7 @@ class CameraSettings {
|
||||
uint8_t dcw;
|
||||
uint8_t colorbar;
|
||||
|
||||
static void read(CameraSettings &settings, JsonObject &root) {
|
||||
static void read(CameraSettings &settings, JsonVariant &root) {
|
||||
root["pixformat"] = settings.pixformat;
|
||||
root["framesize"] = settings.framesize;
|
||||
root["quality"] = settings.quality;
|
||||
@@ -71,7 +71,7 @@ class CameraSettings {
|
||||
root["colorbar"] = settings.colorbar;
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, CameraSettings &settings) {
|
||||
static StateUpdateResult update(JsonVariant &root, CameraSettings &settings) {
|
||||
settings.pixformat = root["pixformat"];
|
||||
settings.framesize = root["framesize"];
|
||||
settings.brightness = root["brightness"];
|
||||
|
||||
@@ -18,12 +18,12 @@ typedef struct {
|
||||
String key;
|
||||
String value;
|
||||
|
||||
void serialize(JsonObject &json) const {
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["key"] = key;
|
||||
json["value"] = value;
|
||||
}
|
||||
|
||||
bool deserialize(const JsonObject &json) {
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
key = json["key"].as<String>();
|
||||
value = json["value"].as<String>();
|
||||
|
||||
@@ -37,7 +37,7 @@ typedef struct {
|
||||
uint16_t port;
|
||||
std::vector<mdns_txt_record_t> txtRecords;
|
||||
|
||||
void serialize(JsonObject &json) const {
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["service"] = service;
|
||||
json["protocol"] = protocol;
|
||||
json["port"] = port;
|
||||
@@ -45,13 +45,13 @@ typedef struct {
|
||||
if (txtRecords.size() > 0) {
|
||||
JsonArray txtArray = json["txt_records"].to<JsonArray>();
|
||||
for (const auto &txt : txtRecords) {
|
||||
JsonObject txtObj = txtArray.add<JsonObject>();
|
||||
JsonVariant txtObj = txtArray.add<JsonVariant>();
|
||||
txt.serialize(txtObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool deserialize(const JsonObject &json) {
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
service = json["service"].as<String>();
|
||||
protocol = json["protocol"].as<String>();
|
||||
port = json["port"] | 0;
|
||||
@@ -59,7 +59,7 @@ typedef struct {
|
||||
txtRecords.clear();
|
||||
if (json["txt_records"].is<JsonArray>()) {
|
||||
JsonArray txtArray = json["txt_records"];
|
||||
for (JsonObject txtObj : txtArray) {
|
||||
for (JsonVariant txtObj : txtArray) {
|
||||
mdns_txt_record_t txt;
|
||||
if (txt.deserialize(txtObj)) {
|
||||
txtRecords.push_back(txt);
|
||||
@@ -78,31 +78,31 @@ class MDNSSettings {
|
||||
std::vector<mdns_service_t> services;
|
||||
std::vector<mdns_txt_record_t> globalTxtRecords;
|
||||
|
||||
static void read(MDNSSettings &settings, JsonObject &root) {
|
||||
static void read(MDNSSettings &settings, JsonVariant &root) {
|
||||
root["hostname"] = settings.hostname;
|
||||
root["instance"] = settings.instance;
|
||||
|
||||
JsonArray servicesArray = root["services"].to<JsonArray>();
|
||||
for (const auto &service : settings.services) {
|
||||
JsonObject serviceObj = servicesArray.add<JsonObject>();
|
||||
JsonVariant serviceObj = servicesArray.add<JsonVariant>();
|
||||
service.serialize(serviceObj);
|
||||
}
|
||||
|
||||
JsonArray txtArray = root["global_txt_records"].to<JsonArray>();
|
||||
for (const auto &txt : settings.globalTxtRecords) {
|
||||
JsonObject txtObj = txtArray.add<JsonObject>();
|
||||
JsonVariant txtObj = txtArray.add<JsonVariant>();
|
||||
txt.serialize(txtObj);
|
||||
}
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, MDNSSettings &settings) {
|
||||
static StateUpdateResult update(JsonVariant &root, MDNSSettings &settings) {
|
||||
settings.hostname = root["hostname"] | FACTORY_MDNS_HOSTNAME;
|
||||
settings.instance = root["instance"] | FACTORY_MDNS_INSTANCE;
|
||||
|
||||
settings.services.clear();
|
||||
if (root["services"].is<JsonArray>()) {
|
||||
JsonArray servicesArray = root["services"];
|
||||
for (JsonObject serviceObj : servicesArray) {
|
||||
for (JsonVariant serviceObj : servicesArray) {
|
||||
mdns_service_t service;
|
||||
if (service.deserialize(serviceObj)) {
|
||||
settings.services.push_back(service);
|
||||
@@ -121,7 +121,7 @@ class MDNSSettings {
|
||||
settings.globalTxtRecords.clear();
|
||||
if (root["global_txt_records"].is<JsonArray>()) {
|
||||
JsonArray txtArray = root["global_txt_records"];
|
||||
for (JsonObject txtObj : txtArray) {
|
||||
for (JsonVariant txtObj : txtArray) {
|
||||
mdns_txt_record_t txt;
|
||||
if (txt.deserialize(txtObj)) {
|
||||
settings.globalTxtRecords.push_back(txt);
|
||||
|
||||
@@ -25,14 +25,14 @@ class NTPSettings {
|
||||
String tzFormat;
|
||||
String server;
|
||||
|
||||
static void read(NTPSettings &settings, JsonObject &root) {
|
||||
static void read(NTPSettings &settings, JsonVariant &root) {
|
||||
root["enabled"] = settings.enabled;
|
||||
root["server"] = settings.server;
|
||||
root["tz_label"] = settings.tzLabel;
|
||||
root["tz_format"] = settings.tzFormat;
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, NTPSettings &settings) {
|
||||
static StateUpdateResult update(JsonVariant &root, NTPSettings &settings) {
|
||||
settings.enabled = root["enabled"] | FACTORY_NTP_ENABLED;
|
||||
settings.server = root["server"] | FACTORY_NTP_SERVER;
|
||||
settings.tzLabel = root["tz_label"] | FACTORY_NTP_TIME_ZONE_LABEL;
|
||||
|
||||
@@ -34,13 +34,13 @@ class PeripheralsConfiguration {
|
||||
long frequency = I2C_FREQUENCY;
|
||||
std::vector<PinConfig> pins;
|
||||
|
||||
static void read(PeripheralsConfiguration &settings, JsonObject &root) {
|
||||
static void read(PeripheralsConfiguration &settings, JsonVariant &root) {
|
||||
root["sda"] = settings.sda;
|
||||
root["scl"] = settings.scl;
|
||||
root["frequency"] = settings.frequency;
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonObject &root, PeripheralsConfiguration &settings) {
|
||||
static StateUpdateResult update(JsonVariant &root, PeripheralsConfiguration &settings) {
|
||||
settings.sda = root["sda"] | SDA_PIN;
|
||||
settings.scl = root["scl"] | SCL_PIN;
|
||||
settings.frequency = root["frequency"] | I2C_FREQUENCY;
|
||||
|
||||
@@ -19,22 +19,22 @@ class ServoSettings {
|
||||
{306, -1, 0, 2.2, "Servo4"}, {306, -1, 45, 2.1055555, "Servo5"}, {306, -1, -90, 1.96923, "Servo6"},
|
||||
{306, 1, 0, 2.2, "Servo7"}, {306, 1, -45, 2.1055555, "Servo8"}, {306, 1, 90, 1.96923, "Servo9"},
|
||||
{306, 1, 0, 2.2, "Servo10"}, {306, -1, 45, 2.1055555, "Servo11"}, {306, -1, -90, 1.96923, "Servo12"}};
|
||||
static void read(ServoSettings &settings, JsonObject &root) {
|
||||
static void read(ServoSettings &settings, JsonVariant &root) {
|
||||
JsonArray servos = root["servos"].to<JsonArray>();
|
||||
for (auto &servo : settings.servos) {
|
||||
JsonObject newServo = servos.add<JsonObject>();
|
||||
JsonVariant newServo = servos.add<JsonVariant>();
|
||||
newServo["center_pwm"] = servo.centerPwm;
|
||||
newServo["direction"] = servo.direction;
|
||||
newServo["center_angle"] = servo.centerAngle;
|
||||
newServo["conversion"] = servo.conversion;
|
||||
}
|
||||
}
|
||||
static StateUpdateResult update(JsonObject &root, ServoSettings &settings) {
|
||||
static StateUpdateResult update(JsonVariant &root, ServoSettings &settings) {
|
||||
if (root["servos"].is<JsonArray>()) {
|
||||
JsonArray servosJson = root["servos"];
|
||||
int i = 0;
|
||||
for (auto servo : servosJson) {
|
||||
JsonObject servoObject = servo.as<JsonObject>();
|
||||
JsonVariant servoObject = servo.as<JsonVariant>();
|
||||
uint8_t servoId = i; // servoObject["id"].as<uint8_t>();
|
||||
settings.servos[servoId].centerPwm = servoObject["center_pwm"].as<float>();
|
||||
settings.servos[servoId].centerAngle = servoObject["center_angle"].as<float>();
|
||||
|
||||
@@ -37,7 +37,7 @@ typedef struct {
|
||||
IPAddress dnsIP2;
|
||||
bool available;
|
||||
|
||||
void serialize(JsonObject &json) const {
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["ssid"] = ssid;
|
||||
json["password"] = password;
|
||||
json["static_ip_config"] = staticIPConfig;
|
||||
@@ -50,7 +50,7 @@ typedef struct {
|
||||
}
|
||||
}
|
||||
|
||||
bool deserialize(const JsonObject &json) {
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
String newSsid = json["ssid"].as<String>();
|
||||
String newPassword = json["password"].as<String>();
|
||||
if (newSsid.length() < 1 || newSsid.length() > 31 || newPassword.length() > 64) {
|
||||
@@ -98,24 +98,24 @@ class WiFiSettings {
|
||||
String hostname;
|
||||
bool priorityBySignalStrength;
|
||||
std::vector<wifi_settings_t> wifiSettings;
|
||||
static void read(WiFiSettings &settings, JsonObject &root) {
|
||||
static void read(WiFiSettings &settings, JsonVariant &root) {
|
||||
root["hostname"] = settings.hostname;
|
||||
root["priority_RSSI"] = settings.priorityBySignalStrength;
|
||||
JsonArray wifiNetworks = root["wifi_networks"].to<JsonArray>();
|
||||
for (const auto &wifi : settings.wifiSettings) {
|
||||
JsonObject wifiNetwork = wifiNetworks.add<JsonObject>();
|
||||
JsonVariant wifiNetwork = wifiNetworks.add<JsonVariant>();
|
||||
wifi.serialize(wifiNetwork);
|
||||
}
|
||||
ESP_LOGV("WiFiSettings", "WiFi Settings read");
|
||||
}
|
||||
static StateUpdateResult update(JsonObject &root, WiFiSettings &settings) {
|
||||
static StateUpdateResult update(JsonVariant &root, WiFiSettings &settings) {
|
||||
settings.hostname = root["hostname"] | FACTORY_WIFI_HOSTNAME;
|
||||
settings.priorityBySignalStrength = root["priority_RSSI"] | true;
|
||||
settings.wifiSettings.clear();
|
||||
if (root["wifi_networks"].is<JsonArray>()) {
|
||||
JsonArray wifiNetworks = root["wifi_networks"];
|
||||
int networkCount = 0;
|
||||
for (JsonObject wifiNetwork : wifiNetworks) {
|
||||
for (JsonVariant wifiNetwork : wifiNetworks) {
|
||||
if (networkCount >= 5) {
|
||||
ESP_LOGE("WiFiSettings", "Too many wifi networks");
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user