mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
fix render long numbers, #2267
This commit is contained in:
@@ -1547,7 +1547,7 @@ void EMSdevice::get_value_json(JsonObject json, DeviceValue & dv) {
|
|||||||
json["circuit"] = tag_to_mqtt(dv.tag);
|
json["circuit"] = tag_to_mqtt(dv.tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
char val[10];
|
char val[20];
|
||||||
switch (dv.type) {
|
switch (dv.type) {
|
||||||
case DeviceValueType::ENUM: {
|
case DeviceValueType::ENUM: {
|
||||||
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
|
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) ? 2
|
||||||
: (dv.uom == DeviceValueUOM::DEGREES_R) ? 1
|
: (dv.uom == DeviceValueUOM::DEGREES_R) ? 1
|
||||||
: 0;
|
: 0;
|
||||||
char val[10] = {'\0'};
|
char val[20] = {'\0'};
|
||||||
if (dv.type == DeviceValueType::INT8) {
|
if (dv.type == DeviceValueType::INT8) {
|
||||||
json[name] = serialized(Helpers::render_value(val, *(int8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
json[name] = serialized(Helpers::render_value(val, *(int8_t *)(dv.value_p), dv.numeric_operator, fahrenheit));
|
||||||
} else if (dv.type == DeviceValueType::UINT8) {
|
} else if (dv.type == DeviceValueType::UINT8) {
|
||||||
|
|||||||
@@ -254,13 +254,13 @@ char * Helpers::render_value(char * result, const double value, const int8_t for
|
|||||||
|
|
||||||
char * ret = result;
|
char * ret = result;
|
||||||
double v = value < 0 ? value - 1.0 / (2 * p[format]) : value + 1.0 / (2 * p[format]);
|
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) {
|
if (whole == 0 && v < 0) {
|
||||||
result[0] = '-';
|
result[0] = '-';
|
||||||
result++;
|
result++;
|
||||||
}
|
}
|
||||||
itoa(whole, result, 10);
|
lltoa(whole, result, 10);
|
||||||
|
|
||||||
while (*result != '\0') {
|
while (*result != '\0') {
|
||||||
result++;
|
result++;
|
||||||
@@ -354,16 +354,16 @@ char * Helpers::render_value(char * result, const uint32_t value, const int8_t f
|
|||||||
|
|
||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
if (!format) {
|
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) {
|
} 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, ".", sizeof(s));
|
||||||
strlcat(result, itoa(((new_value % format) * 10) / format, s, 10), sizeof(s));
|
strlcat(result, itoa(((new_value % format) * 10) / format, s, 10), sizeof(s));
|
||||||
if (format == 100) {
|
if (format == 100) {
|
||||||
strlcat(result, itoa(new_value % 10, s, 10), sizeof(s));
|
strlcat(result, itoa(new_value % 10, s, 10), sizeof(s));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
strlcpy(result, ltoa(new_value * format * -1, s, 10), sizeof(s));
|
strlcpy(result, lltoa(new_value * format * -1, s, 10), sizeof(s));
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (!format) {
|
if (!format) {
|
||||||
@@ -458,26 +458,22 @@ int Helpers::atoint(const char * value) {
|
|||||||
// fahrenheit=0 - off, no conversion
|
// fahrenheit=0 - off, no conversion
|
||||||
// fahrenheit=1 - relative, 1.8t
|
// fahrenheit=1 - relative, 1.8t
|
||||||
// fahrenheit=2 - absolute, 1.8t + 32(fahrenheit-1)
|
// fahrenheit=2 - absolute, 1.8t + 32(fahrenheit-1)
|
||||||
float Helpers::transformNumFloat(float value, const int8_t numeric_operator, const uint8_t fahrenheit) {
|
double Helpers::transformNumFloat(double value, const int8_t numeric_operator, const uint8_t fahrenheit) {
|
||||||
float val;
|
double val;
|
||||||
|
|
||||||
if (numeric_operator == 0) { // DV_NUMOP_NONE
|
if (numeric_operator == 0) { // DV_NUMOP_NONE
|
||||||
val = value * 100 + 0.5;
|
val = value * 100;
|
||||||
} else if (numeric_operator > 0) { // DV_NUMOP_DIVxx
|
} else if (numeric_operator > 0) { // DV_NUMOP_DIVxx
|
||||||
val = value * 100 / numeric_operator + 0.5;
|
val = value * 100 / numeric_operator;
|
||||||
} else { // DV_NUMOP_MULxx
|
} else { // DV_NUMOP_MULxx
|
||||||
val = value * -100 * numeric_operator;
|
val = value * -100 * numeric_operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value < 0) { // negative rounding
|
|
||||||
val = val - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fahrenheit) {
|
if (fahrenheit) {
|
||||||
val = val * 1.8 + 3200 * (fahrenheit - 1);
|
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
|
// abs of a signed 32-bit integer
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class Helpers {
|
|||||||
static uint16_t string2minutes(const std::string & str);
|
static uint16_t string2minutes(const std::string & str);
|
||||||
static float numericoperator2scalefactor(int8_t numeric_operator);
|
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 toLower(std::string const & s);
|
||||||
static std::string toUpper(std::string const & s);
|
static std::string toUpper(std::string const & s);
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
|
|||||||
// output of a single value
|
// output of a single value
|
||||||
// if add_uom is true it will add the UOM string to the 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) {
|
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;
|
std::string name = useVal ? "value" : entity.name;
|
||||||
switch (entity.value_type) {
|
switch (entity.value_type) {
|
||||||
case DeviceValueType::BOOL:
|
case DeviceValueType::BOOL:
|
||||||
@@ -496,7 +496,7 @@ void WebCustomEntityService::generate_value_web(JsonObject output, const bool is
|
|||||||
obj["c"] = entity.name;
|
obj["c"] = entity.name;
|
||||||
include = true;
|
include = true;
|
||||||
if (entity.value_type != DeviceValueType::BOOL && entity.value_type != DeviceValueType::STRING) {
|
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);
|
obj["s"] = Helpers::render_value(s, entity.factor, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -267,10 +267,10 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar
|
|||||||
if (data.is<const char *>()) {
|
if (data.is<const char *>()) {
|
||||||
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
|
return_code = Command::call(device_type, cmd, data.as<const char *>(), true, id, output);
|
||||||
} else if (data.is<int>()) {
|
} 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);
|
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<int32_t>(), 0), true, id, output);
|
||||||
} else if (data.is<float>()) {
|
} 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);
|
return_code = Command::call(device_type, cmd, Helpers::render_value(s, data.as<float>(), 1), true, id, output);
|
||||||
} else if (data.is<bool>()) {
|
} else if (data.is<bool>()) {
|
||||||
return_code = Command::call(device_type, cmd, data.as<bool>() ? "true" : "false", true, id, output);
|
return_code = Command::call(device_type, cmd, data.as<bool>() ? "true" : "false", true, id, output);
|
||||||
|
|||||||
Reference in New Issue
Block a user