Skip to content

Commit

Permalink
WebApi_sunspec: merge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pipelka committed May 3, 2024
1 parent 1a7bc13 commit fabaf4b
Showing 1 changed file with 10 additions and 44 deletions.
54 changes: 10 additions & 44 deletions src/WebApi_sunspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ void WebApiSunSpecClass::onSunSpecGet(AsyncWebServerRequest* request)
}
}

response->setLength();
request->send(response);
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
}

void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
Expand All @@ -84,44 +83,17 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
return;
}

bool reboot{false};
AsyncJsonResponse* response = new AsyncJsonResponse();
auto& retMsg = response->getRoot();
retMsg["type"] = "warning";

if (!request->hasParam("data", true)) {
retMsg["message"] = "No values found!";
retMsg["code"] = WebApiError::GenericNoValueFound;
response->setLength();
request->send(response);
return;
}

const String json = request->getParam("data", true)->value();

if (json.length() > 1024) {
retMsg["message"] = "Data too large!";
retMsg["code"] = WebApiError::GenericDataTooLarge;
response->setLength();
request->send(response);
return;
}

JsonDocument root;
const DeserializationError error = deserializeJson(root, json);

if (error) {
retMsg["message"] = "Failed to parse data!";
retMsg["code"] = WebApiError::GenericParseError;
response->setLength();
request->send(response);
if (!WebApi.parseRequestData(request, response, root)) {
return;
}

auto& config = Configuration.get();
auto& retMsg = response->getRoot();

auto& config = Configuration.get();
auto sunspec_enabled = root["enabled"].as<bool>();
reboot |= (config.SunSpec.Enabled != sunspec_enabled);
bool reboot = (config.SunSpec.Enabled != sunspec_enabled);

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

for (JsonVariant item : inverterArray) {
if (!(item.containsKey("id") && item.containsKey("enabled") && item.containsKey("max_power") && item.containsKey("channel_ac"))) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
response->setLength();
request->send(response);
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
return;
}

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

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

Expand All @@ -178,7 +146,6 @@ void WebApiSunSpecClass::onSunSpecPost(AsyncWebServerRequest* request)
}
}


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

response->setLength();
request->send(response);
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
}

0 comments on commit fabaf4b

Please sign in to comment.