[280] | 1 | // Part of BNC, a utility for retrieving decoding and
|
---|
[464] | 2 | // converting GNSS data streams from NTRIP broadcasters.
|
---|
[280] | 3 | //
|
---|
[464] | 4 | // Copyright (C) 2007
|
---|
[280] | 5 | // German Federal Agency for Cartography and Geodesy (BKG)
|
---|
| 6 | // http://www.bkg.bund.de
|
---|
[464] | 7 | // Czech Technical University Prague, Department of Geodesy
|
---|
[280] | 8 | // http://www.fsv.cvut.cz
|
---|
| 9 | //
|
---|
| 10 | // Email: euref-ip@bkg.bund.de
|
---|
| 11 | //
|
---|
| 12 | // This program is free software; you can redistribute it and/or
|
---|
| 13 | // modify it under the terms of the GNU General Public License
|
---|
| 14 | // as published by the Free Software Foundation, version 2.
|
---|
| 15 | //
|
---|
| 16 | // This program is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU General Public License
|
---|
| 22 | // along with this program; if not, write to the Free Software
|
---|
| 23 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
[35] | 24 |
|
---|
| 25 | /* -------------------------------------------------------------------------
|
---|
[93] | 26 | * BKG NTRIP Client
|
---|
[35] | 27 | * -------------------------------------------------------------------------
|
---|
| 28 | *
|
---|
| 29 | * Class: bncGetThread
|
---|
| 30 | *
|
---|
| 31 | * Purpose: Thread that retrieves data from NTRIP caster
|
---|
| 32 | *
|
---|
| 33 | * Author: L. Mervart
|
---|
| 34 | *
|
---|
| 35 | * Created: 24-Dec-2005
|
---|
| 36 | *
|
---|
| 37 | * Changes:
|
---|
| 38 | *
|
---|
| 39 | * -----------------------------------------------------------------------*/
|
---|
| 40 |
|
---|
[277] | 41 | #include <stdlib.h>
|
---|
| 42 |
|
---|
[35] | 43 | #include <QFile>
|
---|
| 44 | #include <QTextStream>
|
---|
| 45 | #include <QtNetwork>
|
---|
[356] | 46 | #include <QTime>
|
---|
[35] | 47 |
|
---|
| 48 | #include "bncgetthread.h"
|
---|
[192] | 49 | #include "bnctabledlg.h"
|
---|
[243] | 50 | #include "bncapp.h"
|
---|
[352] | 51 | #include "bncutils.h"
|
---|
[408] | 52 | #include "bncrinex.h"
|
---|
[423] | 53 | #include "bnczerodecoder.h"
|
---|
[65] | 54 |
|
---|
[243] | 55 | #include "RTCM/RTCM2Decoder.h"
|
---|
[297] | 56 | #include "RTCM3/RTCM3Decoder.h"
|
---|
[867] | 57 | #include "RTCM3/RTCM3coDecoder.h"
|
---|
[293] | 58 | #include "RTIGS/RTIGSDecoder.h"
|
---|
[35] | 59 |
|
---|
| 60 | using namespace std;
|
---|
| 61 |
|
---|
| 62 | // Constructor
|
---|
| 63 | ////////////////////////////////////////////////////////////////////////////
|
---|
[278] | 64 | bncGetThread::bncGetThread(const QUrl& mountPoint,
|
---|
[366] | 65 | const QByteArray& format,
|
---|
| 66 | const QByteArray& latitude,
|
---|
| 67 | const QByteArray& longitude,
|
---|
| 68 | const QByteArray& nmea, int iMount) {
|
---|
[605] | 69 |
|
---|
| 70 | setTerminationEnabled(true);
|
---|
| 71 |
|
---|
[350] | 72 | _decoder = 0;
|
---|
| 73 | _mountPoint = mountPoint;
|
---|
| 74 | _staID = mountPoint.path().mid(1).toAscii();
|
---|
| 75 | _staID_orig = _staID;
|
---|
| 76 | _format = format;
|
---|
[366] | 77 | _latitude = latitude;
|
---|
| 78 | _longitude = longitude;
|
---|
| 79 | _nmea = nmea;
|
---|
[350] | 80 | _socket = 0;
|
---|
| 81 | _timeOut = 20*1000; // 20 seconds
|
---|
| 82 | _nextSleep = 1; // 1 second
|
---|
| 83 | _iMount = iMount; // index in mountpoints array
|
---|
[255] | 84 |
|
---|
| 85 | // Check name conflict
|
---|
| 86 | // -------------------
|
---|
| 87 | QSettings settings;
|
---|
| 88 | QListIterator<QString> it(settings.value("mountPoints").toStringList());
|
---|
| 89 | int num = 0;
|
---|
[278] | 90 | int ind = -1;
|
---|
[255] | 91 | while (it.hasNext()) {
|
---|
[278] | 92 | ++ind;
|
---|
[255] | 93 | QStringList hlp = it.next().split(" ");
|
---|
| 94 | if (hlp.size() <= 1) continue;
|
---|
| 95 | QUrl url(hlp[0]);
|
---|
| 96 | if (_mountPoint.path() == url.path()) {
|
---|
[278] | 97 | if (_iMount > ind) {
|
---|
| 98 | ++num;
|
---|
[255] | 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
[278] | 102 |
|
---|
| 103 | if (num > 0) {
|
---|
| 104 | _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
|
---|
[255] | 105 | }
|
---|
[408] | 106 |
|
---|
[658] | 107 | // Notice threshold
|
---|
| 108 | // ----------------
|
---|
[686] | 109 | _inspSegm = 50;
|
---|
| 110 | if ( settings.value("obsRate").toString().isEmpty() ) { _inspSegm = 0; }
|
---|
[692] | 111 | if ( settings.value("obsRate").toString().indexOf("5 Hz") != -1 ) { _inspSegm = 2; }
|
---|
[686] | 112 | if ( settings.value("obsRate").toString().indexOf("1 Hz") != -1 ) { _inspSegm = 10; }
|
---|
| 113 | if ( settings.value("obsRate").toString().indexOf("0.5 Hz") != -1 ) { _inspSegm = 20; }
|
---|
| 114 | if ( settings.value("obsRate").toString().indexOf("0.2 Hz") != -1 ) { _inspSegm = 40; }
|
---|
| 115 | if ( settings.value("obsRate").toString().indexOf("0.1 Hz") != -1 ) { _inspSegm = 50; }
|
---|
[668] | 116 | _adviseFail = settings.value("adviseFail").toInt();
|
---|
| 117 | _adviseReco = settings.value("adviseReco").toInt();
|
---|
[722] | 118 | _makePause = false;
|
---|
| 119 | if ( Qt::CheckState(settings.value("makePause").toInt()) == Qt::Checked) {_makePause = true; }
|
---|
[668] | 120 | _adviseScript = settings.value("adviseScript").toString();
|
---|
| 121 | expandEnvVar(_adviseScript);
|
---|
[658] | 122 |
|
---|
[709] | 123 | // Latency interval/average
|
---|
| 124 | // ------------------------
|
---|
[728] | 125 | _perfIntr = 86400;
|
---|
| 126 | if ( settings.value("perfIntr").toString().isEmpty() ) { _perfIntr = 0; }
|
---|
| 127 | if ( settings.value("perfIntr").toString().indexOf("1 min") != -1 ) { _perfIntr = 60; }
|
---|
| 128 | if ( settings.value("perfIntr").toString().indexOf("5 min") != -1 ) { _perfIntr = 300; }
|
---|
| 129 | if ( settings.value("perfIntr").toString().indexOf("15 min") != -1 ) { _perfIntr = 900; }
|
---|
| 130 | if ( settings.value("perfIntr").toString().indexOf("1 hour") != -1 ) { _perfIntr = 3600; }
|
---|
| 131 | if ( settings.value("perfIntr").toString().indexOf("6 hours") != -1 ) { _perfIntr = 21600; }
|
---|
| 132 | if ( settings.value("perfIntr").toString().indexOf("1 day") != -1 ) { _perfIntr = 86400; }
|
---|
[709] | 133 |
|
---|
[408] | 134 | // RINEX writer
|
---|
| 135 | // ------------
|
---|
| 136 | _samplingRate = settings.value("rnxSampl").toInt();
|
---|
| 137 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
---|
| 138 | _rnx = 0;
|
---|
| 139 | }
|
---|
| 140 | else {
|
---|
| 141 | _rnx = new bncRinex(_staID, mountPoint, format, latitude, longitude, nmea);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[319] | 144 | msleep(100); //sleep 0.1 sec
|
---|
[35] | 145 | }
|
---|
| 146 |
|
---|
| 147 | // Destructor
|
---|
| 148 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 149 | bncGetThread::~bncGetThread() {
|
---|
[515] | 150 | if (_socket) {
|
---|
| 151 | _socket->close();
|
---|
[613] | 152 | #if QT_VERSION == 0x040203
|
---|
| 153 | delete _socket;
|
---|
| 154 | #else
|
---|
[612] | 155 | _socket->deleteLater();
|
---|
[613] | 156 | #endif
|
---|
[515] | 157 | }
|
---|
[617] | 158 | delete _decoder;
|
---|
[606] | 159 | delete _rnx;
|
---|
[35] | 160 | }
|
---|
| 161 |
|
---|
[841] | 162 | #define AGENTVERSION "1.6"
|
---|
[35] | 163 | // Connect to Caster, send the Request (static)
|
---|
| 164 | ////////////////////////////////////////////////////////////////////////////
|
---|
[366] | 165 | QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
|
---|
| 166 | QByteArray& latitude, QByteArray& longitude,
|
---|
| 167 | QByteArray& nmea, int timeOut,
|
---|
[136] | 168 | QString& msg) {
|
---|
[35] | 169 |
|
---|
[88] | 170 | // Connect the Socket
|
---|
| 171 | // ------------------
|
---|
| 172 | QSettings settings;
|
---|
| 173 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 174 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 175 |
|
---|
[35] | 176 | QTcpSocket* socket = new QTcpSocket();
|
---|
| 177 | if ( proxyHost.isEmpty() ) {
|
---|
[88] | 178 | socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
[35] | 179 | }
|
---|
| 180 | else {
|
---|
| 181 | socket->connectToHost(proxyHost, proxyPort);
|
---|
| 182 | }
|
---|
| 183 | if (!socket->waitForConnected(timeOut)) {
|
---|
[82] | 184 | msg += "Connect timeout\n";
|
---|
[35] | 185 | delete socket;
|
---|
| 186 | return 0;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | // Send Request
|
---|
| 190 | // ------------
|
---|
[443] | 191 | QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
|
---|
| 192 | QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
|
---|
[645] | 193 | QByteArray userAndPwd;
|
---|
[92] | 194 |
|
---|
[645] | 195 | if(!uName.isEmpty() || !passW.isEmpty())
|
---|
| 196 | {
|
---|
| 197 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
| 198 | passW.toAscii()).toBase64() + "\r\n";
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[92] | 201 | QUrl hlp;
|
---|
| 202 | hlp.setScheme("http");
|
---|
| 203 | hlp.setHost(mountPoint.host());
|
---|
| 204 | hlp.setPort(mountPoint.port());
|
---|
| 205 | hlp.setPath(mountPoint.path());
|
---|
| 206 |
|
---|
[214] | 207 | QByteArray reqStr;
|
---|
| 208 | if ( proxyHost.isEmpty() ) {
|
---|
| 209 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
---|
[645] | 210 | reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n";
|
---|
[214] | 211 | } else {
|
---|
[645] | 212 | reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n";
|
---|
[205] | 213 | }
|
---|
[645] | 214 | reqStr += "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
|
---|
| 215 | "Host: " + hlp.host().toAscii() + "\r\n"
|
---|
| 216 | + userAndPwd + "\r\n";
|
---|
[205] | 217 |
|
---|
[464] | 218 | // NMEA string to handle VRS stream
|
---|
| 219 | // --------------------------------
|
---|
[356] | 220 |
|
---|
| 221 | double lat, lon;
|
---|
[366] | 222 |
|
---|
| 223 | lat = strtod(latitude,NULL);
|
---|
| 224 | lon = strtod(longitude,NULL);
|
---|
| 225 |
|
---|
[410] | 226 | if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
[356] | 227 | const char* flagN="N";
|
---|
| 228 | const char* flagE="E";
|
---|
| 229 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
| 230 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
| 231 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
| 232 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
[566] | 233 | QTime ttime(QDateTime::currentDateTime().toUTC().time());
|
---|
[356] | 234 | int lat_deg = (int)lat;
|
---|
| 235 | double lat_min=(lat-lat_deg)*60.;
|
---|
| 236 | int lon_deg = (int)lon;
|
---|
| 237 | double lon_min=(lon-lon_deg)*60.;
|
---|
| 238 | int hh = 0 , mm = 0;
|
---|
| 239 | double ss = 0.0;
|
---|
| 240 | hh=ttime.hour();
|
---|
| 241 | mm=ttime.minute();
|
---|
| 242 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
| 243 | QString gga;
|
---|
| 244 | gga += "GPGGA,";
|
---|
| 245 | gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
|
---|
| 246 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 247 | gga += flagN;
|
---|
| 248 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 249 | gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
|
---|
| 250 | int xori;
|
---|
| 251 | char XOR = 0;
|
---|
| 252 | char *Buff =gga.toAscii().data();
|
---|
| 253 | int iLen = strlen(Buff);
|
---|
| 254 | for (xori = 0; xori < iLen; xori++) {
|
---|
| 255 | XOR ^= (char)Buff[xori];
|
---|
| 256 | }
|
---|
| 257 | gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
| 258 | reqStr += "$";
|
---|
| 259 | reqStr += gga;
|
---|
| 260 | reqStr += "\r\n";
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[82] | 263 | msg += reqStr;
|
---|
[35] | 264 |
|
---|
| 265 | socket->write(reqStr, reqStr.length());
|
---|
| 266 |
|
---|
| 267 | if (!socket->waitForBytesWritten(timeOut)) {
|
---|
[82] | 268 | msg += "Write timeout\n";
|
---|
[35] | 269 | delete socket;
|
---|
| 270 | return 0;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | return socket;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[136] | 276 | // Init Run
|
---|
[35] | 277 | ////////////////////////////////////////////////////////////////////////////
|
---|
[138] | 278 | t_irc bncGetThread::initRun() {
|
---|
[35] | 279 |
|
---|
[515] | 280 | // Initialize Socket
|
---|
| 281 | // -----------------
|
---|
| 282 | QString msg;
|
---|
[602] | 283 | _socket = this->request(_mountPoint, _latitude, _longitude,
|
---|
| 284 | _nmea, _timeOut, msg);
|
---|
[35] | 285 | if (!_socket) {
|
---|
[138] | 286 | return failure;
|
---|
[35] | 287 | }
|
---|
| 288 |
|
---|
| 289 | // Read Caster Response
|
---|
| 290 | // --------------------
|
---|
[136] | 291 | _socket->waitForReadyRead(_timeOut);
|
---|
[35] | 292 | if (_socket->canReadLine()) {
|
---|
| 293 | QString line = _socket->readLine();
|
---|
[473] | 294 |
|
---|
| 295 | // Skip messages from proxy server
|
---|
| 296 | // -------------------------------
|
---|
| 297 | if (line.indexOf("ICY 200 OK") == -1 &&
|
---|
| 298 | line.indexOf("200 OK") != -1 ) {
|
---|
| 299 | bool proxyRespond = true;
|
---|
| 300 | while (true) {
|
---|
| 301 | if (_socket->canReadLine()) {
|
---|
| 302 | line = _socket->readLine();
|
---|
| 303 | if (!proxyRespond) {
|
---|
| 304 | break;
|
---|
| 305 | }
|
---|
| 306 | if (line.trimmed().isEmpty()) {
|
---|
| 307 | proxyRespond = false;
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | else {
|
---|
| 311 | _socket->waitForReadyRead(_timeOut);
|
---|
[474] | 312 | if (_socket->bytesAvailable() <= 0) {
|
---|
| 313 | break;
|
---|
| 314 | }
|
---|
[473] | 315 | }
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[146] | 319 | if (line.indexOf("Unauthorized") != -1) {
|
---|
[192] | 320 | QStringList table;
|
---|
| 321 | bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
|
---|
| 322 | QString net;
|
---|
| 323 | QStringListIterator it(table);
|
---|
| 324 | while (it.hasNext()) {
|
---|
| 325 | QString line = it.next();
|
---|
| 326 | if (line.indexOf("STR") == 0) {
|
---|
| 327 | QStringList tags = line.split(";");
|
---|
[255] | 328 | if (tags.at(1) == _staID_orig) {
|
---|
[192] | 329 | net = tags.at(7);
|
---|
| 330 | break;
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | QString reg;
|
---|
| 336 | it.toFront();
|
---|
| 337 | while (it.hasNext()) {
|
---|
| 338 | QString line = it.next();
|
---|
| 339 | if (line.indexOf("NET") == 0) {
|
---|
| 340 | QStringList tags = line.split(";");
|
---|
| 341 | if (tags.at(1) == net) {
|
---|
| 342 | reg = tags.at(7);
|
---|
| 343 | break;
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
[194] | 347 | emit(newMessage((_staID + ": Caster Response: " + line +
|
---|
[200] | 348 | " Adjust User-ID and Password Register, see"
|
---|
[194] | 349 | "\n " + reg).toAscii()));
|
---|
[192] | 350 | return fatal;
|
---|
[146] | 351 | }
|
---|
[35] | 352 | if (line.indexOf("ICY 200 OK") != 0) {
|
---|
[190] | 353 | emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
|
---|
[144] | 354 | return failure;
|
---|
[35] | 355 | }
|
---|
| 356 | }
|
---|
| 357 | else {
|
---|
[190] | 358 | emit(newMessage(_staID + ": Response Timeout"));
|
---|
[138] | 359 | return failure;
|
---|
[35] | 360 | }
|
---|
| 361 |
|
---|
| 362 | // Instantiate the filter
|
---|
| 363 | // ----------------------
|
---|
[423] | 364 | if (!_decoder) {
|
---|
[136] | 365 | if (_format.indexOf("RTCM_2") != -1) {
|
---|
| 366 | emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
|
---|
[243] | 367 | _decoder = new RTCM2Decoder();
|
---|
[136] | 368 | }
|
---|
| 369 | else if (_format.indexOf("RTCM_3") != -1) {
|
---|
[569] | 370 | emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
|
---|
[511] | 371 | _decoder = new RTCM3Decoder();
|
---|
[136] | 372 | }
|
---|
| 373 | else if (_format.indexOf("RTIGS") != -1) {
|
---|
| 374 | emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
|
---|
[293] | 375 | _decoder = new RTIGSDecoder();
|
---|
[136] | 376 | }
|
---|
[867] | 377 | else if (_format.indexOf("SP3") != -1) {
|
---|
| 378 | emit(newMessage("Get Corrections: " + _staID + " in RTCM 3.x format"));
|
---|
[875] | 379 | _decoder = new RTCM3coDecoder(_staID);
|
---|
[867] | 380 | }
|
---|
| 381 | else if (_format.indexOf("ZERO") != -1) {
|
---|
[428] | 382 | emit(newMessage("Get Data: " + _staID + " in original format"));
|
---|
[424] | 383 | _decoder = new bncZeroDecoder(_staID);
|
---|
[406] | 384 | }
|
---|
[136] | 385 | else {
|
---|
[190] | 386 | emit(newMessage(_staID + ": Unknown data format " + _format));
|
---|
[250] | 387 | return fatal;
|
---|
[136] | 388 | }
|
---|
[60] | 389 | }
|
---|
[138] | 390 | return success;
|
---|
[136] | 391 | }
|
---|
[59] | 392 |
|
---|
[136] | 393 | // Run
|
---|
| 394 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 395 | void bncGetThread::run() {
|
---|
| 396 |
|
---|
[709] | 397 | const double maxDt = 600.0; // Check observation epoch
|
---|
[699] | 398 | bool wrongEpoch = false;
|
---|
| 399 | bool decode = true;
|
---|
| 400 | int numSucc = 0;
|
---|
| 401 | int secSucc = 0;
|
---|
| 402 | int secFail = 0;
|
---|
[709] | 403 | int initPause = 30; // Initial pause for corrupted streams
|
---|
[699] | 404 | int currPause = 0;
|
---|
| 405 | bool begCorrupt = false;
|
---|
| 406 | bool endCorrupt = false;
|
---|
[728] | 407 | bool followSec = false;
|
---|
[717] | 408 | int oldSecGPS= 0;
|
---|
| 409 | int newSecGPS = 0;
|
---|
[728] | 410 | int numGaps = 0;
|
---|
| 411 | int diffSecGPS = 0;
|
---|
[709] | 412 | int numLat = 0;
|
---|
| 413 | double sumLat = 0.;
|
---|
[728] | 414 | double meanDiff = 0.;
|
---|
[709] | 415 | double minLat = maxDt;
|
---|
| 416 | double maxLat = -maxDt;
|
---|
| 417 | double curLat = 0.;
|
---|
| 418 | double leapsec = 14.; // Leap second for latency estimation
|
---|
[699] | 419 |
|
---|
| 420 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 421 | _decodeSucc = QDateTime::currentDateTime();
|
---|
[229] | 422 | t_irc irc = initRun();
|
---|
| 423 |
|
---|
| 424 | if (irc == fatal) {
|
---|
[192] | 425 | QThread::exit(1);
|
---|
| 426 | return;
|
---|
| 427 | }
|
---|
[229] | 428 | else if (irc != success) {
|
---|
[190] | 429 | emit(newMessage(_staID + ": initRun failed, reconnecting"));
|
---|
[138] | 430 | tryReconnect();
|
---|
| 431 | }
|
---|
[136] | 432 |
|
---|
[658] | 433 | if (initPause < _inspSegm) {
|
---|
| 434 | initPause = _inspSegm;
|
---|
| 435 | }
|
---|
[727] | 436 | if(!_makePause) {initPause = 0;}
|
---|
[658] | 437 | currPause = initPause;
|
---|
| 438 |
|
---|
[35] | 439 | // Read Incoming Data
|
---|
| 440 | // ------------------
|
---|
| 441 | while (true) {
|
---|
[629] | 442 | try {
|
---|
| 443 | if (_socket->state() != QAbstractSocket::ConnectedState) {
|
---|
| 444 | emit(newMessage(_staID + ": Socket not connected, reconnecting"));
|
---|
| 445 | tryReconnect();
|
---|
| 446 | }
|
---|
[621] | 447 |
|
---|
| 448 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 449 | while (it.hasNext()) {
|
---|
| 450 | delete it.next();
|
---|
| 451 | }
|
---|
| 452 | _decoder->_obsList.clear();
|
---|
| 453 |
|
---|
[249] | 454 | _socket->waitForReadyRead(_timeOut);
|
---|
| 455 | qint64 nBytes = _socket->bytesAvailable();
|
---|
| 456 | if (nBytes > 0) {
|
---|
[567] | 457 | emit newBytes(_staID, nBytes);
|
---|
| 458 |
|
---|
[249] | 459 | char* data = new char[nBytes];
|
---|
| 460 | _socket->read(data, nBytes);
|
---|
[406] | 461 |
|
---|
[658] | 462 | if (_inspSegm<1) {
|
---|
| 463 | _decoder->Decode(data, nBytes);
|
---|
| 464 | }
|
---|
| 465 | else {
|
---|
| 466 |
|
---|
| 467 | // Decode data
|
---|
| 468 | // -----------
|
---|
| 469 | if (!_decodePause.isValid() ||
|
---|
[727] | 470 | _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
|
---|
[658] | 471 |
|
---|
| 472 | if (decode) {
|
---|
| 473 | if ( _decoder->Decode(data, nBytes) == success ) {
|
---|
| 474 | numSucc += 1;
|
---|
| 475 | }
|
---|
| 476 | if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
|
---|
| 477 | decode = false;
|
---|
| 478 | }
|
---|
[652] | 479 | }
|
---|
[658] | 480 |
|
---|
| 481 | // Check - once per inspect segment
|
---|
| 482 | // --------------------------------
|
---|
| 483 | if (!decode) {
|
---|
| 484 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 485 | if (numSucc>0) {
|
---|
| 486 | secSucc += _inspSegm;
|
---|
[699] | 487 | _decodeSucc = QDateTime::currentDateTime();
|
---|
[668] | 488 | if (secSucc > _adviseReco * 60) {
|
---|
| 489 | secSucc = _adviseReco * 60 + 1;
|
---|
[658] | 490 | }
|
---|
| 491 | numSucc = 0;
|
---|
| 492 | currPause = initPause;
|
---|
| 493 | _decodePause.setDate(QDate());
|
---|
| 494 | _decodePause.setTime(QTime());
|
---|
| 495 | }
|
---|
| 496 | else {
|
---|
| 497 | secFail += _inspSegm;
|
---|
| 498 | secSucc = 0;
|
---|
[668] | 499 | if (secFail > _adviseFail * 60) {
|
---|
| 500 | secFail = _adviseFail * 60 + 1;
|
---|
[658] | 501 | }
|
---|
[727] | 502 | if (!_decodePause.isValid() || !_makePause) {
|
---|
[658] | 503 | _decodePause = QDateTime::currentDateTime();
|
---|
| 504 | }
|
---|
| 505 | else {
|
---|
| 506 | _decodePause.setDate(QDate());
|
---|
| 507 | _decodePause.setTime(QTime());
|
---|
| 508 | secFail = secFail + currPause - _inspSegm;
|
---|
| 509 | currPause = currPause * 2;
|
---|
| 510 | if (currPause > 960) {
|
---|
| 511 | currPause = 960;
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | // End corrupt threshold
|
---|
| 517 | // ---------------------
|
---|
[668] | 518 | if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
|
---|
[699] | 519 | _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 520 | _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
[704] | 521 | emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
|
---|
[696] | 522 | callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
[658] | 523 | endCorrupt = true;
|
---|
| 524 | begCorrupt = false;
|
---|
| 525 | secFail = 0;
|
---|
| 526 | }
|
---|
| 527 | else {
|
---|
| 528 |
|
---|
| 529 | // Begin corrupt threshold
|
---|
| 530 | // -----------------------
|
---|
[668] | 531 | if ( !begCorrupt && secFail > _adviseFail * 60 ) {
|
---|
[699] | 532 | _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
|
---|
| 533 | _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 534 | emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
|
---|
[696] | 535 | callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
[658] | 536 | begCorrupt = true;
|
---|
| 537 | endCorrupt = false;
|
---|
| 538 | secSucc = 0;
|
---|
| 539 | numSucc = 0;
|
---|
| 540 | }
|
---|
| 541 | }
|
---|
| 542 | decode = true;
|
---|
[650] | 543 | }
|
---|
| 544 | }
|
---|
[658] | 545 | }
|
---|
[650] | 546 |
|
---|
[658] | 547 | // End outage threshold
|
---|
| 548 | // --------------------
|
---|
[668] | 549 | if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
|
---|
[658] | 550 | _decodeStart.setDate(QDate());
|
---|
| 551 | _decodeStart.setTime(QTime());
|
---|
[686] | 552 | if (_inspSegm>0) {
|
---|
[699] | 553 | _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 554 | _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
[704] | 555 | emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
|
---|
[696] | 556 | callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
[686] | 557 | }
|
---|
[658] | 558 | }
|
---|
| 559 |
|
---|
[331] | 560 | delete [] data;
|
---|
[423] | 561 |
|
---|
[621] | 562 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 563 | while (it.hasNext()) {
|
---|
| 564 | p_obs obs = it.next();
|
---|
| 565 |
|
---|
[351] | 566 | // Check observation epoch
|
---|
| 567 | // -----------------------
|
---|
[715] | 568 | int week;
|
---|
| 569 | double sec;
|
---|
| 570 | currentGPSWeeks(week, sec);
|
---|
[351] | 571 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
| 572 |
|
---|
[622] | 573 | if (week < obs->_o.GPSWeek) {
|
---|
[351] | 574 | week += 1;
|
---|
| 575 | sec -= secPerWeek;
|
---|
| 576 | }
|
---|
[622] | 577 | if (week > obs->_o.GPSWeek) {
|
---|
[351] | 578 | week -= 1;
|
---|
| 579 | sec += secPerWeek;
|
---|
| 580 | }
|
---|
[622] | 581 | double dt = fabs(sec - obs->_o.GPSWeeks);
|
---|
| 582 | if (week != obs->_o.GPSWeek || dt > maxDt) {
|
---|
[658] | 583 | if (!wrongEpoch) {
|
---|
[727] | 584 | emit( newMessage(_staID + ": Wrong observation epoch(s)") );
|
---|
[658] | 585 | wrongEpoch = true;
|
---|
| 586 | }
|
---|
[621] | 587 | delete obs;
|
---|
[351] | 588 | continue;
|
---|
| 589 | }
|
---|
[658] | 590 | else {
|
---|
| 591 | wrongEpoch = false;
|
---|
[709] | 592 |
|
---|
[728] | 593 | // Latency and completeness
|
---|
| 594 | // ------------------------
|
---|
| 595 | if (_perfIntr>0) {
|
---|
[717] | 596 | newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
|
---|
| 597 | if (newSecGPS != oldSecGPS) {
|
---|
[728] | 598 | if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
|
---|
[709] | 599 | if (numLat>0) {
|
---|
[728] | 600 | if (meanDiff>0.) {
|
---|
| 601 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs, %6 gaps")
|
---|
| 602 | .arg(_staID.data())
|
---|
| 603 | .arg(int(sumLat/numLat*100)/100.)
|
---|
| 604 | .arg(int(minLat*100)/100.)
|
---|
| 605 | .arg(int(maxLat*100)/100.)
|
---|
| 606 | .arg(numLat)
|
---|
| 607 | .arg(numGaps)
|
---|
| 608 | .toAscii()) );
|
---|
| 609 | } else {
|
---|
| 610 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs")
|
---|
| 611 | .arg(_staID.data())
|
---|
| 612 | .arg(int(sumLat/numLat*100)/100.)
|
---|
| 613 | .arg(int(minLat*100)/100.)
|
---|
| 614 | .arg(int(maxLat*100)/100.)
|
---|
| 615 | .arg(numLat)
|
---|
| 616 | .toAscii()) );
|
---|
| 617 | }
|
---|
[709] | 618 | }
|
---|
[728] | 619 | meanDiff = diffSecGPS/numLat;
|
---|
| 620 | diffSecGPS = 0;
|
---|
| 621 | numGaps = 0;
|
---|
[709] | 622 | sumLat = 0.;
|
---|
| 623 | numLat = 0;
|
---|
| 624 | minLat = maxDt;
|
---|
| 625 | maxLat = -maxDt;
|
---|
| 626 | }
|
---|
[728] | 627 | if (followSec) {
|
---|
| 628 | diffSecGPS += newSecGPS - oldSecGPS;
|
---|
| 629 | if (meanDiff>0.) {
|
---|
| 630 | if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
|
---|
| 631 | numGaps += 1;
|
---|
| 632 | }
|
---|
| 633 | }
|
---|
| 634 | }
|
---|
[709] | 635 | curLat = sec - obs->_o.GPSWeeks + leapsec;
|
---|
| 636 | sumLat += curLat;
|
---|
| 637 | if (curLat < minLat) minLat = curLat;
|
---|
| 638 | if (curLat >= maxLat) maxLat = curLat;
|
---|
| 639 | numLat += 1;
|
---|
[717] | 640 | oldSecGPS = newSecGPS;
|
---|
[728] | 641 | followSec = true;
|
---|
[709] | 642 | }
|
---|
| 643 | }
|
---|
[658] | 644 | }
|
---|
[351] | 645 |
|
---|
[408] | 646 | // RINEX Output
|
---|
| 647 | // ------------
|
---|
| 648 | if (_rnx) {
|
---|
[622] | 649 | long iSec = long(floor(obs->_o.GPSWeeks+0.5));
|
---|
| 650 | long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
|
---|
[408] | 651 | if (_samplingRate == 0 || iSec % _samplingRate == 0) {
|
---|
[621] | 652 | _rnx->deepCopy(obs);
|
---|
[408] | 653 | }
|
---|
| 654 | _rnx->dumpEpoch(newTime);
|
---|
| 655 | }
|
---|
| 656 |
|
---|
[621] | 657 | bool firstObs = (obs == _decoder->_obsList.first());
|
---|
[624] | 658 | obs->_status = t_obs::posted;
|
---|
[621] | 659 | emit newObs(_staID, firstObs, obs);
|
---|
[249] | 660 | }
|
---|
| 661 | _decoder->_obsList.clear();
|
---|
| 662 | }
|
---|
| 663 | else {
|
---|
| 664 | emit(newMessage(_staID + ": Data Timeout, reconnecting"));
|
---|
| 665 | tryReconnect();
|
---|
| 666 | }
|
---|
[35] | 667 | }
|
---|
[249] | 668 | catch (const char* msg) {
|
---|
| 669 | emit(newMessage(_staID + msg));
|
---|
[136] | 670 | tryReconnect();
|
---|
[35] | 671 | }
|
---|
| 672 | }
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | // Exit
|
---|
| 676 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 677 | void bncGetThread::exit(int exitCode) {
|
---|
| 678 | if (exitCode!= 0) {
|
---|
[88] | 679 | emit error(_staID);
|
---|
[35] | 680 | }
|
---|
| 681 | QThread::exit(exitCode);
|
---|
[148] | 682 | terminate();
|
---|
[35] | 683 | }
|
---|
[82] | 684 |
|
---|
[136] | 685 | // Try Re-Connect
|
---|
| 686 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 687 | void bncGetThread::tryReconnect() {
|
---|
[408] | 688 | if (_rnx) {
|
---|
| 689 | _rnx->setReconnectFlag(true);
|
---|
| 690 | }
|
---|
[658] | 691 | if ( !_decodeStart.isValid()) {
|
---|
| 692 | _decodeStop = QDateTime::currentDateTime();
|
---|
| 693 | }
|
---|
[138] | 694 | while (1) {
|
---|
| 695 | delete _socket; _socket = 0;
|
---|
| 696 | sleep(_nextSleep);
|
---|
| 697 | if ( initRun() == success ) {
|
---|
[658] | 698 | if ( !_decodeStop.isValid()) {
|
---|
| 699 | _decodeStart = QDateTime::currentDateTime();
|
---|
| 700 | }
|
---|
[138] | 701 | break;
|
---|
| 702 | }
|
---|
| 703 | else {
|
---|
[658] | 704 |
|
---|
| 705 | // Begin outage threshold
|
---|
| 706 | // ----------------------
|
---|
[668] | 707 | if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
|
---|
[658] | 708 | _decodeStop.setDate(QDate());
|
---|
| 709 | _decodeStop.setTime(QTime());
|
---|
[686] | 710 | if (_inspSegm>0) {
|
---|
[699] | 711 | _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
|
---|
| 712 | _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 713 | emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
|
---|
[696] | 714 | callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
[686] | 715 | }
|
---|
[658] | 716 | }
|
---|
[138] | 717 | _nextSleep *= 2;
|
---|
[442] | 718 | if (_nextSleep > 256) {
|
---|
| 719 | _nextSleep = 256;
|
---|
[152] | 720 | }
|
---|
[277] | 721 | _nextSleep += rand() % 6;
|
---|
[138] | 722 | }
|
---|
| 723 | }
|
---|
| 724 | _nextSleep = 1;
|
---|
[136] | 725 | }
|
---|
[658] | 726 |
|
---|
[668] | 727 | // Call advisory notice script
|
---|
[658] | 728 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 729 | void bncGetThread::callScript(const char* _comment) {
|
---|
[672] | 730 | QMutexLocker locker(&_mutex);
|
---|
[668] | 731 | if (!_adviseScript.isEmpty()) {
|
---|
[672] | 732 | msleep(1);
|
---|
[658] | 733 | #ifdef WIN32
|
---|
[668] | 734 | QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
|
---|
[658] | 735 | #else
|
---|
[668] | 736 | QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
|
---|
[658] | 737 | #endif
|
---|
| 738 | }
|
---|
| 739 | }
|
---|