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 c8007a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
40 changes: 18 additions & 22 deletions smctemp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
#include <iostream>
#include <limits>
#include <string>
#include <vector>

#include "smctemp_string.h"

#if defined(ARCH_TYPE_ARM64)
#include <sys/sysctl.h>
#include <algorithm>
#include <array>
#include <utility>

namespace {
std::string getCPUModel() {
Expand Down Expand Up @@ -384,6 +382,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 387 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:387: { 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 +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<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
4 changes: 4 additions & 0 deletions smctemp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

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

#include "smctemp_types.h"

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

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

public:
Expand Down

0 comments on commit c8007a4

Please sign in to comment.