[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"
|
---|
[293] | 57 | #include "RTIGS/RTIGSDecoder.h"
|
---|
[35] | 58 |
|
---|
| 59 | using namespace std;
|
---|
| 60 |
|
---|
| 61 | // Constructor
|
---|
| 62 | ////////////////////////////////////////////////////////////////////////////
|
---|
[278] | 63 | bncGetThread::bncGetThread(const QUrl& mountPoint,
|
---|
[366] | 64 | const QByteArray& format,
|
---|
| 65 | const QByteArray& latitude,
|
---|
| 66 | const QByteArray& longitude,
|
---|
| 67 | const QByteArray& nmea, int iMount) {
|
---|
[605] | 68 |
|
---|
| 69 | setTerminationEnabled(true);
|
---|
| 70 |
|
---|
[350] | 71 | _decoder = 0;
|
---|
| 72 | _mountPoint = mountPoint;
|
---|
| 73 | _staID = mountPoint.path().mid(1).toAscii();
|
---|
| 74 | _staID_orig = _staID;
|
---|
| 75 | _format = format;
|
---|
[366] | 76 | _latitude = latitude;
|
---|
| 77 | _longitude = longitude;
|
---|
| 78 | _nmea = nmea;
|
---|
[350] | 79 | _socket = 0;
|
---|
| 80 | _timeOut = 20*1000; // 20 seconds
|
---|
| 81 | _nextSleep = 1; // 1 second
|
---|
| 82 | _iMount = iMount; // index in mountpoints array
|
---|
[255] | 83 |
|
---|
| 84 | // Check name conflict
|
---|
| 85 | // -------------------
|
---|
| 86 | QSettings settings;
|
---|
| 87 | QListIterator<QString> it(settings.value("mountPoints").toStringList());
|
---|
| 88 | int num = 0;
|
---|
[278] | 89 | int ind = -1;
|
---|
[255] | 90 | while (it.hasNext()) {
|
---|
[278] | 91 | ++ind;
|
---|
[255] | 92 | QStringList hlp = it.next().split(" ");
|
---|
| 93 | if (hlp.size() <= 1) continue;
|
---|
| 94 | QUrl url(hlp[0]);
|
---|
| 95 | if (_mountPoint.path() == url.path()) {
|
---|
[278] | 96 | if (_iMount > ind) {
|
---|
| 97 | ++num;
|
---|
[255] | 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
[278] | 101 |
|
---|
| 102 | if (num > 0) {
|
---|
| 103 | _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
|
---|
[255] | 104 | }
|
---|
[408] | 105 |
|
---|
[658] | 106 | // Notice threshold
|
---|
| 107 | // ----------------
|
---|
[686] | 108 | _inspSegm = 50;
|
---|
| 109 | if ( settings.value("obsRate").toString().isEmpty() ) { _inspSegm = 0; }
|
---|
[692] | 110 | if ( settings.value("obsRate").toString().indexOf("5 Hz") != -1 ) { _inspSegm = 2; }
|
---|
[686] | 111 | if ( settings.value("obsRate").toString().indexOf("1 Hz") != -1 ) { _inspSegm = 10; }
|
---|
| 112 | if ( settings.value("obsRate").toString().indexOf("0.5 Hz") != -1 ) { _inspSegm = 20; }
|
---|
| 113 | if ( settings.value("obsRate").toString().indexOf("0.2 Hz") != -1 ) { _inspSegm = 40; }
|
---|
| 114 | if ( settings.value("obsRate").toString().indexOf("0.1 Hz") != -1 ) { _inspSegm = 50; }
|
---|
[668] | 115 | _adviseFail = settings.value("adviseFail").toInt();
|
---|
| 116 | _adviseReco = settings.value("adviseReco").toInt();
|
---|
| 117 | _adviseScript = settings.value("adviseScript").toString();
|
---|
| 118 | expandEnvVar(_adviseScript);
|
---|
[658] | 119 |
|
---|
[408] | 120 | // RINEX writer
|
---|
| 121 | // ------------
|
---|
| 122 | _samplingRate = settings.value("rnxSampl").toInt();
|
---|
| 123 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
---|
| 124 | _rnx = 0;
|
---|
| 125 | }
|
---|
| 126 | else {
|
---|
| 127 | _rnx = new bncRinex(_staID, mountPoint, format, latitude, longitude, nmea);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[319] | 130 | msleep(100); //sleep 0.1 sec
|
---|
[35] | 131 | }
|
---|
| 132 |
|
---|
| 133 | // Destructor
|
---|
| 134 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 135 | bncGetThread::~bncGetThread() {
|
---|
[515] | 136 | if (_socket) {
|
---|
| 137 | _socket->close();
|
---|
[613] | 138 | #if QT_VERSION == 0x040203
|
---|
| 139 | delete _socket;
|
---|
| 140 | #else
|
---|
[612] | 141 | _socket->deleteLater();
|
---|
[613] | 142 | #endif
|
---|
[515] | 143 | }
|
---|
[617] | 144 | delete _decoder;
|
---|
[606] | 145 | delete _rnx;
|
---|
[35] | 146 | }
|
---|
| 147 |
|
---|
[645] | 148 | #define AGENTVERSION "1.5"
|
---|
[35] | 149 | // Connect to Caster, send the Request (static)
|
---|
| 150 | ////////////////////////////////////////////////////////////////////////////
|
---|
[366] | 151 | QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
|
---|
| 152 | QByteArray& latitude, QByteArray& longitude,
|
---|
| 153 | QByteArray& nmea, int timeOut,
|
---|
[136] | 154 | QString& msg) {
|
---|
[35] | 155 |
|
---|
[88] | 156 | // Connect the Socket
|
---|
| 157 | // ------------------
|
---|
| 158 | QSettings settings;
|
---|
| 159 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 160 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 161 |
|
---|
[35] | 162 | QTcpSocket* socket = new QTcpSocket();
|
---|
| 163 | if ( proxyHost.isEmpty() ) {
|
---|
[88] | 164 | socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
[35] | 165 | }
|
---|
| 166 | else {
|
---|
| 167 | socket->connectToHost(proxyHost, proxyPort);
|
---|
| 168 | }
|
---|
| 169 | if (!socket->waitForConnected(timeOut)) {
|
---|
[82] | 170 | msg += "Connect timeout\n";
|
---|
[35] | 171 | delete socket;
|
---|
| 172 | return 0;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | // Send Request
|
---|
| 176 | // ------------
|
---|
[443] | 177 | QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
|
---|
| 178 | QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
|
---|
[645] | 179 | QByteArray userAndPwd;
|
---|
[92] | 180 |
|
---|
[645] | 181 | if(!uName.isEmpty() || !passW.isEmpty())
|
---|
| 182 | {
|
---|
| 183 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
| 184 | passW.toAscii()).toBase64() + "\r\n";
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[92] | 187 | QUrl hlp;
|
---|
| 188 | hlp.setScheme("http");
|
---|
| 189 | hlp.setHost(mountPoint.host());
|
---|
| 190 | hlp.setPort(mountPoint.port());
|
---|
| 191 | hlp.setPath(mountPoint.path());
|
---|
| 192 |
|
---|
[214] | 193 | QByteArray reqStr;
|
---|
| 194 | if ( proxyHost.isEmpty() ) {
|
---|
| 195 | if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
|
---|
[645] | 196 | reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n";
|
---|
[214] | 197 | } else {
|
---|
[645] | 198 | reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n";
|
---|
[205] | 199 | }
|
---|
[645] | 200 | reqStr += "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
|
---|
| 201 | "Host: " + hlp.host().toAscii() + "\r\n"
|
---|
| 202 | + userAndPwd + "\r\n";
|
---|
[205] | 203 |
|
---|
[464] | 204 | // NMEA string to handle VRS stream
|
---|
| 205 | // --------------------------------
|
---|
[356] | 206 |
|
---|
| 207 | double lat, lon;
|
---|
[366] | 208 |
|
---|
| 209 | lat = strtod(latitude,NULL);
|
---|
| 210 | lon = strtod(longitude,NULL);
|
---|
| 211 |
|
---|
[410] | 212 | if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
[356] | 213 | const char* flagN="N";
|
---|
| 214 | const char* flagE="E";
|
---|
| 215 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
| 216 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
| 217 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
| 218 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
[566] | 219 | QTime ttime(QDateTime::currentDateTime().toUTC().time());
|
---|
[356] | 220 | int lat_deg = (int)lat;
|
---|
| 221 | double lat_min=(lat-lat_deg)*60.;
|
---|
| 222 | int lon_deg = (int)lon;
|
---|
| 223 | double lon_min=(lon-lon_deg)*60.;
|
---|
| 224 | int hh = 0 , mm = 0;
|
---|
| 225 | double ss = 0.0;
|
---|
| 226 | hh=ttime.hour();
|
---|
| 227 | mm=ttime.minute();
|
---|
| 228 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
| 229 | QString gga;
|
---|
| 230 | gga += "GPGGA,";
|
---|
| 231 | 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'));
|
---|
| 232 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 233 | gga += flagN;
|
---|
| 234 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 235 | gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
|
---|
| 236 | int xori;
|
---|
| 237 | char XOR = 0;
|
---|
| 238 | char *Buff =gga.toAscii().data();
|
---|
| 239 | int iLen = strlen(Buff);
|
---|
| 240 | for (xori = 0; xori < iLen; xori++) {
|
---|
| 241 | XOR ^= (char)Buff[xori];
|
---|
| 242 | }
|
---|
| 243 | gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
| 244 | reqStr += "$";
|
---|
| 245 | reqStr += gga;
|
---|
| 246 | reqStr += "\r\n";
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[82] | 249 | msg += reqStr;
|
---|
[35] | 250 |
|
---|
| 251 | socket->write(reqStr, reqStr.length());
|
---|
| 252 |
|
---|
| 253 | if (!socket->waitForBytesWritten(timeOut)) {
|
---|
[82] | 254 | msg += "Write timeout\n";
|
---|
[35] | 255 | delete socket;
|
---|
| 256 | return 0;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | return socket;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[136] | 262 | // Init Run
|
---|
[35] | 263 | ////////////////////////////////////////////////////////////////////////////
|
---|
[138] | 264 | t_irc bncGetThread::initRun() {
|
---|
[35] | 265 |
|
---|
[515] | 266 | // Initialize Socket
|
---|
| 267 | // -----------------
|
---|
| 268 | QString msg;
|
---|
[602] | 269 | _socket = this->request(_mountPoint, _latitude, _longitude,
|
---|
| 270 | _nmea, _timeOut, msg);
|
---|
[35] | 271 | if (!_socket) {
|
---|
[138] | 272 | return failure;
|
---|
[35] | 273 | }
|
---|
| 274 |
|
---|
| 275 | // Read Caster Response
|
---|
| 276 | // --------------------
|
---|
[136] | 277 | _socket->waitForReadyRead(_timeOut);
|
---|
[35] | 278 | if (_socket->canReadLine()) {
|
---|
| 279 | QString line = _socket->readLine();
|
---|
[473] | 280 |
|
---|
| 281 | // Skip messages from proxy server
|
---|
| 282 | // -------------------------------
|
---|
| 283 | if (line.indexOf("ICY 200 OK") == -1 &&
|
---|
| 284 | line.indexOf("200 OK") != -1 ) {
|
---|
| 285 | bool proxyRespond = true;
|
---|
| 286 | while (true) {
|
---|
| 287 | if (_socket->canReadLine()) {
|
---|
| 288 | line = _socket->readLine();
|
---|
| 289 | if (!proxyRespond) {
|
---|
| 290 | break;
|
---|
| 291 | }
|
---|
| 292 | if (line.trimmed().isEmpty()) {
|
---|
| 293 | proxyRespond = false;
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 | else {
|
---|
| 297 | _socket->waitForReadyRead(_timeOut);
|
---|
[474] | 298 | if (_socket->bytesAvailable() <= 0) {
|
---|
| 299 | break;
|
---|
| 300 | }
|
---|
[473] | 301 | }
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[146] | 305 | if (line.indexOf("Unauthorized") != -1) {
|
---|
[192] | 306 | QStringList table;
|
---|
| 307 | bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
|
---|
| 308 | QString net;
|
---|
| 309 | QStringListIterator it(table);
|
---|
| 310 | while (it.hasNext()) {
|
---|
| 311 | QString line = it.next();
|
---|
| 312 | if (line.indexOf("STR") == 0) {
|
---|
| 313 | QStringList tags = line.split(";");
|
---|
[255] | 314 | if (tags.at(1) == _staID_orig) {
|
---|
[192] | 315 | net = tags.at(7);
|
---|
| 316 | break;
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | QString reg;
|
---|
| 322 | it.toFront();
|
---|
| 323 | while (it.hasNext()) {
|
---|
| 324 | QString line = it.next();
|
---|
| 325 | if (line.indexOf("NET") == 0) {
|
---|
| 326 | QStringList tags = line.split(";");
|
---|
| 327 | if (tags.at(1) == net) {
|
---|
| 328 | reg = tags.at(7);
|
---|
| 329 | break;
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
[194] | 333 | emit(newMessage((_staID + ": Caster Response: " + line +
|
---|
[200] | 334 | " Adjust User-ID and Password Register, see"
|
---|
[194] | 335 | "\n " + reg).toAscii()));
|
---|
[192] | 336 | return fatal;
|
---|
[146] | 337 | }
|
---|
[35] | 338 | if (line.indexOf("ICY 200 OK") != 0) {
|
---|
[190] | 339 | emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
|
---|
[144] | 340 | return failure;
|
---|
[35] | 341 | }
|
---|
| 342 | }
|
---|
| 343 | else {
|
---|
[190] | 344 | emit(newMessage(_staID + ": Response Timeout"));
|
---|
[138] | 345 | return failure;
|
---|
[35] | 346 | }
|
---|
| 347 |
|
---|
| 348 | // Instantiate the filter
|
---|
| 349 | // ----------------------
|
---|
[423] | 350 | if (!_decoder) {
|
---|
[136] | 351 | if (_format.indexOf("RTCM_2") != -1) {
|
---|
| 352 | emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
|
---|
[243] | 353 | _decoder = new RTCM2Decoder();
|
---|
[136] | 354 | }
|
---|
| 355 | else if (_format.indexOf("RTCM_3") != -1) {
|
---|
[569] | 356 | emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
|
---|
[511] | 357 | _decoder = new RTCM3Decoder();
|
---|
[136] | 358 | }
|
---|
| 359 | else if (_format.indexOf("RTIGS") != -1) {
|
---|
| 360 | emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
|
---|
[293] | 361 | _decoder = new RTIGSDecoder();
|
---|
[136] | 362 | }
|
---|
[428] | 363 | else if (_format.indexOf("SP3") != -1 || _format.indexOf("ZERO") != -1) {
|
---|
| 364 | emit(newMessage("Get Data: " + _staID + " in original format"));
|
---|
[424] | 365 | _decoder = new bncZeroDecoder(_staID);
|
---|
[406] | 366 | }
|
---|
[136] | 367 | else {
|
---|
[190] | 368 | emit(newMessage(_staID + ": Unknown data format " + _format));
|
---|
[250] | 369 | return fatal;
|
---|
[136] | 370 | }
|
---|
[60] | 371 | }
|
---|
[138] | 372 | return success;
|
---|
[136] | 373 | }
|
---|
[59] | 374 |
|
---|
[136] | 375 | // Run
|
---|
| 376 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 377 | void bncGetThread::run() {
|
---|
| 378 |
|
---|
[699] | 379 | bool wrongEpoch = false;
|
---|
| 380 | bool decode = true;
|
---|
| 381 | int numSucc = 0;
|
---|
| 382 | int secSucc = 0;
|
---|
| 383 | int secFail = 0;
|
---|
| 384 | int initPause = 30;
|
---|
| 385 | int currPause = 0;
|
---|
| 386 | bool begCorrupt = false;
|
---|
| 387 | bool endCorrupt = false;
|
---|
| 388 |
|
---|
| 389 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 390 | _decodeSucc = QDateTime::currentDateTime();
|
---|
[229] | 391 | t_irc irc = initRun();
|
---|
| 392 |
|
---|
| 393 | if (irc == fatal) {
|
---|
[192] | 394 | QThread::exit(1);
|
---|
| 395 | return;
|
---|
| 396 | }
|
---|
[229] | 397 | else if (irc != success) {
|
---|
[190] | 398 | emit(newMessage(_staID + ": initRun failed, reconnecting"));
|
---|
[138] | 399 | tryReconnect();
|
---|
| 400 | }
|
---|
[136] | 401 |
|
---|
[658] | 402 | if (initPause < _inspSegm) {
|
---|
| 403 | initPause = _inspSegm;
|
---|
| 404 | }
|
---|
| 405 | currPause = initPause;
|
---|
| 406 |
|
---|
[35] | 407 | // Read Incoming Data
|
---|
| 408 | // ------------------
|
---|
| 409 | while (true) {
|
---|
[629] | 410 | try {
|
---|
| 411 | if (_socket->state() != QAbstractSocket::ConnectedState) {
|
---|
| 412 | emit(newMessage(_staID + ": Socket not connected, reconnecting"));
|
---|
| 413 | tryReconnect();
|
---|
| 414 | }
|
---|
[621] | 415 |
|
---|
| 416 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 417 | while (it.hasNext()) {
|
---|
| 418 | delete it.next();
|
---|
| 419 | }
|
---|
| 420 | _decoder->_obsList.clear();
|
---|
| 421 |
|
---|
[249] | 422 | _socket->waitForReadyRead(_timeOut);
|
---|
| 423 | qint64 nBytes = _socket->bytesAvailable();
|
---|
| 424 | if (nBytes > 0) {
|
---|
[567] | 425 | emit newBytes(_staID, nBytes);
|
---|
| 426 |
|
---|
[249] | 427 | char* data = new char[nBytes];
|
---|
| 428 | _socket->read(data, nBytes);
|
---|
[406] | 429 |
|
---|
[658] | 430 | if (_inspSegm<1) {
|
---|
| 431 | _decoder->Decode(data, nBytes);
|
---|
| 432 | }
|
---|
| 433 | else {
|
---|
| 434 |
|
---|
| 435 | // Decode data
|
---|
| 436 | // -----------
|
---|
| 437 | if (!_decodePause.isValid() ||
|
---|
| 438 | _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
|
---|
| 439 |
|
---|
| 440 | if (decode) {
|
---|
| 441 | if ( _decoder->Decode(data, nBytes) == success ) {
|
---|
| 442 | numSucc += 1;
|
---|
| 443 | }
|
---|
| 444 | if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
|
---|
| 445 | decode = false;
|
---|
| 446 | }
|
---|
[652] | 447 | }
|
---|
[658] | 448 |
|
---|
| 449 | // Check - once per inspect segment
|
---|
| 450 | // --------------------------------
|
---|
| 451 | if (!decode) {
|
---|
| 452 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 453 | if (numSucc>0) {
|
---|
| 454 | secSucc += _inspSegm;
|
---|
[699] | 455 | _decodeSucc = QDateTime::currentDateTime();
|
---|
[668] | 456 | if (secSucc > _adviseReco * 60) {
|
---|
| 457 | secSucc = _adviseReco * 60 + 1;
|
---|
[658] | 458 | }
|
---|
| 459 | numSucc = 0;
|
---|
| 460 | currPause = initPause;
|
---|
| 461 | _decodePause.setDate(QDate());
|
---|
| 462 | _decodePause.setTime(QTime());
|
---|
| 463 | }
|
---|
| 464 | else {
|
---|
| 465 | secFail += _inspSegm;
|
---|
| 466 | secSucc = 0;
|
---|
[668] | 467 | if (secFail > _adviseFail * 60) {
|
---|
| 468 | secFail = _adviseFail * 60 + 1;
|
---|
[658] | 469 | }
|
---|
| 470 | if (!_decodePause.isValid()) {
|
---|
| 471 | _decodePause = QDateTime::currentDateTime();
|
---|
| 472 | }
|
---|
| 473 | else {
|
---|
| 474 | _decodePause.setDate(QDate());
|
---|
| 475 | _decodePause.setTime(QTime());
|
---|
| 476 | secFail = secFail + currPause - _inspSegm;
|
---|
| 477 | currPause = currPause * 2;
|
---|
| 478 | if (currPause > 960) {
|
---|
| 479 | currPause = 960;
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | // End corrupt threshold
|
---|
| 485 | // ---------------------
|
---|
[668] | 486 | if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
|
---|
[699] | 487 | _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 488 | _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 489 | emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
|
---|
[696] | 490 | callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
[658] | 491 | endCorrupt = true;
|
---|
| 492 | begCorrupt = false;
|
---|
| 493 | secFail = 0;
|
---|
| 494 | }
|
---|
| 495 | else {
|
---|
| 496 |
|
---|
| 497 | // Begin corrupt threshold
|
---|
| 498 | // -----------------------
|
---|
[668] | 499 | if ( !begCorrupt && secFail > _adviseFail * 60 ) {
|
---|
[699] | 500 | _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
|
---|
| 501 | _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 502 | emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
|
---|
[696] | 503 | callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
[658] | 504 | begCorrupt = true;
|
---|
| 505 | endCorrupt = false;
|
---|
| 506 | secSucc = 0;
|
---|
| 507 | numSucc = 0;
|
---|
| 508 | }
|
---|
| 509 | }
|
---|
| 510 | decode = true;
|
---|
[650] | 511 | }
|
---|
| 512 | }
|
---|
[658] | 513 | }
|
---|
[650] | 514 |
|
---|
[658] | 515 | // End outage threshold
|
---|
| 516 | // --------------------
|
---|
[668] | 517 | if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
|
---|
[658] | 518 | _decodeStart.setDate(QDate());
|
---|
| 519 | _decodeStart.setTime(QTime());
|
---|
[686] | 520 | if (_inspSegm>0) {
|
---|
[699] | 521 | _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 522 | _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 523 | emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
|
---|
[696] | 524 | callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
[686] | 525 | }
|
---|
[658] | 526 | }
|
---|
| 527 |
|
---|
[331] | 528 | delete [] data;
|
---|
[423] | 529 |
|
---|
[621] | 530 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 531 | while (it.hasNext()) {
|
---|
| 532 | p_obs obs = it.next();
|
---|
| 533 |
|
---|
[351] | 534 | // Check observation epoch
|
---|
| 535 | // -----------------------
|
---|
| 536 | int week;
|
---|
| 537 | double sec;
|
---|
| 538 | currentGPSWeeks(week, sec);
|
---|
| 539 |
|
---|
| 540 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
| 541 | const double maxDt = 600.0;
|
---|
| 542 |
|
---|
[622] | 543 | if (week < obs->_o.GPSWeek) {
|
---|
[351] | 544 | week += 1;
|
---|
| 545 | sec -= secPerWeek;
|
---|
| 546 | }
|
---|
[622] | 547 | if (week > obs->_o.GPSWeek) {
|
---|
[351] | 548 | week -= 1;
|
---|
| 549 | sec += secPerWeek;
|
---|
| 550 | }
|
---|
[622] | 551 | double dt = fabs(sec - obs->_o.GPSWeeks);
|
---|
| 552 | if (week != obs->_o.GPSWeek || dt > maxDt) {
|
---|
[658] | 553 | if (!wrongEpoch) {
|
---|
| 554 | emit( newMessage(_staID + ": Wrong observation epoch") );
|
---|
| 555 | wrongEpoch = true;
|
---|
| 556 | }
|
---|
[621] | 557 | delete obs;
|
---|
[351] | 558 | continue;
|
---|
| 559 | }
|
---|
[658] | 560 | else {
|
---|
| 561 | wrongEpoch = false;
|
---|
| 562 | }
|
---|
[351] | 563 |
|
---|
[408] | 564 | // RINEX Output
|
---|
| 565 | // ------------
|
---|
| 566 | if (_rnx) {
|
---|
[622] | 567 | long iSec = long(floor(obs->_o.GPSWeeks+0.5));
|
---|
| 568 | long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
|
---|
[408] | 569 | if (_samplingRate == 0 || iSec % _samplingRate == 0) {
|
---|
[621] | 570 | _rnx->deepCopy(obs);
|
---|
[408] | 571 | }
|
---|
| 572 | _rnx->dumpEpoch(newTime);
|
---|
| 573 | }
|
---|
| 574 |
|
---|
[621] | 575 | bool firstObs = (obs == _decoder->_obsList.first());
|
---|
[624] | 576 | obs->_status = t_obs::posted;
|
---|
[621] | 577 | emit newObs(_staID, firstObs, obs);
|
---|
[249] | 578 | }
|
---|
| 579 | _decoder->_obsList.clear();
|
---|
| 580 | }
|
---|
| 581 | else {
|
---|
| 582 | emit(newMessage(_staID + ": Data Timeout, reconnecting"));
|
---|
| 583 | tryReconnect();
|
---|
| 584 | }
|
---|
[35] | 585 | }
|
---|
[249] | 586 | catch (const char* msg) {
|
---|
| 587 | emit(newMessage(_staID + msg));
|
---|
[136] | 588 | tryReconnect();
|
---|
[35] | 589 | }
|
---|
| 590 | }
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | // Exit
|
---|
| 594 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 595 | void bncGetThread::exit(int exitCode) {
|
---|
| 596 | if (exitCode!= 0) {
|
---|
[88] | 597 | emit error(_staID);
|
---|
[35] | 598 | }
|
---|
| 599 | QThread::exit(exitCode);
|
---|
[148] | 600 | terminate();
|
---|
[35] | 601 | }
|
---|
[82] | 602 |
|
---|
[136] | 603 | // Try Re-Connect
|
---|
| 604 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 605 | void bncGetThread::tryReconnect() {
|
---|
[408] | 606 | if (_rnx) {
|
---|
| 607 | _rnx->setReconnectFlag(true);
|
---|
| 608 | }
|
---|
[658] | 609 | if ( !_decodeStart.isValid()) {
|
---|
| 610 | _decodeStop = QDateTime::currentDateTime();
|
---|
| 611 | }
|
---|
[138] | 612 | while (1) {
|
---|
| 613 | delete _socket; _socket = 0;
|
---|
| 614 | sleep(_nextSleep);
|
---|
| 615 | if ( initRun() == success ) {
|
---|
[658] | 616 | if ( !_decodeStop.isValid()) {
|
---|
| 617 | _decodeStart = QDateTime::currentDateTime();
|
---|
| 618 | }
|
---|
[138] | 619 | break;
|
---|
| 620 | }
|
---|
| 621 | else {
|
---|
[658] | 622 |
|
---|
| 623 | // Begin outage threshold
|
---|
| 624 | // ----------------------
|
---|
[668] | 625 | if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
|
---|
[658] | 626 | _decodeStop.setDate(QDate());
|
---|
| 627 | _decodeStop.setTime(QTime());
|
---|
[686] | 628 | if (_inspSegm>0) {
|
---|
[699] | 629 | _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
|
---|
| 630 | _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 631 | emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
|
---|
[696] | 632 | callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
[686] | 633 | }
|
---|
[658] | 634 | }
|
---|
[138] | 635 | _nextSleep *= 2;
|
---|
[442] | 636 | if (_nextSleep > 256) {
|
---|
| 637 | _nextSleep = 256;
|
---|
[152] | 638 | }
|
---|
[277] | 639 | _nextSleep += rand() % 6;
|
---|
[138] | 640 | }
|
---|
| 641 | }
|
---|
| 642 | _nextSleep = 1;
|
---|
[136] | 643 | }
|
---|
[658] | 644 |
|
---|
[668] | 645 | // Call advisory notice script
|
---|
[658] | 646 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 647 | void bncGetThread::callScript(const char* _comment) {
|
---|
[672] | 648 | QMutexLocker locker(&_mutex);
|
---|
[668] | 649 | if (!_adviseScript.isEmpty()) {
|
---|
[672] | 650 | msleep(1);
|
---|
[658] | 651 | #ifdef WIN32
|
---|
[668] | 652 | QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
|
---|
[658] | 653 | #else
|
---|
[668] | 654 | QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
|
---|
[658] | 655 | #endif
|
---|
| 656 | }
|
---|
| 657 | }
|
---|