[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.;
|
---|
[1052] | 418 | double sumLatQ = 0.;
|
---|
[728] | 419 | double meanDiff = 0.;
|
---|
[709] | 420 | double minLat = maxDt;
|
---|
| 421 | double maxLat = -maxDt;
|
---|
| 422 | double curLat = 0.;
|
---|
[699] | 423 |
|
---|
| 424 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 425 | _decodeSucc = QDateTime::currentDateTime();
|
---|
[229] | 426 | t_irc irc = initRun();
|
---|
| 427 |
|
---|
| 428 | if (irc == fatal) {
|
---|
[192] | 429 | QThread::exit(1);
|
---|
| 430 | return;
|
---|
| 431 | }
|
---|
[229] | 432 | else if (irc != success) {
|
---|
[190] | 433 | emit(newMessage(_staID + ": initRun failed, reconnecting"));
|
---|
[138] | 434 | tryReconnect();
|
---|
| 435 | }
|
---|
[136] | 436 |
|
---|
[658] | 437 | if (initPause < _inspSegm) {
|
---|
| 438 | initPause = _inspSegm;
|
---|
| 439 | }
|
---|
[727] | 440 | if(!_makePause) {initPause = 0;}
|
---|
[658] | 441 | currPause = initPause;
|
---|
| 442 |
|
---|
[35] | 443 | // Read Incoming Data
|
---|
| 444 | // ------------------
|
---|
| 445 | while (true) {
|
---|
[629] | 446 | try {
|
---|
| 447 | if (_socket->state() != QAbstractSocket::ConnectedState) {
|
---|
| 448 | emit(newMessage(_staID + ": Socket not connected, reconnecting"));
|
---|
| 449 | tryReconnect();
|
---|
| 450 | }
|
---|
[621] | 451 |
|
---|
| 452 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 453 | while (it.hasNext()) {
|
---|
| 454 | delete it.next();
|
---|
| 455 | }
|
---|
| 456 | _decoder->_obsList.clear();
|
---|
| 457 |
|
---|
[249] | 458 | _socket->waitForReadyRead(_timeOut);
|
---|
| 459 | qint64 nBytes = _socket->bytesAvailable();
|
---|
| 460 | if (nBytes > 0) {
|
---|
[567] | 461 | emit newBytes(_staID, nBytes);
|
---|
| 462 |
|
---|
[249] | 463 | char* data = new char[nBytes];
|
---|
| 464 | _socket->read(data, nBytes);
|
---|
[406] | 465 |
|
---|
[940] | 466 | if (_inspSegm<1) {
|
---|
| 467 | _decoder->Decode(data, nBytes);
|
---|
| 468 | }
|
---|
| 469 | else {
|
---|
| 470 |
|
---|
| 471 | // Decode data
|
---|
| 472 | // -----------
|
---|
| 473 | if (!_decodePause.isValid() ||
|
---|
| 474 | _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
|
---|
| 475 |
|
---|
| 476 | if (decode) {
|
---|
| 477 | if ( _decoder->Decode(data, nBytes) == success ) {
|
---|
| 478 | numSucc += 1;
|
---|
| 479 | }
|
---|
| 480 | if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
|
---|
| 481 | decode = false;
|
---|
[658] | 482 | }
|
---|
| 483 | }
|
---|
[940] | 484 |
|
---|
| 485 | // Check - once per inspect segment
|
---|
| 486 | // --------------------------------
|
---|
| 487 | if (!decode) {
|
---|
| 488 | _decodeTime = QDateTime::currentDateTime();
|
---|
| 489 | if (numSucc>0) {
|
---|
| 490 | secSucc += _inspSegm;
|
---|
| 491 | _decodeSucc = QDateTime::currentDateTime();
|
---|
| 492 | if (secSucc > _adviseReco * 60) {
|
---|
| 493 | secSucc = _adviseReco * 60 + 1;
|
---|
| 494 | }
|
---|
| 495 | numSucc = 0;
|
---|
| 496 | currPause = initPause;
|
---|
| 497 | _decodePause.setDate(QDate());
|
---|
| 498 | _decodePause.setTime(QTime());
|
---|
[658] | 499 | }
|
---|
[940] | 500 | else {
|
---|
| 501 | secFail += _inspSegm;
|
---|
| 502 | secSucc = 0;
|
---|
| 503 | if (secFail > _adviseFail * 60) {
|
---|
| 504 | secFail = _adviseFail * 60 + 1;
|
---|
| 505 | }
|
---|
| 506 | if (!_decodePause.isValid() || !_makePause) {
|
---|
| 507 | _decodePause = QDateTime::currentDateTime();
|
---|
| 508 | }
|
---|
| 509 | else {
|
---|
| 510 | _decodePause.setDate(QDate());
|
---|
| 511 | _decodePause.setTime(QTime());
|
---|
| 512 | secFail = secFail + currPause - _inspSegm;
|
---|
| 513 | currPause = currPause * 2;
|
---|
| 514 | if (currPause > 960) {
|
---|
| 515 | currPause = 960;
|
---|
| 516 | }
|
---|
| 517 | }
|
---|
[658] | 518 | }
|
---|
[940] | 519 |
|
---|
| 520 | // End corrupt threshold
|
---|
| 521 | // ---------------------
|
---|
| 522 | if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
|
---|
| 523 | _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 524 | _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
| 525 | emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
|
---|
| 526 | callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
| 527 | endCorrupt = true;
|
---|
| 528 | begCorrupt = false;
|
---|
| 529 | secFail = 0;
|
---|
| 530 | }
|
---|
[658] | 531 | else {
|
---|
[940] | 532 |
|
---|
| 533 | // Begin corrupt threshold
|
---|
| 534 | // -----------------------
|
---|
| 535 | if ( !begCorrupt && secFail > _adviseFail * 60 ) {
|
---|
| 536 | _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
|
---|
| 537 | _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
|
---|
| 538 | emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
|
---|
| 539 | callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
| 540 | begCorrupt = true;
|
---|
| 541 | endCorrupt = false;
|
---|
| 542 | secSucc = 0;
|
---|
| 543 | numSucc = 0;
|
---|
[658] | 544 | }
|
---|
| 545 | }
|
---|
[940] | 546 | decode = true;
|
---|
[658] | 547 | }
|
---|
[650] | 548 | }
|
---|
| 549 | }
|
---|
[940] | 550 |
|
---|
| 551 | // End outage threshold
|
---|
| 552 | // --------------------
|
---|
| 553 | if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
|
---|
| 554 | _decodeStart.setDate(QDate());
|
---|
| 555 | _decodeStart.setTime(QTime());
|
---|
| 556 | if (_inspSegm>0) {
|
---|
| 557 | _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
| 558 | _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
| 559 | emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
|
---|
| 560 | callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
| 561 | }
|
---|
[686] | 562 | }
|
---|
[658] | 563 |
|
---|
[331] | 564 | delete [] data;
|
---|
[1044] | 565 |
|
---|
[621] | 566 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
| 567 | while (it.hasNext()) {
|
---|
| 568 | p_obs obs = it.next();
|
---|
| 569 |
|
---|
[351] | 570 | // Check observation epoch
|
---|
| 571 | // -----------------------
|
---|
[715] | 572 | int week;
|
---|
| 573 | double sec;
|
---|
[1035] | 574 | leapsecGPSWeeks(week, sec);
|
---|
[351] | 575 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
| 576 |
|
---|
[622] | 577 | if (week < obs->_o.GPSWeek) {
|
---|
[351] | 578 | week += 1;
|
---|
| 579 | sec -= secPerWeek;
|
---|
| 580 | }
|
---|
[622] | 581 | if (week > obs->_o.GPSWeek) {
|
---|
[351] | 582 | week -= 1;
|
---|
| 583 | sec += secPerWeek;
|
---|
| 584 | }
|
---|
[622] | 585 | double dt = fabs(sec - obs->_o.GPSWeeks);
|
---|
| 586 | if (week != obs->_o.GPSWeek || dt > maxDt) {
|
---|
[658] | 587 | if (!wrongEpoch) {
|
---|
[727] | 588 | emit( newMessage(_staID + ": Wrong observation epoch(s)") );
|
---|
[658] | 589 | wrongEpoch = true;
|
---|
| 590 | }
|
---|
[621] | 591 | delete obs;
|
---|
[351] | 592 | continue;
|
---|
| 593 | }
|
---|
[658] | 594 | else {
|
---|
| 595 | wrongEpoch = false;
|
---|
[709] | 596 |
|
---|
[728] | 597 | // Latency and completeness
|
---|
| 598 | // ------------------------
|
---|
| 599 | if (_perfIntr>0) {
|
---|
[717] | 600 | newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
|
---|
| 601 | if (newSecGPS != oldSecGPS) {
|
---|
[728] | 602 | if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
|
---|
[709] | 603 | if (numLat>0) {
|
---|
[728] | 604 | if (meanDiff>0.) {
|
---|
[1052] | 605 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
|
---|
[728] | 606 | .arg(_staID.data())
|
---|
| 607 | .arg(int(sumLat/numLat*100)/100.)
|
---|
| 608 | .arg(int(minLat*100)/100.)
|
---|
| 609 | .arg(int(maxLat*100)/100.)
|
---|
[1052] | 610 | .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
|
---|
[728] | 611 | .arg(numLat)
|
---|
| 612 | .arg(numGaps)
|
---|
| 613 | .toAscii()) );
|
---|
| 614 | } else {
|
---|
[1052] | 615 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
|
---|
[728] | 616 | .arg(_staID.data())
|
---|
| 617 | .arg(int(sumLat/numLat*100)/100.)
|
---|
| 618 | .arg(int(minLat*100)/100.)
|
---|
| 619 | .arg(int(maxLat*100)/100.)
|
---|
[1052] | 620 | .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
|
---|
[728] | 621 | .arg(numLat)
|
---|
| 622 | .toAscii()) );
|
---|
| 623 | }
|
---|
[709] | 624 | }
|
---|
[728] | 625 | meanDiff = diffSecGPS/numLat;
|
---|
| 626 | diffSecGPS = 0;
|
---|
| 627 | numGaps = 0;
|
---|
[709] | 628 | sumLat = 0.;
|
---|
[1052] | 629 | sumLatQ = 0.;
|
---|
[709] | 630 | numLat = 0;
|
---|
| 631 | minLat = maxDt;
|
---|
| 632 | maxLat = -maxDt;
|
---|
| 633 | }
|
---|
[728] | 634 | if (followSec) {
|
---|
| 635 | diffSecGPS += newSecGPS - oldSecGPS;
|
---|
| 636 | if (meanDiff>0.) {
|
---|
| 637 | if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
|
---|
| 638 | numGaps += 1;
|
---|
| 639 | }
|
---|
| 640 | }
|
---|
| 641 | }
|
---|
[1035] | 642 | curLat = sec - obs->_o.GPSWeeks;
|
---|
[709] | 643 | sumLat += curLat;
|
---|
[1052] | 644 | sumLatQ += curLat * curLat;
|
---|
[709] | 645 | if (curLat < minLat) minLat = curLat;
|
---|
| 646 | if (curLat >= maxLat) maxLat = curLat;
|
---|
| 647 | numLat += 1;
|
---|
[717] | 648 | oldSecGPS = newSecGPS;
|
---|
[728] | 649 | followSec = true;
|
---|
[709] | 650 | }
|
---|
| 651 | }
|
---|
[658] | 652 | }
|
---|
[351] | 653 |
|
---|
[408] | 654 | // RINEX Output
|
---|
| 655 | // ------------
|
---|
| 656 | if (_rnx) {
|
---|
[1044] | 657 | bool dump = true;
|
---|
| 658 |
|
---|
| 659 | ////RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
|
---|
| 660 | ////if ( decoder2 && !_rnx_set_position ) {
|
---|
| 661 | //// double stax, stay, staz;
|
---|
| 662 | //// if ( decoder2->getStaCrd(stax, stay, staz) == success ) {
|
---|
| 663 | //// _rnx->setApproxPos(stax, stay, staz);
|
---|
| 664 | //// _rnx_set_position = true;
|
---|
| 665 | //// }
|
---|
| 666 | //// else {
|
---|
| 667 | //// dump = false;
|
---|
| 668 | //// }
|
---|
| 669 | ////}
|
---|
| 670 |
|
---|
| 671 | if ( dump ) {
|
---|
| 672 | long iSec = long(floor(obs->_o.GPSWeeks+0.5));
|
---|
| 673 | long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
|
---|
| 674 | if (_samplingRate == 0 || iSec % _samplingRate == 0) {
|
---|
| 675 | _rnx->deepCopy(obs);
|
---|
| 676 | }
|
---|
| 677 | _rnx->dumpEpoch(newTime);
|
---|
[1029] | 678 | }
|
---|
| 679 | }
|
---|
[408] | 680 |
|
---|
[1044] | 681 | // Emit new observation signal
|
---|
| 682 | // ---------------------------
|
---|
[621] | 683 | bool firstObs = (obs == _decoder->_obsList.first());
|
---|
[624] | 684 | obs->_status = t_obs::posted;
|
---|
[621] | 685 | emit newObs(_staID, firstObs, obs);
|
---|
[249] | 686 | }
|
---|
| 687 | _decoder->_obsList.clear();
|
---|
[1030] | 688 |
|
---|
| 689 | // RTCM message types
|
---|
| 690 | // ------------------
|
---|
| 691 | if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
|
---|
| 692 | if (0<_decoder->_typeList.size()) {
|
---|
| 693 | QString type;
|
---|
| 694 | for (int ii=0;ii<_decoder->_typeList.size();ii++) {
|
---|
| 695 | type = QString("%1 ").arg(_decoder->_typeList[ii]);
|
---|
| 696 | if (type != "") {
|
---|
[1035] | 697 | emit(newMessage(_staID + ": Received message type " + type.toAscii() ));
|
---|
| 698 | }
|
---|
[1030] | 699 | }
|
---|
| 700 | }
|
---|
| 701 | }
|
---|
| 702 | _decoder->_typeList.clear();
|
---|
[249] | 703 | }
|
---|
[1030] | 704 |
|
---|
| 705 | // Timeout, reconnect
|
---|
| 706 | // ------------------
|
---|
[249] | 707 | else {
|
---|
| 708 | emit(newMessage(_staID + ": Data Timeout, reconnecting"));
|
---|
| 709 | tryReconnect();
|
---|
| 710 | }
|
---|
[35] | 711 | }
|
---|
[249] | 712 | catch (const char* msg) {
|
---|
| 713 | emit(newMessage(_staID + msg));
|
---|
[136] | 714 | tryReconnect();
|
---|
[35] | 715 | }
|
---|
| 716 | }
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | // Exit
|
---|
| 720 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 721 | void bncGetThread::exit(int exitCode) {
|
---|
| 722 | if (exitCode!= 0) {
|
---|
[88] | 723 | emit error(_staID);
|
---|
[35] | 724 | }
|
---|
| 725 | QThread::exit(exitCode);
|
---|
[148] | 726 | terminate();
|
---|
[35] | 727 | }
|
---|
[82] | 728 |
|
---|
[136] | 729 | // Try Re-Connect
|
---|
| 730 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 731 | void bncGetThread::tryReconnect() {
|
---|
[408] | 732 | if (_rnx) {
|
---|
| 733 | _rnx->setReconnectFlag(true);
|
---|
| 734 | }
|
---|
[658] | 735 | if ( !_decodeStart.isValid()) {
|
---|
| 736 | _decodeStop = QDateTime::currentDateTime();
|
---|
| 737 | }
|
---|
[138] | 738 | while (1) {
|
---|
| 739 | delete _socket; _socket = 0;
|
---|
| 740 | sleep(_nextSleep);
|
---|
| 741 | if ( initRun() == success ) {
|
---|
[658] | 742 | if ( !_decodeStop.isValid()) {
|
---|
| 743 | _decodeStart = QDateTime::currentDateTime();
|
---|
| 744 | }
|
---|
[138] | 745 | break;
|
---|
| 746 | }
|
---|
| 747 | else {
|
---|
[658] | 748 |
|
---|
| 749 | // Begin outage threshold
|
---|
| 750 | // ----------------------
|
---|
[668] | 751 | if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
|
---|
[658] | 752 | _decodeStop.setDate(QDate());
|
---|
| 753 | _decodeStop.setTime(QTime());
|
---|
[686] | 754 | if (_inspSegm>0) {
|
---|
[699] | 755 | _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
|
---|
| 756 | _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
|
---|
[701] | 757 | emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
|
---|
[696] | 758 | callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
[686] | 759 | }
|
---|
[658] | 760 | }
|
---|
[138] | 761 | _nextSleep *= 2;
|
---|
[442] | 762 | if (_nextSleep > 256) {
|
---|
| 763 | _nextSleep = 256;
|
---|
[152] | 764 | }
|
---|
[277] | 765 | _nextSleep += rand() % 6;
|
---|
[138] | 766 | }
|
---|
| 767 | }
|
---|
| 768 | _nextSleep = 1;
|
---|
[136] | 769 | }
|
---|
[658] | 770 |
|
---|
[668] | 771 | // Call advisory notice script
|
---|
[658] | 772 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 773 | void bncGetThread::callScript(const char* _comment) {
|
---|
[672] | 774 | QMutexLocker locker(&_mutex);
|
---|
[668] | 775 | if (!_adviseScript.isEmpty()) {
|
---|
[672] | 776 | msleep(1);
|
---|
[658] | 777 | #ifdef WIN32
|
---|
[668] | 778 | QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
|
---|
[658] | 779 | #else
|
---|
[668] | 780 | QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
|
---|
[658] | 781 | #endif
|
---|
| 782 | }
|
---|
| 783 | }
|
---|
[1044] | 784 |
|
---|
| 785 | //
|
---|
| 786 | //////////////////////////////////////////////////////////////////////////////
|
---|
| 787 | void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
|
---|
| 788 | RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
|
---|
| 789 |
|
---|
| 790 | if ( decoder ) {
|
---|
| 791 | QMutexLocker locker(&_mutex);
|
---|
| 792 |
|
---|
| 793 | decoder->storeEph(gpseph);
|
---|
| 794 | }
|
---|
| 795 | }
|
---|
| 796 |
|
---|