File: wsprnet.cpp

package info (click to toggle)
jtdx 2.2.159%2Bimproved-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 75,336 kB
  • sloc: cpp: 38,503; f90: 31,141; python: 27,061; ansic: 11,772; sh: 409; fortran: 353; makefile: 232
file content (232 lines) | stat: -rw-r--r-- 7,552 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Interface to WSPRnet website
//
// by Edson Pereira - PY2SDR

#include "wsprnet.h"

#include <cmath>

#include <QTimer>
#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QDebug>

#include "moc_wsprnet.cpp"

namespace
{
  char const * const wsprNetUrl = "http://wsprnet.org/post?";
  // char const * const wsprNetUrl = "http://127.0.0.1/post?";
};

WSPRNet::WSPRNet(QNetworkAccessManager * manager, QObject *parent)
  : QObject{parent}
  , networkManager {manager}
  , uploadTimer {new QTimer {this}}
  , m_urlQueueSize {0}
{
  connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkReply(QNetworkReply*)));
  connect( uploadTimer, SIGNAL(timeout()), this, SLOT(work()));
}

void WSPRNet::upload(QString const& call, QString const& grid, QString const& rfreq, QString const& tfreq,
                     QString const& mode, QString const& tpct, QString const& dbm, QString const& version,
                     QString const& fileName)
{
    m_call = call;
    m_grid = grid.left(6);
    m_rfreq = rfreq;
    m_tfreq = tfreq;
    m_mode = mode;
    m_tpct = tpct;
    m_dbm = dbm;
    m_vers = version;
    m_file = fileName;

    // Open the wsprd.out file
    QFile wsprdOutFile(fileName);
    if (!wsprdOutFile.open(QIODevice::ReadOnly | QIODevice::Text) ||
            wsprdOutFile.size() == 0) {
        urlQueue.enqueue( wsprNetUrl + urlEncodeNoSpot());
        m_uploadType = 1;
        uploadTimer->start(200);
        return;
    }

    // Read the contents
    while (!wsprdOutFile.atEnd()) {
      QHash<QString,QString> query;
      if ( decodeLine(wsprdOutFile.readLine(), query) ) {
        // Prevent reporting data ouside of the current frequency band
        float f = fabs(m_rfreq.toFloat() - query["tqrg"].toFloat());
        if (f < 0.0002) {
          urlQueue.enqueue( wsprNetUrl + urlEncodeSpot(query));
          m_uploadType = 2;
        }
      }
    }
    m_urlQueueSize = urlQueue.size();
    uploadTimer->start(200);
}

void WSPRNet::networkReply(QNetworkReply *reply)
{
  // check if request was ours
  if (m_outstandingRequests.removeOne (reply)) {
    if (QNetworkReply::NoError != reply->error ()) {
      Q_EMIT uploadStatus (QString {"Error: %1"}.arg (reply->error ()));
      // not clearing queue or halting queuing as it may be a transient
      // one off request error
    }
    else {
      QString serverResponse = reply->readAll();
      if( m_uploadType == 2) {
        if (!serverResponse.contains(QRegExp("spot\\(s\\) added"))) {
          emit uploadStatus(QString {"Upload Failed: %1"}.arg (serverResponse));
          urlQueue.clear();
          uploadTimer->stop();
        }
      }

      if (urlQueue.isEmpty()) {
        emit uploadStatus("done");
        QFile::remove(m_file);
        uploadTimer->stop();
      }
    }

    qDebug () << QString {"WSPRnet.org %1 outstanding requests"}.arg (m_outstandingRequests.size ());

    // delete request object instance on return to the event loop otherwise it is leaked
    reply->deleteLater ();
  }
}

bool WSPRNet::decodeLine(QString const& line, QHash<QString,QString> &query)
{
    // 130223 2256 7    -21 -0.3  14.097090  DU1MGA PK04 37          0    40    0
    // Date   Time Sync dBm  DT   Freq       Msg
    // 1      2    3     4   5     6         -------7------          8     9    10
    QRegExp rx("^(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+([+-]?\\d+)\\s+([+-]?\\d+\\.\\d+)\\s+(\\d+\\.\\d+)\\s+(.*)\\s+([+-]?\\d+)\\s+([+-]?\\d+)\\s+([+-]?\\d+)");
    if (rx.indexIn(line) != -1) {
        int msgType = 0;
        QString msg = rx.cap(7);
        msg.remove(QRegExp("\\s+$"));
        msg.remove(QRegExp("^\\s+"));
        QString call, grid, dbm;
        QRegExp msgRx;

        // Check for Message Type 1
        msgRx.setPattern("^([A-Z0-9]{3,6})\\s+([A-Z]{2}\\d{2})\\s+(\\d+)");
        if (msgRx.indexIn(msg) != -1) {
            msgType = 1;
            call = msgRx.cap(1);
            grid = msgRx.cap(2);
            dbm = msgRx.cap(3);
        }

        // Check for Message Type 2
        msgRx.setPattern("^([A-Z0-9/]+)\\s+(\\d+)");
        if (msgRx.indexIn(msg) != -1) {
            msgType = 2;
            call = msgRx.cap(1);
            grid = "";
            dbm = msgRx.cap(2);
        }

        // Check for Message Type 3
        msgRx.setPattern("^<([A-Z0-9/]+)>\\s+([A-Z]{2}\\d{2}[A-Z]{2})\\s+(\\d+)");
        if (msgRx.indexIn(msg) != -1) {
            msgType = 3;
            call = msgRx.cap(1);
            grid = msgRx.cap(2);
            dbm = msgRx.cap(3);
        }

        // Unknown message format
        if (!msgType) {
            return false;
        }

        query["function"] = "wspr";
        query["date"] = rx.cap(1);
        query["time"] = rx.cap(2);
        query["sig"] = rx.cap(4);
        query["dt"] = rx.cap(5);
        query["drift"] = rx.cap(8);
        query["tqrg"] = rx.cap(6);
        query["tcall"] = call;
        query["tgrid"] = grid;
        query["dbm"] = dbm;
    } else {
        return false;
    }
    return true;
}

QString WSPRNet::urlEncodeNoSpot()
{
    QString queryString;
    queryString += "function=wsprstat&";
    queryString += "rcall=" + m_call + "&";
    queryString += "rgrid=" + m_grid + "&";
    queryString += "rqrg=" + m_rfreq + "&";
    queryString += "tpct=" + m_tpct + "&";
    queryString += "tqrg=" + m_tfreq + "&";
    queryString += "dbm=" + m_dbm + "&";
    queryString += "version=" +  m_vers;
    if(m_mode=="WSPR-2") queryString += "&mode=2";
    if(m_mode=="WSPR-15") queryString += "&mode=15";
    return queryString;;
}

QString WSPRNet::urlEncodeSpot(QHash<QString,QString> const& query)
{
    QString queryString;
    queryString += "function=" + query["function"] + "&";
    queryString += "rcall=" + m_call + "&";
    queryString += "rgrid=" + m_grid + "&";
    queryString += "rqrg=" + m_rfreq + "&";
    queryString += "date=" + query["date"] + "&";
    queryString += "time=" + query["time"] + "&";
    queryString += "sig=" + query["sig"] + "&";
    queryString += "dt=" + query["dt"] + "&";
    queryString += "drift=" + query["drift"] + "&";
    queryString += "tqrg=" + query["tqrg"] + "&";
    queryString += "tcall=" + query["tcall"] + "&";
    queryString += "tgrid=" + query["tgrid"] + "&";
    queryString += "dbm=" + query["dbm"] + "&";
    queryString += "version=" + m_vers;
    if(m_mode=="WSPR-2") queryString += "&mode=2";
    if(m_mode=="WSPR-15") queryString += "&mode=15";
    return queryString;
}

void WSPRNet::work()
{
  if (!urlQueue.isEmpty()) {
#if QT_VERSION < QT_VERSION_CHECK (5, 15, 0)
      if (QNetworkAccessManager::Accessible != networkManager->networkAccessible ()) {
        // try and recover network access for QNAM
        networkManager->setNetworkAccessible (QNetworkAccessManager::Accessible);
      }
#endif
    QUrl url(urlQueue.dequeue());
    QNetworkRequest request(url);
    m_outstandingRequests << networkManager->get(request);
    emit uploadStatus(QString {"Uploading Spot %1/%2"}.arg (m_urlQueueSize - urlQueue.size()).arg (m_urlQueueSize));
  } else {
    uploadTimer->stop();
  }
}

void WSPRNet::abortOutstandingRequests () {
  urlQueue.clear ();
  for (auto& request : m_outstandingRequests) {
    request->abort ();
  }
  m_urlQueueSize = 0;
}