Skip to content

Commit

Permalink
Use auto keyword and references more often
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Jan 4, 2024
1 parent b0b9764 commit 3c37b61
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion include/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class WebApiClass {

static void sendTooManyRequests(AsyncWebServerRequest* request);

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

private:
void loop();
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void WebApiClass::sendTooManyRequests(AsyncWebServerRequest* request)
request->send(response);
}

void WebApiClass::writeConfig(JsonObject& retMsg, const WebApiError code, const String& message)
void WebApiClass::writeConfig(JsonVariant& retMsg, const WebApiError code, const String& message)
{
if (!Configuration.write()) {
retMsg["message"] = "Write failed!";
Expand Down
6 changes: 3 additions & 3 deletions src/WebApi_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void WebApiConfigClass::onConfigDelete(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down Expand Up @@ -125,8 +125,8 @@ void WebApiConfigClass::onConfigListGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
JsonArray data = root.createNestedArray("configs");
auto& root = response->getRoot();
auto data = root.createNestedArray("configs");

File rootfs = LittleFS.open("/");
File file = rootfs.openNextFile();
Expand Down
22 changes: 11 additions & 11 deletions src/WebApi_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ void WebApiDeviceClass::onDeviceAdminGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, MQTT_JSON_DOC_SIZE);
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();
const PinMapping_t& pin = PinMapping.get();

JsonObject curPin = root.createNestedObject("curPin");
auto curPin = root.createNestedObject("curPin");
curPin["name"] = config.Dev_PinMapping;

JsonObject nrfPinObj = curPin.createNestedObject("nrf24");
auto nrfPinObj = curPin.createNestedObject("nrf24");
nrfPinObj["clk"] = pin.nrf24_clk;
nrfPinObj["cs"] = pin.nrf24_cs;
nrfPinObj["en"] = pin.nrf24_en;
nrfPinObj["irq"] = pin.nrf24_irq;
nrfPinObj["miso"] = pin.nrf24_miso;
nrfPinObj["mosi"] = pin.nrf24_mosi;

JsonObject cmtPinObj = curPin.createNestedObject("cmt");
auto cmtPinObj = curPin.createNestedObject("cmt");
cmtPinObj["clk"] = pin.cmt_clk;
cmtPinObj["cs"] = pin.cmt_cs;
cmtPinObj["fcs"] = pin.cmt_fcs;
cmtPinObj["sdio"] = pin.cmt_sdio;
cmtPinObj["gpio2"] = pin.cmt_gpio2;
cmtPinObj["gpio3"] = pin.cmt_gpio3;

JsonObject ethPinObj = curPin.createNestedObject("eth");
auto ethPinObj = curPin.createNestedObject("eth");
ethPinObj["enabled"] = pin.eth_enabled;
ethPinObj["phy_addr"] = pin.eth_phy_addr;
ethPinObj["power"] = pin.eth_power;
Expand All @@ -65,29 +65,29 @@ void WebApiDeviceClass::onDeviceAdminGet(AsyncWebServerRequest* request)
ethPinObj["type"] = pin.eth_type;
ethPinObj["clk_mode"] = pin.eth_clk_mode;

JsonObject displayPinObj = curPin.createNestedObject("display");
auto displayPinObj = curPin.createNestedObject("display");
displayPinObj["type"] = pin.display_type;
displayPinObj["data"] = pin.display_data;
displayPinObj["clk"] = pin.display_clk;
displayPinObj["cs"] = pin.display_cs;
displayPinObj["reset"] = pin.display_reset;

JsonObject ledPinObj = curPin.createNestedObject("led");
auto ledPinObj = curPin.createNestedObject("led");
for (uint8_t i = 0; i < PINMAPPING_LED_COUNT; i++) {
ledPinObj["led" + String(i)] = pin.led[i];
}

JsonObject display = root.createNestedObject("display");
auto display = root.createNestedObject("display");
display["rotation"] = config.Display.Rotation;
display["power_safe"] = config.Display.PowerSafe;
display["screensaver"] = config.Display.ScreenSaver;
display["contrast"] = config.Display.Contrast;
display["language"] = config.Display.Language;
display["diagramduration"] = config.Display.DiagramDuration;

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

Expand All @@ -102,7 +102,7 @@ void WebApiDeviceClass::onDeviceAdminPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, MQTT_JSON_DOC_SIZE);
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_devinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void WebApiDevInfoClass::onDevInfoStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();

uint64_t serial = 0;
if (request->hasParam("inv")) {
Expand Down
4 changes: 2 additions & 2 deletions src/WebApi_dtu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void WebApiDtuClass::onDtuAdminGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();

// DTU Serial is read as HEX
Expand All @@ -57,7 +57,7 @@ void WebApiDtuClass::onDtuAdminPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_eventlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void WebApiEventlogClass::onEventlogStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, 2048);
JsonObject root = response->getRoot();
auto& root = response->getRoot();

uint64_t serial = 0;
if (request->hasParam("inv")) {
Expand Down
4 changes: 2 additions & 2 deletions src/WebApi_gridprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void WebApiGridProfileClass::onGridProfileStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, 8192);
JsonObject root = response->getRoot();
auto& root = response->getRoot();

uint64_t serial = 0;
if (request->hasParam("inv")) {
Expand Down Expand Up @@ -72,7 +72,7 @@ void WebApiGridProfileClass::onGridProfileRawdata(AsyncWebServerRequest* request
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, 4096);
JsonObject root = response->getRoot();
auto& root = response->getRoot();

uint64_t serial = 0;
if (request->hasParam("inv")) {
Expand Down
10 changes: 5 additions & 5 deletions src/WebApi_inverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void WebApiInverterClass::onInverterList(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, 768 * INV_MAX_COUNT);
JsonObject root = response->getRoot();
auto& root = response->getRoot();
JsonArray data = root.createNestedArray("inverter");

const CONFIG_T& config = Configuration.get();
Expand Down Expand Up @@ -94,7 +94,7 @@ void WebApiInverterClass::onInverterAdd(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down Expand Up @@ -191,7 +191,7 @@ void WebApiInverterClass::onInverterEdit(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down Expand Up @@ -333,7 +333,7 @@ void WebApiInverterClass::onInverterDelete(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down Expand Up @@ -404,7 +404,7 @@ void WebApiInverterClass::onInverterOrder(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
4 changes: 2 additions & 2 deletions src/WebApi_limit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void WebApiLimitClass::onLimitStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();

for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
Expand Down Expand Up @@ -64,7 +64,7 @@ void WebApiLimitClass::onLimitPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_maintenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void WebApiMaintenanceClass::onRebootPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, MQTT_JSON_DOC_SIZE);
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/WebApi_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void WebApiMqttClass::onMqttStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, MQTT_JSON_DOC_SIZE);
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();

root["mqtt_enabled"] = config.Mqtt.Enabled;
Expand Down Expand Up @@ -67,7 +67,7 @@ void WebApiMqttClass::onMqttAdminGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, MQTT_JSON_DOC_SIZE);
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();

root["mqtt_enabled"] = config.Mqtt.Enabled;
Expand Down Expand Up @@ -105,7 +105,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse(false, MQTT_JSON_DOC_SIZE);
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/WebApi_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void WebApiNetworkClass::onNetworkStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();

root["sta_status"] = ((WiFi.getMode() & WIFI_STA) != 0);
root["sta_ssid"] = WiFi.SSID();
Expand Down Expand Up @@ -63,7 +63,7 @@ void WebApiNetworkClass::onNetworkAdminGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();

root["hostname"] = config.WiFi.Hostname;
Expand All @@ -89,7 +89,7 @@ void WebApiNetworkClass::onNetworkAdminPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
10 changes: 5 additions & 5 deletions src/WebApi_ntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void WebApiNtpClass::onNtpStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();

root["ntp_server"] = config.Ntp.Server;
Expand Down Expand Up @@ -80,7 +80,7 @@ void WebApiNtpClass::onNtpAdminGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();

root["ntp_server"] = config.Ntp.Server;
Expand All @@ -101,7 +101,7 @@ void WebApiNtpClass::onNtpAdminPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down Expand Up @@ -198,7 +198,7 @@ void WebApiNtpClass::onNtpTimeGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();

struct tm timeinfo;
if (!getLocalTime(&timeinfo, 5)) {
Expand All @@ -225,7 +225,7 @@ void WebApiNtpClass::onNtpTimePost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
4 changes: 2 additions & 2 deletions src/WebApi_power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void WebApiPowerClass::onPowerStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();

for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);
Expand Down Expand Up @@ -57,7 +57,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/WebApi_security.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void WebApiSecurityClass::onSecurityGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();
const CONFIG_T& config = Configuration.get();

root["password"] = config.Security.Password;
Expand All @@ -48,7 +48,7 @@ void WebApiSecurityClass::onSecurityPost(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
Expand Down Expand Up @@ -115,7 +115,7 @@ void WebApiSecurityClass::onAuthenticateGet(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject retMsg = response->getRoot();
auto& retMsg = response->getRoot();
retMsg["type"] = "success";
retMsg["message"] = "Authentication successful!";
retMsg["code"] = WebApiError::SecurityAuthSuccess;
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_sysstatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void WebApiSysstatusClass::onSystemStatus(AsyncWebServerRequest* request)
}

AsyncJsonResponse* response = new AsyncJsonResponse();
JsonObject root = response->getRoot();
auto& root = response->getRoot();

root["hostname"] = NetworkSettings.getHostname();

Expand Down
Loading

0 comments on commit 3c37b61

Please sign in to comment.