From 07d816b29d18f94e4c76d03c965be5eeb1bcb6bc Mon Sep 17 00:00:00 2001 From: Sven Naumann <3747263+sVnsation@users.noreply.github.com> Date: Thu, 1 Dec 2022 09:15:28 +0000 Subject: [PATCH 1/2] move round3 function to helper --- src/utils/helper.cpp | 4 ++++ src/utils/helper.h | 1 + src/web/webApi.cpp | 6 +++--- src/web/webApi.h | 4 ---- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/utils/helper.cpp b/src/utils/helper.cpp index 8108ca3e..9bae7bed 100644 --- a/src/utils/helper.cpp +++ b/src/utils/helper.cpp @@ -26,4 +26,8 @@ namespace ah { else snprintf(str, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); } + + double round3(double value) { + return (int)(value * 1000 + 0.5) / 1000.0; + } } diff --git a/src/utils/helper.h b/src/utils/helper.h index 49351cc9..1898ba7e 100644 --- a/src/utils/helper.h +++ b/src/utils/helper.h @@ -14,6 +14,7 @@ namespace ah { void ip2Arr(uint8_t ip[], const char *ipStr); void ip2Char(uint8_t ip[], char *str); + double round3(double value); } #endif /*__HELPER_H__*/ diff --git a/src/web/webApi.cpp b/src/web/webApi.cpp index 4987e147..b11637fa 100644 --- a/src/web/webApi.cpp +++ b/src/web/webApi.cpp @@ -448,7 +448,7 @@ void webApi::getLive(JsonObject obj) { JsonObject obj2 = invArr.createNestedObject(); obj2[F("name")] = String(iv->config->name); obj2[F("channels")] = iv->channels; - obj2[F("power_limit_read")] = round3(iv->actPowerLimit); + obj2[F("power_limit_read")] = ah::round3(iv->actPowerLimit); obj2[F("last_alarm")] = String(iv->lastAlarmMsg); obj2[F("ts_last_success")] = rec->ts; @@ -457,7 +457,7 @@ void webApi::getLive(JsonObject obj) { obj2[F("ch_names")][0] = "AC"; for (uint8_t fld = 0; fld < sizeof(list); fld++) { pos = (iv->getPosByChFld(CH0, list[fld], rec)); - ch0[fld] = (0xff != pos) ? round3(iv->getValue(pos, rec)) : 0.0; + ch0[fld] = (0xff != pos) ? ah::round3(iv->getValue(pos, rec)) : 0.0; obj[F("ch0_fld_units")][fld] = (0xff != pos) ? String(iv->getUnit(pos, rec)) : notAvail; obj[F("ch0_fld_names")][fld] = (0xff != pos) ? String(iv->getFieldName(pos, rec)) : notAvail; } @@ -474,7 +474,7 @@ void webApi::getLive(JsonObject obj) { case 4: pos = (iv->getPosByChFld(j, FLD_YT, rec)); break; case 5: pos = (iv->getPosByChFld(j, FLD_IRR, rec)); break; } - cur[k] = (0xff != pos) ? round3(iv->getValue(pos, rec)) : 0.0; + cur[k] = (0xff != pos) ? ah::round3(iv->getValue(pos, rec)) : 0.0; if(1 == j) { obj[F("fld_units")][k] = (0xff != pos) ? String(iv->getUnit(pos, rec)) : notAvail; obj[F("fld_names")][k] = (0xff != pos) ? String(iv->getFieldName(pos, rec)) : notAvail; diff --git a/src/web/webApi.h b/src/web/webApi.h index 8b539e05..04e16635 100644 --- a/src/web/webApi.h +++ b/src/web/webApi.h @@ -59,10 +59,6 @@ class webApi { bool setCtrl(JsonObject jsonIn, JsonObject jsonOut); bool setSetup(JsonObject jsonIn, JsonObject jsonOut); - double round3(double value) { - return (int)(value * 1000 + 0.5) / 1000.0; - } - AsyncWebServer *mSrv; app *mApp; From 08e417ee85b7c6a77ffc728545e1d0fc6fc00170 Mon Sep 17 00:00:00 2001 From: Sven Naumann <3747263+sVnsation@users.noreply.github.com> Date: Thu, 1 Dec 2022 09:34:38 +0000 Subject: [PATCH 2/2] mqtt avoid trailing zeroes --- src/publisher/pubMqtt.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 89d8633b..91e6545e 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -378,7 +378,7 @@ class PubMqtt { if(iv->isAvailable(*mUtcTimestamp, rec)) { for (uint8_t i = 0; i < rec->length; i++) { snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/ch%d/%s", iv->config->name, rec->assign[i].ch, fields[rec->assign[i].fieldId]); - snprintf(val, 40, "%.3f", iv->getValue(i, rec)); + snprintf(val, sizeof(val), "%g", ah::round3(iv->getValue(i, rec))); publish(topic, val); // calculate total values for RealTimeRunData_Debug @@ -427,7 +427,7 @@ class PubMqtt { break; } snprintf(topic, 32 + MAX_NAME_LENGTH, "total/%s", fields[fieldId]); - snprintf(val, 40, "%.3f", total[i]); + snprintf(val, sizeof(val), "%g", ah::round3(total[i])); publish(topic, val); } }