Add deserialization logic
This commit is contained in:
@@ -52,9 +52,8 @@ Mapping::createMapping(vector<shared_ptr<Sensor>> rpmSensors,
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Setting fan back to 100% and wait for settling" << endl;
|
||||
cout << "Setting fan back to 100%" << endl;
|
||||
c->pwm(100);
|
||||
this_thread::sleep_for(chrono::seconds(SETTLE_TIMEOUT));
|
||||
}
|
||||
|
||||
cout << "Resetting all fans" << endl;
|
||||
|
||||
@@ -2,12 +2,15 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <Serializer.h>
|
||||
#include <fan/HwmonFan.h>
|
||||
#include <fan/PwmControl.h>
|
||||
#include <sensor/HwmonSensor.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
#define SERIALIZATION_DIR "/etc/fantasize"
|
||||
#define FANS_JSON_FILENAME "fans.json"
|
||||
using namespace std;
|
||||
namespace fs = filesystem;
|
||||
|
||||
Serializer::Serializer() {
|
||||
if (!fs::exists(SERIALIZATION_DIR)) {
|
||||
@@ -15,24 +18,52 @@ Serializer::Serializer() {
|
||||
}
|
||||
}
|
||||
|
||||
void Serializer::Serialize(std::vector<std::shared_ptr<Fan>> fans) {
|
||||
void Serializer::Serialize(vector<shared_ptr<Fan>> fans) {
|
||||
json fansArr;
|
||||
|
||||
for (auto f : fans) {
|
||||
fansArr.push_back(f->toJson());
|
||||
fansArr.emplace_back(f->toJson());
|
||||
}
|
||||
|
||||
json obj;
|
||||
|
||||
obj["fans"] = fansArr;
|
||||
|
||||
std::cout << "Json obj: " << obj.dump(2) << std::endl;
|
||||
cout << "Json obj: " << obj.dump(2) << endl;
|
||||
|
||||
WriteJson(obj);
|
||||
}
|
||||
|
||||
vector<shared_ptr<Fan>>
|
||||
Serializer::Deserialize(vector<shared_ptr<Sensor>> availableSensors) {
|
||||
vector<shared_ptr<Fan>> mapping;
|
||||
|
||||
// Create a for the sensors first, then searching becomes cheaper
|
||||
map<string, shared_ptr<Sensor>> sensorMap;
|
||||
for (auto s : availableSensors) {
|
||||
sensorMap[s->toString()] = s;
|
||||
}
|
||||
|
||||
auto data = ReadJson();
|
||||
try {
|
||||
for (auto &el : data["fans"].items()) {
|
||||
auto pwmControl = make_shared<PwmControl>(el.value()["PwmControl"]);
|
||||
auto rpmSensor = sensorMap[el.value()["HwmonSensor"]];
|
||||
|
||||
mapping.push_back(make_shared<HwmonFan>(pwmControl, rpmSensor));
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
std::cout << "Deserialization error! Message: " << e.what() << std::endl;
|
||||
}
|
||||
return mapping;
|
||||
}
|
||||
|
||||
void Serializer::WriteJson(json o) {
|
||||
std::ofstream ostrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME,
|
||||
std::ios::trunc);
|
||||
ofstream ostrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME, ios::trunc);
|
||||
ostrm << o.dump(2) << "\n";
|
||||
}
|
||||
|
||||
json Serializer::ReadJson() {
|
||||
ifstream istrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME);
|
||||
return json::parse(istrm);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "fan/PwmControl.h"
|
||||
#include <boost/json/object.hpp>
|
||||
#include <fan/HwmonFan.h>
|
||||
|
||||
@@ -13,6 +14,11 @@ int HwmonFan::rpm() { return mRpmSensor->value(); }
|
||||
|
||||
json HwmonFan::toJson() const {
|
||||
json obj;
|
||||
obj["HwmonFan"] = {mPwmControl->toJson(), mRpmSensor->toJson()};
|
||||
obj = {mPwmControl->toJson(), mRpmSensor->toJson()};
|
||||
return obj;
|
||||
}
|
||||
|
||||
const string HwmonFan::toString() const {
|
||||
return "Fan!\nPwmControl: " + mPwmControl->toString() +
|
||||
"\nRpmSensor: " + mRpmSensor->toString();
|
||||
}
|
||||
|
||||
@@ -77,6 +77,6 @@ const string PwmControl::toString() const {
|
||||
}
|
||||
|
||||
json PwmControl::toJson() const {
|
||||
json obj = {"PwmControl", toString()};
|
||||
json obj = {"PwmControl", mControlPath};
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
#include <Mapping.h>
|
||||
#include <Serializer.h>
|
||||
#include <fan/Fan.h>
|
||||
#include <sensor/NvidiaSensor.h>
|
||||
#include <sensor/SensorsWrapper.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
int main() {
|
||||
|
||||
SensorsWrapper sensorsWrapper;
|
||||
@@ -15,10 +21,20 @@ int main() {
|
||||
auto controls = sensorsWrapper.PwmControls();
|
||||
|
||||
Mapping m;
|
||||
auto mapping = m.createMapping(pwmSensors, controls);
|
||||
|
||||
Serializer s;
|
||||
s.Serialize(mapping);
|
||||
|
||||
std::vector<std::shared_ptr<Fan>> fans;
|
||||
|
||||
if (fs::exists(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME)) {
|
||||
fans = s.Deserialize(pwmSensors);
|
||||
} else {
|
||||
fans = m.createMapping(pwmSensors, controls);
|
||||
s.Serialize(fans);
|
||||
}
|
||||
|
||||
for (auto f : fans) {
|
||||
std::cout << f->toString() << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user