fix render long numbers, #2267

This commit is contained in:
MichaelDvP
2024-12-03 12:39:30 +01:00
parent 473cf7c8af
commit 607f949638
5 changed files with 17 additions and 21 deletions

View File

@@ -1547,7 +1547,7 @@ void EMSdevice::get_value_json(JsonObject json, DeviceValue & dv) {
json["circuit"] = tag_to_mqtt(dv.tag);
}
char val[10];
char val[20];
switch (dv.type) {
case DeviceValueType::ENUM: {
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
@@ -1785,7 +1785,7 @@ bool EMSdevice::generate_values(JsonObject output, const int8_t tag_filter, cons
: (dv.uom == DeviceValueUOM::DEGREES) ? 2
: (dv.uom == DeviceValueUOM::DEGREES_R) ? 1
: 0;
char val[10] = {'\0'};
char val[20] = {'\0'};
if (dv.type == DeviceValueType::INT8) {
json[name] = serialized(Helpers::render_value(val, *(int8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
} else if (dv.type == DeviceValueType::UINT8) {

View File

@@ -254,13 +254,13 @@ char * Helpers::render_value(char * result, const double value, const int8_t for
char * ret = result;
double v = value < 0 ? value - 1.0 / (2 * p[format]) : value + 1.0 / (2 * p[format]);
auto whole = (int32_t)v;
auto whole = (long long)v;
if (whole == 0 && v < 0) {
result[0] = '-';
result++;
}
itoa(whole, result, 10);
lltoa(whole, result, 10);
while (*result != '\0') {
result++;
@@ -354,16 +354,16 @@ char * Helpers::render_value(char * result, const uint32_t value, const int8_t f
#ifndef EMSESP_STANDALONE
if (!format) {
strlcpy(result, ltoa(new_value, s, 10), sizeof(s)); // format is 0
strlcpy(result, lltoa(new_value, s, 10), sizeof(s)); // format is 0
} else if (format > 0) {
strlcpy(result, ltoa(new_value / format, s, 10), sizeof(s));
strlcpy(result, lltoa(new_value / format, s, 10), sizeof(s));
strlcat(result, ".", sizeof(s));
strlcat(result, itoa(((new_value % format) * 10) / format, s, 10), sizeof(s));
if (format == 100) {
strlcat(result, itoa(new_value % 10, s, 10), sizeof(s));
}
} else {
strlcpy(result, ltoa(new_value * format * -1, s, 10), sizeof(s));
strlcpy(result, lltoa(new_value * format * -1, s, 10), sizeof(s));
}
#else
if (!format) {
@@ -458,26 +458,22 @@ int Helpers::atoint(const char * value) {
// fahrenheit=0 - off, no conversion
// fahrenheit=1 - relative, 1.8t
// fahrenheit=2 - absolute, 1.8t + 32(fahrenheit-1)
float Helpers::transformNumFloat(float value, const int8_t numeric_operator, const uint8_t fahrenheit) {
float val;
double Helpers::transformNumFloat(double value, const int8_t numeric_operator, const uint8_t fahrenheit) {
double val;
if (numeric_operator == 0) { // DV_NUMOP_NONE
val = value * 100 + 0.5;
val = value * 100;
} else if (numeric_operator > 0) { // DV_NUMOP_DIVxx
val = value * 100 / numeric_operator + 0.5;
val = value * 100 / numeric_operator;
} else { // DV_NUMOP_MULxx
val = value * -100 * numeric_operator;
}
if (value < 0) { // negative rounding
val = val - 1;
}
if (fahrenheit) {
val = val * 1.8 + 3200 * (fahrenheit - 1);
}
return ((int32_t)val) / 100.0;
return (round(val)) / 100.0;
}
// abs of a signed 32-bit integer

View File

@@ -51,7 +51,7 @@ class Helpers {
static uint16_t string2minutes(const std::string & str);
static float numericoperator2scalefactor(int8_t numeric_operator);
static float transformNumFloat(float value, const int8_t numeric_operator, const uint8_t fahrenheit = 0);
static double transformNumFloat(double value, const int8_t numeric_operator, const uint8_t fahrenheit = 0);
static std::string toLower(std::string const & s);
static std::string toUpper(std::string const & s);

View File

@@ -214,7 +214,7 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
// output of a single value
// if add_uom is true it will add the UOM string to the value
void WebCustomEntityService::render_value(JsonObject output, CustomEntityItem & entity, const bool useVal, const bool web, const bool add_uom) {
char payload[12];
char payload[20];
std::string name = useVal ? "value" : entity.name;
switch (entity.value_type) {
case DeviceValueType::BOOL:
@@ -496,7 +496,7 @@ void WebCustomEntityService::generate_value_web(JsonObject output, const bool is
obj["c"] = entity.name;
include = true;
if (entity.value_type != DeviceValueType::BOOL && entity.value_type != DeviceValueType::STRING) {
char s[10];
char s[20];
obj["s"] = Helpers::render_value(s, entity.factor, 1);
}
}

View File

@@ -267,10 +267,10 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
if (data.is<const char *>()) {
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
} else if (data.is<int>()) {
char s[10];
char s[20];
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<int32_t>(), 0), true, id, output);
} else if (data.is<float>()) {
char s[10];
char s[20];
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<float>(), 1), true, id, output);
} else if (data.is<bool>()) {
return_code = Command::call(device_type, cmd, data.as<bool>() ? "true" : "false", true, id, output);