// Producer.cxx // SPDX-License-Identifier: GPL-3.0-or-later // Author: Unai Blazquez #include "Producer.hpp" #include "SysfsRead.hpp" Producer::Producer(const std::filesystem::path& sysfs_path, std::function send_fn, RandomFn random_fn, LogFn log_fn, SleepFn sleep_fn) : m_reader(sysfs_path), m_send(std::move(send_fn)), m_random(std::move(random_fn)), m_log(std::move(log_fn)), m_sleep(std::move(sleep_fn)) { } std::chrono::milliseconds Producer::compute_delay(SysfsStatus status) const { using namespace std::chrono_literals; auto standard = 1000ms; // example: 1s, must be < 7s auto hot = 7000ms; // exactly 7s if (status == SysfsStatus::ErrorTempTooHigh) { // when error = temp too high return hot; } else { return standard; } } void Producer::start() { m_running.store(true); m_thread = std::thread(&Producer::run_loop, this); } void Producer::stop() { m_running.store(false); if (m_thread.joinable()) { m_thread.join(); } } void Producer::run_loop() { while (m_running.load()) { auto status = m_reader.read_status(); switch (status) { case SysfsStatus::Enabled: m_send(m_random()); if (m_log) m_log("Producer: Enabled"); break; case SysfsStatus::Unreachable: // do nothing for now if (m_log) m_log("Producer: SysfsFile Unreachable"); break; case SysfsStatus::Empty: if (m_log) m_log("Producer: SysfsFile Empty"); break; case SysfsStatus::ErrorTempTooHigh: if (m_log) m_log("Producer: Error temp too high!!"); break; case SysfsStatus::UnexpectedValue: if (m_log) m_log("Producer: UnexpectedValue"); break; } auto delay = compute_delay(status); m_sleep(delay); // Thread will end here (for now) stop will join it } }