IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::camera::StatusMonitor Class Reference

Class that offers an easy way to run a background monitorin thread that periodically inspects and reports information about the camera interface passed as argument. More...

#include <monitor.hpp>

Public Member Functions

 StatusMonitor (const Interface &cam, irsol::types::duration_t monitorInterval=std::chrono::milliseconds(100))
 Constructs a StatusMonitor for the given camera interface.
 
 ~StatusMonitor ()
 Destructor stops the monitoring thread if running.
 
void start ()
 Starts the monitoring thread. If the monitor is already running, this call has no effect.
 
void stop ()
 Stops the monitoring thread and waits for it to finish. If the monitor is not running, this call has no effect.
 

Protected Member Functions

virtual void runMonitor () const
 The main monitoring loop executed on the background thread.
 

Private Attributes

const irsol::camera::Interfacem_cam
 Reference to the camera interface being monitored.
 
irsol::types::duration_t m_monitorInterval
 Interval at which the monitor thread executes runMonitor()
 
std::atomic< bool > m_hasStartedMonitor
 Flag indicating if the monitor thread has been started.
 
std::thread m_monitorThread
 Thread running the periodic monitoring loop.
 

Detailed Description

Class that offers an easy way to run a background monitorin thread that periodically inspects and reports information about the camera interface passed as argument.

See also
irsol::camera::Interface

Check out 01-loading-images for an example usage.

Definition at line 43 of file monitor.hpp.

Constructor & Destructor Documentation

◆ StatusMonitor()

irsol::camera::StatusMonitor::StatusMonitor ( const Interface cam,
irsol::types::duration_t  monitorInterval = std::chrono::milliseconds(100) 
)

Constructs a StatusMonitor for the given camera interface.

Parameters
camReference to the camera interface to monitor.
monitorIntervalInterval at which the monitoring task should run. Defaults to 100ms.

Definition at line 9 of file monitor.cpp.

10 : m_cam(cam), m_monitorInterval(monitorInterval), m_hasStartedMonitor(false)
11{
12 IRSOL_ASSERT_ERROR(cam.isConnected(), "Camera is not connected.");
13}
std::atomic< bool > m_hasStartedMonitor
Flag indicating if the monitor thread has been started.
Definition monitor.hpp:89
const irsol::camera::Interface & m_cam
Reference to the camera interface being monitored.
Definition monitor.hpp:83
irsol::types::duration_t m_monitorInterval
Interval at which the monitor thread executes runMonitor()
Definition monitor.hpp:86
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134

◆ ~StatusMonitor()

irsol::camera::StatusMonitor::~StatusMonitor ( )

Destructor stops the monitoring thread if running.

Definition at line 15 of file monitor.cpp.

16{
18 IRSOL_NAMED_LOG_DEBUG("status_monitor", "Automatic stopping monitoring of camera.");
19 stop();
20 }
21}
void stop()
Stops the monitoring thread and waits for it to finish. If the monitor is not running,...
Definition monitor.cpp:52
#define IRSOL_NAMED_LOG_DEBUG(name,...)
Logs a debug-level message using a named logger.
Definition logging.hpp:174

Member Function Documentation

◆ runMonitor()

void irsol::camera::StatusMonitor::runMonitor ( ) const
protectedvirtual

The main monitoring loop executed on the background thread.

Override this method in derived classes to implement specific monitoring logic. It is called repeatedly with the frequency specified by monitorInterval.

Definition at line 24 of file monitor.cpp.

25{
26 IRSOL_NAMED_LOG_DEBUG("status_monitor", "Monitoring camera status...");
27
28 while(m_hasStartedMonitor) {
29 auto nextIterationTime = irsol::types::clock_t::now() + m_monitorInterval;
30
31 bool stopRequested = false;
32 IRSOL_NAMED_LOG_INFO("status_monitor", "\n{}", m_cam.cameraStatusAsString());
33 if(stopRequested) {
34 break;
35 }
36
37 std::this_thread::sleep_until(nextIterationTime);
38 }
39}
std::string cameraStatusAsString() const
Get current camera status.
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
Definition logging.hpp:176

◆ start()

void irsol::camera::StatusMonitor::start ( )

Starts the monitoring thread. If the monitor is already running, this call has no effect.

Definition at line 42 of file monitor.cpp.

43{
44 IRSOL_ASSERT_ERROR(!m_hasStartedMonitor.load(), "Monitor is already running!");
45 m_hasStartedMonitor.store(true);
46
47 m_monitorThread = std::thread([this]() { runMonitor(); });
48 IRSOL_NAMED_LOG_DEBUG("status_monitor", "Camera monitor has started.");
49}
virtual void runMonitor() const
The main monitoring loop executed on the background thread.
Definition monitor.cpp:24
std::thread m_monitorThread
Thread running the periodic monitoring loop.
Definition monitor.hpp:92

◆ stop()

void irsol::camera::StatusMonitor::stop ( )

Stops the monitoring thread and waits for it to finish. If the monitor is not running, this call has no effect.

Definition at line 52 of file monitor.cpp.

53{
55 m_hasStartedMonitor.load(), "Cannot 'stop' monitor without having started it before!");
56 m_hasStartedMonitor.store(false);
57
58 if(m_monitorThread.joinable()) {
59 m_monitorThread.join();
60 }
61 IRSOL_NAMED_LOG_DEBUG("status_monitor", "Camera monitor has stopped.");
62}

Member Data Documentation

◆ m_cam

const irsol::camera::Interface& irsol::camera::StatusMonitor::m_cam
private

Reference to the camera interface being monitored.

Definition at line 83 of file monitor.hpp.

◆ m_hasStartedMonitor

std::atomic<bool> irsol::camera::StatusMonitor::m_hasStartedMonitor
private

Flag indicating if the monitor thread has been started.

Definition at line 89 of file monitor.hpp.

◆ m_monitorInterval

irsol::types::duration_t irsol::camera::StatusMonitor::m_monitorInterval
private

Interval at which the monitor thread executes runMonitor()

Definition at line 86 of file monitor.hpp.

◆ m_monitorThread

std::thread irsol::camera::StatusMonitor::m_monitorThread
private

Thread running the periodic monitoring loop.

Definition at line 92 of file monitor.hpp.


The documentation for this class was generated from the following files: