From d3cd6857d052ac6e0ad6e8ecd487389f1fd3c086 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 24 Sep 2022 17:23:12 +0200 Subject: [PATCH] Add deserialization logic --- app/include/Serializer.h | 7 ++++++ app/include/fan/Fan.h | 3 ++- app/include/fan/HwmonFan.h | 2 ++ app/src/Mapping.cxx | 3 +-- app/src/Serializer.cxx | 49 +++++++++++++++++++++++++++++++------- app/src/fan/HwmonFan.cxx | 8 ++++++- app/src/fan/PwmControl.cxx | 2 +- app/src/main.cxx | 22 ++++++++++++++--- 8 files changed, 79 insertions(+), 17 deletions(-) diff --git a/app/include/Serializer.h b/app/include/Serializer.h index 921ddeb..4f11042 100644 --- a/app/include/Serializer.h +++ b/app/include/Serializer.h @@ -7,6 +7,10 @@ #include #include +#include + +#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> fans); + std::vector> + Deserialize(std::vector> availableSensors); private: void WriteJson(json o); + json ReadJson(); }; #endif // SERIALIZER_H_ diff --git a/app/include/fan/Fan.h b/app/include/fan/Fan.h index 9993802..9a97865 100644 --- a/app/include/fan/Fan.h +++ b/app/include/fan/Fan.h @@ -1,9 +1,10 @@ #ifndef FAN_H_ #define FAN_H_ +#include #include -class Fan : public Serializable { +class Fan : public Serializable, public Printable { public: virtual void pwm(int percent) = 0; virtual int rpm() = 0; diff --git a/app/include/fan/HwmonFan.h b/app/include/fan/HwmonFan.h index 6c97644..7ddd940 100644 --- a/app/include/fan/HwmonFan.h +++ b/app/include/fan/HwmonFan.h @@ -18,6 +18,8 @@ public: json toJson() const override; + const std::string toString() const override; + private: std::shared_ptr mPwmControl; std::shared_ptr mRpmSensor; diff --git a/app/src/Mapping.cxx b/app/src/Mapping.cxx index 50615a3..28e7cc5 100644 --- a/app/src/Mapping.cxx +++ b/app/src/Mapping.cxx @@ -52,9 +52,8 @@ Mapping::createMapping(vector> 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; diff --git a/app/src/Serializer.cxx b/app/src/Serializer.cxx index bb31a58..ceb7e77 100644 --- a/app/src/Serializer.cxx +++ b/app/src/Serializer.cxx @@ -2,12 +2,15 @@ #include #include +#include + #include +#include +#include +#include -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> fans) { +void Serializer::Serialize(vector> 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> +Serializer::Deserialize(vector> availableSensors) { + vector> mapping; + + // Create a for the sensors first, then searching becomes cheaper + map> sensorMap; + for (auto s : availableSensors) { + sensorMap[s->toString()] = s; + } + + auto data = ReadJson(); + try { + for (auto &el : data["fans"].items()) { + auto pwmControl = make_shared(el.value()["PwmControl"]); + auto rpmSensor = sensorMap[el.value()["HwmonSensor"]]; + + mapping.push_back(make_shared(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); +} diff --git a/app/src/fan/HwmonFan.cxx b/app/src/fan/HwmonFan.cxx index 277fecd..2ff4a83 100644 --- a/app/src/fan/HwmonFan.cxx +++ b/app/src/fan/HwmonFan.cxx @@ -1,3 +1,4 @@ +#include "fan/PwmControl.h" #include #include @@ -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(); +} diff --git a/app/src/fan/PwmControl.cxx b/app/src/fan/PwmControl.cxx index 820bec5..f6f58ac 100644 --- a/app/src/fan/PwmControl.cxx +++ b/app/src/fan/PwmControl.cxx @@ -77,6 +77,6 @@ const string PwmControl::toString() const { } json PwmControl::toJson() const { - json obj = {"PwmControl", toString()}; + json obj = {"PwmControl", mControlPath}; return obj; } diff --git a/app/src/main.cxx b/app/src/main.cxx index ecf6bc8..ca372a9 100644 --- a/app/src/main.cxx +++ b/app/src/main.cxx @@ -1,8 +1,14 @@ +#include +#include + #include #include +#include #include #include +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> 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; }