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

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;
}