From d57a5f7ea62181facd19c364755f5d67daa757d1 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Fri, 16 Sep 2022 18:18:32 +0200 Subject: [PATCH] Added web api to set limit --- include/WebApi_limit.h | 1 + src/WebApi_limit.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/WebApi_limit.h b/include/WebApi_limit.h index 59ed4e7..026f7ef 100644 --- a/include/WebApi_limit.h +++ b/include/WebApi_limit.h @@ -10,6 +10,7 @@ public: private: void onLimitStatus(AsyncWebServerRequest* request); + void onLimitPost(AsyncWebServerRequest* request); AsyncWebServer* _server; }; \ No newline at end of file diff --git a/src/WebApi_limit.cpp b/src/WebApi_limit.cpp index 8d4de0e..b22bc7a 100644 --- a/src/WebApi_limit.cpp +++ b/src/WebApi_limit.cpp @@ -14,6 +14,7 @@ void WebApiLimitClass::init(AsyncWebServer* server) _server = server; _server->on("/api/limit/status", HTTP_GET, std::bind(&WebApiLimitClass::onLimitStatus, this, _1)); + _server->on("/api/limit/config", HTTP_POST, std::bind(&WebApiLimitClass::onLimitPost, this, _1)); } void WebApiLimitClass::loop() @@ -37,6 +38,75 @@ void WebApiLimitClass::onLimitStatus(AsyncWebServerRequest* request) root[buffer]["limit"] = inv->SystemConfigPara()->getLimitPercent(); } + response->setLength(); + request->send(response); +} + +void WebApiLimitClass::onLimitPost(AsyncWebServerRequest* request) +{ + AsyncJsonResponse* response = new AsyncJsonResponse(); + JsonObject retMsg = response->getRoot(); + retMsg[F("type")] = F("warning"); + + if (!request->hasParam("data", true)) { + retMsg[F("message")] = F("No values found!"); + response->setLength(); + request->send(response); + return; + } + + String json = request->getParam("data", true)->value(); + + if (json.length() > 1024) { + retMsg[F("message")] = F("Data too large!"); + response->setLength(); + request->send(response); + return; + } + + DynamicJsonDocument root(1024); + DeserializationError error = deserializeJson(root, json); + + if (error) { + retMsg[F("message")] = F("Failed to parse data!"); + response->setLength(); + request->send(response); + return; + } + + if (!(root.containsKey("serial") + && root.containsKey("limit_value") + && root.containsKey("limit_type"))) { + retMsg[F("message")] = F("Values are missing!"); + response->setLength(); + request->send(response); + return; + } + + if (root[F("serial")].as() == 0) { + retMsg[F("message")] = F("Serial must be a number > 0!"); + response->setLength(); + request->send(response); + return; + } + + if (root[F("limit_value")].as() == 0 || root[F("limit_value")].as() > 1500) { + retMsg[F("message")] = F("Limit must between 1 and 1500!"); + response->setLength(); + request->send(response); + return; + } + + uint64_t serial = strtoll(root[F("serial")].as().c_str(), NULL, 16); + uint64_t limit = root[F("serial")].as(); + + auto inv = Hoymiles.getInverterBySerial(serial); + + inv->sendActivePowerControlRequest(Hoymiles.getRadio(), limit, PowerLimitControlType::RelativNonPersistent); + + retMsg[F("type")] = F("success"); + retMsg[F("message")] = F("Settings saved!"); + response->setLength(); request->send(response); } \ No newline at end of file