diff --git a/src/blackcore/context_network.cpp b/src/blackcore/context_network.cpp index 115edd3e9..c267de62a 100644 --- a/src/blackcore/context_network.cpp +++ b/src/blackcore/context_network.cpp @@ -24,9 +24,10 @@ namespace BlackCore * Init this context */ CContextNetwork::CContextNetwork(CCoreRuntime *runtime) : - IContextNetwork(runtime), m_network(nullptr), m_networkManager(nullptr), - m_atcBookingTimer(nullptr), m_dataUpdateTimer(nullptr) + IContextNetwork(runtime), m_network(nullptr), m_bookingReader(nullptr), m_dataUpdateTimer(nullptr) { + Q_ASSERT(this->getRuntime()); + Q_ASSERT(this->getRuntime()->getIContextSettings()); // 1. Init by "network driver" this->m_network = new CNetworkVatlib(this); @@ -34,12 +35,10 @@ namespace BlackCore // 2. Init own aircraft this->initOwnAircraft(); - // 3a. Init network access driver for XML data (bookings) - this->m_networkManager = new QNetworkAccessManager(this); - this->m_atcBookingTimer = new QTimer(this); - this->connect(this->m_networkManager, &QNetworkAccessManager::finished, this, &CContextNetwork::psAtcBookingsRead); - this->connect(this->m_atcBookingTimer, &QTimer::timeout, this, &CContextNetwork::readAtcBookingsFromSource); - this->m_atcBookingTimer->start(10 * 1000); // will be reset in method to a longer time + // 3a. Init VATSIM bookings + this->m_bookingReader = new CVatsimBookingReader(this->getRuntime()->getIContextSettings()->getNetworkSettings().getBookingServiceUrl(), this); + this->connect(this->m_bookingReader, &CVatsimBookingReader::bookingsRead, this, &CContextNetwork::psReceivedBookings); + this->m_bookingReader->setInterval(10 * 1000); // first read // 3b. Update timer for data this->m_dataUpdateTimer = new QTimer(this); diff --git a/src/blackcore/context_network.h b/src/blackcore/context_network.h index 802c5d772..2b51beb4f 100644 --- a/src/blackcore/context_network.h +++ b/src/blackcore/context_network.h @@ -9,6 +9,7 @@ #include "blackcore/dbus_server.h" #include "blackcore/network_vatlib.h" #include "blackcore/coreruntime.h" +#include "blackcore/vatsimbookingreader.h" #include "blackcore/context_network_interface.h" #include "blackcore/context_settings_interface.h" #include "blackmisc/avcallsignlist.h" @@ -61,7 +62,7 @@ namespace BlackCore public slots: //! \copydoc IContextNetwork::readAtcBookingsFromSource() - virtual void readAtcBookingsFromSource(); + virtual void readAtcBookingsFromSource() override; /*! * \copydoc IContextNetwork::getAtcStationsOnline() @@ -147,10 +148,8 @@ namespace BlackCore QMap m_metarCache /*!< Keep METARs for a while */; // for reading XML - QNetworkAccessManager *m_networkManager; - QTimer *m_atcBookingTimer; //!< ATC stations bookings + CVatsimBookingReader *m_bookingReader; QTimer *m_dataUpdateTimer; //!< general updates such as ATIS, frequencies, see requestDataUpdates() - QDateTime m_atcBookingsUpdateTimestamp; //! \brief Replace value by new values void setAtcStationsBooked(const BlackMisc::Aviation::CAtcStationList &newStations); @@ -182,6 +181,9 @@ namespace BlackCore } private slots: + //! \brief ATC bookings received + void psReceivedBookings(BlackMisc::Aviation::CAtcStationList bookedStations); + /*! * \brief Connection status changed? * \param from old status @@ -235,13 +237,6 @@ namespace BlackCore //! \brief Radio text messages received void psFsdTextMessageReceived(const BlackMisc::Network::CTextMessageList &messages); - - /*! - * \brief Bookings via XML read - * \todo encapsulate reading from WWW in some class - */ - void psAtcBookingsRead(QNetworkReply *nwReply); - }; } diff --git a/src/blackcore/context_network_atc.cpp b/src/blackcore/context_network_atc.cpp index 16b734e8b..4a8dbec40 100644 --- a/src/blackcore/context_network_atc.cpp +++ b/src/blackcore/context_network_atc.cpp @@ -26,18 +26,27 @@ namespace BlackCore { /* - * Read bookings + * Reload bookings */ void CContextNetwork::readAtcBookingsFromSource() { - const int updateTime = 60 * 1000; // 1min - if (!(this->m_atcBookingTimer->interval() == updateTime)) this->m_atcBookingTimer->setInterval(updateTime); // 1min + Q_ASSERT(this->m_bookingReader); + this->m_bookingReader->read(); + } - QUrl url(this->getNetworkSettings().getBookingServiceUrl()); - if (url.isEmpty()) return; - - QNetworkRequest request(url); - this->m_networkManager->get(request); + /* + * Update bookings + */ + void CContextNetwork::psReceivedBookings(CAtcStationList bookedStations) + { + const int interval = 60 * 1000; + if (this->m_bookingReader->interval() < interval) this->m_bookingReader->setInterval(interval); + this->m_atcStationsBooked.clear(); + foreach(CAtcStation bookedStation, bookedStations) + { + this->m_atcStationsOnline.mergeWithBooking(bookedStation); + this->m_atcStationsBooked.push_back(bookedStation); + } } /* @@ -322,85 +331,4 @@ namespace BlackCore this->m_metarCache.insert(icaoCode, metar); } - /* - * Bookings read from XML - * TODO: encapsulate reading from WWW in some class - */ - void CContextNetwork::psAtcBookingsRead(QNetworkReply *nwReply) - { - if (nwReply->error() == QNetworkReply::NoError) - { - QString xmlData = nwReply->readAll(); - QDomDocument doc; - - if (doc.setContent(xmlData)) - { - QDomNode atc = doc.elementsByTagName("atcs").at(0); - QDomNodeList bookingNodes = atc.toElement().elementsByTagName("booking"); - int size = bookingNodes.size(); - CSequence stations; - for (int i = 0; i < size; i++) - { - QDomNode bookingNode = bookingNodes.at(i); - QDomNodeList bookingNodeValues = bookingNode.childNodes(); - CAtcStation bookedStation; - CUser user; - for (int v = 0; v < bookingNodeValues.size(); v++) - { - QDomNode bookingNodeValue = bookingNodeValues.at(v); - QString name = bookingNodeValue.nodeName().toLower(); - QString value = bookingNodeValue.toElement().text(); - if (name == "id") - { - // could be used as unique key - } - else if (name == "callsign") - { - bookedStation.setCallsign(CCallsign(value)); - } - else if (name == "name") - { - user.setRealName(value); - } - else if (name == "cid") - { - user.setId(value); - } - else if (name == "time_end") - { - QDateTime t = QDateTime::fromString(value, "yyyy-MM-dd HH:mm:ss"); - bookedStation.setBookedUntilUtc(t); - } - else if (name == "time_start") - { - QDateTime t = QDateTime::fromString(value, "yyyy-MM-dd HH:mm:ss"); - bookedStation.setBookedFromUtc(t); - } - } - // time checks - QDateTime now = QDateTime::currentDateTimeUtc(); - if (now.msecsTo(bookedStation.getBookedUntilUtc()) < (1000 * 60 * 15)) continue; // until n mins in past - if (now.msecsTo(bookedStation.getBookedFromUtc()) > (1000 * 60 * 60 * 24)) continue; // to far in the future, n hours - - // booking does not have position, so distance cannot be calculated - // bookedStation.calculcateDistanceToPlane(this->m_ownAircraft.getPosition()); - - bookedStation.setController(user); - - // consolidate and append - this->m_atcStationsOnline.mergeWithBooking(bookedStation); - stations.push_back(bookedStation); - } - nwReply->close(); - nwReply->deleteLater(); - - // set the new values - if (this->getAtcStationsBooked() != stations) - { - this->atcStationsBooked() = stations; - emit this->changedAtcStationsBooked(); - } - } // node - } // content - } // method } // namespace diff --git a/src/blackcore/context_network_interface.h b/src/blackcore/context_network_interface.h index 970dcafd8..4700ddcb7 100644 --- a/src/blackcore/context_network_interface.h +++ b/src/blackcore/context_network_interface.h @@ -155,8 +155,7 @@ namespace BlackCore public slots: /*! - * \brief Read ATC bookings - * \return + * \brief Reload bookings from booking service */ virtual void readAtcBookingsFromSource();