Skip to content

Commit

Permalink
Introduce numeric error codes in power webapi
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Dec 23, 2022
1 parent 534f200 commit 5bc264f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/WebApi_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ enum WebApiError {
SecurityBase = 10000,
SecurityPasswordLength,
SecurityAuthSuccess,

PowerBase = 11000,
PowerSerialZero,
PowerInvalidInverter,
};
8 changes: 8 additions & 0 deletions src/WebApi_power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "WebApi_power.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include <AsyncJson.h>
#include <Hoymiles.h>

Expand Down Expand Up @@ -61,6 +62,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)

if (!request->hasParam("data", true)) {
retMsg[F("message")] = F("No values found!");
retMsg[F("code")] = WebApiError::GenericNoValueFound;
response->setLength();
request->send(response);
return;
Expand All @@ -70,6 +72,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)

if (json.length() > 1024) {
retMsg[F("message")] = F("Data too large!");
retMsg[F("code")] = WebApiError::GenericDataTooLarge;
response->setLength();
request->send(response);
return;
Expand All @@ -80,6 +83,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)

if (error) {
retMsg[F("message")] = F("Failed to parse data!");
retMsg[F("code")] = WebApiError::GenericParseError;
response->setLength();
request->send(response);
return;
Expand All @@ -88,13 +92,15 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
if (!(root.containsKey("serial")
&& (root.containsKey("power") || root.containsKey("restart")))) {
retMsg[F("message")] = F("Values are missing!");
retMsg[F("code")] = WebApiError::GenericValueMissing;
response->setLength();
request->send(response);
return;
}

if (root[F("serial")].as<uint64_t>() == 0) {
retMsg[F("message")] = F("Serial must be a number > 0!");
retMsg[F("code")] = WebApiError::PowerSerialZero;
response->setLength();
request->send(response);
return;
Expand All @@ -104,6 +110,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)
auto inv = Hoymiles.getInverterBySerial(serial);
if (inv == nullptr) {
retMsg[F("message")] = F("Invalid inverter specified!");
retMsg[F("code")] = WebApiError::PowerInvalidInverter;
response->setLength();
request->send(response);
return;
Expand All @@ -120,6 +127,7 @@ void WebApiPowerClass::onPowerPost(AsyncWebServerRequest* request)

retMsg[F("type")] = F("success");
retMsg[F("message")] = F("Settings saved!");
retMsg[F("code")] = WebApiError::GenericSuccess;

response->setLength();
request->send(response);
Expand Down

0 comments on commit 5bc264f

Please sign in to comment.