Add deserialization logic

This commit is contained in:
2022-09-24 17:23:12 +02:00
parent 61923121f0
commit d3cd6857d0
8 changed files with 79 additions and 17 deletions

View File

@@ -7,6 +7,10 @@
#include <nlohmann/json.hpp>
#include <fan/Fan.h>
#include <sensor/Sensor.h>
#define SERIALIZATION_DIR "/etc/fantasize"
#define FANS_JSON_FILENAME "fans.json"
using json = nlohmann::json;
@@ -14,9 +18,12 @@ class Serializer {
public:
Serializer();
void Serialize(std::vector<std::shared_ptr<Fan>> fans);
std::vector<std::shared_ptr<Fan>>
Deserialize(std::vector<std::shared_ptr<Sensor>> availableSensors);
private:
void WriteJson(json o);
json ReadJson();
};
#endif // SERIALIZER_H_

View File

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

View File

@@ -18,6 +18,8 @@ public:
json toJson() const override;
const std::string toString() const override;
private:
std::shared_ptr<PwmControl> mPwmControl;
std::shared_ptr<Sensor> mRpmSensor;

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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();
}

View File

@@ -77,6 +77,6 @@ const string PwmControl::toString() const {
}
json PwmControl::toJson() const {
json obj = {"PwmControl", toString()};
json obj = {"PwmControl", mControlPath};
return obj;
}

View File

@@ -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;
}