diff --git a/src/blackcore/context_network_atc.cpp b/src/blackcore/context_network_atc.cpp index 53f56c5e7..806258c4b 100644 --- a/src/blackcore/context_network_atc.cpp +++ b/src/blackcore/context_network_atc.cpp @@ -87,13 +87,10 @@ namespace BlackCore */ CVoiceRoomList CContextNetwork::getSelectedVoiceRooms() const { - CFrequency com1 = this->m_ownAircraft.getCom1System().getFrequencyActive(); - CFrequency com2 = this->m_ownAircraft.getCom2System().getFrequencyActive(); - - CAtcStationList stationsCom1 = this->m_atcStationsOnline.findBy(&CAtcStation::getFrequency, com1); - CAtcStationList stationsCom2 = this->m_atcStationsOnline.findBy(&CAtcStation::getFrequency, com2); - stationsCom1.sortBy(&CAtcStation::getDistanceToPlane); + CAtcStationList stationsCom1 = this->m_atcStationsOnline.findIfComUnitTunedIn25KHz(this->m_ownAircraft.getCom1System()); + CAtcStationList stationsCom2 = this->m_atcStationsOnline.findIfComUnitTunedIn25KHz(this->m_ownAircraft.getCom2System()); stationsCom1.sortBy(&CAtcStation::getDistanceToPlane); + stationsCom2.sortBy(&CAtcStation::getDistanceToPlane); CVoiceRoom vr; CVoiceRoomList rooms; diff --git a/src/blackmisc/avaircraft.h b/src/blackmisc/avaircraft.h index 2db7cb1f6..d38f2286b 100644 --- a/src/blackmisc/avaircraft.h +++ b/src/blackmisc/avaircraft.h @@ -188,6 +188,16 @@ namespace BlackMisc */ void setCom2System(const CComSystem &comSystem) { this->m_com2system = comSystem; } + + /*! + * \brief Is any (COM1/2) active frequency within 8.3383kHz channel? + */ + bool isActiveFrequencyWithin8_33kHzChannel(const BlackMisc::PhysicalQuantities::CFrequency &comFrequency) + { + return this->m_com1system.isActiveFrequencyWithin8_33kHzChannel(comFrequency) || + this->m_com2system.isActiveFrequencyWithin8_33kHzChannel(comFrequency); + } + /*! * \brief Get transponder */ diff --git a/src/blackmisc/avatcstation.h b/src/blackmisc/avatcstation.h index 45a525f66..00229d69b 100644 --- a/src/blackmisc/avatcstation.h +++ b/src/blackmisc/avatcstation.h @@ -10,15 +10,15 @@ #ifndef BLACKMISC_ATCSTATION_H #define BLACKMISC_ATCSTATION_H -#include "valuemap.h" +#include "vvoiceroom.h" +#include "aviocomsystem.h" +#include "avinformationmessage.h" +#include "avcallsign.h" +#include "nwuser.h" +#include "coordinategeodetic.h" #include "pqfrequency.h" #include "pqlength.h" #include "pqtime.h" -#include "nwuser.h" -#include "vvoiceroom.h" -#include "coordinategeodetic.h" -#include "avcallsign.h" -#include "avinformationmessage.h" #include #include #include @@ -319,6 +319,14 @@ namespace BlackMisc */ bool isBookedNow() const; + /*! + * \brief Tuned in within 25KHz channel spacing + */ + bool isComUnitTunedIn25KHz(const BlackMisc::Aviation::CComSystem &comUnit) const + { + return comUnit.isActiveFrequencyWithin25kHzChannel(this->getFrequency()); + } + /*! * When booked, 0 means now, * negative values mean booking in past, diff --git a/src/blackmisc/avatcstationlist.cpp b/src/blackmisc/avatcstationlist.cpp index 377292e96..0e6dd146b 100644 --- a/src/blackmisc/avatcstationlist.cpp +++ b/src/blackmisc/avatcstationlist.cpp @@ -55,12 +55,23 @@ namespace BlackMisc }); } + /* + * Find if on frequency of COM unit + */ + CAtcStationList CAtcStationList::findIfComUnitTunedIn25KHz(const CComSystem &comUnit) const + { + return this->findBy([&](const CAtcStation & atcStation) + { + return atcStation.isComUnitTunedIn25KHz(comUnit); + }); + } + /* * Distances to own plane */ void CAtcStationList::calculateDistancesToPlane(const Geo::CCoordinateGeodetic &position) { - std::for_each(this->begin(), this->end(), [ & ](CAtcStation &station) + std::for_each(this->begin(), this->end(), [ & ](CAtcStation & station) { station.calculcateDistanceToPlane(position); }); diff --git a/src/blackmisc/avatcstationlist.h b/src/blackmisc/avatcstationlist.h index 8685d1fb6..44beeffd2 100644 --- a/src/blackmisc/avatcstationlist.h +++ b/src/blackmisc/avatcstationlist.h @@ -49,22 +49,21 @@ namespace BlackMisc /*! * \brief Find 0..n stations by callsign - * \param callsign - * \return */ CAtcStationList findByCallsign(const CCallsign &callsign) const; /*! * \brief Find 0..n stations within range of given coordinate - * \param coordinate - * \param range - * \return */ CAtcStationList findWithinRange(const BlackMisc::Geo::ICoordinateGeodetic &coordinate, const BlackMisc::PhysicalQuantities::CLength &range) const; /*! - * \brief Update distances to own plane - * \param position + * \brief Find 0..n stations tune in frequency of COM unit (with 25kHt channel spacing + */ + CAtcStationList findIfComUnitTunedIn25KHz(const BlackMisc::Aviation::CComSystem &comUnit) const; + + /*! + * \brief Update distances to coordinate, usually own aircraft's position */ void calculateDistancesToPlane(const BlackMisc::Geo::CCoordinateGeodetic &position); diff --git a/src/blackmisc/aviobase.h b/src/blackmisc/aviobase.h index 160503b44..6421796fb 100644 --- a/src/blackmisc/aviobase.h +++ b/src/blackmisc/aviobase.h @@ -79,8 +79,7 @@ namespace BlackMisc } /*! - * \brief Stream to DBus << - * \param argument + * \copydoc CValueObject::marshallToDbus() */ virtual void marshallToDbus(QDBusArgument &argument) const { @@ -88,8 +87,7 @@ namespace BlackMisc } /*! - * \brief Stream from DBus >> - * \param argument + * \copydoc CValueObject::unmarshallFromDbus() */ virtual void unmarshallFromDbus(const QDBusArgument &argument) { diff --git a/src/blackmisc/aviocomsystem.cpp b/src/blackmisc/aviocomsystem.cpp index eeae69198..d8702d26a 100644 --- a/src/blackmisc/aviocomsystem.cpp +++ b/src/blackmisc/aviocomsystem.cpp @@ -41,7 +41,7 @@ namespace BlackMisc { CFrequency f(frequencyMHz, CFrequencyUnit::MHz()); if (f == this->getFrequencyActive()) return; // save all the comparisons / rounding - CComSystem::roundTo25KHz(f); + CComSystem::roundToChannelSpacing(f, this->m_channelSpacing); this->CModulator::setFrequencyActive(f); this->validate(true); } @@ -50,24 +50,74 @@ namespace BlackMisc { CFrequency f(frequencyMHz, CFrequencyUnit::MHz()); if (f == this->getFrequencyStandby()) return; // save all the comparisons / rounding - CComSystem::roundTo25KHz(f); + CComSystem::roundToChannelSpacing(f, this->m_channelSpacing); this->CModulator::setFrequencyStandby(f); this->validate(true); } /* - * Round to 25KHz + * Round to channel spacing */ - void CComSystem::roundTo25KHz(PhysicalQuantities::CFrequency &frequency) + void CComSystem::roundToChannelSpacing(PhysicalQuantities::CFrequency &frequency, ChannelSpacing channelSpacing) { + double channelSpacingKHz = CComSystem::channelSpacingToFrequencyKHz(channelSpacing); double f = frequency.valueRounded(CFrequencyUnit::kHz(), 0); - quint32 d = static_cast(f / 25.0); + quint32 d = static_cast(f / channelSpacingKHz); frequency.switchUnit(CFrequencyUnit::MHz()); double f0 = frequency.valueRounded(CFrequencyUnit::MHz(), 3); - double f1 = CMath::round(d * (25.0 / 1000.0), 3); - double f2 = CMath::round((d + 1) * (25.0 / 1000.0), 3); + double f1 = CMath::round(d * (channelSpacingKHz / 1000.0), 3); + double f2 = CMath::round((d + 1) * (channelSpacingKHz / 1000.0), 3); bool down = qAbs(f1 - f0) < qAbs(f2 - f0); // which is the closest value frequency.setCurrentUnitValue(down ? f1 : f2); } + + /* + * Within channel spacing + */ + bool CComSystem::isWithinChannelSpacing(const CFrequency &setFrequency, const CFrequency &compareFrequency, CComSystem::ChannelSpacing channelSpacing) + { + if (setFrequency == compareFrequency) return true; // shortcut for many of such comparisons + double channelSpacingKHz = 0.5 * CComSystem::channelSpacingToFrequencyKHz(channelSpacing); + double compareFrequencyKHz = compareFrequency.value(CFrequencyUnit::kHz()); + double setFrequencyKHz = setFrequency.value(CFrequencyUnit::kHz()); + return (setFrequencyKHz - channelSpacingKHz < compareFrequencyKHz) && + (setFrequencyKHz + channelSpacingKHz > compareFrequencyKHz); + } + + /* + * Helper, give me number for channels spacing + */ + double CComSystem::channelSpacingToFrequencyKHz(ChannelSpacing channelSpacing) + { + switch (channelSpacing) + { + case ChannelSpacing50KHz: return 50.0; + case ChannelSpacing25KHz: return 25.0; + case ChannelSpacing8_33KHz: return 25.0 / 3.0; + default: qFatal("Wrong channel spacing"); return 0.0; // return just supressing compiler warning + } + } + + /* + * Marshall + */ + void CComSystem::marshallToDbus(QDBusArgument &argument) const + { + CModulator::marshallToDbus(argument); + argument << static_cast(this->m_channelSpacing); + } + + /* + * Unmarshall + */ + void CComSystem::unmarshallFromDbus(const QDBusArgument &argument) + { + CModulator::unmarshallFromDbus(argument); + uint cs; + argument >> cs; + this->m_channelSpacing = static_cast(cs); + } + + } // namespace } diff --git a/src/blackmisc/aviocomsystem.h b/src/blackmisc/aviocomsystem.h index 1f74775d3..8aee1cd46 100644 --- a/src/blackmisc/aviocomsystem.h +++ b/src/blackmisc/aviocomsystem.h @@ -18,7 +18,20 @@ namespace BlackMisc */ class CComSystem : public CModulator { + public: + /*! + * Channel spacing frequency + */ + enum ChannelSpacing + { + ChannelSpacing50KHz, + ChannelSpacing25KHz, + ChannelSpacing8_33KHz + }; + private: + ChannelSpacing m_channelSpacing; + /*! * \brief Constructor * \param validate @@ -29,11 +42,17 @@ namespace BlackMisc * */ CComSystem(bool validate, const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency, int digits = 3): - CModulator(name, activeFrequency, standbyFrequency, digits) + CModulator(name, activeFrequency, standbyFrequency, digits), m_channelSpacing(ChannelSpacing25KHz) { this->validate(validate); } + /*! + * \brief Give me channel spacing in KHz + * \remarks Just a helper method, that is why no CFrequency is returned + */ + static double channelSpacingToFrequencyKHz(ChannelSpacing channelSpacing); + protected: /*! * \brief Are the set values valid / in range? @@ -50,16 +69,35 @@ namespace BlackMisc */ bool validate(bool strict = true) const; + /*! + * \copydoc CValueObject::marshallFromDbus() + */ + virtual void marshallToDbus(QDBusArgument &argument) const; + + /*! + * \copydoc CValueObject::unmarshallFromDbus() + */ + virtual void unmarshallFromDbus(const QDBusArgument &argument); + public: /*! * Default constructor */ - CComSystem() : CModulator() {} + CComSystem() : CModulator(), m_channelSpacing(ChannelSpacing25KHz) {} + + /*! + * \brief Constructor + */ + CComSystem(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency = CModulator::FrequencyNotSet(), int digits = 3): + CModulator(name, activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency, digits), m_channelSpacing(ChannelSpacing25KHz) + { + this->validate(true); + } /*! * \brief Copy constructor */ - CComSystem(const CComSystem &other) : CModulator(other) {} + CComSystem(const CComSystem &other) : CModulator(other), m_channelSpacing(other.m_channelSpacing) {} /*! * \copydoc CValueObject::toQVariant @@ -78,27 +116,35 @@ namespace BlackMisc return CModulator::getValueHash(); } - /*! - * \brief Constructor - */ - CComSystem(const QString &name, const BlackMisc::PhysicalQuantities::CFrequency &activeFrequency, const BlackMisc::PhysicalQuantities::CFrequency &standbyFrequency = CModulator::FrequencyNotSet(), int digits = 3): - CModulator(name, activeFrequency, standbyFrequency == CModulator::FrequencyNotSet() ? activeFrequency : standbyFrequency, digits) - { - this->validate(true); - } - /*! * \brief Set active frequency - * \remarks will be rounded to 25KHz + * \remarks will be rounded to channel spacing + * \see ChannelSpacing */ void setFrequencyActiveMHz(double frequencyMHz); /*! * \brief Set standby frequency - * \remarks will be rounded to 25KHz + * \remarks will be rounded to channel spacing */ void setFrequencyStandbyMHz(double frequencyMHz); + /*! + * \brief Is active frequency within 8.3383kHz channel? + */ + bool isActiveFrequencyWithin8_33kHzChannel(const BlackMisc::PhysicalQuantities::CFrequency &comFrequency) + { + return CComSystem::isWithinChannelSpacing(this->getFrequencyActive(), comFrequency, ChannelSpacing8_33KHz); + } + + /*! + * \brief Is active frequency within 25kHz channel? + */ + bool isActiveFrequencyWithin25kHzChannel(const BlackMisc::PhysicalQuantities::CFrequency &comFrequency) const + { + return CComSystem::isWithinChannelSpacing(this->getFrequencyActive(), comFrequency, ChannelSpacing25KHz); + } + /*! * \brief Set UNICOM frequency as active */ @@ -119,8 +165,6 @@ namespace BlackMisc /*! * \brief operator == - * \param other - * \return */ bool operator ==(const CComSystem &other) const { @@ -129,8 +173,6 @@ namespace BlackMisc /*! * \brief operator != - * \param other - * \return */ bool operator !=(const CComSystem &other) const { @@ -328,9 +370,15 @@ namespace BlackMisc } /*! - * \brief Round to 25KHz, set MHz as unit + * \brief Round to channel spacing, set MHz as unit + * \see ChannelSpacing */ - static void roundTo25KHz(BlackMisc::PhysicalQuantities::CFrequency &frequency); + static void roundToChannelSpacing(BlackMisc::PhysicalQuantities::CFrequency &frequency, ChannelSpacing channelSpacing); + + /*! + * \brief Is compareFrequency within channel spacing of setFrequency + */ + static bool isWithinChannelSpacing(const BlackMisc::PhysicalQuantities::CFrequency &setFrequency, const BlackMisc::PhysicalQuantities::CFrequency &compareFrequency, ChannelSpacing channelSpacing); }; diff --git a/src/blackmisc/aviomodulator.h b/src/blackmisc/aviomodulator.h index 9aaa993eb..611ba9d1d 100644 --- a/src/blackmisc/aviomodulator.h +++ b/src/blackmisc/aviomodulator.h @@ -205,20 +205,17 @@ namespace BlackMisc } /*! - * \brief Stream to DBus << - * \param argument + * \copydoc CValueObject::marshallFromDbus() */ virtual void marshallToDbus(QDBusArgument &argument) const; /*! - * \brief Stream from DBus >> - * \param argument + * \copydoc CValueObject::unmarshallFromDbus() */ virtual void unmarshallFromDbus(const QDBusArgument &argument); /*! - * \brief Value hash - * \return + * \copydoc CValueObject::getValueHash() */ virtual uint getValueHash() const;