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

@@ -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_