Implement fan curves and extend serialization logic
This commit is contained in:
@@ -17,6 +17,7 @@ add_executable(app
|
||||
src/pwm/PWMControl.cxx
|
||||
src/pwm/PWMControlFacade.cxx
|
||||
src/fan/HwmonFan.cxx
|
||||
src/fan/FanCurve.cxx
|
||||
src/FanGenerator.cxx
|
||||
src/Serializer.cxx
|
||||
src/sensor/SensorManager.cxx
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#ifndef SERIALIZER_H_
|
||||
#define SERIALIZER_H_
|
||||
|
||||
#include "fan/HwmonFan.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <fan/Fan.h>
|
||||
#include <fan/FanCurve.h>
|
||||
#include <sensor/Sensor.h>
|
||||
|
||||
#define SERIALIZATION_DIR "/etc/fantasize"
|
||||
@@ -20,10 +22,9 @@ public:
|
||||
void SerializeFans(std::vector<std::shared_ptr<Fan>> fans);
|
||||
std::vector<std::shared_ptr<Fan>>
|
||||
DeserializeFans(std::vector<std::shared_ptr<Sensor>> availableSensors);
|
||||
|
||||
void SerializeTempSensors(std::vector<std::shared_ptr<Sensor>> senors);
|
||||
std::vector<std::shared_ptr<Sensor>>
|
||||
DeserializeTempSensors(std::vector<std::shared_ptr<Sensor>> availableSensors);
|
||||
std::vector<std::shared_ptr<FanCurve>>
|
||||
DeserializeFanCurves(std::vector<std::shared_ptr<Sensor>> availableSensors,
|
||||
std::vector<std::shared_ptr<Fan>> availableFans);
|
||||
|
||||
private:
|
||||
void WriteJson(json o);
|
||||
|
||||
@@ -14,6 +14,10 @@ struct FanStep {
|
||||
|
||||
class FanCurve {
|
||||
public:
|
||||
FanCurve(std::vector<FanStep> steps,
|
||||
std::vector<std::shared_ptr<Sensor>> sensors,
|
||||
std::vector<std::shared_ptr<Fan>> fans);
|
||||
|
||||
void DoFanControl();
|
||||
|
||||
private:
|
||||
@@ -21,7 +25,7 @@ private:
|
||||
|
||||
std::vector<FanStep> mSteps;
|
||||
std::vector<std::shared_ptr<Sensor>> mTempSensors;
|
||||
std::vector<std::shared_ptr<HwmonFan>> mFans;
|
||||
std::vector<std::shared_ptr<Fan>> mFans;
|
||||
};
|
||||
|
||||
#endif // FANCURVE_H_
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include "fan/FanCurve.h"
|
||||
#include "sensor/Sensor.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -72,16 +74,42 @@ json Serializer::ReadJson() {
|
||||
return json::parse(istrm);
|
||||
}
|
||||
|
||||
void Serializer::SerializeTempSensors(vector<shared_ptr<Sensor>> sensors) {
|
||||
json obj;
|
||||
vector<shared_ptr<FanCurve>> Serializer::DeserializeFanCurves(
|
||||
std::vector<std::shared_ptr<Sensor>> availableSensors,
|
||||
std::vector<std::shared_ptr<Fan>> availableFans) {
|
||||
auto data = ReadJson();
|
||||
|
||||
for (auto s : sensors) {
|
||||
obj["tempSensors"].push_back(s->toJson());
|
||||
map<string, shared_ptr<Sensor>> sensorMap;
|
||||
for (auto s : availableSensors) {
|
||||
sensorMap[s->toString()] = s;
|
||||
}
|
||||
|
||||
WriteJson(obj);
|
||||
map<string, shared_ptr<Fan>> fanMap;
|
||||
for (auto f : availableFans) {
|
||||
fanMap[f->toString()] = f;
|
||||
}
|
||||
vector<shared_ptr<Sensor>>
|
||||
DeserializeTempSensors(vector<shared_ptr<Sensor>> availableSensors) {
|
||||
return vector<shared_ptr<Sensor>>();
|
||||
|
||||
vector<shared_ptr<FanCurve>> curves;
|
||||
|
||||
for (auto &el : data["fancurves"].items()) {
|
||||
vector<FanStep> steps;
|
||||
vector<shared_ptr<Sensor>> sensors;
|
||||
vector<shared_ptr<Fan>> fans;
|
||||
|
||||
for (auto &step : el.value()["FanSteps"].items()) {
|
||||
FanStep fanStep{step.value()[0], step.value()[1]};
|
||||
}
|
||||
|
||||
for (auto &sensor : el.value()["Sensors"].items()) {
|
||||
sensors.push_back(sensorMap[sensor.value()]);
|
||||
}
|
||||
|
||||
for (auto &fan : el.value()["Fans"].items()) {
|
||||
fans.push_back(fanMap[fan.value()]);
|
||||
}
|
||||
|
||||
curves.push_back(make_shared<FanCurve>(steps, sensors, fans));
|
||||
}
|
||||
|
||||
return curves;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
FanCurve::FanCurve(std::vector<FanStep> steps,
|
||||
std::vector<std::shared_ptr<Sensor>> sensors,
|
||||
std::vector<std::shared_ptr<Fan>> fans)
|
||||
: mSteps(steps), mTempSensors(sensors), mFans(fans) {}
|
||||
|
||||
void FanCurve::DoFanControl() {
|
||||
int temp = AggregateTemperature();
|
||||
|
||||
@@ -30,7 +35,7 @@ void FanCurve::DoFanControl() {
|
||||
int targetFanSpeed = p0 + ((p1 - p0) / (t1 - t0)) * (temp - t0);
|
||||
|
||||
for (auto f : mFans) {
|
||||
f->pwm(targetFanSpeed);
|
||||
f->PWM(targetFanSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
int main() {
|
||||
SensorManager sensorManager;
|
||||
auto pwmSensors = sensorManager.RPMSensors();
|
||||
auto tempSensors = sensorManager.TemperatureSensors();
|
||||
|
||||
PWMControlFacade pwmControlFacade;
|
||||
auto controls = pwmControlFacade.PWMControls();
|
||||
@@ -25,10 +26,7 @@ int main() {
|
||||
// s.SerializeFans(fans);
|
||||
fans = s.DeserializeFans(pwmSensors);
|
||||
|
||||
std::for_each(std::execution::par, std::begin(fans), std::end(fans),
|
||||
[](auto &&f) { f->FindMinPWM(); });
|
||||
|
||||
s.SerializeFans(fans);
|
||||
auto curves = s.DeserializeFanCurves(tempSensors, fans);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user