Skip to content

Commit

Permalink
avoid deprecated containsKey() method of ArduinoJson 7.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
schlimmchen committed Sep 21, 2024
1 parent 38726b9 commit 97f95f8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/WebApi_Huawei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void WebApiHuaweiClass::onPost(AsyncWebServerRequest* request)

auto& retMsg = response->getRoot();

if (root.containsKey("online")) {
if (root["online"].is<bool>()) {
online = root["online"].as<bool>();
if (online) {
minimal_voltage = HUAWEI_MINIMAL_ONLINE_VOLTAGE;
Expand All @@ -98,7 +98,7 @@ void WebApiHuaweiClass::onPost(AsyncWebServerRequest* request)
return;
}

if (root.containsKey("voltage_valid")) {
if (root["voltage_valid"].is<bool>()) {
if (root["voltage_valid"].as<bool>()) {
if (root["voltage"].as<float>() < minimal_voltage || root["voltage"].as<float>() > 58) {
retMsg["message"] = "voltage not in range between 42 (online)/48 (offline and 58V !";
Expand All @@ -119,7 +119,7 @@ void WebApiHuaweiClass::onPost(AsyncWebServerRequest* request)
}
}

if (root.containsKey("current_valid")) {
if (root["current_valid"].is<bool>()) {
if (root["current_valid"].as<bool>()) {
if (root["current"].as<float>() < 0 || root["current"].as<float>() > 60) {
retMsg["message"] = "current must be in range between 0 and 60!";
Expand Down Expand Up @@ -186,13 +186,13 @@ void WebApiHuaweiClass::onAdminPost(AsyncWebServerRequest* request)

auto& retMsg = response->getRoot();

if (!(root.containsKey("enabled")) ||
!(root.containsKey("can_controller_frequency")) ||
!(root.containsKey("auto_power_enabled")) ||
!(root.containsKey("emergency_charge_enabled")) ||
!(root.containsKey("voltage_limit")) ||
!(root.containsKey("lower_power_limit")) ||
!(root.containsKey("upper_power_limit"))) {
if (!(root["enabled"].is<bool>()) ||
!(root["can_controller_frequency"].is<uint32_t>()) ||
!(root["auto_power_enabled"].is<bool>()) ||
!(root["emergency_charge_enabled"].is<bool>()) ||
!(root["voltage_limit"].is<float>()) ||
!(root["lower_power_limit"].is<float>()) ||
!(root["upper_power_limit"].is<float>())) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
response->setLength();
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void WebApiBatteryClass::onAdminPost(AsyncWebServerRequest* request)

auto& retMsg = response->getRoot();

if (!root.containsKey("enabled") || !root.containsKey("provider")) {
if (!root["enabled"].is<bool>() || !root["provider"].is<uint8_t>()) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
Expand Down
2 changes: 1 addition & 1 deletion src/WebApi_powerlimiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void WebApiPowerLimiterClass::onAdminPost(AsyncWebServerRequest* request)
// anyways to always include the keys accessed below. if we wanted to
// support a simpler API, like only sending the "enabled" key which only
// changes that key, we need to refactor all of the code below.
if (!root.containsKey("enabled")) {
if (!root["enabled"].is<bool>()) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
response->setLength();
Expand Down
8 changes: 4 additions & 4 deletions src/WebApi_powermeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ void WebApiPowerMeterClass::onAdminPost(AsyncWebServerRequest* request)

auto& retMsg = response->getRoot();

if (!(root.containsKey("enabled") && root.containsKey("source"))) {
if (!(root["enabled"].is<bool>() && root["source"].is<uint32_t>())) {
retMsg["message"] = "Values are missing!";
response->setLength();
request->send(response);
return;
}

auto checkHttpConfig = [&](JsonObject const& cfg) -> bool {
if (!cfg.containsKey("url")
if (!cfg["url"].is<String>()
|| (!cfg["url"].as<String>().startsWith("http://")
&& !cfg["url"].as<String>().startsWith("https://"))) {
retMsg["message"] = "URL must either start with http:// or https://!";
Expand All @@ -107,7 +107,7 @@ void WebApiPowerMeterClass::onAdminPost(AsyncWebServerRequest* request)
return false;
}

if (!cfg.containsKey("timeout")
if (!cfg["timeout"].is<uint16_t>()
|| cfg["timeout"].as<uint16_t>() <= 0) {
retMsg["message"] = "Timeout must be greater than 0 ms!";
response->setLength();
Expand All @@ -134,7 +134,7 @@ void WebApiPowerMeterClass::onAdminPost(AsyncWebServerRequest* request)
}
}

if (!valueConfig.containsKey("json_path")
if (!valueConfig["json_path"].is<String>()
|| valueConfig["json_path"].as<String>().length() == 0) {
retMsg["message"] = "Json path must not be empty!";
response->setLength();
Expand Down
6 changes: 3 additions & 3 deletions src/WebApi_vedirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ void WebApiVedirectClass::onVedirectAdminPost(AsyncWebServerRequest* request)

auto& retMsg = response->getRoot();

if (!root.containsKey("vedirect_enabled") ||
!root.containsKey("verbose_logging") ||
!root.containsKey("vedirect_updatesonly") ) {
if (!root["vedirect_enabled"].is<bool>() ||
!root["verbose_logging"].is<bool>() ||
!root["vedirect_updatesonly"].is<bool>() ) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
response->setLength();
Expand Down

0 comments on commit 97f95f8

Please sign in to comment.