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