Skip to content

Commit fabaf4b

Browse files
committed
WebApi_sunspec: merge fix
1 parent 1a7bc13 commit fabaf4b

File tree

1 file changed

+10
-44
lines changed

1 file changed

+10
-44
lines changed

src/WebApi_sunspec.cpp

+10-44
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ void WebApiSunSpecClass::onSunSpecGet(AsyncWebServerRequest* request)
7474
}
7575
}
7676

77-
response->setLength();
78-
request->send(response);
77+
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
7978
}
8079

8180
void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
@@ -84,44 +83,17 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
8483
return;
8584
}
8685

87-
bool reboot{false};
8886
AsyncJsonResponse* response = new AsyncJsonResponse();
89-
auto& retMsg = response->getRoot();
90-
retMsg["type"] = "warning";
91-
92-
if (!request->hasParam("data", true)) {
93-
retMsg["message"] = "No values found!";
94-
retMsg["code"] = WebApiError::GenericNoValueFound;
95-
response->setLength();
96-
request->send(response);
97-
return;
98-
}
99-
100-
const String json = request->getParam("data", true)->value();
101-
102-
if (json.length() > 1024) {
103-
retMsg["message"] = "Data too large!";
104-
retMsg["code"] = WebApiError::GenericDataTooLarge;
105-
response->setLength();
106-
request->send(response);
107-
return;
108-
}
109-
11087
JsonDocument root;
111-
const DeserializationError error = deserializeJson(root, json);
112-
113-
if (error) {
114-
retMsg["message"] = "Failed to parse data!";
115-
retMsg["code"] = WebApiError::GenericParseError;
116-
response->setLength();
117-
request->send(response);
88+
if (!WebApi.parseRequestData(request, response, root)) {
11889
return;
11990
}
12091

121-
auto& config = Configuration.get();
92+
auto& retMsg = response->getRoot();
12293

94+
auto& config = Configuration.get();
12395
auto sunspec_enabled = root["enabled"].as<bool>();
124-
reboot |= (config.SunSpec.Enabled != sunspec_enabled);
96+
bool reboot = (config.SunSpec.Enabled != sunspec_enabled);
12597

12698
config.SunSpec.Enabled = sunspec_enabled;
12799
config.SunSpec.RemoteControl = root["remote_control"].as<bool>();
@@ -135,34 +107,30 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
135107
if (inverterArray.size() > INV_MAX_COUNT) {
136108
retMsg["message"] = "Invalid amount of max channel setting given!";
137109
retMsg["code"] = WebApiError::InverterInvalidMaxChannel;
138-
response->setLength();
139-
request->send(response);
110+
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
140111
return;
141112
}
142113

143114
for (JsonVariant item : inverterArray) {
144115
if (!(item.containsKey("id") && item.containsKey("enabled") && item.containsKey("max_power") && item.containsKey("channel_ac"))) {
145116
retMsg["message"] = "Values are missing!";
146117
retMsg["code"] = WebApiError::GenericValueMissing;
147-
response->setLength();
148-
request->send(response);
118+
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
149119
return;
150120
}
151121

152122
if (item["id"].as<uint8_t>() > INV_MAX_COUNT - 1) {
153123
retMsg["message"] = "Invalid ID specified!";
154124
retMsg["code"] = WebApiError::InverterInvalidId;
155-
response->setLength();
156-
request->send(response);
125+
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
157126
return;
158127
}
159128

160129
JsonArray channelArrayAC = item["channel_ac"].as<JsonArray>();
161130
if (channelArrayAC.size() == 0 || channelArrayAC.size() > INV_MAX_CHAN_COUNT) {
162131
retMsg["message"] = "Invalid amount of max channel setting given!";
163132
retMsg["code"] = WebApiError::InverterInvalidMaxChannel;
164-
response->setLength();
165-
request->send(response);
133+
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
166134
return;
167135
}
168136

@@ -178,7 +146,6 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
178146
}
179147
}
180148

181-
182149
if(reboot) {
183150
WebApi.writeConfig(retMsg, WebApiError::MaintenanceRebootTriggered, "Inverter SunSpec configuration changed!");
184151
Utils::restartDtu();
@@ -187,6 +154,5 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
187154
WebApi.writeConfig(retMsg, WebApiError::SunSpecSettingsChanged, "Inverter SunSpec configuration changed!");
188155
}
189156

190-
response->setLength();
191-
request->send(response);
157+
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
192158
}

0 commit comments

Comments
 (0)