Skip to content

Commit

Permalink
Feature: Migrated ArduinoJson 6 to 7
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Apr 5, 2024
1 parent e7a9c96 commit 2e3125f
Show file tree
Hide file tree
Showing 26 changed files with 149 additions and 152 deletions.
2 changes: 0 additions & 2 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

#define DEV_MAX_MAPPING_NAME_STRLEN 63

#define JSON_BUFFER_SIZE 12288

struct CHANNEL_CONFIG_T {
uint16_t MaxChannelPower;
char Name[CHAN_MAX_NAME_STRLEN];
Expand Down
6 changes: 3 additions & 3 deletions include/MqttHandleHass.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class MqttHandleHassClass {
void publishInverterNumber(std::shared_ptr<InverterAbstract> inv, const char* caption, const char* icon, const char* category, const char* commandTopic, const char* stateTopic, const char* unitOfMeasure, const int16_t min = 1, const int16_t max = 100);
void publishInverterBinarySensor(std::shared_ptr<InverterAbstract> inv, const char* caption, const char* subTopic, const char* payload_on, const char* payload_off);

static void createInverterInfo(DynamicJsonDocument& doc, std::shared_ptr<InverterAbstract> inv);
static void createDtuInfo(DynamicJsonDocument& doc);
static void createInverterInfo(JsonDocument& doc, std::shared_ptr<InverterAbstract> inv);
static void createDtuInfo(JsonDocument& doc);

static void createDeviceInfo(DynamicJsonDocument& doc, const String& name, const String& identifiers, const String& configuration_url, const String& manufacturer, const String& model, const String& sw_version, const String& via_device = "");
static void createDeviceInfo(JsonDocument& doc, const String& name, const String& identifiers, const String& configuration_url, const String& manufacturer, const String& model, const String& sw_version, const String& via_device = "");

static String getDtuUniqueId();
static String getDtuUrl();
Expand Down
2 changes: 1 addition & 1 deletion include/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class Utils {
static uint64_t generateDtuSerial();
static int getTimezoneOffset();
static void restartDtu();
static bool checkJsonAlloc(const DynamicJsonDocument& doc, const char* function, const uint16_t line);
static bool checkJsonAlloc(const JsonDocument& doc, const char* function, const uint16_t line);
static void removeAllFiles();
};
2 changes: 1 addition & 1 deletion include/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WebApiClass {

static void writeConfig(JsonVariant& retMsg, const WebApiError code = WebApiError::GenericSuccess, const String& message = "Settings saved!");

static bool parseRequestData(AsyncWebServerRequest* request, AsyncJsonResponse* response, DynamicJsonDocument& json_document, size_t max_document_size = 1024);
static bool parseRequestData(AsyncWebServerRequest* request, AsyncJsonResponse* response, JsonDocument& json_document);

private:
AsyncWebServer _server;
Expand Down
2 changes: 1 addition & 1 deletion include/WebApi_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum WebApiError {
GenericBase = 1000,
GenericSuccess,
GenericNoValueFound,
GenericDataTooLarge,
GenericDataTooLarge, // not used anymore
GenericParseError,
GenericValueMissing,
GenericWriteFailed,
Expand Down
2 changes: 0 additions & 2 deletions include/WebApi_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <ESPAsyncWebServer.h>
#include <TaskSchedulerDeclarations.h>

#define MQTT_JSON_DOC_SIZE 10240

class WebApiMqttClass {
public:
void init(AsyncWebServer& server, Scheduler& scheduler);
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ build_unflags =

lib_deps =
mathieucarbou/ESP Async WebServer @ 2.9.0
bblanchon/ArduinoJson @ ^6.21.5
bblanchon/ArduinoJson @ ^7.0.4
https://github.com/bertmelis/espMqttClient.git#v1.6.0
nrf24/RF24 @ ^1.4.8
olikraus/U8g2 @ ^2.35.15
Expand Down
66 changes: 33 additions & 33 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ bool ConfigurationClass::write()
}
config.Cfg.SaveCount++;

DynamicJsonDocument doc(JSON_BUFFER_SIZE);
JsonDocument doc;

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return false;
}

JsonObject cfg = doc.createNestedObject("cfg");
JsonObject cfg = doc["cfg"].to<JsonObject>();
cfg["version"] = config.Cfg.Version;
cfg["save_count"] = config.Cfg.SaveCount;

JsonObject wifi = doc.createNestedObject("wifi");
JsonObject wifi = doc["wifi"].to<JsonObject>();
wifi["ssid"] = config.WiFi.Ssid;
wifi["password"] = config.WiFi.Password;
wifi["ip"] = IPAddress(config.WiFi.Ip).toString();
Expand All @@ -47,18 +43,18 @@ bool ConfigurationClass::write()
wifi["hostname"] = config.WiFi.Hostname;
wifi["aptimeout"] = config.WiFi.ApTimeout;

JsonObject mdns = doc.createNestedObject("mdns");
JsonObject mdns = doc["mdns"].to<JsonObject>();
mdns["enabled"] = config.Mdns.Enabled;

JsonObject ntp = doc.createNestedObject("ntp");
JsonObject ntp = doc["ntp"].to<JsonObject>();
ntp["server"] = config.Ntp.Server;
ntp["timezone"] = config.Ntp.Timezone;
ntp["timezone_descr"] = config.Ntp.TimezoneDescr;
ntp["latitude"] = config.Ntp.Latitude;
ntp["longitude"] = config.Ntp.Longitude;
ntp["sunsettype"] = config.Ntp.SunsetType;

JsonObject mqtt = doc.createNestedObject("mqtt");
JsonObject mqtt = doc["mqtt"].to<JsonObject>();
mqtt["enabled"] = config.Mqtt.Enabled;
mqtt["hostname"] = config.Mqtt.Hostname;
mqtt["port"] = config.Mqtt.Port;
Expand All @@ -69,42 +65,42 @@ bool ConfigurationClass::write()
mqtt["publish_interval"] = config.Mqtt.PublishInterval;
mqtt["clean_session"] = config.Mqtt.CleanSession;

JsonObject mqtt_lwt = mqtt.createNestedObject("lwt");
JsonObject mqtt_lwt = mqtt["lwt"].to<JsonObject>();
mqtt_lwt["topic"] = config.Mqtt.Lwt.Topic;
mqtt_lwt["value_online"] = config.Mqtt.Lwt.Value_Online;
mqtt_lwt["value_offline"] = config.Mqtt.Lwt.Value_Offline;
mqtt_lwt["qos"] = config.Mqtt.Lwt.Qos;

JsonObject mqtt_tls = mqtt.createNestedObject("tls");
JsonObject mqtt_tls = mqtt["tls"].to<JsonObject>();
mqtt_tls["enabled"] = config.Mqtt.Tls.Enabled;
mqtt_tls["root_ca_cert"] = config.Mqtt.Tls.RootCaCert;
mqtt_tls["certlogin"] = config.Mqtt.Tls.CertLogin;
mqtt_tls["client_cert"] = config.Mqtt.Tls.ClientCert;
mqtt_tls["client_key"] = config.Mqtt.Tls.ClientKey;

JsonObject mqtt_hass = mqtt.createNestedObject("hass");
JsonObject mqtt_hass = mqtt["hass"].to<JsonObject>();
mqtt_hass["enabled"] = config.Mqtt.Hass.Enabled;
mqtt_hass["retain"] = config.Mqtt.Hass.Retain;
mqtt_hass["topic"] = config.Mqtt.Hass.Topic;
mqtt_hass["individual_panels"] = config.Mqtt.Hass.IndividualPanels;
mqtt_hass["expire"] = config.Mqtt.Hass.Expire;

JsonObject dtu = doc.createNestedObject("dtu");
JsonObject dtu = doc["dtu"].to<JsonObject>();
dtu["serial"] = config.Dtu.Serial;
dtu["poll_interval"] = config.Dtu.PollInterval;
dtu["nrf_pa_level"] = config.Dtu.Nrf.PaLevel;
dtu["cmt_pa_level"] = config.Dtu.Cmt.PaLevel;
dtu["cmt_frequency"] = config.Dtu.Cmt.Frequency;
dtu["cmt_country_mode"] = config.Dtu.Cmt.CountryMode;

JsonObject security = doc.createNestedObject("security");
JsonObject security = doc["security"].to<JsonObject>();
security["password"] = config.Security.Password;
security["allow_readonly"] = config.Security.AllowReadonly;

JsonObject device = doc.createNestedObject("device");
JsonObject device = doc["device"].to<JsonObject>();
device["pinmapping"] = config.Dev_PinMapping;

JsonObject display = device.createNestedObject("display");
JsonObject display = device["display"].to<JsonObject>();
display["powersafe"] = config.Display.PowerSafe;
display["screensaver"] = config.Display.ScreenSaver;
display["rotation"] = config.Display.Rotation;
Expand All @@ -113,15 +109,15 @@ bool ConfigurationClass::write()
display["diagram_duration"] = config.Display.Diagram.Duration;
display["diagram_mode"] = config.Display.Diagram.Mode;

JsonArray leds = device.createNestedArray("led");
JsonArray leds = device["led"].to<JsonArray>();
for (uint8_t i = 0; i < PINMAPPING_LED_COUNT; i++) {
JsonObject led = leds.createNestedObject();
JsonObject led = leds.add<JsonObject>();
led["brightness"] = config.Led_Single[i].Brightness;
}

JsonArray inverters = doc.createNestedArray("inverters");
JsonArray inverters = doc["inverters"].to<JsonArray>();
for (uint8_t i = 0; i < INV_MAX_COUNT; i++) {
JsonObject inv = inverters.createNestedObject();
JsonObject inv = inverters.add<JsonObject>();
inv["serial"] = config.Inverter[i].Serial;
inv["name"] = config.Inverter[i].Name;
inv["order"] = config.Inverter[i].Order;
Expand All @@ -134,15 +130,19 @@ bool ConfigurationClass::write()
inv["zero_day"] = config.Inverter[i].ZeroYieldDayOnMidnight;
inv["yieldday_correction"] = config.Inverter[i].YieldDayCorrection;

JsonArray channel = inv.createNestedArray("channel");
JsonArray channel = inv["channel"].to<JsonArray>();
for (uint8_t c = 0; c < INV_MAX_CHAN_COUNT; c++) {
JsonObject chanData = channel.createNestedObject();
JsonObject chanData = channel.add<JsonObject>();
chanData["name"] = config.Inverter[i].channel[c].Name;
chanData["max_power"] = config.Inverter[i].channel[c].MaxChannelPower;
chanData["yield_total_offset"] = config.Inverter[i].channel[c].YieldTotalOffset;
}
}

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return false;
}

// Serialize JSON to file
if (serializeJson(doc, f) == 0) {
MessageOutput.println("Failed to write file");
Expand All @@ -157,18 +157,18 @@ bool ConfigurationClass::read()
{
File f = LittleFS.open(CONFIG_FILENAME, "r", false);

DynamicJsonDocument doc(JSON_BUFFER_SIZE);

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return false;
}
JsonDocument doc;

// Deserialize the JSON document
const DeserializationError error = deserializeJson(doc, f);
if (error) {
MessageOutput.println("Failed to read file, using default configuration");
}

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return false;
}

JsonObject cfg = doc["cfg"];
config.Cfg.Version = cfg["version"] | CONFIG_VERSION;
config.Cfg.SaveCount = cfg["save_count"] | 0;
Expand Down Expand Up @@ -324,11 +324,7 @@ void ConfigurationClass::migrate()
return;
}

DynamicJsonDocument doc(JSON_BUFFER_SIZE);

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return;
}
JsonDocument doc;

// Deserialize the JSON document
const DeserializationError error = deserializeJson(doc, f);
Expand All @@ -337,6 +333,10 @@ void ConfigurationClass::migrate()
return;
}

if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) {
return;
}

if (config.Cfg.Version < 0x00011700) {
JsonArray inverters = doc["inverters"];
for (uint8_t i = 0; i < INV_MAX_COUNT; i++) {
Expand Down
Loading

0 comments on commit 2e3125f

Please sign in to comment.