From e98a420d5dce43ebe197dce05457e1da7e5c6160 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 1 Nov 2018 20:07:45 +0100 Subject: [PATCH] Improved FSX/P3D time sync --- .../fsxcommon/simulatorfsxcommon.cpp | 25 ++++++++++++++----- .../simulator/fsxcommon/simulatorfsxcommon.h | 2 +- .../fsxcommon/simulatorfsxsimconnectproc.cpp | 11 +------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp index c8ca0d2ef..1b33966ea 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp @@ -2016,15 +2016,23 @@ namespace BlackSimPlugin return lla; } - void CSimulatorFsxCommon::synchronizeTime(const CTime &zuluTimeSim, const CTime &localTimeSim) + void CSimulatorFsxCommon::synchronizeTime(const DataDefinitionSimEnvironment *simEnv) { if (!m_simTimeSynced) { return; } if (!this->isConnected()) { return; } - if (m_syncDeferredCounter > 0) + if (m_syncTimeDeferredCounter > 0) { - --m_syncDeferredCounter; + --m_syncTimeDeferredCounter; + return; // wait some time before we snyc again } - Q_UNUSED(localTimeSim); + + const int zh = simEnv->zuluTimeSeconds / 3600; + const int zm = (simEnv->zuluTimeSeconds - (zh * 3600)) / 60; + const CTime zuluTimeSim(zh, zm); + // const int lh = simEnv->localTimeSeconds / 3600; + // const int lm = (simEnv->localTimeSeconds - (lh * 3600)) / 60; + // const CTime localTimeSim(lh, lm); + // Q_UNUSED(localTimeSim); QDateTime myDateTime = QDateTime::currentDateTimeUtc(); if (!m_syncTimeOffset.isZeroEpsilonConsidered()) @@ -2038,7 +2046,12 @@ namespace BlackSimPlugin const int targetMins = myTime.hour() * 60 + myTime.minute(); const int simMins = zuluTimeSim.valueInteger(CTimeUnit::min()); const int diffMins = qAbs(targetMins - simMins); - if (diffMins < 2) { return; } + if (diffMins < 2) + { + // checked and no relevant difference + m_syncTimeDeferredCounter = 10; // wait some time to check again + return; + } const HRESULT hr1 = SimConnect_TransmitClientEvent(m_hSimConnect, 0, EventSetTimeZuluHours, h, SIMCONNECT_GROUP_PRIORITY_STANDARD, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); const HRESULT hr2 = SimConnect_TransmitClientEvent(m_hSimConnect, 0, EventSetTimeZuluMinutes, m, SIMCONNECT_GROUP_PRIORITY_STANDARD, SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY); @@ -2048,7 +2061,7 @@ namespace BlackSimPlugin } else { - m_syncDeferredCounter = 5; // allow some time to sync + m_syncTimeDeferredCounter = 5; // allow some time to sync CLogMessage(this).info("Synchronized time to UTC: '%1'") << myTime.toString(); } } diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h index 2112e2caa..c2953def1 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.h @@ -475,7 +475,7 @@ namespace BlackSimPlugin CSimConnectObject getSimObjectForObjectId(DWORD objectId) const; //! Sync time with user's computer - void synchronizeTime(const BlackMisc::PhysicalQuantities::CTime &zuluTimeSim, const BlackMisc::PhysicalQuantities::CTime &localTimeSim); + void synchronizeTime(const DataDefinitionSimEnvironment *simEnv); //! Request data for a CSimConnectObject (aka remote aircraft) bool requestPositionDataForSimObject(const CSimConnectObject &simObject, SIMCONNECT_PERIOD period = SIMCONNECT_PERIOD_SECOND); diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxsimconnectproc.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxsimconnectproc.cpp index 3b6d30fd4..879d68fda 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxsimconnectproc.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxsimconnectproc.cpp @@ -253,16 +253,7 @@ namespace BlackSimPlugin case CSimConnectDefinitions::RequestSimEnvironment: { const DataDefinitionSimEnvironment *simEnv = reinterpret_cast(&pObjData->dwData); - if (simulatorFsxP3D->isTimeSynchronized()) - { - const int zh = simEnv->zuluTimeSeconds / 3600; - const int zm = (simEnv->zuluTimeSeconds - (zh * 3600)) / 60; - const CTime zulu(zh, zm); - const int lh = simEnv->localTimeSeconds / 3600; - const int lm = (simEnv->localTimeSeconds - (lh * 3600)) / 60; - const CTime local(lh, lm); - simulatorFsxP3D->synchronizeTime(zulu, local); - } + simulatorFsxP3D->synchronizeTime(simEnv); break; } default: