From 7e909a983073baa858e0245adf7f7694cb13d88c Mon Sep 17 00:00:00 2001 From: wangjue Date: Thu, 19 Dec 2024 19:23:12 +0800 Subject: [PATCH] Only add sensors in ipmi sensor json file to active sensor list Signed-off-by: wangjue --- redfish-core/lib/sensors.hpp | 84 +++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp index 04e48c5c..20f21a93 100644 --- a/redfish-core/lib/sensors.hpp +++ b/redfish-core/lib/sensors.hpp @@ -432,6 +432,81 @@ void getConnections(std::shared_ptr sensorsAsyncResp, std::move(objectsWithConnectionCb)); } +struct SensorInfo +{ + std::string sensorName; + uint8_t readingType; + uint8_t sensorType; +}; + +using SensorMessages = std::unordered_map; + +static constexpr auto sensorJsonPath = + "/usr/share/ipmi-providers/sensor-data-record.json"; + +SensorMessages loadSensorFromJson() +{ + try + { + const std::filesystem::path path(sensorJsonPath); + SensorMessages sensorMessages; + const nlohmann::json empty{}; + nlohmann::json js; + if (!std::filesystem::exists(path) || std::filesystem::is_empty(path)) + { + std::cout << "Incorrect File Path or empty file, FILE_PATH = " + << path << std::endl; + return sensorMessages; + } + + try + { + std::ifstream jsonFile(path); + js = nlohmann::json::parse(jsonFile); + } + catch (const std::exception& e) + { + std::cout << "Failed to parse config file, ERROR = " << e.what() + << ", FILE_PATH = " << path << std::endl; + throw std::runtime_error("Failed to parse config file"); + } + + for (const auto& sensor : js.items()) + { + SensorInfo info; + std::string sensorPath = sensor.key(); + auto sensorParam = sensor.value(); + info.sensorName = sensorParam["sensorName"]; + info.readingType = sensorParam["sensorReadingType"]; + info.sensorType = sensorParam["sensorType"]; + sensorMessages.insert(std::make_pair(sensorPath, std::move(info))); + } + return sensorMessages; + } + catch (const std::exception& e) + { + throw std::runtime_error("Failed to parse sensor config file"); + } +} + +const SensorMessages& getSensorMessages() +{ + static const SensorMessages sensorMessages = loadSensorFromJson(); + return sensorMessages; +} + +std::optional + getSensorInfo(const std::string& sensorPath) +{ + const auto& sensorMessages = getSensorMessages(); + auto ite = sensorMessages.find(sensorPath); + if (ite == sensorMessages.end()) + { + return std::nullopt; + } + return std::make_optional(ite->second); +} + /** * @brief Shrinks the list of sensors for processing * @param SensorsAysncResp The class holding the Redfish response @@ -443,7 +518,7 @@ void getConnections(std::shared_ptr sensorsAsyncResp, */ inline void reduceSensorList( crow::Response& res, std::string_view chassisSubNode, - std::span sensorTypes, + [[maybe_unused]] std::span sensorTypes, const std::vector* allSensors, const std::shared_ptr>& activeSensors) { @@ -468,8 +543,13 @@ inline void reduceSensorList( { if (sensor.starts_with(type)) { - activeSensors->emplace(sensor); + auto sensorInfo = getSensorInfo(sensor); + if (sensorInfo != std::nullopt) + { + activeSensors->emplace(sensor); + } } + } } } -- 2.34.1