44#include < logger.h>
55
66constexpr auto LABEL = " scanner" ;
7+ constexpr auto LOOP_TIMEOUT = std::chrono::milliseconds(10 );
78
89Scanner::Scanner (const Config& config, const Device& device, Mqtt& mqtt, RemoteController& remoteController, const int recordersCount)
910 : m_device(config, device, mqtt, m_notification, recordersCount),
@@ -34,13 +35,35 @@ Scanner::~Scanner() {
3435 m_thread.join ();
3536}
3637
38+ void Scanner::runScheduler (const std::optional<FrequencyRange>& activeRange) {
39+ auto recordings = m_scheduler.getRecordings (getTime ());
40+ if (recordings) {
41+ m_device.updateRecordings ({});
42+ m_device.setFrequencyRange (recordings->first );
43+ while (m_isRunning && recordings) {
44+ m_device.updateRecordings (recordings->second );
45+ recordings = m_scheduler.getRecordings (getTime ());
46+ std::this_thread::sleep_for (LOOP_TIMEOUT);
47+ }
48+ m_device.updateRecordings ({});
49+ if (activeRange) {
50+ m_device.setFrequencyRange (*activeRange);
51+ }
52+ }
53+ }
54+
3755void Scanner::worker () {
3856 Logger::info (LABEL, " thread started" );
3957 if (m_ranges.empty ()) {
4058 Logger::warn (LABEL, " empty scanned ranges" );
59+ while (m_isRunning) {
60+ runScheduler (std::nullopt );
61+ std::this_thread::sleep_for (LOOP_TIMEOUT);
62+ }
4163 } else if (m_ranges.size () == 1 ) {
4264 m_device.setFrequencyRange (m_ranges.front ());
4365 while (m_isRunning) {
66+ runScheduler (m_ranges.front ());
4467 m_device.updateRecordings (m_notification.wait ());
4568 }
4669 } else {
@@ -51,6 +74,7 @@ void Scanner::worker() {
5174 const auto startScanningTime = getTime ();
5275 bool isRecording = true ;
5376 while ((getTime () <= startScanningTime + RANGE_SCANNING_TIME || isRecording) && m_isRunning) {
77+ runScheduler (range);
5478 const auto notification = m_notification.wait ();
5579 isRecording = !notification.empty ();
5680 m_device.updateRecordings (notification);
0 commit comments