From bd3c5343e33d9df93b71c0db370c31f4c5acffba Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Fri, 6 Nov 2020 16:42:25 +0000 Subject: [PATCH] Don't connect twice, as that results in multiple signals being emitted for each download. Speed up reading of airport DB. Fix a few compiler warnings. --- plugins/channelrx/demodadsb/adsbdemodgui.cpp | 3 +- .../channelrx/demodadsb/adsbdemodsinkworker.h | 6 +- plugins/channelrx/demodadsb/osndb.h | 22 +-- plugins/channelrx/demodadsb/ourairportsdb.h | 175 ++++++++++++------ 4 files changed, 134 insertions(+), 72 deletions(-) diff --git a/plugins/channelrx/demodadsb/adsbdemodgui.cpp b/plugins/channelrx/demodadsb/adsbdemodgui.cpp index d2aeaf3a7..60c69866f 100644 --- a/plugins/channelrx/demodadsb/adsbdemodgui.cpp +++ b/plugins/channelrx/demodadsb/adsbdemodgui.cpp @@ -1182,7 +1182,6 @@ void ADSBDemodGUI::on_getOSNDB_clicked(bool checked) { // Download Opensky network database to a file QUrl dbURL(QString(OSNDB_URL)); - connect(&m_dlm, &HttpDownloadManager::downloadComplete, this, &ADSBDemodGUI::downloadFinished); m_progressDialog = new QProgressDialog(this); m_progressDialog->setAttribute(Qt::WA_DeleteOnClose); m_progressDialog->setCancelButton(nullptr); @@ -1199,7 +1198,6 @@ void ADSBDemodGUI::on_getAirportDB_clicked(bool checked) { // Download Opensky network database to a file QUrl dbURL(QString(AIRPORTS_URL)); - connect(&m_dlm, &HttpDownloadManager::downloadComplete, this, &ADSBDemodGUI::downloadFinished); m_progressDialog = new QProgressDialog(this); m_progressDialog->setAttribute(Qt::WA_DeleteOnClose); m_progressDialog->setCancelButton(nullptr); @@ -1560,6 +1558,7 @@ ADSBDemodGUI::ADSBDemodGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, Baseb connect(this, SIGNAL(widgetRolled(QWidget*,bool)), this, SLOT(onWidgetRolled(QWidget*,bool))); connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onMenuDialogCalled(const QPoint &))); + connect(&m_dlm, &HttpDownloadManager::downloadComplete, this, &ADSBDemodGUI::downloadFinished); m_adsbDemod = reinterpret_cast(rxChannel); //new ADSBDemod(m_deviceUISet->m_deviceSourceAPI); m_adsbDemod->setMessageQueueToGUI(getInputMessageQueue()); diff --git a/plugins/channelrx/demodadsb/adsbdemodsinkworker.h b/plugins/channelrx/demodadsb/adsbdemodsinkworker.h index b0fa34f77..15b9bf04f 100644 --- a/plugins/channelrx/demodadsb/adsbdemodsinkworker.h +++ b/plugins/channelrx/demodadsb/adsbdemodsinkworker.h @@ -27,8 +27,8 @@ #include "adsbdemodstats.h" class ADSBDemodSink; -class ADSBDemodSettings; -class ADSBDemodStats; +struct ADSBDemodSettings; +struct ADSBDemodStats; class ADSBDemodSinkWorker : public QThread { Q_OBJECT @@ -60,7 +60,7 @@ public: ADSBDemodSinkWorker(ADSBDemodSink *sink) : m_sink(sink), m_demodStats(), - m_correlationThresholdLinear(0.02), + m_correlationThresholdLinear(0.02f), m_crc() { } diff --git a/plugins/channelrx/demodadsb/osndb.h b/plugins/channelrx/demodadsb/osndb.h index 6d9f474d8..263d475c0 100644 --- a/plugins/channelrx/demodadsb/osndb.h +++ b/plugins/channelrx/demodadsb/osndb.h @@ -63,7 +63,7 @@ struct AircraftInformation { int operatorICAOCol = 11; int registeredCol = 15; - qDebug() << "readOSNDB: " << filename; + qDebug() << "AircraftInformation::readOSNDB: " << filename; FILE *file; QByteArray utfFilename = filename.toUtf8(); @@ -106,19 +106,19 @@ struct AircraftInformation { int icao = 0; char *icaoString = NULL; char *registration = NULL; - int registrationLen = 0; + size_t registrationLen = 0; char *manufacturerName = NULL; - int manufacturerNameLen = 0; + size_t manufacturerNameLen = 0; char *model = NULL; - int modelLen = 0; + size_t modelLen = 0; char *owner = NULL; - int ownerLen = 0; + size_t ownerLen = 0; char *operatorName = NULL; - int operatorNameLen = 0; + size_t operatorNameLen = 0; char *operatorICAO = NULL; - int operatorICAOLen = 0; + size_t operatorICAOLen = 0; char *registered = NULL; - int registeredLen = 0; + size_t registeredLen = 0; p = strtok(row, ","); idx = 0; @@ -213,9 +213,9 @@ struct AircraftInformation { fclose(file); } else - qDebug() << "Failed to open " << filename; + qDebug() << "AircraftInformation::readOSNDB: Failed to open " << filename; - qDebug() << "readOSNDB - read " << cnt << " aircraft"; + qDebug() << "AircraftInformation::readOSNDB: Read " << cnt << " aircraft"; return aircraftInfo; } @@ -265,7 +265,7 @@ struct AircraftInformation { int cnt = 0; QHash *aircraftInfo = nullptr; - qDebug() << "AircraftInformation::readFastOSNDB: " << filename; + qDebug() << "AircraftInformation::readFastDB: " << filename; FILE *file; QByteArray utfFilename = filename.toUtf8(); diff --git a/plugins/channelrx/demodadsb/ourairportsdb.h b/plugins/channelrx/demodadsb/ourairportsdb.h index 8ae88a5ba..ee16810f1 100644 --- a/plugins/channelrx/demodadsb/ourairportsdb.h +++ b/plugins/channelrx/demodadsb/ourairportsdb.h @@ -25,6 +25,10 @@ #include #include +#include +#include + +#include "csv.h" #include "adsbdemodsettings.h" #define AIRPORTS_URL "https://ourairports.com/data/airports.csv" @@ -56,11 +60,11 @@ struct AirportInformation { } // Read OurAirport's airport CSV file + // See comments for readOSNDB static QHash *readAirportsDB(const QString &filename) { int cnt = 0; - QHash *airportInfo = new QHash(); - airportInfo->reserve(70000); + QHash *airportInfo = nullptr; // Column numbers used for the data as of 2020/10/28 int idCol = 0; @@ -73,71 +77,130 @@ struct AirportInformation { qDebug() << "AirportInformation::readAirportsDB: " << filename; - QFile file(filename); - if (file.open(QIODevice::ReadOnly)) + FILE *file; + QByteArray utfFilename = filename.toUtf8(); + if ((file = fopen(utfFilename.constData(), "r")) != NULL) { - QList colNames; + char row[2048]; int idx; - // Read header - if (!file.atEnd()) + if (fgets(row, sizeof(row), file)) { - QByteArray row = file.readLine().trimmed(); - colNames = row.split(','); - // Work out which columns the data is in, based on the headers - idx = colNames.indexOf("id"); - if (idx >= 0) - idCol = idx; - idx = colNames.indexOf("ident"); - if (idx >= 0) - identCol = idx; - idx = colNames.indexOf("type"); - if (idx >= 0) - typeCol = idx; - idx = colNames.indexOf("name"); - if (idx >= 0) - nameCol = idx; - idx = colNames.indexOf("latitude_deg"); - if (idx >= 0) - latitudeCol = idx; - idx = colNames.indexOf("longitude_deg"); - if (idx >= 0) - longitudeCol = idx; - idx = colNames.indexOf("elevation_ft"); - if (idx >= 0) - elevationCol = idx; - } - // Read data - while (!file.atEnd()) - { - QByteArray row = file.readLine(); - QList cols = row.split(','); + airportInfo = new QHash(); + airportInfo->reserve(70000); - bool ok = false; - int id = trimQuotes(cols[idCol]).toInt(&ok, 10); - if (ok) + // Read header + int idx = 0; + char *p = strtok(row, ","); + while (p != NULL) { - QString ident = trimQuotes(cols[identCol]); - QString type = trimQuotes(cols[typeCol]); - QString name = trimQuotes(cols[nameCol]); - float latitude = cols[latitudeCol].toFloat(); - float longitude = cols[longitudeCol].toFloat(); - float elevation = cols[elevationCol].toFloat(); + if (!strcmp(p, "id")) + idCol = idx; + else if (!strcmp(p, "ident")) + identCol = idx; + else if (!strcmp(p, "type")) + typeCol = idx; + else if (!strcmp(p, "name")) + nameCol = idx; + else if (!strcmp(p, "latitude_deg")) + latitudeCol = idx; + else if (!strcmp(p, "longitude_deg")) + longitudeCol = idx; + else if (!strcmp(p, "elevation_ft")) + elevationCol = idx; + p = strtok(NULL, ","); + idx++; + } + // Read data + while (fgets(row, sizeof(row), file)) + { + int id = 0; + char *idString = NULL; + char *ident = NULL; + size_t identLen = 0; + char *type = NULL; + size_t typeLen = 0; + char *name = NULL; + size_t nameLen = 0; + float latitude = 0.0f; + char *latitudeString = NULL; + size_t latitudeLen = 0; + float longitude = 0.0f; + char *longitudeString = NULL; + size_t longitudeLen = 0; + float elevation = 0.0f; + char *elevationString = NULL; + size_t elevationLen = 0; - if (type != "closed") + p = strtok(row, ","); + idx = 0; + while (p != NULL) + { + // Read strings, stripping quotes + if (idx == idCol) + { + idString = p; + idString[strlen(idString)] = '\0'; + id = strtol(idString, NULL, 10); + } + else if (idx == identCol) + { + ident = p+1; + identLen = strlen(ident)-1; + ident[identLen] = '\0'; + } + else if (idx == typeCol) + { + type = p+1; + typeLen = strlen(type)-1; + type[typeLen] = '\0'; + } + else if (idx == nameCol) + { + name = p+1; + nameLen = strlen(name)-1; + name[nameLen] = '\0'; + } + else if (idx == latitudeCol) + { + latitudeString = p; + latitudeLen = strlen(latitudeString)-1; + latitudeString[latitudeLen] = '\0'; + latitude = atof(latitudeString); + } + else if (idx == longitudeCol) + { + longitudeString = p; + longitudeLen = strlen(longitudeString)-1; + longitudeString[longitudeLen] = '\0'; + longitude = atof(longitudeString); + } + else if (idx == elevationCol) + { + elevationString = p; + elevationLen = strlen(elevationString)-1; + elevationString[elevationLen] = '\0'; + elevation = atof(elevationString); + } + p = strtok(NULL, ","); + idx++; + } + + // Only create the entry if we have some interesting data + if (((latitude != 0.0f) || (longitude != 0.0f)) && (type && strcmp(type, "closed"))) { AirportInformation *airport = new AirportInformation(); airport->m_id = id; - airport->m_ident = ident; - if (type == "small_airport") + airport->m_ident = QString(ident); + if (!strcmp(type, "small_airport")) airport->m_type = ADSBDemodSettings::AirportType::Small; - else if (type == "medium_airport") + else if (!strcmp(type, "medium_airport")) airport->m_type = ADSBDemodSettings::AirportType::Medium; - else if (type == "large_airport") + else if (!strcmp(type, "large_airport")) airport->m_type = ADSBDemodSettings::AirportType::Large; - else if (type == "heliport") + else if (!strcmp(type, "heliport")) airport->m_type = ADSBDemodSettings::AirportType::Heliport; - airport->m_name = name; + airport->m_name = QString(name); airport->m_latitude = latitude; airport->m_longitude = longitude; airport->m_elevation = elevation; @@ -146,12 +209,12 @@ struct AirportInformation { } } } - file.close(); + fclose(file); } else - qDebug() << "Failed to open " << filename << " " << file.errorString(); + qDebug() << "AirportInformation::readAirportsDB: Failed to open " << filename; - qDebug() << "AirportInformation::readAirportsDB: - read " << cnt << " airports"; + qDebug() << "AirportInformation::readAirportsDB: Read " << cnt << " airports"; return airportInfo; }