Moving some methods from threshold to utils
Two methods splitFileName and readFile seems to be more generic
and used not only by thresholds. Moving splitFileName and readFile
from the threshold namespace to utils.
Tested:
Manual tests where done on CPUSensor. No differences detected
comparing to results before these changes.
Signed-off-by: Zbigniew Kurzynski <zbigniew.kurzynski@intel.com>
Change-Id: I8ab58099eca740b6111a382f68f2ab32d7762a78
diff --git a/include/Thresholds.hpp b/include/Thresholds.hpp
index 56bb541..183fa34 100644
--- a/include/Thresholds.hpp
+++ b/include/Thresholds.hpp
@@ -127,13 +127,6 @@
Sensor* sensor;
};
-// The common scheme for sysfs files naming is: <type><number>_<item>.
-// This function returns optionally these 3 elements as a tuple.
-std::optional<std::tuple<std::string, std::string, std::string>>
- splitFileName(const std::filesystem::path& filePath);
-std::optional<double> readFile(const std::string& thresholdFile,
- const double& scaleFactor);
-
bool parseThresholdsFromConfig(
const SensorData& sensorData,
std::vector<thresholds::Threshold>& thresholdVector,
diff --git a/include/Utils.hpp b/include/Utils.hpp
index 224842c..28a2df3 100644
--- a/include/Utils.hpp
+++ b/include/Utils.hpp
@@ -230,3 +230,10 @@
std::function<void(ManagedObjectType& resp)> callback;
ManagedObjectType respData;
};
+
+// The common scheme for sysfs files naming is: <type><number>_<item>.
+// This function returns optionally these 3 elements as a tuple.
+std::optional<std::tuple<std::string, std::string, std::string>>
+ splitFileName(const std::filesystem::path& filePath);
+std::optional<double> readFile(const std::string& thresholdFile,
+ const double& scaleFactor);
\ No newline at end of file
diff --git a/src/CPUSensor.cpp b/src/CPUSensor.cpp
index 6a803e2..801138a 100644
--- a/src/CPUSensor.cpp
+++ b/src/CPUSensor.cpp
@@ -53,7 +53,7 @@
nameTcontrol += " CPU" + std::to_string(cpuId);
if (show)
{
- if (auto fileParts = thresholds::splitFileName(path))
+ if (auto fileParts = splitFileName(path))
{
auto [type, nr, item] = *fileParts;
std::string interfacePath;
@@ -129,7 +129,7 @@
},
};
- if (auto fileParts = thresholds::splitFileName(path))
+ if (auto fileParts = splitFileName(path))
{
auto [fileType, fileNr, fileItem] = *fileParts;
const auto mapIt = map.find(fileItem);
@@ -139,8 +139,8 @@
{
auto [suffix, oldValue, dbusName] = vectorItem;
auto attrPath = boost::replace_all_copy(path, fileItem, suffix);
- if (auto newVal = thresholds::readFile(
- attrPath, CPUSensor::sensorScaleFactor))
+ if (auto newVal =
+ readFile(attrPath, CPUSensor::sensorScaleFactor))
{
genericUpdateValue(oldValue, *newVal, dbusName);
}
diff --git a/src/CPUSensorMain.cpp b/src/CPUSensorMain.cpp
index 42336c6..0ee9ea7 100644
--- a/src/CPUSensorMain.cpp
+++ b/src/CPUSensorMain.cpp
@@ -277,7 +277,7 @@
// iterate through all found temp sensors
for (const auto& inputPath : inputPaths)
{
- auto fileParts = thresholds::splitFileName(inputPath);
+ auto fileParts = splitFileName(inputPath);
if (!fileParts)
{
continue;
diff --git a/src/Thresholds.cpp b/src/Thresholds.cpp
index 9e0066f..61f951b 100644
--- a/src/Thresholds.cpp
+++ b/src/Thresholds.cpp
@@ -399,48 +399,6 @@
interface->set_property(property, assert);
}
-std::optional<double> readFile(const std::string& thresholdFile,
- const double& scaleFactor)
-{
- std::string line;
- std::ifstream labelFile(thresholdFile);
- if (labelFile.good())
- {
- std::getline(labelFile, line);
- labelFile.close();
-
- try
- {
- return std::stod(line) / scaleFactor;
- }
- catch (const std::invalid_argument&)
- {
- return std::nullopt;
- }
- }
- return std::nullopt;
-}
-
-std::optional<std::tuple<std::string, std::string, std::string>>
- splitFileName(const std::filesystem::path& filePath)
-{
- if (filePath.has_filename())
- {
- const auto fileName = filePath.filename().string();
- const std::regex rx(R"((\w+)(\d+)_(.*))");
- std::smatch mr;
-
- if (std::regex_search(fileName, mr, rx))
- {
- if (mr.size() == 4)
- {
- return std::make_optional(std::make_tuple(mr[1], mr[2], mr[3]));
- }
- }
- }
- return std::nullopt;
-}
-
bool parseThresholdsFromAttr(
std::vector<thresholds::Threshold>& thresholdVector,
const std::string& inputPath, const double& scaleFactor,
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 6b9ddf9..1add0e6 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -322,3 +322,45 @@
std::array<std::string, 1>{
"xyz.openbmc_project.Inventory.Item.System"});
}
+
+std::optional<double> readFile(const std::string& thresholdFile,
+ const double& scaleFactor)
+{
+ std::string line;
+ std::ifstream labelFile(thresholdFile);
+ if (labelFile.good())
+ {
+ std::getline(labelFile, line);
+ labelFile.close();
+
+ try
+ {
+ return std::stod(line) / scaleFactor;
+ }
+ catch (const std::invalid_argument&)
+ {
+ return std::nullopt;
+ }
+ }
+ return std::nullopt;
+}
+
+std::optional<std::tuple<std::string, std::string, std::string>>
+ splitFileName(const std::filesystem::path& filePath)
+{
+ if (filePath.has_filename())
+ {
+ const auto fileName = filePath.filename().string();
+ const std::regex rx(R"((\w+)(\d+)_(.*))");
+ std::smatch mr;
+
+ if (std::regex_search(fileName, mr, rx))
+ {
+ if (mr.size() == 4)
+ {
+ return std::make_optional(std::make_tuple(mr[1], mr[2], mr[3]));
+ }
+ }
+ }
+ return std::nullopt;
+}
\ No newline at end of file