// Producer.cxx // SPDX-License-Identifier: GPL-3.0-only // Author: Unai Blazquez #include "Producer.hpp" #include "SysfsRead.hpp" Producer::Producer(const std::filesystem::path& sysfs_path, std::function send_fn, RandomFn random_fn : m_reader(sysfs_path), m_send(std::move(send_fn)), m_random(std::move(random_fn)), m_sleep(std::move(sleep_fn)) { } 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() { auto status = m_reader.read_status(); switch (status) { case SysfsStatus::Enabled: m_send(m_random()); break; case SysfsStatus::Unreachable: // do nothing for now break; case SysfsStatus::Empty: break; case SysfsStatus::ErrorTempTooHigh: break; case SysfsStatus::UnexpectedValue: break; } m_sleep(delay); // Thread will end here (for now) stop will join it }