Skip to content

Commit

Permalink
Move parsing of serial from web request to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Apr 5, 2024
1 parent 980e847 commit ea28903
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 34 deletions.
1 change: 1 addition & 0 deletions include/WebApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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, JsonDocument& json_document);
static uint64_t parseSerialFromRequest(AsyncWebServerRequest* request, String param_name = "inv");
static bool sendJsonResponse(AsyncWebServerRequest* request, AsyncJsonResponse* response, const char* function, const uint16_t line);

private:
Expand Down
10 changes: 10 additions & 0 deletions src/WebApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ bool WebApiClass::parseRequestData(AsyncWebServerRequest* request, AsyncJsonResp
return true;
}

uint64_t WebApiClass::parseSerialFromRequest(AsyncWebServerRequest* request, String param_name)
{
if (request->hasParam(param_name)) {
String s = request->getParam(param_name)->value();
return strtoll(s.c_str(), NULL, 16);
}

return 0;
}

bool WebApiClass::sendJsonResponse(AsyncWebServerRequest* request, AsyncJsonResponse* response, const char* function, const uint16_t line)
{
bool ret_val = true;
Expand Down
8 changes: 1 addition & 7 deletions src/WebApi_devinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ void WebApiDevInfoClass::onDevInfoStatus(AsyncWebServerRequest* request)

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

uint64_t serial = 0;
if (request->hasParam("inv")) {
String s = request->getParam("inv")->value();
serial = strtoll(s.c_str(), NULL, 16);
}

auto serial = WebApi.parseSerialFromRequest(request);
auto inv = Hoymiles.getInverterBySerial(serial);

if (inv != nullptr) {
Expand Down
7 changes: 1 addition & 6 deletions src/WebApi_eventlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ void WebApiEventlogClass::onEventlogStatus(AsyncWebServerRequest* request)

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

uint64_t serial = 0;
if (request->hasParam("inv")) {
String s = request->getParam("inv")->value();
serial = strtoll(s.c_str(), NULL, 16);
}
auto serial = WebApi.parseSerialFromRequest(request);

AlarmMessageLocale_t locale = AlarmMessageLocale_t::EN;
if (request->hasParam("locale")) {
Expand Down
16 changes: 2 additions & 14 deletions src/WebApi_gridprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ void WebApiGridProfileClass::onGridProfileStatus(AsyncWebServerRequest* request)

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

uint64_t serial = 0;
if (request->hasParam("inv")) {
String s = request->getParam("inv")->value();
serial = strtoll(s.c_str(), NULL, 16);
}

auto serial = WebApi.parseSerialFromRequest(request);
auto inv = Hoymiles.getInverterBySerial(serial);

if (inv != nullptr) {
Expand Down Expand Up @@ -66,13 +60,7 @@ void WebApiGridProfileClass::onGridProfileRawdata(AsyncWebServerRequest* request

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

uint64_t serial = 0;
if (request->hasParam("inv")) {
String s = request->getParam("inv")->value();
serial = strtoll(s.c_str(), NULL, 16);
}

auto serial = WebApi.parseSerialFromRequest(request);
auto inv = Hoymiles.getInverterBySerial(serial);

if (inv != nullptr) {
Expand Down
8 changes: 1 addition & 7 deletions src/WebApi_ws_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,8 @@ void WebApiWsLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
std::lock_guard<std::mutex> lock(_mutex);
AsyncJsonResponse* response = new AsyncJsonResponse();
auto& root = response->getRoot();

auto invArray = root["inverters"].to<JsonArray>();

uint64_t serial = 0;
if (request->hasParam("inv")) {
String s = request->getParam("inv")->value();
serial = strtoll(s.c_str(), NULL, 16);
}
auto serial = WebApi.parseSerialFromRequest(request);

if (serial > 0) {
auto inv = Hoymiles.getInverterBySerial(serial);
Expand Down

0 comments on commit ea28903

Please sign in to comment.