Sensors: Create utility class for sensors
Create a separate utility class for sensors. The goal is to make these
functions easily available for use outside of sensors for paths which
need reference sensors.
Moved splitSensorNameAndType() into new utility class.
Created new utility function getSensorId(). The Id for a sensor is
built from its name and type in a few different locations. These are
modified to call the new function. The function has also been
simplified to use std::format() to build the Id.
Tested:
- Checked before and after results for queries using this function:
(Note: I was not able to confirm the setSensorsOverride() caller as
it is only being used for redfish-allow-deprecated-power-thermal
URI.)
'''
- https://${bmc}/redfish/v1/Chassis/chassis/Sensors
- https://${bmc}/redfish/v1/Chassis/chassis/Sensors/<str>
where <str> was sensors of different types
- https://${bmc}/redfish/v1/Chassis/chassis/Sensors?\$expand=*
- https://${bmc}/redfish/v1/Chassis/chassis/ThermalSubsystem/Fans
- https://${bmc}/redfish/v1/Chassis/chassis/PowerSubsystem/PowerSupplies
'''
- Redfish Validator passed
Change-Id: Ifa200b6e63f8e52d47f70c33d350999f5c527bbd
Signed-off-by: Janet Adkins <janeta@us.ibm.com>
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 8627194..4d045d0 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -28,6 +28,7 @@
#include "utils/dbus_utils.hpp"
#include "utils/json_utils.hpp"
#include "utils/query_param.hpp"
+#include "utils/sensor_utils.hpp"
#include <boost/system/error_code.hpp>
#include <boost/url/format.hpp>
@@ -753,14 +754,10 @@
{
if (chassisSubNode == sensors::node::sensors)
{
- std::string subNodeEscaped(sensorType);
- auto remove = std::ranges::remove(subNodeEscaped, '_');
- subNodeEscaped.erase(std::ranges::begin(remove), subNodeEscaped.end());
-
+ std::string subNodeEscaped =
+ redfish::sensor_utils::getSensorId(sensorName, sensorType);
// For sensors in SensorCollection we set Id instead of MemberId,
// including power sensors.
- subNodeEscaped += '_';
- subNodeEscaped += sensorName;
sensorJson["Id"] = std::move(subNodeEscaped);
std::string sensorNameEs(sensorName);
@@ -2404,15 +2401,9 @@
if (sensorSchema == sensors::node::sensors &&
!sensorsAsyncResp->efficientExpand)
{
- std::string sensorTypeEscaped(sensorType);
- auto remove =
- std::ranges::remove(sensorTypeEscaped, '_');
-
- sensorTypeEscaped.erase(std::ranges::begin(remove),
- sensorTypeEscaped.end());
- std::string sensorId(sensorTypeEscaped);
- sensorId += "_";
- sensorId += sensorName;
+ std::string sensorId =
+ redfish::sensor_utils::getSensorId(sensorName,
+ sensorType);
sensorsAsyncResp->asyncResp->res
.jsonValue["@odata.id"] = boost::urls::format(
@@ -2502,14 +2493,9 @@
}
else if (fieldName == "Members")
{
- std::string sensorTypeEscaped(sensorType);
- auto remove =
- std::ranges::remove(sensorTypeEscaped, '_');
- sensorTypeEscaped.erase(std::ranges::begin(remove),
- sensorTypeEscaped.end());
- std::string sensorId(sensorTypeEscaped);
- sensorId += "_";
- sensorId += sensorName;
+ std::string sensorId =
+ redfish::sensor_utils::getSensorId(sensorName,
+ sensorType);
nlohmann::json::object_t member;
member["@odata.id"] = boost::urls::format(
@@ -2661,24 +2647,6 @@
return false;
}
-inline std::pair<std::string, std::string>
- splitSensorNameAndType(std::string_view sensorId)
-{
- size_t index = sensorId.find('_');
- if (index == std::string::npos)
- {
- return std::make_pair<std::string, std::string>("", "");
- }
- std::string sensorType{sensorId.substr(0, index)};
- std::string sensorName{sensorId.substr(index + 1)};
- // fan_pwm and fan_tach need special handling
- if (sensorType == "fantach" || sensorType == "fanpwm")
- {
- sensorType.insert(3, 1, '_');
- }
- return std::make_pair(sensorType, sensorName);
-}
-
/**
* @brief Entry point for overriding sensor values of given sensor
*
@@ -2738,7 +2706,7 @@
{
const auto& sensor = item.first;
std::pair<std::string, std::string> sensorNameType =
- splitSensorNameAndType(sensor);
+ redfish::sensor_utils::splitSensorNameAndType(sensor);
if (!findSensorNameUsingSensorPath(sensorNameType.second,
*sensorsList, *sensorNames))
{
@@ -2778,11 +2746,8 @@
messages::internalError(sensorAsyncResp->asyncResp->res);
return;
}
- std::string id = path.parent_path().filename();
- auto remove = std::ranges::remove(id, '_');
- id.erase(std::ranges::begin(remove), id.end());
- id += "_";
- id += sensorName;
+ std::string id = redfish::sensor_utils::getSensorId(
+ sensorName, path.parent_path().filename());
const auto& iterator = overrideMap.find(id);
if (iterator == overrideMap.end())
@@ -2872,15 +2837,9 @@
return;
}
std::string type = path.parent_path().filename();
- // fan_tach has an underscore in it, so remove it to "normalize" the
- // type in the URI
- auto remove = std::ranges::remove(type, '_');
- type.erase(std::ranges::begin(remove), type.end());
+ std::string id = redfish::sensor_utils::getSensorId(sensorName, type);
nlohmann::json::object_t member;
- std::string id = type;
- id += "_";
- id += sensorName;
member["@odata.id"] = boost::urls::format(
"/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
@@ -2972,7 +2931,7 @@
return;
}
std::pair<std::string, std::string> nameType =
- splitSensorNameAndType(sensorId);
+ redfish::sensor_utils::splitSensorNameAndType(sensorId);
if (nameType.first.empty() || nameType.second.empty())
{
messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");