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

@@ -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,82 +0,0 @@
#include <boost/json/object.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <fan/PwmControl.h>
#define PWM_POSTFIX_ENABLE "_enable"
#define PWM_POSTFIX_MODE "_mode"
#define PWM_MAX_VALUE 255
using namespace std;
namespace fs = filesystem;
PwmControl::PwmControl(string controlPath) : mControlPath(controlPath) {
fs::path pathEnable(mControlPath + PWM_POSTFIX_ENABLE);
fs::path pathMode(mControlPath + PWM_POSTFIX_MODE);
mEnablePath = pathEnable;
mModePath = pathMode;
ifstream istrm;
istrm.open(mEnablePath);
istrm >> mInitialEnable;
istrm.close();
istrm.open(mInitialMode);
istrm >> mInitialMode;
istrm.close();
}
PwmControl::~PwmControl() {
cout << "Cleanup" << endl;
Reset();
}
void PwmControl::pwm(int percent) {
int pwmValue = PWM_MAX_VALUE * percent / 100;
ofstream ostrm(mControlPath, ios::trunc);
ostrm << pwmValue;
ostrm.close();
}
int PwmControl::pwm() {
int value;
ifstream istrm;
istrm.open(mControlPath);
istrm >> value;
return value;
}
void PwmControl::EnableManualControl() {
ofstream ostrm(mEnablePath, ios::trunc);
ostrm << static_cast<int>(PWM_ENABLE::MANUAL_CONTROL);
ostrm.close();
}
void PwmControl::Reset() {
ofstream ostrm(mEnablePath, ios::trunc);
ostrm << mInitialEnable;
ostrm.close();
ostrm.open(mModePath, ios::trunc);
ostrm << mInitialMode;
ostrm.close();
}
const string PwmControl::toString() const {
return fs::path(mControlPath).filename();
}
json PwmControl::toJson() const {
json obj = {"PwmControl", mControlPath};
return obj;
}