Add mapping logic and serialization for writing

This commit is contained in:
2022-09-20 23:17:40 +02:00
parent df1c8f3821
commit 8ceb762fe1
68 changed files with 49901 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ project(fantasize)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(Boost 1.79.0 COMPONENTS json)
find_package(nlohmann_json 3.11.2 REQUIRED)
add_executable(app
src/main.cxx
@@ -17,9 +17,10 @@ add_executable(app
src/fan/PwmControl.cxx
src/fan/HwmonFan.cxx
src/Mapping.cxx
src/Serializer.cxx
)
set_property(TARGET app PROPERTY CXX_STANDARD 17)
target_include_directories(app PUBLIC include /opt/cuda)
target_link_libraries(app PUBLIC sensors nvidia-ml ${Boost_LIBRARIES})
target_link_libraries(app PUBLIC sensors nvidia-ml nlohmann_json::nlohmann_json)

View File

@@ -0,0 +1,13 @@
#ifndef SERIALIZABLE_H_
#define SERIALIZABLE_H_
#include <nlohmann/json.hpp>
using json = nlohmann::json;
class Serializable {
public:
virtual json toJson() const = 0;
};
#endif // SERIALIZABLE_H_

22
app/include/Serializer.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef SERIALIZER_H_
#define SERIALIZER_H_
#include <memory>
#include <vector>
#include <nlohmann/json.hpp>
#include <fan/Fan.h>
using json = nlohmann::json;
class Serializer {
public:
Serializer();
void Serialize(std::vector<std::shared_ptr<Fan>> fans);
private:
void WriteJson(json o);
};
#endif // SERIALIZER_H_

View File

@@ -1,7 +1,9 @@
#ifndef FAN_H_
#define FAN_H_
class Fan {
#include <Serializable.h>
class Fan : public Serializable {
public:
virtual void pwm(int percent) = 0;
virtual int rpm() = 0;

View File

@@ -1,6 +1,7 @@
#ifndef HWMONFAN_H_
#define HWMONFAN_H_
#include <boost/json/object.hpp>
#include <memory>
#include <fan/Fan.h>
@@ -15,6 +16,8 @@ public:
void pwm(int percent) override;
int rpm() override;
json toJson() const override;
private:
std::shared_ptr<PwmControl> mPwmControl;
std::shared_ptr<Sensor> mRpmSensor;

View File

@@ -1,26 +1,31 @@
#ifndef PWMCONTROL_H_
#define PWMCONTROL_H_
#include <boost/json/object.hpp>
#include <string>
#include <Printable.h>
#include <Serializable.h>
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 {
class PwmControl : public Printable, public Serializable {
public:
PwmControl(std::string controlPath);
~PwmControl();
void pwm(int percent);
int pwm();
void enableManualControl();
void reset();
void EnableManualControl();
void Reset();
const std::string toString() const override;
json toJson() const override;
private:
int readValue(std::string path);

View File

@@ -13,6 +13,8 @@ public:
int value() override;
const std::string toString() const override;
json toJson() const override;
private:
const sensors_chip_name *mChipName;
const sensors_feature *mFeature;

View File

@@ -11,6 +11,8 @@ public:
int value() override;
const std::string toString() const override;
json toJson() const override;
};
#endif // NVIDIASENSOR_H_

View File

@@ -2,8 +2,9 @@
#define SENSOR_H_
#include <Printable.h>
#include <Serializable.h>
class Sensor : public Printable {
class Sensor : public Printable, public Serializable {
public:
// Read the current value
virtual int value() = 0;

View File

@@ -19,9 +19,9 @@ Mapping::createMapping(vector<shared_ptr<Sensor>> rpmSensors,
print("PWM controllers", pwmControls);
vector<shared_ptr<Fan>> mapping;
cout << "Settings all fans to maximum speed" << endl;
cout << "Setting all fans to maximum speed" << endl;
for (auto c : pwmControls) {
c->enableManualControl();
c->EnableManualControl();
c->pwm(100);
}
@@ -59,7 +59,7 @@ Mapping::createMapping(vector<shared_ptr<Sensor>> rpmSensors,
cout << "Resetting all fans" << endl;
for (auto c : pwmControls) {
c->reset();
c->Reset();
}
return mapping;

38
app/src/Serializer.cxx Normal file
View File

@@ -0,0 +1,38 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <Serializer.h>
namespace fs = std::filesystem;
#define SERIALIZATION_DIR "/etc/fantasize"
#define FANS_JSON_FILENAME "fans.json"
Serializer::Serializer() {
if (!fs::exists(SERIALIZATION_DIR)) {
fs::create_directory(SERIALIZATION_DIR);
}
}
void Serializer::Serialize(std::vector<std::shared_ptr<Fan>> fans) {
json fansArr;
for (auto f : fans) {
fansArr.push_back(f->toJson());
}
json obj;
obj["fans"] = fansArr;
std::cout << "Json obj: " << obj.dump(2) << std::endl;
WriteJson(obj);
}
void Serializer::WriteJson(json o) {
std::ofstream ostrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME,
std::ios::trunc);
ostrm << o.dump(2) << "\n";
}

View File

@@ -1,3 +1,4 @@
#include <boost/json/object.hpp>
#include <fan/HwmonFan.h>
using namespace std;
@@ -9,3 +10,9 @@ HwmonFan::HwmonFan(shared_ptr<PwmControl> pwmControl,
void HwmonFan::pwm(int percent) { mPwmControl->pwm(percent); }
int HwmonFan::rpm() { return mRpmSensor->value(); }
json HwmonFan::toJson() const {
json obj;
obj["HwmonFan"] = {mPwmControl->toJson(), mRpmSensor->toJson()};
return obj;
}

View File

@@ -1,5 +1,7 @@
#include <boost/json/object.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <fan/PwmControl.h>
@@ -29,6 +31,11 @@ PwmControl::PwmControl(string controlPath) : mControlPath(controlPath) {
istrm.close();
}
PwmControl::~PwmControl() {
cout << "Cleanup" << endl;
Reset();
}
void PwmControl::pwm(int percent) {
int pwmValue = PWM_MAX_VALUE * percent / 100;
@@ -47,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;
@@ -68,3 +75,8 @@ void PwmControl::reset() {
const string PwmControl::toString() const {
return fs::path(mControlPath).filename();
}
json PwmControl::toJson() const {
json obj = {"PwmControl", toString()};
return obj;
}

View File

@@ -1,9 +1,10 @@
#include <Mapping.h>
#include <Serializer.h>
#include <sensor/NvidiaSensor.h>
#include <sensor/SensorsWrapper.h>
int main() {
SensorsWrapper sensorsWrapper;
auto tempSensors = sensorsWrapper.Sensors(SENSORS_SUBFEATURE_TEMP_INPUT);
@@ -14,7 +15,10 @@ int main() {
auto controls = sensorsWrapper.PwmControls();
Mapping m;
m.createMapping(pwmSensors, controls);
auto mapping = m.createMapping(pwmSensors, controls);
Serializer s;
s.Serialize(mapping);
return 0;
}

View File

@@ -1,3 +1,4 @@
#include <boost/json/kind.hpp>
#include <sensor/HwmonSensor.h>
#include <sensors/sensors.h>
@@ -18,3 +19,8 @@ int HwmonSensor::value() {
const string HwmonSensor::toString() const {
return sensors_get_label(mChipName, mFeature);
}
json HwmonSensor::toJson() const {
json obj = {"HwmonSensor", toString()};
return obj;
}

View File

@@ -1,3 +1,4 @@
#include <boost/json/object.hpp>
#include <include/nvml.h>
#include <sensor/NvidiaSensor.h>
@@ -19,3 +20,8 @@ int NvidiaSensor::value() {
}
const std::string NvidiaSensor::toString() const { return "Nvidia GPU"; }
json NvidiaSensor::toJson() const {
json obj = {"NvidiaSensor", toString()};
return obj;
}