From 289c55b78c1cd297a1dda834a42190c82ec408a2 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 27 Sep 2022 00:09:30 +0200 Subject: [PATCH] Refactoring, housekeeping, documentation Add a class diagram. Still needs some details. Refactor to better respect SOLID principles. Housekeeping, move and rename classes/files. --- app/CMakeLists.txt | 12 +- app/doc/ApplicationStart.plantuml | 8 +- app/doc/Class.plantuml | 79 +++++++++---- app/doc/Initialization.plantuml | 23 ++++ app/include/ConfigManager.h | 28 +++++ app/include/{Mapping.h => FanGenerator.h} | 8 +- app/include/fan/HwmonFan.h | 6 +- app/include/fan/Pwm.h | 39 ------- .../{fan/PwmControl.h => pwm/PWMControl.h} | 6 +- app/include/pwm/PWMControlFacade.h | 18 +++ app/include/sensor/GPUSensorsFacade.h | 14 +++ .../sensor/{HwmonSensor.h => LMSensor.h} | 6 +- app/include/sensor/LMSensorsFacade.h | 27 +++++ app/include/sensor/SensorManager.h | 20 ++++ app/include/sensor/SensorsWrapper.h | 26 ----- app/src/ConfigManager.cxx | 29 +++++ app/src/{Mapping.cxx => FanGenerator.cxx} | 11 +- app/src/Serializer.cxx | 8 +- app/src/fan/HwmonFan.cxx | 12 +- app/src/fan/Pwm.cxx | 109 ------------------ app/src/main.cxx | 22 ++-- .../PwmControl.cxx => pwm/PWMControl.cxx} | 20 ++-- app/src/pwm/PWMControlFacade.cxx | 37 ++++++ app/src/sensor/GPUSensorsFacade.cxx | 9 ++ .../sensor/{HwmonSensor.cxx => LMSensor.cxx} | 14 +-- app/src/sensor/LMSensorsFacade.cxx | 50 ++++++++ app/src/sensor/SensorManager.cxx | 22 ++++ app/src/sensor/SensorsWrapper.cxx | 78 ------------- 28 files changed, 402 insertions(+), 339 deletions(-) create mode 100644 app/doc/Initialization.plantuml create mode 100644 app/include/ConfigManager.h rename app/include/{Mapping.h => FanGenerator.h} (64%) delete mode 100644 app/include/fan/Pwm.h rename app/include/{fan/PwmControl.h => pwm/PWMControl.h} (85%) create mode 100644 app/include/pwm/PWMControlFacade.h create mode 100644 app/include/sensor/GPUSensorsFacade.h rename app/include/sensor/{HwmonSensor.h => LMSensor.h} (68%) create mode 100644 app/include/sensor/LMSensorsFacade.h create mode 100644 app/include/sensor/SensorManager.h delete mode 100644 app/include/sensor/SensorsWrapper.h create mode 100644 app/src/ConfigManager.cxx rename app/src/{Mapping.cxx => FanGenerator.cxx} (84%) delete mode 100644 app/src/fan/Pwm.cxx rename app/src/{fan/PwmControl.cxx => pwm/PWMControl.cxx} (76%) create mode 100644 app/src/pwm/PWMControlFacade.cxx create mode 100644 app/src/sensor/GPUSensorsFacade.cxx rename app/src/sensor/{HwmonSensor.cxx => LMSensor.cxx} (54%) create mode 100644 app/src/sensor/LMSensorsFacade.cxx create mode 100644 app/src/sensor/SensorManager.cxx delete mode 100644 app/src/sensor/SensorsWrapper.cxx diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 054d163..c314f47 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -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) diff --git a/app/doc/ApplicationStart.plantuml b/app/doc/ApplicationStart.plantuml index b3a261e..a7e7844 100644 --- a/app/doc/ApplicationStart.plantuml +++ b/app/doc/ApplicationStart.plantuml @@ -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 diff --git a/app/doc/Class.plantuml b/app/doc/Class.plantuml index 3764179..0dbfbac 100644 --- a/app/doc/Class.plantuml +++ b/app/doc/Class.plantuml @@ -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 RPMSensors() + + List TemperatureSensors() +} + +class GPUSensorsFacade +{ + + List TemperatureSensors() +} + +class SensorManager +{ + - LMSensorsFacade + - GPUSensorsFacade + + + List RPMSensors() + + List TemperatureSensors() +} + +class FanGenerator +{ + + FanList FindFans(List, List) +} + +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 diff --git a/app/doc/Initialization.plantuml b/app/doc/Initialization.plantuml new file mode 100644 index 0000000..9837786 --- /dev/null +++ b/app/doc/Initialization.plantuml @@ -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 + +main -> lms: TemperatureSensors() +main <-- lms: vector + +main -> gpus: GPUSensor() +main <-- gpus: Sensor +note right + Add GPUSensor to list + of temperature sensors +end note + +main -> pwmfc: PWMControllers() +main <-- pwmfc: vector + +@enduml diff --git a/app/include/ConfigManager.h b/app/include/ConfigManager.h new file mode 100644 index 0000000..ae11194 --- /dev/null +++ b/app/include/ConfigManager.h @@ -0,0 +1,28 @@ +#ifndef CONFIGMANAGER_H_ +#define CONFIGMANAGER_H_ + +#include +#include + +#include + +#include + +#define CONFIG_FILE "/etc/fantasize/config.json" + +using json = nlohmann::json; + +class ConfigManager { +public: + ConfigManager(); + ~ConfigManager(); + + void WriteConfig(); + + void SaveFans(std::vector> fans); + +private: + json mConfig; +}; + +#endif // CONFIGMANAGER_H_ diff --git a/app/include/Mapping.h b/app/include/FanGenerator.h similarity index 64% rename from app/include/Mapping.h rename to app/include/FanGenerator.h index 49cc848..d45665e 100644 --- a/app/include/Mapping.h +++ b/app/include/FanGenerator.h @@ -5,14 +5,14 @@ #include #include -#include +#include #include -class Mapping { +class FanGenerator { public: std::vector> - createMapping(std::vector> rpmSensors, - std::vector> pwmControls); + FindFans(std::vector> rpmSensors, + std::vector> pwmControls); private: template diff --git a/app/include/fan/HwmonFan.h b/app/include/fan/HwmonFan.h index 7ddd940..0a26e13 100644 --- a/app/include/fan/HwmonFan.h +++ b/app/include/fan/HwmonFan.h @@ -5,12 +5,12 @@ #include #include -#include +#include #include class HwmonFan : public Fan { public: - HwmonFan(std::shared_ptr pwmControl, + HwmonFan(std::shared_ptr pwmControl, std::shared_ptr rpmSensor); void pwm(int percent) override; @@ -21,7 +21,7 @@ public: const std::string toString() const override; private: - std::shared_ptr mPwmControl; + std::shared_ptr mPWMControl; std::shared_ptr mRpmSensor; }; diff --git a/app/include/fan/Pwm.h b/app/include/fan/Pwm.h deleted file mode 100644 index d902986..0000000 --- a/app/include/fan/Pwm.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef PWM_H_ -#define PWM_H_ - -#include -#include -#include - -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 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 mPwmControls; -}; - -#endif // PWM_H_ diff --git a/app/include/fan/PwmControl.h b/app/include/pwm/PWMControl.h similarity index 85% rename from app/include/fan/PwmControl.h rename to app/include/pwm/PWMControl.h index a8d1b45..60e951f 100644 --- a/app/include/fan/PwmControl.h +++ b/app/include/pwm/PWMControl.h @@ -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(); diff --git a/app/include/pwm/PWMControlFacade.h b/app/include/pwm/PWMControlFacade.h new file mode 100644 index 0000000..6776524 --- /dev/null +++ b/app/include/pwm/PWMControlFacade.h @@ -0,0 +1,18 @@ +#ifndef PWMCONTROLFACADE_H_ +#define PWMCONTROLFACADE_H_ + +#include +#include +#include +#include + +#include + +#define HWMON_BASE_PATH "/sys/class/hwmon" + +class PWMControlFacade { +public: + std::vector> PWMControls(); +}; + +#endif // PWMCONTROLFACADE_H_ diff --git a/app/include/sensor/GPUSensorsFacade.h b/app/include/sensor/GPUSensorsFacade.h new file mode 100644 index 0000000..d7b98d3 --- /dev/null +++ b/app/include/sensor/GPUSensorsFacade.h @@ -0,0 +1,14 @@ +#ifndef GPUSENSORSFACADE_H_ +#define GPUSENSORSFACADE_H_ + +#include +#include + +#include + +class GPUSensorsFacade { +public: + std::vector> TemperatureSensors(); +}; + +#endif // GPUSENSORSFACADE_H_ diff --git a/app/include/sensor/HwmonSensor.h b/app/include/sensor/LMSensor.h similarity index 68% rename from app/include/sensor/HwmonSensor.h rename to app/include/sensor/LMSensor.h index adc33fe..bdb6c9b 100644 --- a/app/include/sensor/HwmonSensor.h +++ b/app/include/sensor/LMSensor.h @@ -5,10 +5,10 @@ #include -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; diff --git a/app/include/sensor/LMSensorsFacade.h b/app/include/sensor/LMSensorsFacade.h new file mode 100644 index 0000000..3ba19a0 --- /dev/null +++ b/app/include/sensor/LMSensorsFacade.h @@ -0,0 +1,27 @@ +#ifndef LMSENSORSFACADE_H_ +#define LMSENSORSFACADE_H_ + +#include +#include + +#include + +#include + +class LMSensorsFacade { +public: + LMSensorsFacade(); + ~LMSensorsFacade(); + + std::vector> TemperatureSensors(); + std::vector> RPMSensors(); + +private: + template + std::vector> Sensors(); + +private: + FILE *mConfigFile; +}; + +#endif // LMSENSORSFACADE_H_ diff --git a/app/include/sensor/SensorManager.h b/app/include/sensor/SensorManager.h new file mode 100644 index 0000000..3b76cad --- /dev/null +++ b/app/include/sensor/SensorManager.h @@ -0,0 +1,20 @@ +#ifndef SENSORMANAGER_H_ +#define SENSORMANAGER_H_ + +#include +#include +#include + +class SensorManager { +public: + SensorManager(); + + std::vector> TemperatureSensors(); + std::vector> RPMSensors(); + +private: + std::unique_ptr mLMSensorsFacade; + std::unique_ptr mGPUSensorsFacade; +}; + +#endif // SENSORMANAGER_H_ diff --git a/app/include/sensor/SensorsWrapper.h b/app/include/sensor/SensorsWrapper.h deleted file mode 100644 index 7cfef9a..0000000 --- a/app/include/sensor/SensorsWrapper.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef SENSORSWRAPPER_H_ -#define SENSORSWRAPPER_H_ - -#include -#include - -#include - -#include -#include - -class SensorsWrapper { -public: - SensorsWrapper(); - ~SensorsWrapper(); - - std::vector> - Sensors(sensors_subfeature_type sensorType); - - std::vector> PwmControls(); - -private: - FILE *mConfigFile; -}; - -#endif // SENSORSWRAPPER_H_ diff --git a/app/src/ConfigManager.cxx b/app/src/ConfigManager.cxx new file mode 100644 index 0000000..95cd4e0 --- /dev/null +++ b/app/src/ConfigManager.cxx @@ -0,0 +1,29 @@ +#include +#include + +#include +#include + +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> fans) { + json obj; + + for (auto fan : fans) { + } +} + +ConfigManager::~ConfigManager() { WriteConfig(); } + +void ConfigManager::WriteConfig() { + ofstream f(CONFIG_FILE, ios::trunc); + f << mConfig.dump(2) << endl; +} diff --git a/app/src/Mapping.cxx b/app/src/FanGenerator.cxx similarity index 84% rename from app/src/Mapping.cxx rename to app/src/FanGenerator.cxx index 28e7cc5..06f5255 100644 --- a/app/src/Mapping.cxx +++ b/app/src/FanGenerator.cxx @@ -1,20 +1,21 @@ +#include "FanGenerator.h" #include #include #include #include #include -#include +#include #include -#include +#include #define SETTLE_TIMEOUT 5 using namespace std; vector> -Mapping::createMapping(vector> rpmSensors, - vector> pwmControls) { +FanGenerator::FindFans(vector> rpmSensors, + vector> pwmControls) { print("RPM Sensors", rpmSensors); print("PWM controllers", pwmControls); vector> mapping; @@ -65,7 +66,7 @@ Mapping::createMapping(vector> rpmSensors, } template -void Mapping::print(string listLabel, vector> list) { +void FanGenerator::print(string listLabel, vector> list) { cout << listLabel << ": " << endl; for (auto i : list) { diff --git a/app/src/Serializer.cxx b/app/src/Serializer.cxx index ea6bcfb..fdd730b 100644 --- a/app/src/Serializer.cxx +++ b/app/src/Serializer.cxx @@ -6,8 +6,8 @@ #include #include -#include -#include +#include +#include using namespace std; namespace fs = filesystem; @@ -41,8 +41,8 @@ Serializer::DeserializeFans(vector> availableSensors) { auto data = ReadJson(); try { for (auto &el : data["fans"].items()) { - auto pwmControl = make_shared(el.value()["PwmControl"]); - auto rpmSensor = sensorMap[el.value()["HwmonSensor"]]; + auto pwmControl = make_shared(el.value()["PWMControl"]); + auto rpmSensor = sensorMap[el.value()["LMSensor"]]; mapping.push_back(make_shared(pwmControl, rpmSensor)); } diff --git a/app/src/fan/HwmonFan.cxx b/app/src/fan/HwmonFan.cxx index 2ff4a83..6b171eb 100644 --- a/app/src/fan/HwmonFan.cxx +++ b/app/src/fan/HwmonFan.cxx @@ -1,24 +1,24 @@ -#include "fan/PwmControl.h" +#include "pwm/PWMControl.h" #include #include using namespace std; -HwmonFan::HwmonFan(shared_ptr pwmControl, +HwmonFan::HwmonFan(shared_ptr pwmControl, shared_ptr 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(); } diff --git a/app/src/fan/Pwm.cxx b/app/src/fan/Pwm.cxx deleted file mode 100644 index fdc54b7..0000000 --- a/app/src/fan/Pwm.cxx +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include - -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::getControls() { - vector 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(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); -} diff --git a/app/src/main.cxx b/app/src/main.cxx index 4510248..0450d46 100644 --- a/app/src/main.cxx +++ b/app/src/main.cxx @@ -1,26 +1,22 @@ #include #include -#include +#include #include #include -#include -#include +#include +#include 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()); - - auto pwmSensors = sensorsWrapper.Sensors(SENSORS_SUBFEATURE_FAN_INPUT); - - auto controls = sensorsWrapper.PwmControls(); - - Mapping m; + FanGenerator m; Serializer s; std::vector> 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); } diff --git a/app/src/fan/PwmControl.cxx b/app/src/pwm/PWMControl.cxx similarity index 76% rename from app/src/fan/PwmControl.cxx rename to app/src/pwm/PWMControl.cxx index f6f58ac..bc01937 100644 --- a/app/src/fan/PwmControl.cxx +++ b/app/src/pwm/PWMControl.cxx @@ -3,7 +3,7 @@ #include #include -#include +#include #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(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; } diff --git a/app/src/pwm/PWMControlFacade.cxx b/app/src/pwm/PWMControlFacade.cxx new file mode 100644 index 0000000..26a2456 --- /dev/null +++ b/app/src/pwm/PWMControlFacade.cxx @@ -0,0 +1,37 @@ +#include +#include +#include + +#include + +using namespace std; +namespace fs = std::filesystem; + +vector> PWMControlFacade::PWMControls() { + vector> 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(controlPath)); + } + } + } + } + + return controls; +} diff --git a/app/src/sensor/GPUSensorsFacade.cxx b/app/src/sensor/GPUSensorsFacade.cxx new file mode 100644 index 0000000..3fb215f --- /dev/null +++ b/app/src/sensor/GPUSensorsFacade.cxx @@ -0,0 +1,9 @@ +#include + +#include + +using namespace std; + +vector> GPUSensorsFacade::TemperatureSensors() { + return vector>{make_shared()}; +} diff --git a/app/src/sensor/HwmonSensor.cxx b/app/src/sensor/LMSensor.cxx similarity index 54% rename from app/src/sensor/HwmonSensor.cxx rename to app/src/sensor/LMSensor.cxx index 9ac60f4..e273cb3 100644 --- a/app/src/sensor/HwmonSensor.cxx +++ b/app/src/sensor/LMSensor.cxx @@ -1,26 +1,26 @@ #include -#include +#include #include 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(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; } diff --git a/app/src/sensor/LMSensorsFacade.cxx b/app/src/sensor/LMSensorsFacade.cxx new file mode 100644 index 0000000..ec92880 --- /dev/null +++ b/app/src/sensor/LMSensorsFacade.cxx @@ -0,0 +1,50 @@ +#include + +#include + +#include + +#include +#include + +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> LMSensorsFacade::TemperatureSensors() { + return Sensors(); +} + +std::vector> LMSensorsFacade::RPMSensors() { + return Sensors(); +} + +template +std::vector> LMSensorsFacade::Sensors() { + std::vector> 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(chipName, feature, subFeature)); + } + } + } + + return sensors; +} diff --git a/app/src/sensor/SensorManager.cxx b/app/src/sensor/SensorManager.cxx new file mode 100644 index 0000000..a707291 --- /dev/null +++ b/app/src/sensor/SensorManager.cxx @@ -0,0 +1,22 @@ +#include + +using namespace std; + +SensorManager::SensorManager() + : mLMSensorsFacade(make_unique()), + mGPUSensorsFacade(make_unique()) {} + +vector> SensorManager::TemperatureSensors() { + vector> tempSensors; + + tempSensors = mLMSensorsFacade->TemperatureSensors(); + + auto gpuSensors = mGPUSensorsFacade->TemperatureSensors(); + tempSensors.insert(tempSensors.end(), gpuSensors.begin(), gpuSensors.end()); + + return tempSensors; +} + +vector> SensorManager::RPMSensors() { + return mLMSensorsFacade->RPMSensors(); +} diff --git a/app/src/sensor/SensorsWrapper.cxx b/app/src/sensor/SensorsWrapper.cxx deleted file mode 100644 index b3f52fe..0000000 --- a/app/src/sensor/SensorsWrapper.cxx +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include -#include - -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> -SensorsWrapper::Sensors(sensors_subfeature_type sensorType) { - std::vector> 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(chipName, feature, subFeature)); - } - } - } - - return sensors; -} - -std::vector> SensorsWrapper::PwmControls() { - std::vector> 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(controlPath)); - } - } - } - } - - return controls; -}