mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-14 11:50:20 +03:00
feat: add enum support for metrics endpoint
This commit is contained in:
@@ -1704,9 +1704,10 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// only process number and boolean types for now
|
// only process number, boolean and enum types
|
||||||
if (dv.type != DeviceValueType::BOOL && dv.type != DeviceValueType::UINT8 && dv.type != DeviceValueType::INT8 && dv.type != DeviceValueType::UINT16
|
if (dv.type != DeviceValueType::BOOL && dv.type != DeviceValueType::UINT8 && dv.type != DeviceValueType::INT8 && dv.type != DeviceValueType::UINT16
|
||||||
&& dv.type != DeviceValueType::INT16 && dv.type != DeviceValueType::UINT24 && dv.type != DeviceValueType::UINT32 && dv.type != DeviceValueType::TIME) {
|
&& dv.type != DeviceValueType::INT16 && dv.type != DeviceValueType::UINT24 && dv.type != DeviceValueType::UINT32 && dv.type != DeviceValueType::TIME
|
||||||
|
&& dv.type != DeviceValueType::ENUM) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1752,6 +1753,12 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
|||||||
metric_value = *(uint32_t *)(dv.value_p);
|
metric_value = *(uint32_t *)(dv.value_p);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case DeviceValueType::ENUM:
|
||||||
|
if (*(uint8_t *)(dv.value_p) < dv.options_size) {
|
||||||
|
has_value = true;
|
||||||
|
metric_value = *(uint8_t *)(dv.value_p);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1794,8 +1801,19 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string uom_str;
|
std::string uom_str;
|
||||||
|
std::string enum_mapping;
|
||||||
if (dv.type == DeviceValueType::BOOL) {
|
if (dv.type == DeviceValueType::BOOL) {
|
||||||
uom_str = "boolean";
|
uom_str = "boolean";
|
||||||
|
} else if (dv.type == DeviceValueType::ENUM) {
|
||||||
|
// build enum mapping string: "(0: Wert1; 1: Wert2; ...)"
|
||||||
|
enum_mapping = "(";
|
||||||
|
for (uint8_t i = 0; i < dv.options_size; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
enum_mapping += "; ";
|
||||||
|
}
|
||||||
|
enum_mapping += std::to_string(i) + ": " + std::string(Helpers::translated_word(dv.options[i]));
|
||||||
|
}
|
||||||
|
enum_mapping += ")";
|
||||||
} else if (dv.uom != DeviceValueUOM::NONE) {
|
} else if (dv.uom != DeviceValueUOM::NONE) {
|
||||||
uom_str = uom_to_string(dv.uom);
|
uom_str = uom_to_string(dv.uom);
|
||||||
}
|
}
|
||||||
@@ -1804,6 +1822,9 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
|||||||
if (!uom_str.empty()) {
|
if (!uom_str.empty()) {
|
||||||
help_line += ", " + uom_str;
|
help_line += ", " + uom_str;
|
||||||
}
|
}
|
||||||
|
if (!enum_mapping.empty()) {
|
||||||
|
help_line += ", enum, " + enum_mapping;
|
||||||
|
}
|
||||||
|
|
||||||
bool readable = dv.type != DeviceValueType::CMD && !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE);
|
bool readable = dv.type != DeviceValueType::CMD && !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE);
|
||||||
bool writeable = dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY);
|
bool writeable = dv.has_cmd && !dv.has_state(DeviceValueState::DV_READONLY);
|
||||||
@@ -1854,7 +1875,7 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
double rounded = (final_value >= 0) ? (double)((int64_t)(final_value + 0.5)) : (double)((int64_t)(final_value - 0.5));
|
double rounded = (final_value >= 0) ? (double)((int64_t)(final_value + 0.5)) : (double)((int64_t)(final_value - 0.5));
|
||||||
if (dv.type == DeviceValueType::BOOL || (final_value == rounded)) {
|
if (dv.type == DeviceValueType::BOOL || dv.type == DeviceValueType::ENUM || (final_value == rounded)) {
|
||||||
snprintf(val_str, sizeof(val_str), "%.0f", final_value);
|
snprintf(val_str, sizeof(val_str), "%.0f", final_value);
|
||||||
} else {
|
} else {
|
||||||
snprintf(val_str, sizeof(val_str), "%.2f", final_value);
|
snprintf(val_str, sizeof(val_str), "%.2f", final_value);
|
||||||
|
|||||||
Reference in New Issue
Block a user