HFT sensor name should be trimmed name, not full key Created a sensor utility function to reuse the timming logic for both redfish and hft Tested: UT #tlbmc #tlbmc-hft PiperOrigin-RevId: 773884192 Change-Id: Id2b6ba0bfd2ebde82ef444f48d2264fd44a0d9df
diff --git a/tlbmc/redfish/routes/sensor.cc b/tlbmc/redfish/routes/sensor.cc index 3277c96..3ecad0b 100644 --- a/tlbmc/redfish/routes/sensor.cc +++ b/tlbmc/redfish/routes/sensor.cc
@@ -72,14 +72,6 @@ FillResponseWithSensorData(sensor, sensor_pointer, resp); } -absl::string_view TrimSensorId(absl::string_view sensor_id) { - uint64_t under_pos = sensor_id.find('_'); - if (under_pos == std::string::npos) { - return sensor_id; - } - return sensor_id.substr(under_pos + 1); -} - } // namespace void FillResponseWithSensorData( @@ -101,8 +93,7 @@ resp.SetKeyInJsonBody(sensor_pointer / "@odata.type", "#Sensor.v1_2_0.Sensor"); std::string sensor_name = - std::string(TrimSensorId(attributes_static.attributes().key())); - std::replace(sensor_name.begin(), sensor_name.end(), '_', ' '); + GetTrimmedSensorName(attributes_static.attributes().key()); resp.SetKeyInJsonBody(sensor_pointer / "Name", sensor_name); resp.SetKeyInJsonBody(sensor_pointer / "Id", attributes_static.attributes().key());
diff --git a/tlbmc/sensors/sensor.cc b/tlbmc/sensors/sensor.cc index 94c3c98..e4acd2a 100644 --- a/tlbmc/sensors/sensor.cc +++ b/tlbmc/sensors/sensor.cc
@@ -6,6 +6,7 @@ #include <iterator> #include <memory> #include <optional> +#include <string> #include <utility> #include <vector> @@ -40,6 +41,17 @@ } // namespace +std::string GetTrimmedSensorName(absl::string_view sensor_name) { + std::string stripped_sensor_name; + if (uint64_t under_pos = sensor_name.find('_'); + under_pos != std::string::npos) { + stripped_sensor_name = sensor_name.substr(under_pos + 1); + } + std::replace(stripped_sensor_name.begin(), stripped_sensor_name.end(), '_', + ' '); + return stripped_sensor_name; +} + SensorAttributesStatic Sensor::CreateStaticAttributes( absl::string_view sensor_name, const SensorUnit &sensor_unit, const I2cCommonConfig &i2c_common_config,
diff --git a/tlbmc/sensors/sensor.h b/tlbmc/sensors/sensor.h index 24a3e51..3d57dc7 100644 --- a/tlbmc/sensors/sensor.h +++ b/tlbmc/sensors/sensor.h
@@ -26,6 +26,8 @@ namespace milotic_tlbmc { +std::string GetTrimmedSensorName(absl::string_view sensor_name); + // Any implementation of this interface must be thread-safe regarding the listed // functions. class Sensor {
diff --git a/tlbmc/store/store_hft_adapter.cc b/tlbmc/store/store_hft_adapter.cc index 94eadf1..f824560 100644 --- a/tlbmc/store/store_hft_adapter.cc +++ b/tlbmc/store/store_hft_adapter.cc
@@ -91,8 +91,7 @@ } else { sensor_identifier_from_readings->set_devpath(devpath); } - std::string sensor_name = sensor->GetKey(); - std::replace(sensor_name.begin(), sensor_name.end(), '_', ' '); + std::string sensor_name = GetTrimmedSensorName(sensor->GetKey()); sensor_identifier_from_readings->set_name(sensor_name); sensor_identifier_from_readings->set_source(milotic_hft::SENSOR_SOURCE_BMC); sensor_identifier_from_readings->set_units(