Add deserialization logic
This commit is contained in:
@@ -7,6 +7,10 @@
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include <fan/Fan.h>
|
#include <fan/Fan.h>
|
||||||
|
#include <sensor/Sensor.h>
|
||||||
|
|
||||||
|
#define SERIALIZATION_DIR "/etc/fantasize"
|
||||||
|
#define FANS_JSON_FILENAME "fans.json"
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
@@ -14,9 +18,12 @@ class Serializer {
|
|||||||
public:
|
public:
|
||||||
Serializer();
|
Serializer();
|
||||||
void Serialize(std::vector<std::shared_ptr<Fan>> fans);
|
void Serialize(std::vector<std::shared_ptr<Fan>> fans);
|
||||||
|
std::vector<std::shared_ptr<Fan>>
|
||||||
|
Deserialize(std::vector<std::shared_ptr<Sensor>> availableSensors);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void WriteJson(json o);
|
void WriteJson(json o);
|
||||||
|
json ReadJson();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SERIALIZER_H_
|
#endif // SERIALIZER_H_
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
#ifndef FAN_H_
|
#ifndef FAN_H_
|
||||||
#define FAN_H_
|
#define FAN_H_
|
||||||
|
|
||||||
|
#include <Printable.h>
|
||||||
#include <Serializable.h>
|
#include <Serializable.h>
|
||||||
|
|
||||||
class Fan : public Serializable {
|
class Fan : public Serializable, public Printable {
|
||||||
public:
|
public:
|
||||||
virtual void pwm(int percent) = 0;
|
virtual void pwm(int percent) = 0;
|
||||||
virtual int rpm() = 0;
|
virtual int rpm() = 0;
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ public:
|
|||||||
|
|
||||||
json toJson() const override;
|
json toJson() const override;
|
||||||
|
|
||||||
|
const std::string toString() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<PwmControl> mPwmControl;
|
std::shared_ptr<PwmControl> mPwmControl;
|
||||||
std::shared_ptr<Sensor> mRpmSensor;
|
std::shared_ptr<Sensor> mRpmSensor;
|
||||||
|
|||||||
@@ -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);
|
c->pwm(100);
|
||||||
this_thread::sleep_for(chrono::seconds(SETTLE_TIMEOUT));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "Resetting all fans" << endl;
|
cout << "Resetting all fans" << endl;
|
||||||
|
|||||||
@@ -2,12 +2,15 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <Serializer.h>
|
#include <Serializer.h>
|
||||||
|
#include <fan/HwmonFan.h>
|
||||||
|
#include <fan/PwmControl.h>
|
||||||
|
#include <sensor/HwmonSensor.h>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
using namespace std;
|
||||||
|
namespace fs = filesystem;
|
||||||
#define SERIALIZATION_DIR "/etc/fantasize"
|
|
||||||
#define FANS_JSON_FILENAME "fans.json"
|
|
||||||
|
|
||||||
Serializer::Serializer() {
|
Serializer::Serializer() {
|
||||||
if (!fs::exists(SERIALIZATION_DIR)) {
|
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;
|
json fansArr;
|
||||||
|
|
||||||
for (auto f : fans) {
|
for (auto f : fans) {
|
||||||
fansArr.push_back(f->toJson());
|
fansArr.emplace_back(f->toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
json obj;
|
json obj;
|
||||||
|
|
||||||
obj["fans"] = fansArr;
|
obj["fans"] = fansArr;
|
||||||
|
|
||||||
std::cout << "Json obj: " << obj.dump(2) << std::endl;
|
cout << "Json obj: " << obj.dump(2) << endl;
|
||||||
|
|
||||||
WriteJson(obj);
|
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) {
|
void Serializer::WriteJson(json o) {
|
||||||
std::ofstream ostrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME,
|
ofstream ostrm(fs::path(SERIALIZATION_DIR) / FANS_JSON_FILENAME, ios::trunc);
|
||||||
std::ios::trunc);
|
|
||||||
ostrm << o.dump(2) << "\n";
|
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 <boost/json/object.hpp>
|
||||||
#include <fan/HwmonFan.h>
|
#include <fan/HwmonFan.h>
|
||||||
|
|
||||||
@@ -13,6 +14,11 @@ int HwmonFan::rpm() { return mRpmSensor->value(); }
|
|||||||
|
|
||||||
json HwmonFan::toJson() const {
|
json HwmonFan::toJson() const {
|
||||||
json obj;
|
json obj;
|
||||||
obj["HwmonFan"] = {mPwmControl->toJson(), mRpmSensor->toJson()};
|
obj = {mPwmControl->toJson(), mRpmSensor->toJson()};
|
||||||
return obj;
|
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 PwmControl::toJson() const {
|
||||||
json obj = {"PwmControl", toString()};
|
json obj = {"PwmControl", mControlPath};
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <Mapping.h>
|
#include <Mapping.h>
|
||||||
#include <Serializer.h>
|
#include <Serializer.h>
|
||||||
|
#include <fan/Fan.h>
|
||||||
#include <sensor/NvidiaSensor.h>
|
#include <sensor/NvidiaSensor.h>
|
||||||
#include <sensor/SensorsWrapper.h>
|
#include <sensor/SensorsWrapper.h>
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
SensorsWrapper sensorsWrapper;
|
SensorsWrapper sensorsWrapper;
|
||||||
@@ -15,10 +21,20 @@ int main() {
|
|||||||
auto controls = sensorsWrapper.PwmControls();
|
auto controls = sensorsWrapper.PwmControls();
|
||||||
|
|
||||||
Mapping m;
|
Mapping m;
|
||||||
auto mapping = m.createMapping(pwmSensors, controls);
|
|
||||||
|
|
||||||
Serializer s;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user