diff --git a/smctemp.cc b/smctemp.cc index b3e40ae..0c106b7 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 > 0.0) { + 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..827f574 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: