2026-01-10 21:33:24 -05:00
|
|
|
/**
|
|
|
|
|
* @file DriftingDateTime.cpp
|
|
|
|
|
* @brief Implementation of DriftingDateTimeSingleton
|
|
|
|
|
*/
|
2026-01-18 12:53:55 -06:00
|
|
|
|
|
|
|
|
#include "DriftingDateTime.h"
|
|
|
|
|
|
2025-10-25 00:07:15 +02:00
|
|
|
#include <QLoggingCategory>
|
2026-01-01 15:12:24 -06:00
|
|
|
#include <QMutexLocker>
|
2025-10-25 00:07:15 +02:00
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
|
|
Q_DECLARE_LOGGING_CATEGORY(driftingdatetime_js8)
|
2018-09-18 17:24:07 -04:00
|
|
|
|
2026-01-10 21:33:24 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Get the singleton instance
|
2026-01-18 12:53:55 -06:00
|
|
|
*
|
|
|
|
|
* @return DriftingDateTimeSingleton&
|
2026-01-10 21:33:24 -05:00
|
|
|
*/
|
2026-01-01 15:12:24 -06:00
|
|
|
DriftingDateTimeSingleton &DriftingDateTimeSingleton::getSingleton() {
|
2025-10-25 00:07:15 +02:00
|
|
|
if (singleton.isNull()) {
|
|
|
|
|
singleton = QPointer{new DriftingDateTimeSingleton{}};
|
2024-09-06 17:24:38 -07:00
|
|
|
}
|
2025-10-25 00:07:15 +02:00
|
|
|
return *(singleton.data());
|
|
|
|
|
}
|
2018-09-18 17:24:07 -04:00
|
|
|
|
2026-01-10 21:33:24 -05:00
|
|
|
/**
|
2026-01-18 12:53:55 -06:00
|
|
|
* @brief Construct a new Drifting Date Time Singleton:: Drifting Date Time
|
|
|
|
|
* Singleton object
|
|
|
|
|
*
|
2026-01-10 21:33:24 -05:00
|
|
|
*/
|
2026-01-01 15:12:24 -06:00
|
|
|
DriftingDateTimeSingleton::DriftingDateTimeSingleton() : driftMS(0) {}
|
2018-09-18 17:24:07 -04:00
|
|
|
|
2026-01-10 21:33:24 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Retrieve drift, in milliseconds.
|
|
|
|
|
*
|
|
|
|
|
* Positive values indicate the drifted clock is behind the system clock,
|
|
|
|
|
* negative, it is early.
|
2026-01-18 12:53:55 -06:00
|
|
|
*
|
|
|
|
|
* @return qint64
|
2026-01-10 21:33:24 -05:00
|
|
|
*/
|
2025-10-25 00:07:15 +02:00
|
|
|
qint64 DriftingDateTimeSingleton::drift() const {
|
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
|
return driftMS;
|
|
|
|
|
}
|
2018-09-18 17:24:07 -04:00
|
|
|
|
2026-01-10 21:33:24 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Set the drift inner
|
2026-01-18 12:53:55 -06:00
|
|
|
*
|
|
|
|
|
* @param ms
|
2026-01-10 21:33:24 -05:00
|
|
|
*/
|
2025-10-25 00:07:15 +02:00
|
|
|
void DriftingDateTimeSingleton::setDriftInner(qint64 ms) {
|
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
|
driftMS = ms;
|
|
|
|
|
}
|
2024-09-06 17:24:38 -07:00
|
|
|
|
2026-01-10 21:33:24 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Set the drift
|
2026-01-18 12:53:55 -06:00
|
|
|
*
|
|
|
|
|
* @param ms
|
2026-01-10 21:33:24 -05:00
|
|
|
*/
|
2025-10-25 00:07:15 +02:00
|
|
|
void DriftingDateTimeSingleton::setDrift(qint64 ms) {
|
|
|
|
|
qint64 old_drift = drift();
|
|
|
|
|
setDriftInner(ms);
|
2026-01-01 15:12:24 -06:00
|
|
|
if (ms != old_drift) {
|
|
|
|
|
qCDebug(driftingdatetime_js8)
|
|
|
|
|
<< "Changed drift from" << old_drift << "to" << ms << "ms";
|
2025-10-25 00:07:15 +02:00
|
|
|
emit driftChanged(ms);
|
|
|
|
|
} else {
|
2026-01-01 15:12:24 -06:00
|
|
|
qCDebug(driftingdatetime_js8)
|
|
|
|
|
<< "Incoming signal without change of drift, still" << old_drift
|
|
|
|
|
<< "ms";
|
2024-09-06 17:24:38 -07:00
|
|
|
}
|
2025-10-25 00:07:15 +02:00
|
|
|
}
|
2024-09-08 06:29:38 -07:00
|
|
|
|
2026-01-10 21:33:24 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Emits to the driftChanged signal (as per TwoPhaseSignal contract).
|
2026-01-18 12:53:55 -06:00
|
|
|
*
|
2026-01-10 21:33:24 -05:00
|
|
|
*/
|
2025-10-25 00:07:15 +02:00
|
|
|
void DriftingDateTimeSingleton::onPlumbingCompleted() const {
|
|
|
|
|
emit driftChanged(drift());
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-10 21:33:24 -05:00
|
|
|
/**
|
|
|
|
|
* @brief Static member initialization
|
2026-01-18 12:53:55 -06:00
|
|
|
*
|
2026-01-10 21:33:24 -05:00
|
|
|
*/
|
2026-01-01 15:12:24 -06:00
|
|
|
QPointer<DriftingDateTimeSingleton> DriftingDateTimeSingleton::singleton =
|
|
|
|
|
QPointer<DriftingDateTimeSingleton>{};
|
2025-10-25 00:07:15 +02:00
|
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(driftingdatetime_js8, "driftingdatetime.js8", QtWarningMsg)
|