feat: add enum support for metrics endpoint

This commit is contained in:
Jakob
2025-12-12 10:03:52 +01:00
parent bb15fcdc46
commit df15485d7c

View File

@@ -1704,9 +1704,10 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
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
&& 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;
}
@@ -1752,6 +1753,12 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
metric_value = *(uint32_t *)(dv.value_p);
}
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:
break;
}
@@ -1794,8 +1801,19 @@ std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
}
std::string uom_str;
std::string enum_mapping;
if (dv.type == DeviceValueType::BOOL) {
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) {
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()) {
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 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));
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);
} else {
snprintf(val_str, sizeof(val_str), "%.2f", final_value);