IRSOL
C++ code implementing socket server for interacting with Baumer camera.
monitor.cpp
Go to the documentation of this file.
2
3#include "irsol/assert.hpp"
4#include "irsol/logging.hpp"
5#include "irsol/types.hpp"
6
7namespace irsol {
8namespace camera {
10 : m_cam(cam), m_monitorInterval(monitorInterval), m_hasStartedMonitor(false)
11{
12 IRSOL_ASSERT_ERROR(cam.isConnected(), "Camera is not connected.");
13}
14
16{
18 IRSOL_NAMED_LOG_DEBUG("status_monitor", "Automatic stopping monitoring of camera.");
19 stop();
20 }
21}
22
23void
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}
40
41void
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}
50
51void
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}
63} // namespace camera
64} // namespace irsol
Assertion macros and utilities based on the PPK_ASSERT library.
High-level wrapper around the NeoAPI camera for synchronized access.
Definition interface.hpp:61
std::string cameraStatusAsString() const
Get current camera status.
bool isConnected() const
Check if the camera is currently connected.
void start()
Starts the monitoring thread. If the monitor is already running, this call has no effect.
Definition monitor.cpp:42
~StatusMonitor()
Destructor stops the monitoring thread if running.
Definition monitor.cpp:15
virtual void runMonitor() const
The main monitoring loop executed on the background thread.
Definition monitor.cpp:24
void stop()
Stops the monitoring thread and waits for it to finish. If the monitor is not running,...
Definition monitor.cpp:52
std::thread m_monitorThread
Thread running the periodic monitoring loop.
Definition monitor.hpp:92
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
StatusMonitor(const Interface &cam, irsol::types::duration_t monitorInterval=std::chrono::milliseconds(100))
Constructs a StatusMonitor for the given camera interface.
Definition monitor.cpp:9
#define IRSOL_ASSERT_ERROR
Error-level assertion macro.
Definition assert.hpp:134
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
Definition logging.hpp:176
#define IRSOL_NAMED_LOG_DEBUG(name,...)
Logs a debug-level message using a named logger.
Definition logging.hpp:174
Logging utilities and configuration for the irsol library.
Defines the StatusMonitor class to periodically monitor camera status in a background thread.
clock_t::duration duration_t
Alias for a duration of time as defined by clock_t.
Definition types.hpp:128
Core type definitions for networking, time handling, and protocol values used throughout the irsol li...