| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "sysmodelsyncer.h" |
| 5 | |
| 6 | #include "iprofileview.h" |
| 7 | #include <QStringList> |
| 8 | #include <QVariant> |
| 9 | #include <chrono> |
| 10 | #include <utility> |
| 11 | |
| 12 | SysModelSyncer::SysModelSyncer(std::unique_ptr<ISysModel> &&sysModel, |
| 13 | std::unique_ptr<IHelperSysCtl> &&helperSysCtl) noexcept |
| 14 | : sysModel_(std::move(sysModel)) |
| 15 | , helperSysCtl_(std::move(helperSysCtl)) |
| 16 | , cmds_({"pp_od_clk_voltage", "fan_curve", "fan_zero_rpm_enable", |
| 17 | "fan_zero_rpm_stop_temperature", "fan_target_temperature", |
| 18 | "fan_minimum_pwm", "acoustic_target_rpm_threshold", |
| 19 | "acoustic_limit_rpm_threshold"}) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | ISysModel &SysModelSyncer::sysModel() const |
| 24 | { |
| 25 | return *sysModel_; |
| 26 | } |
| 27 | |
| 28 | void SysModelSyncer::settingChanged(QString const &key, QVariant const &value) |
| 29 | { |
| 30 | if (key == "Workarounds/ignoredSensors") { |
| 31 | std::lock_guard<std::mutex> lock(sensorsMutex_); |
| 32 | ignoredSensors_.clear(); |
| 33 | |
| 34 | auto const sensorList = value.toStringList(); |
| 35 | for (auto const &sensor : sensorList) { |
| 36 | auto componentSensorIdList = sensor.split('/'); |
| 37 | if (componentSensorIdList.size() == 2) { |
| 38 | auto component = componentSensorIdList.at(0).toStdString(); |
| 39 | auto sensorId = componentSensorIdList.at(1).toStdString(); |
| 40 | |
| 41 | if (ignoredSensors_.count(component) == 0) |
| 42 | ignoredSensors_[component] = {}; |
| 43 | |
| 44 | ignoredSensors_[component].emplace(sensorId); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void SysModelSyncer::logCommands(bool enable) |
| 51 | { |
| 52 | helperSysCtl_->logCommands(enable); |
| 53 | } |
| 54 | |
| 55 | void SysModelSyncer::init() |
| 56 | { |
| 57 | helperSysCtl_->init(); |
| 58 | |
| 59 | sysModel_->preInit(cmds_); |
| 60 | helperSysCtl_->apply(cmds_); |
| 61 | |
| 62 | // NOTE give some time to the helper so it applies the pre-init commands |
| 63 | // before the model initialization. |
| 64 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 65 | |
| 66 | sysModel_->init(); |
| 67 | |
| 68 | sysModel_->postInit(cmds_); |
| 69 | helperSysCtl_->apply(cmds_); |
| 70 | |
| 71 | // start the sensor updating thread |
| 72 | updateThread_ = std::make_unique<std::thread>([&]() { |
| 73 | while (!stopSignal_.load(std::memory_order_relaxed)) { |
| 74 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 75 | updateSensors(); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | // start the model syncing thread |
| 80 | syncThread_ = std::make_unique<std::thread>([&]() { |
| 81 | while (!stopSignal_.load(std::memory_order_relaxed)) { |
| 82 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 83 | syncModel(); |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | void SysModelSyncer::stop() |
| 89 | { |
| 90 | stopSignal_.store(true, std::memory_order_relaxed); |
| 91 | updateThread_->join(); |
| 92 | syncThread_->join(); |
| 93 | } |
| 94 | |
| 95 | void SysModelSyncer::apply(IProfileView &profileView) |
| 96 | { |
| 97 | std::lock_guard<std::mutex> lock(syncMutex_); |
| 98 | sysModel_->importWith(profileView); |
| 99 | } |
| 100 | |
| 101 | void SysModelSyncer::updateSensors() |
| 102 | { |
| 103 | std::lock_guard<std::mutex> lock(sensorsMutex_); |
| 104 | sysModel_->updateSensors(ignoredSensors_); |
| 105 | } |
| 106 | |
| 107 | void SysModelSyncer::syncModel() |
| 108 | { |
| 109 | std::lock_guard<std::mutex> lock(syncMutex_); |
| 110 | sysModel_->sync(cmds_); |
| 111 | helperSysCtl_->apply(cmds_); |
| 112 | } |