implemented Producer API and header, stub and tests. Builds cleanly, tests fail for now
This commit is contained in:
55
src/core/Producer.cxx
Normal file
55
src/core/Producer.cxx
Normal file
@@ -0,0 +1,55 @@
|
||||
// Producer.cxx
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// Author: Unai Blazquez <unaibg2000@gmail.com>
|
||||
|
||||
#include "Producer.hpp"
|
||||
|
||||
#include "SysfsRead.hpp"
|
||||
|
||||
Producer::Producer(const std::filesystem::path& sysfs_path,
|
||||
std::function<void(int)> 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
|
||||
}
|
||||
Reference in New Issue
Block a user