Skip to content

Commit

Permalink
Factor common code into CalculateAverageTemperature
Browse files Browse the repository at this point in the history
  • Loading branch information
enen92 committed Aug 28, 2023
1 parent 0fdbdc4 commit d4c4a96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
39 changes: 18 additions & 21 deletions smctemp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <sys/sysctl.h>
#include <algorithm>
#include <array>
#include <utility>

namespace {
std::string getCPUModel() {
Expand Down Expand Up @@ -384,6 +383,22 @@ kern_return_t SmcAccessor::PrintAll() {
return kIOReturnSuccess;
}

double SmcTemp::CalculateAverageTemperature(const std::vector<std::string>& sensors,
const std::pair<unsigned int, unsigned int> limits)
{

Check failure on line 388 in smctemp.cc

View workflow job for this annotation

GitHub Actions / runner / cpplint

[cpplint] reported by reviewdog 🐶 { should almost always be at the end of the previous line [whitespace/braces] [4] Raw Output: smctemp.cc:388: { should almost always be at the end of the previous line [whitespace/braces] [4]
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)
Expand Down Expand Up @@ -459,30 +474,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<double>::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;
}
Expand Down
3 changes: 3 additions & 0 deletions smctemp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <IOKit/IOKitLib.h>
#include <string>
#include <utility>

#include "smctemp_types.h"

Expand Down Expand Up @@ -92,6 +93,8 @@ class SmcAccessor {

class SmcTemp {
private:
double CalculateAverageTemperature(const std::vector<std::string>& sensors,

Check failure on line 96 in smctemp.h

View workflow job for this annotation

GitHub Actions / runner / cpplint

[cpplint] reported by reviewdog 🐶 Add #include <vector> for vector<> [build/include_what_you_use] [4] Raw Output: smctemp.h:96: Add #include <vector> for vector<> [build/include_what_you_use] [4]
const std::pair<unsigned int, unsigned int> limits);
SmcAccessor smc_accessor_;

public:
Expand Down

0 comments on commit d4c4a96

Please sign in to comment.