From b4b2e3bafefb4ab895473237a4ef328734350e7f Mon Sep 17 00:00:00 2001 From: Miguel Borges de Freitas <92enen@gmail.com> Date: Mon, 28 Aug 2023 16:57:29 +0100 Subject: [PATCH] Factor common code into CalculateAverageTemperature --- smctemp.cc | 40 ++++++++++++++++++---------------------- smctemp.h | 4 ++++ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/smctemp.cc b/smctemp.cc index e4bfedb..3a36770 100755 --- a/smctemp.cc +++ b/smctemp.cc @@ -29,7 +29,6 @@ #include #include #include -#include #include "smctemp_string.h" @@ -37,7 +36,6 @@ #include #include #include -#include namespace { std::string getCPUModel() { @@ -384,6 +382,22 @@ kern_return_t SmcAccessor::PrintAll() { return kIOReturnSuccess; } +double SmcTemp::CalculateAverageTemperature(const std::vector& sensors, + const std::pair& limits) { + double temp = 0.0; + size_t valid_sensor_count = 0; + for (auto sensor : sensors) { + auto sensor_value = smc_accessor_.ReadValue(sensor.c_str()); + if (sensor_value >= limits.first && + sensor_value <= limits.second) { + temp += sensor_value; + valid_sensor_count++; + } + } + temp /= valid_sensor_count; + return temp; +} + double SmcTemp::GetCpuTemp() { double temp = 0.0; #if defined(ARCH_TYPE_X86_64) @@ -459,30 +473,12 @@ double SmcTemp::GetCpuTemp() { return temp; } - size_t valid_sensor_count = 0; - for (auto sensor : sensors) { - auto sensor_value = smc_accessor_.ReadValue(sensor.c_str()); - if (sensor_value >= valid_temperature_limits.first && sensor_value <= valid_temperature_limits.second) { - temp += sensor_value; - valid_sensor_count++; - } - } - temp /= valid_sensor_count; + temp = CalculateAverageTemperature(sensors, valid_temperature_limits); if (temp > std::numeric_limits::epsilon()) { return temp; } - size_t valid_aux_sensor_count = 0; - for (auto sensor : aux_sensors) { - auto sensor_value = smc_accessor_.ReadValue(sensor.c_str()); - if (sensor_value > valid_temperature_limits.first && sensor_value < valid_temperature_limits.second) { - temp += sensor_value; - valid_aux_sensor_count++; - } - } - if (valid_aux_sensor_count > 0) { - temp /= valid_aux_sensor_count; - } + temp += CalculateAverageTemperature(aux_sensors, valid_temperature_limits); #endif return temp; } diff --git a/smctemp.h b/smctemp.h index ab40009..6b16d24 100755 --- a/smctemp.h +++ b/smctemp.h @@ -25,6 +25,8 @@ #include #include +#include +#include #include "smctemp_types.h" @@ -92,6 +94,8 @@ class SmcAccessor { class SmcTemp { private: + double CalculateAverageTemperature(const std::vector& sensors, + const std::pair& limits); SmcAccessor smc_accessor_; public: