Refactoring, housekeeping, documentation

Add a class diagram. Still needs some details.
Refactor to better respect SOLID principles.
Housekeeping, move and rename classes/files.
This commit is contained in:
2022-09-27 00:09:30 +02:00
parent 4f6a1dfc4f
commit 289c55b78c
28 changed files with 402 additions and 339 deletions

View File

@@ -9,15 +9,17 @@ find_package(nlohmann_json 3.11.2 REQUIRED)
add_executable(app
src/main.cxx
src/sensor/SensorsWrapper.cxx
src/sensor/LMSensorsFacade.cxx
src/sensor/GPUSensorsFacade.cxx
src/sensor/Sensor.cxx
src/sensor/NvidiaSensor.cxx
src/sensor/HwmonSensor.cxx
src/fan/Pwm.cxx
src/fan/PwmControl.cxx
src/sensor/LMSensor.cxx
src/pwm/PWMControl.cxx
src/pwm/PWMControlFacade.cxx
src/fan/HwmonFan.cxx
src/Mapping.cxx
src/FanGenerator.cxx
src/Serializer.cxx
src/sensor/SensorManager.cxx
)
set_property(TARGET app PROPERTY CXX_STANDARD 17)

View File

@@ -3,14 +3,14 @@
start
:Initialization;
if (Mapping exists) then (yes)
:Load mapping;
if (Config exists) then (yes)
:Load config;
else (no)
:Generate mapping;
:Do first-start initialization;
endif
:Enter fan control loop;
:Load Fan Curves;
:Enter fan control loop;
stop

View File

@@ -1,32 +1,71 @@
@startuml
interface Fan {
{abstract} void PWM(int percent)
{abstract} int RPM()
}
interface Sensor {
{abstract} int value()
{abstract} string name()
+ {abstract} int Value()
}
class PwmControl {
+void pwm(int percent)
+int pwm()
class LMSensor {
- Identifier: string
+ void enableManualControl()
+ void reset()
- System Paths
- Initial Values
+ int Value()
}
Fan <|-- HwmonFan
class GPUSensor {
+ int Value()
}
Sensor <|-- HwmonSensor
Sensor <|-- NvidiaSensor
class HWMONFan {
- Identifier: string
- RPMSensor: Sensor
- PWMControl: PWMControl
HwmonFan - Sensor
HwmonFan -- PwmControl
+ int RPM()
+ void PWM(value: int)
}
class PWMControl {
+ void PWM(value: int)
+ int PWM()
}
class LMSensorsFacade
{
+ List<Sensor> RPMSensors()
+ List<Sensor> TemperatureSensors()
}
class GPUSensorsFacade
{
+ List<Sensor> TemperatureSensors()
}
class SensorManager
{
- LMSensorsFacade
- GPUSensorsFacade
+ List<Sensor> RPMSensors()
+ List<Sensor> TemperatureSensors()
}
class FanGenerator
{
+ FanList FindFans(List<RPMSensor>, List<PWMControl>)
}
HWMONFan -- Sensor
HWMONFan -- PWMControl
Sensor <|-- LMSensor
Sensor <|-- GPUSensor
SensorManager - Sensor
LMSensor -- LMSensorsFacade
GPUSensor -- GPUSensorsFacade
LMSensorsFacade -- SensorManager
GPUSensorsFacade -- SensorManager
FanGenerator - HWMONFan
FanGenerator - SensorManager
FanGenerator - PWMControl
@enduml

View File

@@ -0,0 +1,23 @@
@startuml
participant main
participant LMSensorsFacade as lms
participant GPUSensor as gpus
participant PWMControlFactory as pwmfc
main -> lms: RPMSensors()
main <-- lms: vector<Sensor>
main -> lms: TemperatureSensors()
main <-- lms: vector<Sensor>
main -> gpus: GPUSensor()
main <-- gpus: Sensor
note right
Add GPUSensor to list
of temperature sensors
end note
main -> pwmfc: PWMControllers()
main <-- pwmfc: vector<PWMControl>
@enduml

View File

@@ -0,0 +1,28 @@
#ifndef CONFIGMANAGER_H_
#define CONFIGMANAGER_H_
#include <memory>
#include <vector>
#include <nlohmann/json.hpp>
#include <fan/Fan.h>
#define CONFIG_FILE "/etc/fantasize/config.json"
using json = nlohmann::json;
class ConfigManager {
public:
ConfigManager();
~ConfigManager();
void WriteConfig();
void SaveFans(std::vector<std::shared_ptr<Fan>> fans);
private:
json mConfig;
};
#endif // CONFIGMANAGER_H_

View File

@@ -5,14 +5,14 @@
#include <vector>
#include <fan/Fan.h>
#include <fan/PwmControl.h>
#include <pwm/PWMControl.h>
#include <sensor/Sensor.h>
class Mapping {
class FanGenerator {
public:
std::vector<std::shared_ptr<Fan>>
createMapping(std::vector<std::shared_ptr<Sensor>> rpmSensors,
std::vector<std::shared_ptr<PwmControl>> pwmControls);
FindFans(std::vector<std::shared_ptr<Sensor>> rpmSensors,
std::vector<std::shared_ptr<PWMControl>> pwmControls);
private:
template <class Printable>

View File

@@ -5,12 +5,12 @@
#include <memory>
#include <fan/Fan.h>
#include <fan/PwmControl.h>
#include <pwm/PWMControl.h>
#include <sensor/Sensor.h>
class HwmonFan : public Fan {
public:
HwmonFan(std::shared_ptr<PwmControl> pwmControl,
HwmonFan(std::shared_ptr<PWMControl> pwmControl,
std::shared_ptr<Sensor> rpmSensor);
void pwm(int percent) override;
@@ -21,7 +21,7 @@ public:
const std::string toString() const override;
private:
std::shared_ptr<PwmControl> mPwmControl;
std::shared_ptr<PWMControl> mPWMControl;
std::shared_ptr<Sensor> mRpmSensor;
};

View File

@@ -1,39 +0,0 @@
#ifndef PWM_H_
#define PWM_H_
#include <string>
#include <unordered_map>
#include <vector>
enum class PWM_CONTROL_PROPERTY { CONTROL, ENABLE, MODE };
struct PWM_CONTROL {
std::string controlPath;
std::string enablePath;
std::string modePath;
std::string initialEnable;
std::string initialMode;
};
enum class PWM_ENABLE { FULL_SPEED = 0, MANUAL_CONTROL };
enum class PWM_MODE { DC = 0, PWM };
class PWM {
public:
PWM();
void dumpValues();
std::vector<PWM_CONTROL> getControls();
int readValue(PWM_CONTROL control, PWM_CONTROL_PROPERTY property);
void setEnable(PWM_CONTROL control, PWM_ENABLE value);
void setMode(PWM_CONTROL control, PWM_MODE mode);
void setValuePwm(PWM_CONTROL control, int pwm);
void setValuePercent(PWM_CONTROL control, int percentage);
private:
std::unordered_map<std::string, PWM_CONTROL> mPwmControls;
};
#endif // PWM_H_

View File

@@ -11,10 +11,10 @@ enum class PWM_CONTROL_PROPERTY { CONTROL, ENABLE, MODE };
enum class PWM_ENABLE { FULL_SPEED = 0, MANUAL_CONTROL };
enum class PWM_MODE { DC = 0, PWM };
class PwmControl : public Printable, public Serializable {
class PWMControl : public Printable, public Serializable {
public:
PwmControl(std::string controlPath);
~PwmControl();
PWMControl(std::string controlPath);
~PWMControl();
void pwm(int percent);
int pwm();

View File

@@ -0,0 +1,18 @@
#ifndef PWMCONTROLFACADE_H_
#define PWMCONTROLFACADE_H_
#include <filesystem>
#include <memory>
#include <regex>
#include <vector>
#include <pwm/PWMControl.h>
#define HWMON_BASE_PATH "/sys/class/hwmon"
class PWMControlFacade {
public:
std::vector<std::shared_ptr<PWMControl>> PWMControls();
};
#endif // PWMCONTROLFACADE_H_

View File

@@ -0,0 +1,14 @@
#ifndef GPUSENSORSFACADE_H_
#define GPUSENSORSFACADE_H_
#include <memory>
#include <vector>
#include <sensor/Sensor.h>
class GPUSensorsFacade {
public:
std::vector<std::shared_ptr<Sensor>> TemperatureSensors();
};
#endif // GPUSENSORSFACADE_H_

View File

@@ -5,10 +5,10 @@
#include <sensor/Sensor.h>
class HwmonSensor : public Sensor {
class LMSensor : public Sensor {
public:
HwmonSensor(const sensors_chip_name *chipName, const sensors_feature *feature,
const sensors_subfeature *subfeature);
LMSensor(const sensors_chip_name *chipName, const sensors_feature *feature,
const sensors_subfeature *subfeature);
int value() override;
const std::string toString() const override;

View File

@@ -0,0 +1,27 @@
#ifndef LMSENSORSFACADE_H_
#define LMSENSORSFACADE_H_
#include <memory>
#include <vector>
#include <sensors/sensors.h>
#include <sensor/Sensor.h>
class LMSensorsFacade {
public:
LMSensorsFacade();
~LMSensorsFacade();
std::vector<std::shared_ptr<Sensor>> TemperatureSensors();
std::vector<std::shared_ptr<Sensor>> RPMSensors();
private:
template <sensors_subfeature_type T>
std::vector<std::shared_ptr<Sensor>> Sensors();
private:
FILE *mConfigFile;
};
#endif // LMSENSORSFACADE_H_

View File

@@ -0,0 +1,20 @@
#ifndef SENSORMANAGER_H_
#define SENSORMANAGER_H_
#include <memory>
#include <sensor/GPUSensorsFacade.h>
#include <sensor/LMSensorsFacade.h>
class SensorManager {
public:
SensorManager();
std::vector<std::shared_ptr<Sensor>> TemperatureSensors();
std::vector<std::shared_ptr<Sensor>> RPMSensors();
private:
std::unique_ptr<LMSensorsFacade> mLMSensorsFacade;
std::unique_ptr<GPUSensorsFacade> mGPUSensorsFacade;
};
#endif // SENSORMANAGER_H_

View File

@@ -1,26 +0,0 @@
#ifndef SENSORSWRAPPER_H_
#define SENSORSWRAPPER_H_
#include <memory>
#include <vector>
#include <sensors/sensors.h>
#include <fan/PwmControl.h>
#include <sensor/Sensor.h>
class SensorsWrapper {
public:
SensorsWrapper();
~SensorsWrapper();
std::vector<std::shared_ptr<Sensor>>
Sensors(sensors_subfeature_type sensorType);
std::vector<std::shared_ptr<PwmControl>> PwmControls();
private:
FILE *mConfigFile;
};
#endif // SENSORSWRAPPER_H_

29
app/src/ConfigManager.cxx Normal file
View File

@@ -0,0 +1,29 @@
#include <filesystem>
#include <fstream>
#include <ConfigManager.h>
#include <memory>
using namespace std;
namespace fs = std::filesystem;
ConfigManager::ConfigManager() {
if (fs::exists(CONFIG_FILE)) {
ifstream f(CONFIG_FILE);
mConfig = json::parse(f);
}
}
void ConfigManager::SaveFans(vector<shared_ptr<Fan>> fans) {
json obj;
for (auto fan : fans) {
}
}
ConfigManager::~ConfigManager() { WriteConfig(); }
void ConfigManager::WriteConfig() {
ofstream f(CONFIG_FILE, ios::trunc);
f << mConfig.dump(2) << endl;
}

View File

@@ -1,20 +1,21 @@
#include "FanGenerator.h"
#include <chrono>
#include <iostream>
#include <map>
#include <memory>
#include <thread>
#include <Mapping.h>
#include <FanGenerator.h>
#include <fan/HwmonFan.h>
#include <fan/PwmControl.h>
#include <pwm/PWMControl.h>
#define SETTLE_TIMEOUT 5
using namespace std;
vector<shared_ptr<Fan>>
Mapping::createMapping(vector<shared_ptr<Sensor>> rpmSensors,
vector<shared_ptr<PwmControl>> pwmControls) {
FanGenerator::FindFans(vector<shared_ptr<Sensor>> rpmSensors,
vector<shared_ptr<PWMControl>> pwmControls) {
print("RPM Sensors", rpmSensors);
print("PWM controllers", pwmControls);
vector<shared_ptr<Fan>> mapping;
@@ -65,7 +66,7 @@ Mapping::createMapping(vector<shared_ptr<Sensor>> rpmSensors,
}
template <class Printable>
void Mapping::print(string listLabel, vector<shared_ptr<Printable>> list) {
void FanGenerator::print(string listLabel, vector<shared_ptr<Printable>> list) {
cout << listLabel << ": " << endl;
for (auto i : list) {

View File

@@ -6,8 +6,8 @@
#include <Serializer.h>
#include <fan/HwmonFan.h>
#include <fan/PwmControl.h>
#include <sensor/HwmonSensor.h>
#include <pwm/PWMControl.h>
#include <sensor/LMSensor.h>
using namespace std;
namespace fs = filesystem;
@@ -41,8 +41,8 @@ Serializer::DeserializeFans(vector<shared_ptr<Sensor>> availableSensors) {
auto data = ReadJson();
try {
for (auto &el : data["fans"].items()) {
auto pwmControl = make_shared<PwmControl>(el.value()["PwmControl"]);
auto rpmSensor = sensorMap[el.value()["HwmonSensor"]];
auto pwmControl = make_shared<PWMControl>(el.value()["PWMControl"]);
auto rpmSensor = sensorMap[el.value()["LMSensor"]];
mapping.push_back(make_shared<HwmonFan>(pwmControl, rpmSensor));
}

View File

@@ -1,24 +1,24 @@
#include "fan/PwmControl.h"
#include "pwm/PWMControl.h"
#include <boost/json/object.hpp>
#include <fan/HwmonFan.h>
using namespace std;
HwmonFan::HwmonFan(shared_ptr<PwmControl> pwmControl,
HwmonFan::HwmonFan(shared_ptr<PWMControl> pwmControl,
shared_ptr<Sensor> rpmSensor)
: mPwmControl(pwmControl), mRpmSensor(rpmSensor) {}
: mPWMControl(pwmControl), mRpmSensor(rpmSensor) {}
void HwmonFan::pwm(int percent) { mPwmControl->pwm(percent); }
void HwmonFan::pwm(int percent) { mPWMControl->pwm(percent); }
int HwmonFan::rpm() { return mRpmSensor->value(); }
json HwmonFan::toJson() const {
json obj;
obj = {mPwmControl->toJson(), mRpmSensor->toJson()};
obj = {mPWMControl->toJson(), mRpmSensor->toJson()};
return obj;
}
const string HwmonFan::toString() const {
return "Fan!\nPwmControl: " + mPwmControl->toString() +
return "Fan!\nPWMControl: " + mPWMControl->toString() +
"\nRpmSensor: " + mRpmSensor->toString();
}

View File

@@ -1,109 +0,0 @@
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <fan/Pwm.h>
namespace fs = std::filesystem;
using namespace std;
#define HWMON_BASE_PATH "/sys/class/hwmon"
#define PWM_POSTFIX_ENABLE "_enable"
#define PWM_POSTFIX_MODE "_mode"
#define PWM_MAX_VALUE 255
PWM::PWM() {
const regex re_ctrl_enable("pwm[0-9]_enable");
const regex re_ctrl_mode("pwm[0-9]_mode");
const regex re_ctrl("pwm[0-9]");
if (!fs::exists(HWMON_BASE_PATH)) {
cerr << HWMON_BASE_PATH << " doesn't exist" << endl;
} else {
for (const fs::directory_entry &hwmon_device :
fs::directory_iterator{HWMON_BASE_PATH}) {
for (const fs::directory_entry &control :
fs::directory_iterator{hwmon_device}) {
auto filename = control.path().filename().string();
if (regex_match(filename, re_ctrl)) {
auto controlPath = control.path().string();
fs::path path_ctrl_enable(
string(controlPath + string(PWM_POSTFIX_ENABLE)));
fs::path path_ctrl_mode(
string(controlPath + string(PWM_POSTFIX_MODE)));
mPwmControls.insert(
{controlPath, PWM_CONTROL{controlPath, path_ctrl_enable.string(),
path_ctrl_mode.string()}});
}
}
}
}
}
void PWM::dumpValues() {
for (auto control : mPwmControls) {
cout << control.second.controlPath << ", " << control.second.enablePath
<< ": " << control.second.modePath << endl;
}
}
vector<PWM_CONTROL> PWM::getControls() {
vector<PWM_CONTROL> vec;
for (auto elem : mPwmControls) {
vec.push_back(elem.second);
}
return vec;
}
void PWM::setEnable(PWM_CONTROL control, PWM_ENABLE value) {
cout << control.controlPath << endl;
ofstream ostrm(control.enablePath, ios::trunc);
ostrm << static_cast<int>(value);
ostrm.close();
}
void PWM::setValuePwm(PWM_CONTROL control, int pwm) {
if (pwm < 0 || pwm > 255)
return;
ofstream ostrm(control.controlPath, ios::trunc);
ostrm << pwm;
ostrm.close();
}
int PWM::readValue(PWM_CONTROL control, PWM_CONTROL_PROPERTY property) {
int result;
ifstream istrm;
switch (property) {
case PWM_CONTROL_PROPERTY::CONTROL:
istrm.open(control.controlPath, ios::in);
istrm >> result;
break;
case PWM_CONTROL_PROPERTY::ENABLE:
istrm.open(control.enablePath, ios::in);
istrm >> result;
break;
case PWM_CONTROL_PROPERTY::MODE:
istrm.open(control.modePath, ios::in);
istrm >> result;
break;
}
return result;
}
void PWM::setValuePercent(PWM_CONTROL control, int percentage) {
setValuePwm(control, PWM_MAX_VALUE * percentage / 100);
}

View File

@@ -1,26 +1,22 @@
#include <filesystem>
#include <iostream>
#include <Mapping.h>
#include <FanGenerator.h>
#include <Serializer.h>
#include <fan/Fan.h>
#include <sensor/NvidiaSensor.h>
#include <sensor/SensorsWrapper.h>
#include <pwm/PWMControlFacade.h>
#include <sensor/SensorManager.h>
namespace fs = std::filesystem;
int main() {
SensorManager sensorManager;
auto pwmSensors = sensorManager.RPMSensors();
SensorsWrapper sensorsWrapper;
PWMControlFacade pwmControlFacade;
auto controls = pwmControlFacade.PWMControls();
auto tempSensors = sensorsWrapper.Sensors(SENSORS_SUBFEATURE_TEMP_INPUT);
tempSensors.push_back(std::make_shared<NvidiaSensor>());
auto pwmSensors = sensorsWrapper.Sensors(SENSORS_SUBFEATURE_FAN_INPUT);
auto controls = sensorsWrapper.PwmControls();
Mapping m;
FanGenerator m;
Serializer s;
std::vector<std::shared_ptr<Fan>> fans;
@@ -28,7 +24,7 @@ int main() {
if (fs::exists(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME)) {
fans = s.DeserializeFans(pwmSensors);
} else {
fans = m.createMapping(pwmSensors, controls);
fans = m.FindFans(pwmSensors, controls);
s.SerializeFans(fans);
}

View File

@@ -3,7 +3,7 @@
#include <fstream>
#include <iostream>
#include <fan/PwmControl.h>
#include <pwm/PWMControl.h>
#define PWM_POSTFIX_ENABLE "_enable"
#define PWM_POSTFIX_MODE "_mode"
@@ -13,7 +13,7 @@
using namespace std;
namespace fs = filesystem;
PwmControl::PwmControl(string controlPath) : mControlPath(controlPath) {
PWMControl::PWMControl(string controlPath) : mControlPath(controlPath) {
fs::path pathEnable(mControlPath + PWM_POSTFIX_ENABLE);
fs::path pathMode(mControlPath + PWM_POSTFIX_MODE);
@@ -31,12 +31,12 @@ PwmControl::PwmControl(string controlPath) : mControlPath(controlPath) {
istrm.close();
}
PwmControl::~PwmControl() {
PWMControl::~PWMControl() {
cout << "Cleanup" << endl;
Reset();
}
void PwmControl::pwm(int percent) {
void PWMControl::pwm(int percent) {
int pwmValue = PWM_MAX_VALUE * percent / 100;
ofstream ostrm(mControlPath, ios::trunc);
@@ -44,7 +44,7 @@ void PwmControl::pwm(int percent) {
ostrm.close();
}
int PwmControl::pwm() {
int PWMControl::pwm() {
int value;
ifstream istrm;
@@ -54,13 +54,13 @@ int PwmControl::pwm() {
return value;
}
void PwmControl::EnableManualControl() {
void PWMControl::EnableManualControl() {
ofstream ostrm(mEnablePath, ios::trunc);
ostrm << static_cast<int>(PWM_ENABLE::MANUAL_CONTROL);
ostrm.close();
}
void PwmControl::Reset() {
void PWMControl::Reset() {
ofstream ostrm(mEnablePath, ios::trunc);
ostrm << mInitialEnable;
@@ -72,11 +72,11 @@ void PwmControl::Reset() {
ostrm.close();
}
const string PwmControl::toString() const {
const string PWMControl::toString() const {
return fs::path(mControlPath).filename();
}
json PwmControl::toJson() const {
json obj = {"PwmControl", mControlPath};
json PWMControl::toJson() const {
json obj = {"PWMControl", mControlPath};
return obj;
}

View File

@@ -0,0 +1,37 @@
#include <filesystem>
#include <iostream>
#include <regex>
#include <pwm/PWMControlFacade.h>
using namespace std;
namespace fs = std::filesystem;
vector<shared_ptr<PWMControl>> PWMControlFacade::PWMControls() {
vector<shared_ptr<PWMControl>> controls;
const regex re_ctrl_enable("pwm[0-9]_enable");
const regex re_ctrl_mode("pwm[0-9]_mode");
const regex re_ctrl("pwm[0-9]");
if (!fs::exists(HWMON_BASE_PATH)) {
cout << HWMON_BASE_PATH << " doesn't exist" << endl;
} else {
for (const fs::directory_entry &hwmon_device :
fs::directory_iterator{HWMON_BASE_PATH}) {
for (const fs::directory_entry &control :
fs::directory_iterator{hwmon_device}) {
auto filename = control.path().filename().string();
if (regex_match(filename, re_ctrl)) {
auto controlPath = control.path().string();
controls.push_back(make_shared<PWMControl>(controlPath));
}
}
}
}
return controls;
}

View File

@@ -0,0 +1,9 @@
#include <sensor/GPUSensorsFacade.h>
#include <sensor/NvidiaSensor.h>
using namespace std;
vector<shared_ptr<Sensor>> GPUSensorsFacade::TemperatureSensors() {
return vector<shared_ptr<Sensor>>{make_shared<NvidiaSensor>()};
}

View File

@@ -1,26 +1,26 @@
#include <boost/json/kind.hpp>
#include <sensor/HwmonSensor.h>
#include <sensor/LMSensor.h>
#include <sensors/sensors.h>
using namespace std;
HwmonSensor::HwmonSensor(const sensors_chip_name *chipName,
const sensors_feature *feature,
const sensors_subfeature *subfeature)
LMSensor::LMSensor(const sensors_chip_name *chipName,
const sensors_feature *feature,
const sensors_subfeature *subfeature)
: mChipName(chipName), mFeature(feature), mSubFeature(subfeature) {}
int HwmonSensor::value() {
int LMSensor::value() {
double value;
sensors_get_value(mChipName, mSubFeature->number, &value);
return static_cast<int>(value);
}
const string HwmonSensor::toString() const {
const string LMSensor::toString() const {
return sensors_get_label(mChipName, mFeature);
}
json HwmonSensor::toJson() const {
json LMSensor::toJson() const {
json obj = {"HwmonSensor", toString()};
return obj;
}

View File

@@ -0,0 +1,50 @@
#include <stdexcept>
#include <sensors/sensors.h>
#include <sensor/LMSensorsFacade.h>
#include <sensor/LMSensor.h>
#include <sensor/NvidiaSensor.h>
using namespace std;
#define CONFIG_FILE "/etc/conf.d/sensors"
LMSensorsFacade::LMSensorsFacade() : mConfigFile(fopen(CONFIG_FILE, "r")) {
if (sensors_init(mConfigFile) != 0) {
throw runtime_error("Config file doesn't exist");
}
}
LMSensorsFacade::~LMSensorsFacade() { sensors_cleanup(); }
std::vector<std::shared_ptr<Sensor>> LMSensorsFacade::TemperatureSensors() {
return Sensors<SENSORS_SUBFEATURE_TEMP_INPUT>();
}
std::vector<std::shared_ptr<Sensor>> LMSensorsFacade::RPMSensors() {
return Sensors<SENSORS_SUBFEATURE_FAN_INPUT>();
}
template <sensors_subfeature_type T>
std::vector<std::shared_ptr<Sensor>> LMSensorsFacade::Sensors() {
std::vector<std::shared_ptr<Sensor>> sensors;
int c = 0;
for (const sensors_chip_name *chipName;
(chipName = sensors_get_detected_chips(0, &c)) != NULL;) {
int d = 0;
for (const sensors_feature *feature;
(feature = sensors_get_features(chipName, &d)) != NULL;) {
auto subFeature = sensors_get_subfeature(chipName, feature, T);
if (subFeature) {
sensors.push_back(
std::make_shared<LMSensor>(chipName, feature, subFeature));
}
}
}
return sensors;
}

View File

@@ -0,0 +1,22 @@
#include <sensor/SensorManager.h>
using namespace std;
SensorManager::SensorManager()
: mLMSensorsFacade(make_unique<LMSensorsFacade>()),
mGPUSensorsFacade(make_unique<GPUSensorsFacade>()) {}
vector<shared_ptr<Sensor>> SensorManager::TemperatureSensors() {
vector<shared_ptr<Sensor>> tempSensors;
tempSensors = mLMSensorsFacade->TemperatureSensors();
auto gpuSensors = mGPUSensorsFacade->TemperatureSensors();
tempSensors.insert(tempSensors.end(), gpuSensors.begin(), gpuSensors.end());
return tempSensors;
}
vector<shared_ptr<Sensor>> SensorManager::RPMSensors() {
return mLMSensorsFacade->RPMSensors();
}

View File

@@ -1,78 +0,0 @@
#include <cstdio>
#include <filesystem>
#include <iostream>
#include <regex>
#include <stdexcept>
#include <type_traits>
#include <sensors/sensors.h>
#include <sensor/SensorsWrapper.h>
#include <sensor/HwmonSensor.h>
#include <sensor/NvidiaSensor.h>
using namespace std;
namespace fs = std::filesystem;
#define CONFIG_FILE "/etc/conf.d/sensors"
#define HWMON_BASE_PATH "/sys/class/hwmon"
SensorsWrapper::SensorsWrapper() : mConfigFile(fopen(CONFIG_FILE, "r")) {
if (sensors_init(mConfigFile) != 0) {
throw runtime_error("Config file doesn't exist");
}
}
SensorsWrapper::~SensorsWrapper() { sensors_cleanup(); }
std::vector<std::shared_ptr<Sensor>>
SensorsWrapper::Sensors(sensors_subfeature_type sensorType) {
std::vector<std::shared_ptr<Sensor>> sensors;
int c = 0;
for (const sensors_chip_name *chipName;
(chipName = sensors_get_detected_chips(0, &c)) != NULL;) {
int d = 0;
for (const sensors_feature *feature;
(feature = sensors_get_features(chipName, &d)) != NULL;) {
auto subFeature = sensors_get_subfeature(chipName, feature, sensorType);
if (subFeature) {
sensors.push_back(
std::make_shared<HwmonSensor>(chipName, feature, subFeature));
}
}
}
return sensors;
}
std::vector<std::shared_ptr<PwmControl>> SensorsWrapper::PwmControls() {
std::vector<std::shared_ptr<PwmControl>> controls;
const std::regex re_ctrl_enable("pwm[0-9]_enable");
const std::regex re_ctrl_mode("pwm[0-9]_mode");
const std::regex re_ctrl("pwm[0-9]");
if (!fs::exists(HWMON_BASE_PATH)) {
std::cout << HWMON_BASE_PATH << " doesn't exist" << std::endl;
} else {
for (const fs::directory_entry &hwmon_device :
fs::directory_iterator{HWMON_BASE_PATH}) {
for (const fs::directory_entry &control :
fs::directory_iterator{hwmon_device}) {
auto filename = control.path().filename().string();
if (regex_match(filename, re_ctrl)) {
auto controlPath = control.path().string();
controls.push_back(std::make_shared<PwmControl>(controlPath));
}
}
}
}
return controls;
}