Add mapping logic and serialization for writing
This commit is contained in:
@@ -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
38
app/src/Serializer.cxx
Normal 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";
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user