Prometheus Endpoint: Publish only relevant amount of digits

Implemented method to return the correctly formatted field value as string
This commit is contained in:
Thomas Basler 2023-08-22 11:45:14 +02:00
parent b025c079c5
commit 4bf9083b23
4 changed files with 11 additions and 7 deletions

View File

@ -150,6 +150,13 @@ float StatisticsParser::getChannelFieldValue(ChannelType_t type, ChannelNum_t ch
return 0; return 0;
} }
String StatisticsParser::getChannelFieldValueString(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)
{
return String(
getChannelFieldValue(type, channel, fieldId),
static_cast<unsigned int>(getChannelFieldDigits(type, channel, fieldId)));
}
bool StatisticsParser::hasChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId) bool StatisticsParser::hasChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)
{ {
const byteAssign_t* pos = getAssignmentByChannelField(type, channel, fieldId); const byteAssign_t* pos = getAssignmentByChannelField(type, channel, fieldId);

View File

@ -119,6 +119,7 @@ public:
fieldSettings_t* getSettingByChannelField(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId); fieldSettings_t* getSettingByChannelField(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
float getChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId); float getChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
String getChannelFieldValueString(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
bool hasChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId); bool hasChannelFieldValue(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
const char* getChannelFieldUnit(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId); const char* getChannelFieldUnit(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
const char* getChannelFieldName(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId); const char* getChannelFieldName(ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);

View File

@ -126,11 +126,7 @@ void MqttHandleInverterClass::publishField(std::shared_ptr<InverterAbstract> inv
return; return;
} }
String value = String( MqttSettings.publish(topic, inv->Statistics()->getChannelFieldValueString(type, channel, fieldId));
inv->Statistics()->getChannelFieldValue(type, channel, fieldId),
static_cast<unsigned int>(inv->Statistics()->getChannelFieldDigits(type, channel, fieldId)));
MqttSettings.publish(topic, value);
} }
String MqttHandleInverterClass::getTopic(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId) String MqttHandleInverterClass::getTopic(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)

View File

@ -114,14 +114,14 @@ void WebApiPrometheusClass::addField(AsyncResponseStream* stream, String& serial
stream->printf("# HELP opendtu_%s in %s\n", chanName, inv->Statistics()->getChannelFieldUnit(type, channel, fieldId)); stream->printf("# HELP opendtu_%s in %s\n", chanName, inv->Statistics()->getChannelFieldUnit(type, channel, fieldId));
stream->printf("# TYPE opendtu_%s %s\n", chanName, _metricTypes[_fieldMetricAssignment[fieldId]]); stream->printf("# TYPE opendtu_%s %s\n", chanName, _metricTypes[_fieldMetricAssignment[fieldId]]);
} }
stream->printf("opendtu_%s{serial=\"%s\",unit=\"%d\",name=\"%s\",type=\"%s\",channel=\"%d\"} %f\n", stream->printf("opendtu_%s{serial=\"%s\",unit=\"%d\",name=\"%s\",type=\"%s\",channel=\"%d\"} %s\n",
chanName, chanName,
serial.c_str(), serial.c_str(),
idx, idx,
inv->name(), inv->name(),
inv->Statistics()->getChannelTypeName(type), inv->Statistics()->getChannelTypeName(type),
channel, channel,
inv->Statistics()->getChannelFieldValue(type, channel, fieldId)); inv->Statistics()->getChannelFieldValueString(type, channel, fieldId).c_str());
} }
} }