[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>
|
---|
[1044] | 42 | #include <iomanip>
|
---|
[277] | 43 |
|
---|
[35] | 44 | #include <QFile>
|
---|
| 45 | #include <QTextStream>
|
---|
| 46 | #include <QtNetwork>
|
---|
[356] | 47 | #include <QTime>
|
---|
[35] | 48 |
|
---|
| 49 | #include "bncgetthread.h"
|
---|
[192] | 50 | #include "bnctabledlg.h"
|
---|
[243] | 51 | #include "bncapp.h"
|
---|
[352] | 52 | #include "bncutils.h"
|
---|
[408] | 53 | #include "bncrinex.h"
|
---|
[423] | 54 | #include "bnczerodecoder.h"
|
---|
[65] | 55 |
|
---|
[243] | 56 | #include "RTCM/RTCM2Decoder.h"
|
---|
[297] | 57 | #include "RTCM3/RTCM3Decoder.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 |
|
---|
[1030] | 134 | // RTCM message types
|
---|
| 135 | // ------------------
|
---|
| 136 | _checkMountPoint = settings.value("messTypes").toString();
|
---|
| 137 |
|
---|
[408] | 138 | // RINEX writer
|
---|
| 139 | // ------------
|
---|
| 140 | _samplingRate = settings.value("rnxSampl").toInt();
|
---|
| 141 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
---|
| 142 | _rnx = 0;
|
---|
| 143 | }
|
---|
| 144 | else {
|
---|
| 145 | _rnx = new bncRinex(_staID, mountPoint, format, latitude, longitude, nmea);
|
---|
| 146 | }
|
---|
[1044] | 147 | _rnx_set_position = false;
|
---|
[408] | 148 |
|
---|
[319] | 149 | msleep(100); //sleep 0.1 sec
|
---|
[35] | 150 | }
|
---|
| 151 |
|
---|
| 152 | // Destructor
|
---|
| 153 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 154 | bncGetThread::~bncGetThread() {
|
---|
[515] | 155 | if (_socket) {
|
---|
| 156 | _socket->close();
|
---|
[613] | 157 | #if QT_VERSION == 0x040203
|
---|
| 158 | delete _socket;
|
---|
| 159 | #else
|
---|
[612] | 160 | _socket->deleteLater();
|
---|
[613] | 161 | #endif
|
---|
[515] | 162 | }
|
---|
[617] | 163 | delete _decoder;
|
---|
[1044] | 164 | delete _rnx;
|
---|
[35] | 165 | }
|
---|
| 166 |
|
---|
[841] | 167 | #define AGENTVERSION "1.6"
|
---|
[35] | 168 | // Connect to Caster, send the Request (static)
|
---|
| 169 | ////////////////////////////////////////////////////////////////////////////
|
---|
[366] | 170 | QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
|
---|
| 171 | QByteArray& latitude, QByteArray& longitude,
|
---|
| 172 | QByteArray& nmea, int timeOut,
|
---|
[136] | 173 | QString& msg) {
|
---|
[35] | 174 |
|
---|
[88] | 175 | // Connect the Socket
|
---|
| 176 | // ------------------
|
---|
| 177 | QSettings settings;
|
---|
| 178 | QString proxyHost = settings.value("proxyHost").toString();
|
---|
| 179 | int proxyPort = settings.value("proxyPort").toInt();
|
---|
| 180 |
|
---|
[35] | 181 | QTcpSocket* socket = new QTcpSocket();
|
---|
| 182 | if ( proxyHost.isEmpty() ) {
|
---|
[88] | 183 | socket->connectToHost(mountPoint.host(), mountPoint.port());
|
---|
[35] | 184 | }
|
---|
| 185 | else {
|
---|
| 186 | socket->connectToHost(proxyHost, proxyPort);
|
---|
| 187 | }
|
---|
| 188 | if (!socket->waitForConnected(timeOut)) {
|
---|
[82] | 189 | msg += "Connect timeout\n";
|
---|
[35] | 190 | delete socket;
|
---|
| 191 | return 0;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | // Send Request
|
---|
| 195 | // ------------
|
---|
[443] | 196 | QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
|
---|
| 197 | QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
|
---|
[645] | 198 | QByteArray userAndPwd;
|
---|
[92] | 199 |
|
---|
[645] | 200 | if(!uName.isEmpty() || !passW.isEmpty())
|
---|
| 201 | {
|
---|
| 202 | userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
|
---|
| 203 | passW.toAscii()).toBase64() + "\r\n";
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[92] | 206 | QUrl hlp;
|
---|
| 207 | hlp.setScheme("http");
|
---|
| 208 | hlp.setHost(mountPoint.host());
|
---|
| 209 | hlp.setPort(mountPoint.port());
|
---|
| 210 | hlp.setPath(mountPoint.path());
|
---|
| 211 |
|
---|
[214] | 212 | QByteArray reqStr;
|
---|
| 213 | if ( proxyHost.isEmpty() ) {
|
---|
[1020] | 214 | if (hlp.path().indexOf("/") != 0) {
|
---|
| 215 | hlp.setPath("/");
|
---|
| 216 | }
|
---|
| 217 | reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n";
|
---|
| 218 | } else {
|
---|
| 219 | reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n";
|
---|
[205] | 220 | }
|
---|
[645] | 221 | reqStr += "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
|
---|
[1020] | 222 | "Host: " + hlp.host().toAscii() + "\r\n"
|
---|
| 223 | + userAndPwd + "\r\n";
|
---|
[205] | 224 |
|
---|
[1020] | 225 | // NMEA string to handle VRS stream
|
---|
| 226 | // --------------------------------
|
---|
[356] | 227 | double lat, lon;
|
---|
[366] | 228 |
|
---|
| 229 | lat = strtod(latitude,NULL);
|
---|
| 230 | lon = strtod(longitude,NULL);
|
---|
| 231 |
|
---|
[410] | 232 | if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
|
---|
[356] | 233 | const char* flagN="N";
|
---|
| 234 | const char* flagE="E";
|
---|
| 235 | if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
|
---|
| 236 | if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
|
---|
| 237 | if (lon < -180.) {lon=(lon+360.); flagE="E";}
|
---|
| 238 | if (lat < 0.) {lat=lat*(-1.); flagN="S";}
|
---|
[566] | 239 | QTime ttime(QDateTime::currentDateTime().toUTC().time());
|
---|
[356] | 240 | int lat_deg = (int)lat;
|
---|
| 241 | double lat_min=(lat-lat_deg)*60.;
|
---|
| 242 | int lon_deg = (int)lon;
|
---|
| 243 | double lon_min=(lon-lon_deg)*60.;
|
---|
| 244 | int hh = 0 , mm = 0;
|
---|
| 245 | double ss = 0.0;
|
---|
| 246 | hh=ttime.hour();
|
---|
| 247 | mm=ttime.minute();
|
---|
| 248 | ss=(double)ttime.second()+0.001*ttime.msec();
|
---|
| 249 | QString gga;
|
---|
| 250 | gga += "GPGGA,";
|
---|
| 251 | 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'));
|
---|
| 252 | gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 253 | gga += flagN;
|
---|
| 254 | gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
|
---|
| 255 | gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
|
---|
| 256 | int xori;
|
---|
| 257 | char XOR = 0;
|
---|
| 258 | char *Buff =gga.toAscii().data();
|
---|
| 259 | int iLen = strlen(Buff);
|
---|
| 260 | for (xori = 0; xori < iLen; xori++) {
|
---|
| 261 | XOR ^= (char)Buff[xori];
|
---|
| 262 | }
|
---|
| 263 | gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
|
---|
| 264 | reqStr += "$";
|
---|
| 265 | reqStr += gga;
|
---|
| 266 | reqStr += "\r\n";
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[82] | 269 | msg += reqStr;
|
---|
[35] | 270 |
|
---|
| 271 | socket->write(reqStr, reqStr.length());
|
---|
| 272 |
|
---|
| 273 | if (!socket->waitForBytesWritten(timeOut)) {
|
---|
[82] | 274 | msg += "Write timeout\n";
|
---|
[35] | 275 | delete socket;
|
---|
| 276 | return 0;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | return socket;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[136] | 282 | // Init Run
|
---|
[35] | 283 | ////////////////////////////////////////////////////////////////////////////
|
---|
[138] | 284 | t_irc bncGetThread::initRun() {
|
---|
[35] | 285 |
|
---|
[515] | 286 | // Initialize Socket
|
---|
| 287 | // -----------------
|
---|
| 288 | QString msg;
|
---|
[602] | 289 | _socket = this->request(_mountPoint, _latitude, _longitude,
|
---|
| 290 | _nmea, _timeOut, msg);
|
---|
[35] | 291 | if (!_socket) {
|
---|
[138] | 292 | return failure;
|
---|
[35] | 293 | }
|
---|
| 294 |
|
---|
| 295 | // Read Caster Response
|
---|
| 296 | // --------------------
|
---|
[136] | 297 | _socket->waitForReadyRead(_timeOut);
|
---|
[35] | 298 | if (_socket->canReadLine()) {
|
---|
| 299 | QString line = _socket->readLine();
|
---|
[473] | 300 |
|
---|
| 301 | // Skip messages from proxy server
|
---|
| 302 | // -------------------------------
|
---|
| 303 | if (line.indexOf("ICY 200 OK") == -1 &&
|
---|
| 304 | line.indexOf("200 OK") != -1 ) {
|
---|
| 305 | bool proxyRespond = true;
|
---|
| 306 | while (true) {
|
---|
| 307 | if (_socket->canReadLine()) {
|
---|
| 308 | line = _socket->readLine();
|
---|
| 309 | if (!proxyRespond) {
|
---|
| 310 | break;
|
---|
| 311 | }
|
---|
| 312 | if (line.trimmed().isEmpty()) {
|
---|
| 313 | proxyRespond = false;
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | else {
|
---|
| 317 | _socket->waitForReadyRead(_timeOut);
|
---|
[474] | 318 | if (_socket->bytesAvailable() <= 0) {
|
---|
| 319 | break;
|
---|
| 320 | }
|
---|
[473] | 321 | }
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[146] | 325 | if (line.indexOf("Unauthorized") != -1) {
|
---|
[192] | 326 | QStringList table;
|
---|
| 327 | bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
|
---|
| 328 | QString net;
|
---|
| 329 | QStringListIterator it(table);
|
---|
| 330 | while (it.hasNext()) {
|
---|
| 331 | QString line = it.next();
|
---|
| 332 | if (line.indexOf("STR") == 0) {
|
---|
| 333 | QStringList tags = line.split(";");
|
---|
[255] | 334 | if (tags.at(1) == _staID_orig) {
|
---|
[192] | 335 | net = tags.at(7);
|
---|
| 336 | break;
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | QString reg;
|
---|
| 342 | it.toFront();
|
---|
| 343 | while (it.hasNext()) {
|
---|
| 344 | QString line = it.next();
|
---|
| 345 | if (line.indexOf("NET") == 0) {
|
---|
| 346 | QStringList tags = line.split(";");
|
---|
| 347 | if (tags.at(1) == net) {
|
---|
| 348 | reg = tags.at(7);
|
---|
| 349 | break;
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
[194] | 353 | emit(newMessage((_staID + ": Caster Response: " + line +
|
---|
[200] | 354 | " Adjust User-ID and Password Register, see"
|
---|
[194] | 355 | "\n " + reg).toAscii()));
|
---|
[192] | 356 | return fatal;
|
---|
[146] | 357 | }
|
---|
[35] | 358 | if (line.indexOf("ICY 200 OK") != 0) {
|
---|
[190] | 359 | emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
|
---|
[144] | 360 | return failure;
|
---|
[35] | 361 | }
|
---|
| 362 | }
|
---|
| 363 | else {
|
---|
[190] | 364 | emit(newMessage(_staID + ": Response Timeout"));
|
---|
[138] | 365 | return failure;
|
---|
[35] | 366 | }
|
---|
| 367 |
|
---|
| 368 | // Instantiate the filter
|
---|
| 369 | // ----------------------
|
---|
[423] | 370 | if (!_decoder) {
|
---|
[136] | 371 | if (_format.indexOf("RTCM_2") != -1) {
|
---|
| 372 | emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
|
---|
[1044] | 373 | _decoder = new RTCM2Decoder(_staID.data());
|
---|
[136] | 374 | }
|
---|
| 375 | else if (_format.indexOf("RTCM_3") != -1) {
|
---|
[569] | 376 | emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
|
---|
[880] | 377 | _decoder = new RTCM3Decoder(_staID);
|
---|
[1022] | 378 | connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray)),
|
---|
| 379 | this, SIGNAL(newMessage(QByteArray)));
|
---|
[136] | 380 | }
|
---|
| 381 | else if (_format.indexOf("RTIGS") != -1) {
|
---|
| 382 | emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
|
---|
[293] | 383 | _decoder = new RTIGSDecoder();
|
---|
[136] | 384 | }
|
---|
[867] | 385 | else if (_format.indexOf("ZERO") != -1) {
|
---|
[428] | 386 | emit(newMessage("Get Data: " + _staID + " in original format"));
|
---|
[424] | 387 | _decoder = new bncZeroDecoder(_staID);
|
---|
[406] | 388 | }
|
---|
[136] | 389 | else {
|
---|
[190] | 390 | emit(newMessage(_staID + ": Unknown data format " + _format));
|
---|
[250] | 391 | return fatal;
|
---|
[136] | 392 | }
|
---|
[60] | 393 | }
|
---|
[138] | 394 | return success;
|
---|
[136] | 395 | }
|
---|
[59] | 396 |
|
---|
[136] | 397 | // Run
|
---|
| 398 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 399 | void bncGetThread::run() {
|
---|
| 400 |
|
---|
[709] | 401 | const double maxDt = 600.0; // Check observation epoch
|
---|
[699] | 402 | bool wrongEpoch = false;
|
---|
| 403 | bool decode = true;
|
---|
| 404 | int numSucc = 0;
|
---|
| 405 | int secSucc = 0;
|
---|
| 406 | int secFail = 0;
|
---|
[709] | 407 | int initPause = 30; // Initial pause for corrupted streams
|
---|
[699] | 408 | int currPause = 0;
|
---|
| 409 | bool begCorrupt = false;
|
---|
| 410 | bool endCorrupt = false;
|
---|
[728] | 411 | bool followSec = false;
|
---|
[717] | 412 | int oldSecGPS= 0;
|
---|
| 413 | int newSecGPS = 0;
|
---|
[728] | 414 | int numGaps = 0;
|
---|
| 415 | int diffSecGPS = 0;
|
---|
[709] | 416 | int numLat = 0;
|
---|
| 417 | double sumLat = 0.;
|
---|
[728] | 418 | double meanDiff = 0.;
|
---|
[709] | 419 | double minLat = maxDt;
|
---|
| 420 | double maxLat = -maxDt;
|
---|
| 421 | double curLat = 0.;
|
---|
[699] | 422 |
|
---|
| 423 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 424 | _decodeSucc = QDateTime::currentDateTime();
|
---|
[229] | 425 | t_irc irc = initRun();
|
---|
| 426 |
|
---|
| 427 | if (irc == fatal) {
|
---|
[192] | 428 | QThread::exit(1);
|
---|
| 429 | return;
|
---|
| 430 | }
|
---|
[229] | 431 | else if (irc != success) {
|
---|
[190] | 432 | emit(newMessage(_staID + ": initRun failed, reconnecting"));
|
---|
[138] | 433 | tryReconnect();
|
---|
| 434 | }
|
---|
[136] | 435 |
|
---|
[658] | 436 | if (initPause < _inspSegm) {
|
---|
| 437 | initPause = _inspSegm;
|
---|
| 438 | }
|
---|
[727] | 439 | if(!_makePause) {initPause = 0;}
|
---|
[658] | 440 | currPause = initPause;
|
---|
| 441 |
|
---|
[35] | 442 | // Read Incoming Data
|
---|
| 443 | // ------------------
|
---|
| 444 | while (true) {
|
---|
[629] | 445 | try {
|
---|
| 446 | if (_socket->state() != QAbstractSocket::ConnectedState) {
|
---|
| 447 | emit(newMessage(_staID + ": Socket not connected, reconnecting"));
|
---|
| 448 | tryReconnect();
|
---|
| 449 | }
|
---|
[621] | 450 |
|
---|
| 451 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 452 | while (it.hasNext()) {
|
---|
| 453 | delete it.next();
|
---|
| 454 | }
|
---|
| 455 | _decoder->_obsList.clear();
|
---|
| 456 |
|
---|
[249] | 457 | _socket->waitForReadyRead(_timeOut);
|
---|
| 458 | qint64 nBytes = _socket->bytesAvailable();
|
---|
| 459 | if (nBytes > 0) {
|
---|
[567] | 460 | emit newBytes(_staID, nBytes);
|
---|
| 461 |
|
---|
[249] | 462 | char* data = new char[nBytes];
|
---|
| 463 | _socket->read(data, nBytes);
|
---|
[406] | 464 |
|
---|
[940] | 465 | if (_inspSegm<1) {
|
---|
| 466 | _decoder->Decode(data, nBytes);
|
---|
| 467 | }
|
---|
| 468 | else {
|
---|
| 469 |
|
---|
| 470 | // Decode data
|
---|
| 471 | // -----------
|
---|
| 472 | if (!_decodePause.isValid() ||
|
---|
| 473 | _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
|
---|
| 474 |
|
---|
| 475 | if (decode) {
|
---|
| 476 | if ( _decoder->Decode(data, nBytes) == success ) {
|
---|
| 477 | numSucc += 1;
|
---|
| 478 | }
|
---|
| 479 | if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
|
---|
| 480 | decode = false;
|
---|
[658] | 481 | }
|
---|
| 482 | }
|
---|
[940] | 483 |
|
---|
| 484 | // Check - once per inspect segment
|
---|
| 485 | // --------------------------------
|
---|
| 486 | if (!decode) {
|
---|
| 487 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 488 | if (numSucc>0) {
|
---|
| 489 | secSucc += _inspSegm;
|
---|
| 490 | _decodeSucc = QDateTime::currentDateTime();
|
---|
| 491 | if (secSucc > _adviseReco * 60) {
|
---|
| 492 | secSucc = _adviseReco * 60 + 1;
|
---|
| 493 | }
|
---|
| 494 | numSucc = 0;
|
---|
| 495 | currPause = initPause;
|
---|
| 496 | _decodePause.setDate(QDate());
|
---|
| 497 | _decodePause.setTime(QTime());
|
---|
[658] | 498 | }
|
---|
[940] | 499 | else {
|
---|
| 500 | secFail += _inspSegm;
|
---|
| 501 | secSucc = 0;
|
---|
| 502 | if (secFail > _adviseFail * 60) {
|
---|
| 503 | secFail = _adviseFail * 60 + 1;
|
---|
| 504 | }
|
---|
| 505 | if (!_decodePause.isValid() || !_makePause) {
|
---|
| 506 | _decodePause = QDateTime::currentDateTime();
|
---|
| 507 | }
|
---|
| 508 | else {
|
---|
| 509 | _decodePause.setDate(QDate());
|
---|
| 510 | _decodePause.setTime(QTime());
|
---|
| 511 | secFail = secFail + currPause - _inspSegm;
|
---|
| 512 | currPause = currPause * 2;
|
---|
| 513 | if (currPause > 960) {
|
---|
| 514 | currPause = 960;
|
---|
| 515 | }
|
---|
| 516 | }
|
---|
[658] | 517 | }
|
---|
[940] | 518 |
|
---|
| 519 | // End corrupt threshold
|
---|
| 520 | // ---------------------
|
---|
| 521 | if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
|
---|
| 522 | _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 523 | _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
| 524 | emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
|
---|
| 525 | callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
| 526 | endCorrupt = true;
|
---|
| 527 | begCorrupt = false;
|
---|
| 528 | secFail = 0;
|
---|
| 529 | }
|
---|
[658] | 530 | else {
|
---|
[940] | 531 |
|
---|
| 532 | // Begin corrupt threshold
|
---|
| 533 | // -----------------------
|
---|
| 534 | if ( !begCorrupt && secFail > _adviseFail * 60 ) {
|
---|
| 535 | _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
|
---|
| 536 | _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
|
---|
| 537 | emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
|
---|
| 538 | callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
| 539 | begCorrupt = true;
|
---|
| 540 | endCorrupt = false;
|
---|
| 541 | secSucc = 0;
|
---|
| 542 | numSucc = 0;
|
---|
[658] | 543 | }
|
---|
| 544 | }
|
---|
[940] | 545 | decode = true;
|
---|
[658] | 546 | }
|
---|
[650] | 547 | }
|
---|
| 548 | }
|
---|
[940] | 549 |
|
---|
| 550 | // End outage threshold
|
---|
| 551 | // --------------------
|
---|
| 552 | if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
|
---|
| 553 | _decodeStart.setDate(QDate());
|
---|
| 554 | _decodeStart.setTime(QTime());
|
---|
| 555 | if (_inspSegm>0) {
|
---|
| 556 | _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 557 | _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
| 558 | emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
|
---|
| 559 | callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
| 560 | }
|
---|
[686] | 561 | }
|
---|
[658] | 562 |
|
---|
[331] | 563 | delete [] data;
|
---|
[1044] | 564 |
|
---|
[621] | 565 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 566 | while (it.hasNext()) {
|
---|
| 567 | p_obs obs = it.next();
|
---|
| 568 |
|
---|
[351] | 569 | // Check observation epoch
|
---|
| 570 | // -----------------------
|
---|
[715] | 571 | int week;
|
---|
| 572 | double sec;
|
---|
[1035] | 573 | leapsecGPSWeeks(week, sec);
|
---|
[351] | 574 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
| 575 |
|
---|
[622] | 576 | if (week < obs->_o.GPSWeek) {
|
---|
[351] | 577 | week += 1;
|
---|
| 578 | sec -= secPerWeek;
|
---|
| 579 | }
|
---|
[622] | 580 | if (week > obs->_o.GPSWeek) {
|
---|
[351] | 581 | week -= 1;
|
---|
| 582 | sec += secPerWeek;
|
---|
| 583 | }
|
---|
[622] | 584 | double dt = fabs(sec - obs->_o.GPSWeeks);
|
---|
| 585 | if (week != obs->_o.GPSWeek || dt > maxDt) {
|
---|
[658] | 586 | if (!wrongEpoch) {
|
---|
[727] | 587 | emit( newMessage(_staID + ": Wrong observation epoch(s)") );
|
---|
[658] | 588 | wrongEpoch = true;
|
---|
| 589 | }
|
---|
[621] | 590 | delete obs;
|
---|
[351] | 591 | continue;
|
---|
| 592 | }
|
---|
[658] | 593 | else {
|
---|
| 594 | wrongEpoch = false;
|
---|
[709] | 595 |
|
---|
[728] | 596 | // Latency and completeness
|
---|
| 597 | // ------------------------
|
---|
| 598 | if (_perfIntr>0) {
|
---|
[717] | 599 | newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
|
---|
| 600 | if (newSecGPS != oldSecGPS) {
|
---|
[728] | 601 | if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
|
---|
[709] | 602 | if (numLat>0) {
|
---|
[728] | 603 | if (meanDiff>0.) {
|
---|
| 604 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs, %6 gaps")
|
---|
| 605 | .arg(_staID.data())
|
---|
| 606 | .arg(int(sumLat/numLat*100)/100.)
|
---|
| 607 | .arg(int(minLat*100)/100.)
|
---|
| 608 | .arg(int(maxLat*100)/100.)
|
---|
| 609 | .arg(numLat)
|
---|
| 610 | .arg(numGaps)
|
---|
| 611 | .toAscii()) );
|
---|
| 612 | } else {
|
---|
| 613 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs")
|
---|
| 614 | .arg(_staID.data())
|
---|
| 615 | .arg(int(sumLat/numLat*100)/100.)
|
---|
| 616 | .arg(int(minLat*100)/100.)
|
---|
| 617 | .arg(int(maxLat*100)/100.)
|
---|
| 618 | .arg(numLat)
|
---|
| 619 | .toAscii()) );
|
---|
| 620 | }
|
---|
[709] | 621 | }
|
---|
[728] | 622 | meanDiff = diffSecGPS/numLat;
|
---|
| 623 | diffSecGPS = 0;
|
---|
| 624 | numGaps = 0;
|
---|
[709] | 625 | sumLat = 0.;
|
---|
| 626 | numLat = 0;
|
---|
| 627 | minLat = maxDt;
|
---|
| 628 | maxLat = -maxDt;
|
---|
| 629 | }
|
---|
[728] | 630 | if (followSec) {
|
---|
| 631 | diffSecGPS += newSecGPS - oldSecGPS;
|
---|
| 632 | if (meanDiff>0.) {
|
---|
| 633 | if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
|
---|
| 634 | numGaps += 1;
|
---|
| 635 | }
|
---|
| 636 | }
|
---|
| 637 | }
|
---|
[1035] | 638 | curLat = sec - obs->_o.GPSWeeks;
|
---|
[709] | 639 | sumLat += curLat;
|
---|
| 640 | if (curLat < minLat) minLat = curLat;
|
---|
| 641 | if (curLat >= maxLat) maxLat = curLat;
|
---|
| 642 | numLat += 1;
|
---|
[717] | 643 | oldSecGPS = newSecGPS;
|
---|
[728] | 644 | followSec = true;
|
---|
[709] | 645 | }
|
---|
| 646 | }
|
---|
[658] | 647 | }
|
---|
[351] | 648 |
|
---|
[408] | 649 | // RINEX Output
|
---|
| 650 | // ------------
|
---|
| 651 | if (_rnx) {
|
---|
[1044] | 652 | bool dump = true;
|
---|
| 653 |
|
---|
| 654 | ////RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
|
---|
| 655 | ////if ( decoder2 && !_rnx_set_position ) {
|
---|
| 656 | //// double stax, stay, staz;
|
---|
| 657 | //// if ( decoder2->getStaCrd(stax, stay, staz) == success ) {
|
---|
| 658 | //// _rnx->setApproxPos(stax, stay, staz);
|
---|
| 659 | //// _rnx_set_position = true;
|
---|
| 660 | //// }
|
---|
| 661 | //// else {
|
---|
| 662 | //// dump = false;
|
---|
| 663 | //// }
|
---|
| 664 | ////}
|
---|
| 665 |
|
---|
| 666 | if ( dump ) {
|
---|
| 667 | long iSec = long(floor(obs->_o.GPSWeeks+0.5));
|
---|
| 668 | long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
|
---|
| 669 | if (_samplingRate == 0 || iSec % _samplingRate == 0) {
|
---|
| 670 | _rnx->deepCopy(obs);
|
---|
| 671 | }
|
---|
| 672 | _rnx->dumpEpoch(newTime);
|
---|
[1029] | 673 | }
|
---|
| 674 | }
|
---|
[408] | 675 |
|
---|
[1044] | 676 | // Emit new observation signal
|
---|
| 677 | // ---------------------------
|
---|
[621] | 678 | bool firstObs = (obs == _decoder->_obsList.first());
|
---|
[624] | 679 | obs->_status = t_obs::posted;
|
---|
[621] | 680 | emit newObs(_staID, firstObs, obs);
|
---|
[249] | 681 | }
|
---|
| 682 | _decoder->_obsList.clear();
|
---|
[1030] | 683 |
|
---|
| 684 | // RTCM message types
|
---|
| 685 | // ------------------
|
---|
| 686 | if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
|
---|
| 687 | if (0<_decoder->_typeList.size()) {
|
---|
| 688 | QString type;
|
---|
| 689 | for (int ii=0;ii<_decoder->_typeList.size();ii++) {
|
---|
| 690 | type = QString("%1 ").arg(_decoder->_typeList[ii]);
|
---|
| 691 | if (type != "") {
|
---|
[1035] | 692 | emit(newMessage(_staID + ": Received message type " + type.toAscii() ));
|
---|
| 693 | }
|
---|
[1030] | 694 | }
|
---|
| 695 | }
|
---|
| 696 | }
|
---|
| 697 | _decoder->_typeList.clear();
|
---|
[249] | 698 | }
|
---|
[1030] | 699 |
|
---|
| 700 | // Timeout, reconnect
|
---|
| 701 | // ------------------
|
---|
[249] | 702 | else {
|
---|
| 703 | emit(newMessage(_staID + ": Data Timeout, reconnecting"));
|
---|
| 704 | tryReconnect();
|
---|
| 705 | }
|
---|
[35] | 706 | }
|
---|
[249] | 707 | catch (const char* msg) {
|
---|
| 708 | emit(newMessage(_staID + msg));
|
---|
[136] | 709 | tryReconnect();
|
---|
[35] | 710 | }
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | // Exit
|
---|
| 715 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 716 | void bncGetThread::exit(int exitCode) {
|
---|
| 717 | if (exitCode!= 0) {
|
---|
[88] | 718 | emit error(_staID);
|
---|
[35] | 719 | }
|
---|
| 720 | QThread::exit(exitCode);
|
---|
[148] | 721 | terminate();
|
---|
[35] | 722 | }
|
---|
[82] | 723 |
|
---|
[136] | 724 | // Try Re-Connect
|
---|
| 725 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 726 | void bncGetThread::tryReconnect() {
|
---|
[408] | 727 | if (_rnx) {
|
---|
| 728 | _rnx->setReconnectFlag(true);
|
---|
| 729 | }
|
---|
[658] | 730 | if ( !_decodeStart.isValid()) {
|
---|
| 731 | _decodeStop = QDateTime::currentDateTime();
|
---|
| 732 | }
|
---|
[138] | 733 | while (1) {
|
---|
| 734 | delete _socket; _socket = 0;
|
---|
| 735 | sleep(_nextSleep);
|
---|
| 736 | if ( initRun() == success ) {
|
---|
[658] | 737 | if ( !_decodeStop.isValid()) {
|
---|
| 738 | _decodeStart = QDateTime::currentDateTime();
|
---|
| 739 | }
|
---|
[138] | 740 | break;
|
---|
| 741 | }
|
---|
| 742 | else {
|
---|
[658] | 743 |
|
---|
| 744 | // Begin outage threshold
|
---|
| 745 | // ----------------------
|
---|
[668] | 746 | if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
|
---|
[658] | 747 | _decodeStop.setDate(QDate());
|
---|
| 748 | _decodeStop.setTime(QTime());
|
---|
[686] | 749 | if (_inspSegm>0) {
|
---|
[699] | 750 | _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
|
---|
| 751 | _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 752 | emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
|
---|
[696] | 753 | callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
[686] | 754 | }
|
---|
[658] | 755 | }
|
---|
[138] | 756 | _nextSleep *= 2;
|
---|
[442] | 757 | if (_nextSleep > 256) {
|
---|
| 758 | _nextSleep = 256;
|
---|
[152] | 759 | }
|
---|
[277] | 760 | _nextSleep += rand() % 6;
|
---|
[138] | 761 | }
|
---|
| 762 | }
|
---|
| 763 | _nextSleep = 1;
|
---|
[136] | 764 | }
|
---|
[658] | 765 |
|
---|
[668] | 766 | // Call advisory notice script
|
---|
[658] | 767 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 768 | void bncGetThread::callScript(const char* _comment) {
|
---|
[672] | 769 | QMutexLocker locker(&_mutex);
|
---|
[668] | 770 | if (!_adviseScript.isEmpty()) {
|
---|
[672] | 771 | msleep(1);
|
---|
[658] | 772 | #ifdef WIN32
|
---|
[668] | 773 | QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
|
---|
[658] | 774 | #else
|
---|
[668] | 775 | QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
|
---|
[658] | 776 | #endif
|
---|
| 777 | }
|
---|
| 778 | }
|
---|
[1044] | 779 |
|
---|
| 780 | //
|
---|
| 781 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 782 | void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
|
---|
| 783 | RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
|
---|
| 784 |
|
---|
| 785 | if ( decoder ) {
|
---|
| 786 | QMutexLocker locker(&_mutex);
|
---|
| 787 |
|
---|
| 788 | decoder->storeEph(gpseph);
|
---|
| 789 | }
|
---|
| 790 | }
|
---|
| 791 |
|
---|