source: ntrip/trunk/BNC/bncgetthread.cpp@ 1299

Last change on this file since 1299 was 1299, checked in by zdenek, 15 years ago

* empty log message *

File size: 32.9 KB
RevLine 
[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>
[1218]43#include <sstream>
[277]44
[35]45#include <QFile>
46#include <QTextStream>
47#include <QtNetwork>
[356]48#include <QTime>
[35]49
50#include "bncgetthread.h"
[192]51#include "bnctabledlg.h"
[243]52#include "bncapp.h"
[352]53#include "bncutils.h"
[408]54#include "bncrinex.h"
[423]55#include "bnczerodecoder.h"
[65]56
[243]57#include "RTCM/RTCM2Decoder.h"
[297]58#include "RTCM3/RTCM3Decoder.h"
[293]59#include "RTIGS/RTIGSDecoder.h"
[35]60
61using namespace std;
62
[1143]63// Constructor 1
[35]64////////////////////////////////////////////////////////////////////////////
[1138]65bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
66 const QByteArray& format) {
67
[1141]68 _format = format;
[1139]69
[1159]70 int iSep = rawInpFileName.lastIndexOf(QDir::separator());
71 _staID = rawInpFileName.mid(iSep+1,4);
72
[1139]73 initialize();
74
75 _inspSegm = 0;
76
[1138]77 _rawInpFile = new QFile(rawInpFileName);
78 _rawInpFile->open(QIODevice::ReadOnly);
[1147]79
80 if (!_rnx) {
81 cerr << "no RINEX path specified" << endl;
82 ::exit(0);
83 }
[1138]84}
85
[1143]86// Constructor 2
87////////////////////////////////////////////////////////////////////////////
[278]88bncGetThread::bncGetThread(const QUrl& mountPoint,
[366]89 const QByteArray& format,
90 const QByteArray& latitude,
91 const QByteArray& longitude,
92 const QByteArray& nmea, int iMount) {
[605]93
94 setTerminationEnabled(true);
95
[350]96 _mountPoint = mountPoint;
97 _staID = mountPoint.path().mid(1).toAscii();
98 _format = format;
[366]99 _latitude = latitude;
100 _longitude = longitude;
101 _nmea = nmea;
[1139]102 _iMount = iMount; // index in mountpoints array
103
104 initialize();
105}
106
[1143]107// Initialization
108////////////////////////////////////////////////////////////////////////////
[1139]109void bncGetThread::initialize() {
110
[1225]111
112 bncApp* app = (bncApp*) qApp;
[1299]113 app->connect(this, SIGNAL(newMessage(QByteArray,bool)),
114 app, SLOT(slotMessage(const QByteArray,bool)));
[1225]115
[1139]116 _decoder = 0;
[350]117 _socket = 0;
[1139]118 _timeOut = 20*1000; // 20 seconds
119 _nextSleep = 1; // 1 second
[1138]120 _rawInpFile = 0;
[1139]121 _rawOutFile = 0;
[1143]122 _staID_orig = _staID;
[255]123
124 // Check name conflict
125 // -------------------
126 QSettings settings;
127 QListIterator<QString> it(settings.value("mountPoints").toStringList());
128 int num = 0;
[278]129 int ind = -1;
[255]130 while (it.hasNext()) {
[278]131 ++ind;
[255]132 QStringList hlp = it.next().split(" ");
133 if (hlp.size() <= 1) continue;
134 QUrl url(hlp[0]);
135 if (_mountPoint.path() == url.path()) {
[1162]136 if (_iMount > ind || _iMount < 0) {
[278]137 ++num;
[255]138 }
139 }
140 }
[278]141
142 if (num > 0) {
143 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
[255]144 }
[408]145
[658]146 // Notice threshold
147 // ----------------
[686]148 _inspSegm = 50;
149 if ( settings.value("obsRate").toString().isEmpty() ) { _inspSegm = 0; }
[692]150 if ( settings.value("obsRate").toString().indexOf("5 Hz") != -1 ) { _inspSegm = 2; }
[686]151 if ( settings.value("obsRate").toString().indexOf("1 Hz") != -1 ) { _inspSegm = 10; }
152 if ( settings.value("obsRate").toString().indexOf("0.5 Hz") != -1 ) { _inspSegm = 20; }
153 if ( settings.value("obsRate").toString().indexOf("0.2 Hz") != -1 ) { _inspSegm = 40; }
154 if ( settings.value("obsRate").toString().indexOf("0.1 Hz") != -1 ) { _inspSegm = 50; }
[668]155 _adviseFail = settings.value("adviseFail").toInt();
156 _adviseReco = settings.value("adviseReco").toInt();
[722]157 _makePause = false;
158 if ( Qt::CheckState(settings.value("makePause").toInt()) == Qt::Checked) {_makePause = true; }
[668]159 _adviseScript = settings.value("adviseScript").toString();
160 expandEnvVar(_adviseScript);
[658]161
[709]162 // Latency interval/average
163 // ------------------------
[728]164 _perfIntr = 86400;
165 if ( settings.value("perfIntr").toString().isEmpty() ) { _perfIntr = 0; }
166 if ( settings.value("perfIntr").toString().indexOf("1 min") != -1 ) { _perfIntr = 60; }
167 if ( settings.value("perfIntr").toString().indexOf("5 min") != -1 ) { _perfIntr = 300; }
168 if ( settings.value("perfIntr").toString().indexOf("15 min") != -1 ) { _perfIntr = 900; }
169 if ( settings.value("perfIntr").toString().indexOf("1 hour") != -1 ) { _perfIntr = 3600; }
170 if ( settings.value("perfIntr").toString().indexOf("6 hours") != -1 ) { _perfIntr = 21600; }
171 if ( settings.value("perfIntr").toString().indexOf("1 day") != -1 ) { _perfIntr = 86400; }
[709]172
[1030]173 // RTCM message types
174 // ------------------
175 _checkMountPoint = settings.value("messTypes").toString();
176
[408]177 // RINEX writer
178 // ------------
179 _samplingRate = settings.value("rnxSampl").toInt();
180 if ( settings.value("rnxPath").toString().isEmpty() ) {
181 _rnx = 0;
182 }
183 else {
[1139]184 _rnx = new bncRinex(_staID, _mountPoint,
185 _format, _latitude, _longitude, _nmea);
[408]186 }
[1044]187 _rnx_set_position = false;
[408]188
[1168]189 connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
190 this, SLOT(slotNewEphGPS(gpsephemeris)));
191
[1137]192 // Raw Output
193 // ----------
[1140]194 // QByteArray rawOutFileName = "./" + _staID + ".raw";
195 // _rawOutFile = new QFile(rawOutFileName);
196 // _rawOutFile->open(QIODevice::WriteOnly);
[1137]197
[319]198 msleep(100); //sleep 0.1 sec
[35]199}
200
201// Destructor
202////////////////////////////////////////////////////////////////////////////
203bncGetThread::~bncGetThread() {
[515]204 if (_socket) {
205 _socket->close();
[613]206#if QT_VERSION == 0x040203
207 delete _socket;
208#else
[612]209 _socket->deleteLater();
[613]210#endif
[515]211 }
[617]212 delete _decoder;
[1044]213 delete _rnx;
[1138]214 delete _rawInpFile;
215 delete _rawOutFile;
[35]216}
217
[1292]218#define AGENTVERSION "1.7"
[35]219// Connect to Caster, send the Request (static)
220////////////////////////////////////////////////////////////////////////////
[366]221QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
222 QByteArray& latitude, QByteArray& longitude,
223 QByteArray& nmea, int timeOut,
[136]224 QString& msg) {
[35]225
[88]226 // Connect the Socket
227 // ------------------
[1280]228 QSettings settings;
229 QString proxyHost = settings.value("proxyHost").toString();
230 int proxyPort = settings.value("proxyPort").toInt();
231
[35]232 QTcpSocket* socket = new QTcpSocket();
[1280]233 if ( proxyHost.isEmpty() ) {
234 socket->connectToHost(mountPoint.host(), mountPoint.port());
235 }
236 else {
237 socket->connectToHost(proxyHost, proxyPort);
238 }
[35]239 if (!socket->waitForConnected(timeOut)) {
[82]240 msg += "Connect timeout\n";
[35]241 delete socket;
242 return 0;
243 }
244
245 // Send Request
246 // ------------
[443]247 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
248 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
[645]249 QByteArray userAndPwd;
[92]250
[645]251 if(!uName.isEmpty() || !passW.isEmpty())
252 {
253 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
254 passW.toAscii()).toBase64() + "\r\n";
255 }
256
[92]257 QUrl hlp;
258 hlp.setScheme("http");
259 hlp.setHost(mountPoint.host());
260 hlp.setPort(mountPoint.port());
261 hlp.setPath(mountPoint.path());
[1280]262
[1298]263 QByteArray reqStr;
264 if ( proxyHost.isEmpty() ) {
265 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
266 reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n"
267 + "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
268 + userAndPwd + "\r\n";
269 } else {
270 reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n"
271 + "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
272 + "Host: " + hlp.host().toAscii() + "\r\n"
273 + userAndPwd + "\r\n";
274 }
[205]275
[1020]276 // NMEA string to handle VRS stream
277 // --------------------------------
[356]278 double lat, lon;
[366]279
280 lat = strtod(latitude,NULL);
281 lon = strtod(longitude,NULL);
282
[410]283 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
[356]284 const char* flagN="N";
285 const char* flagE="E";
286 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
287 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
288 if (lon < -180.) {lon=(lon+360.); flagE="E";}
289 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
[566]290 QTime ttime(QDateTime::currentDateTime().toUTC().time());
[356]291 int lat_deg = (int)lat;
292 double lat_min=(lat-lat_deg)*60.;
293 int lon_deg = (int)lon;
294 double lon_min=(lon-lon_deg)*60.;
295 int hh = 0 , mm = 0;
296 double ss = 0.0;
297 hh=ttime.hour();
298 mm=ttime.minute();
299 ss=(double)ttime.second()+0.001*ttime.msec();
300 QString gga;
301 gga += "GPGGA,";
302 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'));
303 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
304 gga += flagN;
305 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
306 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
307 int xori;
308 char XOR = 0;
309 char *Buff =gga.toAscii().data();
310 int iLen = strlen(Buff);
311 for (xori = 0; xori < iLen; xori++) {
312 XOR ^= (char)Buff[xori];
313 }
314 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
315 reqStr += "$";
316 reqStr += gga;
317 reqStr += "\r\n";
318 }
319
[82]320 msg += reqStr;
[35]321
322 socket->write(reqStr, reqStr.length());
323
324 if (!socket->waitForBytesWritten(timeOut)) {
[82]325 msg += "Write timeout\n";
[35]326 delete socket;
327 return 0;
328 }
329
330 return socket;
331}
332
[136]333// Init Run
[35]334////////////////////////////////////////////////////////////////////////////
[138]335t_irc bncGetThread::initRun() {
[35]336
[1138]337 if (!_rawInpFile) {
[35]338
[1138]339 // Initialize Socket
340 // -----------------
341 QString msg;
342 _socket = this->request(_mountPoint, _latitude, _longitude,
343 _nmea, _timeOut, msg);
344 if (!_socket) {
345 return failure;
346 }
347
348 // Read Caster Response
349 // --------------------
350 _socket->waitForReadyRead(_timeOut);
351 if (_socket->canReadLine()) {
352 QString line = _socket->readLine();
353
354 // Skip messages from proxy server
355 // -------------------------------
356 if (line.indexOf("ICY 200 OK") == -1 &&
357 line.indexOf("200 OK") != -1 ) {
358 bool proxyRespond = true;
359 while (true) {
360 if (_socket->canReadLine()) {
361 line = _socket->readLine();
362 if (!proxyRespond) {
363 break;
364 }
365 if (line.trimmed().isEmpty()) {
366 proxyRespond = false;
367 }
[473]368 }
[1138]369 else {
370 _socket->waitForReadyRead(_timeOut);
371 if (_socket->bytesAvailable() <= 0) {
372 break;
373 }
[473]374 }
375 }
[1138]376 }
377
378 if (line.indexOf("Unauthorized") != -1) {
379 QStringList table;
380 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
381 QString net;
382 QStringListIterator it(table);
383 while (it.hasNext()) {
384 QString line = it.next();
385 if (line.indexOf("STR") == 0) {
386 QStringList tags = line.split(";");
387 if (tags.at(1) == _staID_orig) {
388 net = tags.at(7);
389 break;
390 }
[474]391 }
[473]392 }
[1138]393
394 QString reg;
395 it.toFront();
396 while (it.hasNext()) {
397 QString line = it.next();
398 if (line.indexOf("NET") == 0) {
399 QStringList tags = line.split(";");
400 if (tags.at(1) == net) {
401 reg = tags.at(7);
402 break;
403 }
[192]404 }
405 }
[1138]406 emit(newMessage((_staID + ": Caster Response: " + line +
407 " Adjust User-ID and Password Register, see"
[1299]408 "\n " + reg).toAscii(), true));
[1138]409 return fatal;
[192]410 }
[1138]411 if (line.indexOf("ICY 200 OK") != 0) {
[1299]412 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii(), true));
[1138]413 return failure;
[192]414 }
[146]415 }
[1138]416 else {
[1299]417 emit(newMessage(_staID + ": Response Timeout", true));
[144]418 return failure;
[35]419 }
420 }
421
422 // Instantiate the filter
423 // ----------------------
[423]424 if (!_decoder) {
[136]425 if (_format.indexOf("RTCM_2") != -1) {
[1299]426 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format", true));
[1044]427 _decoder = new RTCM2Decoder(_staID.data());
[136]428 }
429 else if (_format.indexOf("RTCM_3") != -1) {
[1299]430 emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format", true));
[880]431 _decoder = new RTCM3Decoder(_staID);
[1299]432 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
433 this, SIGNAL(newMessage(QByteArray,bool)));
[136]434 }
435 else if (_format.indexOf("RTIGS") != -1) {
[1299]436 emit(newMessage("Get Data: " + _staID + " in RTIGS format", true));
[293]437 _decoder = new RTIGSDecoder();
[136]438 }
[867]439 else if (_format.indexOf("ZERO") != -1) {
[1299]440 emit(newMessage("Get Data: " + _staID + " in original format", true));
[424]441 _decoder = new bncZeroDecoder(_staID);
[406]442 }
[136]443 else {
[1299]444 emit(newMessage(_staID + ": Unknown data format " + _format, true));
[1144]445 if (_rawInpFile) {
[1145]446 cerr << "Uknown data format" << endl;
[1144]447 ::exit(0);
448 }
449 else {
450 return fatal;
451 }
[136]452 }
[60]453 }
[138]454 return success;
[136]455}
[59]456
[136]457// Run
458////////////////////////////////////////////////////////////////////////////
459void bncGetThread::run() {
460
[709]461 const double maxDt = 600.0; // Check observation epoch
[699]462 bool wrongEpoch = false;
463 bool decode = true;
464 int numSucc = 0;
465 int secSucc = 0;
466 int secFail = 0;
[709]467 int initPause = 30; // Initial pause for corrupted streams
[699]468 int currPause = 0;
469 bool begCorrupt = false;
470 bool endCorrupt = false;
[728]471 bool followSec = false;
[717]472 int oldSecGPS= 0;
473 int newSecGPS = 0;
[728]474 int numGaps = 0;
475 int diffSecGPS = 0;
[709]476 int numLat = 0;
477 double sumLat = 0.;
[1052]478 double sumLatQ = 0.;
[728]479 double meanDiff = 0.;
[709]480 double minLat = maxDt;
481 double maxLat = -maxDt;
482 double curLat = 0.;
[699]483
484 _decodeTime = QDateTime::currentDateTime();
485 _decodeSucc = QDateTime::currentDateTime();
[229]486 t_irc irc = initRun();
487
488 if (irc == fatal) {
[192]489 QThread::exit(1);
490 return;
491 }
[229]492 else if (irc != success) {
[1299]493 emit(newMessage(_staID + ": initRun failed, reconnecting", true));
[138]494 tryReconnect();
495 }
[136]496
[658]497 if (initPause < _inspSegm) {
498 initPause = _inspSegm;
499 }
[727]500 if(!_makePause) {initPause = 0;}
[658]501 currPause = initPause;
502
[35]503 // Read Incoming Data
504 // ------------------
505 while (true) {
[629]506 try {
[1138]507 if (_socket && _socket->state() != QAbstractSocket::ConnectedState) {
[1299]508 emit(newMessage(_staID + ": Socket not connected, reconnecting", true));
[629]509 tryReconnect();
510 }
[621]511
512 QListIterator<p_obs> it(_decoder->_obsList);
513 while (it.hasNext()) {
514 delete it.next();
515 }
516 _decoder->_obsList.clear();
517
[1138]518 qint64 nBytes = 0;
519
520 if (_socket) {
521 _socket->waitForReadyRead(_timeOut);
522 nBytes = _socket->bytesAvailable();
523 }
524 else if (_rawInpFile) {
[1146]525 const qint64 maxBytes = 1024;
[1138]526 nBytes = maxBytes;
527 }
528
[249]529 if (nBytes > 0) {
[567]530 emit newBytes(_staID, nBytes);
531
[249]532 char* data = new char[nBytes];
[406]533
[1138]534 if (_socket) {
535 _socket->read(data, nBytes);
[1137]536 }
[1138]537 else if (_rawInpFile) {
538 nBytes = _rawInpFile->read(data, nBytes);
539 if (nBytes <= 0) {
540 cout << "no more data" << endl;
541 ::exit(0);
542 }
543 }
[1137]544
[1138]545 if (_rawOutFile) {
546 _rawOutFile->write(data, nBytes);
547 _rawOutFile->flush();
548 }
549
[940]550 if (_inspSegm<1) {
[1218]551 vector<string> errmsg;
552 _decoder->Decode(data, nBytes, errmsg);
553#ifdef DEBUG_RTCM2_2021
554 for (unsigned ii = 0; ii < errmsg.size(); ii++) {
[1299]555 emit newMessage(_staID + ": " + errmsg[ii].c_str(), false);
[1218]556 }
557#endif
[940]558 }
559 else {
560
561 // Decode data
562 // -----------
563 if (!_decodePause.isValid() ||
564 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
565
566 if (decode) {
[1218]567 vector<string> errmsg;
568 if ( _decoder->Decode(data, nBytes, errmsg) == success ) {
[940]569 numSucc += 1;
570 }
571 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
572 decode = false;
[658]573 }
[1218]574#ifdef DEBUG_RTCM2_2021
575 for (unsigned ii = 0; ii < errmsg.size(); ii++) {
[1299]576 emit newMessage(_staID + ": " + errmsg[ii].c_str(), false);
[1218]577 }
578#endif
[658]579 }
[940]580
581 // Check - once per inspect segment
582 // --------------------------------
583 if (!decode) {
584 _decodeTime = QDateTime::currentDateTime();
585 if (numSucc>0) {
586 secSucc += _inspSegm;
587 _decodeSucc = QDateTime::currentDateTime();
588 if (secSucc > _adviseReco * 60) {
589 secSucc = _adviseReco * 60 + 1;
590 }
591 numSucc = 0;
592 currPause = initPause;
593 _decodePause.setDate(QDate());
594 _decodePause.setTime(QTime());
[658]595 }
[940]596 else {
597 secFail += _inspSegm;
598 secSucc = 0;
599 if (secFail > _adviseFail * 60) {
600 secFail = _adviseFail * 60 + 1;
601 }
602 if (!_decodePause.isValid() || !_makePause) {
603 _decodePause = QDateTime::currentDateTime();
604 }
605 else {
606 _decodePause.setDate(QDate());
607 _decodePause.setTime(QTime());
608 secFail = secFail + currPause - _inspSegm;
609 currPause = currPause * 2;
610 if (currPause > 960) {
611 currPause = 960;
612 }
613 }
[658]614 }
[940]615
616 // End corrupt threshold
617 // ---------------------
618 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
619 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
620 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
[1299]621 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended "
622 + _endDateCor + " " + _endTimeCor).toAscii(), true));
[940]623 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
624 endCorrupt = true;
625 begCorrupt = false;
626 secFail = 0;
627 }
[658]628 else {
[940]629
630 // Begin corrupt threshold
631 // -----------------------
632 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
633 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
634 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
[1299]635 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since "
636 + _begDateCor + " " + _begTimeCor).toAscii(), true));
[940]637 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
638 begCorrupt = true;
639 endCorrupt = false;
640 secSucc = 0;
641 numSucc = 0;
[658]642 }
643 }
[940]644 decode = true;
[658]645 }
[650]646 }
647 }
[940]648
649 // End outage threshold
650 // --------------------
651 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
652 _decodeStart.setDate(QDate());
653 _decodeStart.setTime(QTime());
654 if (_inspSegm>0) {
655 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
656 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
[1299]657 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended "
658 + _endDateOut + " " + _endTimeOut).toAscii(), true));
[940]659 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
660 }
[686]661 }
[658]662
[331]663 delete [] data;
[1044]664
[1271]665
666 // RTCM scan output
667 // ----------------
668 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
669
670 // RTCMv3 message types
671 // --------------------
672 if (0<_decoder->_typeList.size()) {
673 QString type;
674 for (int ii=0;ii<_decoder->_typeList.size();ii++) {
675 type = QString("%1 ").arg(_decoder->_typeList[ii]);
[1299]676 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
[1271]677 }
678 }
679
680 // RTCMv3 antenna descriptor
681 // -------------------------
682 if (0<_decoder->_antType.size()) {
683 QString ant1;
684 for (int ii=0;ii<_decoder->_antType.size();ii++) {
685 ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
[1299]686 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
[1271]687 }
688 }
689
690 // antenna XYZ
691 // ------------------
692 if (0<_decoder->_antList.size()) {
693 for (int ii=0;ii<_decoder->_antList.size();++ii) {
694 QByteArray ant1,ant2,ant3, antT;
695 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
696 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
697 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
698 switch (_decoder->_antList[ii].type) {
699 case GPSDecoder::t_antInfo::ARP: antT = "ARP"; break;
700 case GPSDecoder::t_antInfo::APC: antT = "APC"; break;
701 }
[1299]702 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
703 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
704 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
[1271]705 if (_decoder->_antList[ii].height_f) {
706 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
[1299]707 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
[1271]708 }
709 emit(newAntCrd(_staID,
[1272]710 _decoder->_antList[ii].xx, _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
[1271]711 antT));
712 }
713 }
714 }
715 if ( _checkMountPoint == "ANTCRD_ONLY" && _decoder->_antList.size() ) {
716 for (int ii=0;ii<_decoder->_antList.size();++ii) {
717 QByteArray antT;
718 switch (_decoder->_antList[ii].type) {
719 case GPSDecoder::t_antInfo::ARP: antT = "ARP"; break;
720 case GPSDecoder::t_antInfo::APC: antT = "APC"; break;
721 }
722 emit(newAntCrd(_staID,
[1272]723 _decoder->_antList[ii].xx, _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
[1271]724 antT));
725 }
726 }
727
728 _decoder->_typeList.clear();
729 _decoder->_antType.clear();
730 _decoder->_antList.clear();
731
732 // Loop over all observations (observations output)
733 // ------------------------------------------------
[621]734 QListIterator<p_obs> it(_decoder->_obsList);
735 while (it.hasNext()) {
736 p_obs obs = it.next();
737
[351]738 // Check observation epoch
739 // -----------------------
[1142]740 if (!_rawInpFile) {
741 int week;
742 double sec;
[1153]743 currentGPSWeeks(week, sec);
[1142]744 const double secPerWeek = 7.0 * 24.0 * 3600.0;
745
746 if (week < obs->_o.GPSWeek) {
747 week += 1;
748 sec -= secPerWeek;
[658]749 }
[1142]750 if (week > obs->_o.GPSWeek) {
751 week -= 1;
752 sec += secPerWeek;
753 }
754 double dt = fabs(sec - obs->_o.GPSWeeks);
755 if (week != obs->_o.GPSWeek || dt > maxDt) {
756 if (!wrongEpoch) {
[1299]757 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
[1142]758 wrongEpoch = true;
759 }
760 delete obs;
761 continue;
762 }
763 else {
764 wrongEpoch = false;
765
766 // Latency and completeness
767 // ------------------------
768 if (_perfIntr>0) {
769 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
770 if (newSecGPS != oldSecGPS) {
771 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
772 if (numLat>0) {
773 if (meanDiff>0.) {
774 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
775 .arg(_staID.data())
776 .arg(int(sumLat/numLat*100)/100.)
777 .arg(int(minLat*100)/100.)
778 .arg(int(maxLat*100)/100.)
779 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
780 .arg(numLat)
781 .arg(numGaps)
[1299]782 .toAscii(), true) );
[1142]783 } else {
784 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
785 .arg(_staID.data())
786 .arg(int(sumLat/numLat*100)/100.)
787 .arg(int(minLat*100)/100.)
788 .arg(int(maxLat*100)/100.)
789 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
790 .arg(numLat)
[1299]791 .toAscii(), true) );
[1142]792 }
[728]793 }
[1142]794 meanDiff = diffSecGPS/numLat;
795 diffSecGPS = 0;
796 numGaps = 0;
797 sumLat = 0.;
798 sumLatQ = 0.;
799 numLat = 0;
800 minLat = maxDt;
801 maxLat = -maxDt;
[709]802 }
[1142]803 if (followSec) {
804 diffSecGPS += newSecGPS - oldSecGPS;
805 if (meanDiff>0.) {
806 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
807 numGaps += 1;
808 }
[728]809 }
810 }
[1142]811 curLat = sec - obs->_o.GPSWeeks;
812 sumLat += curLat;
813 sumLatQ += curLat * curLat;
814 if (curLat < minLat) minLat = curLat;
815 if (curLat >= maxLat) maxLat = curLat;
816 numLat += 1;
817 oldSecGPS = newSecGPS;
818 followSec = true;
[728]819 }
[709]820 }
821 }
[658]822 }
[351]823
[408]824 // RINEX Output
825 // ------------
826 if (_rnx) {
[1044]827 bool dump = true;
828
[1268]829 //// // RTCMv2 XYZ
830 //// // ----------
831 //// RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
832 //// if ( decoder2 && !_rnx_set_position ) {
833 //// double stax, stay, staz;
834 //// double dL1[3], dL2[3];
835 //// if ( decoder2->getStaCrd(stax, stay, staz,
836 //// dL1[0], dL1[1], dL1[2],
837 //// dL2[0], dL2[1], dL2[2]) == success ) {
838 ////
839 //// if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
840 //// QString ant1;
841 //// ant1 = QString("%1 ").arg(stax,0,'f',4);
842 //// emit(newMessage(_staID + ": ARP X " + ant1.toAscii() + "m" ));
843 //// ant1 = QString("%1 ").arg(stay,0,'f',4);
844 //// emit(newMessage(_staID + ": ARP Y " + ant1.toAscii() + "m" ));
845 //// ant1 = QString("%1 ").arg(staz,0,'f',4);
846 //// emit(newMessage(_staID + ": ARP Z " + ant1.toAscii() + "m" ));
847 //// ant1 = QString("%1 ").arg(dL1[0],0,'f',4);
848 //// emit(newMessage(_staID + ": L1 APC DX " + ant1.toAscii() + "m" ));
849 //// ant1 = QString("%1 ").arg(dL1[1],0,'f',4);
850 //// emit(newMessage(_staID + ": L1 APC DY " + ant1.toAscii() + "m" ));
851 //// ant1 = QString("%1 ").arg(dL1[2],0,'f',4);
852 //// emit(newMessage(_staID + ": L1 APC DZ " + ant1.toAscii() + "m" ));
853 //// ant1 = QString("%1 ").arg(dL2[0],0,'f',4);
854 //// emit(newMessage(_staID + ": L2 APC DX " + ant1.toAscii() + "m" ));
855 //// ant1 = QString("%1 ").arg(dL2[1],0,'f',4);
856 //// emit(newMessage(_staID + ": L2 APC DY " + ant1.toAscii() + "m" ));
857 //// ant1 = QString("%1 ").arg(dL2[2],0,'f',4);
858 //// emit(newMessage(_staID + ": L2 APC DZ " + ant1.toAscii() + "m" ));
859 //// }
860 //// _rnx_set_position = true;
861 //// }
862 //// }
[1218]863
[1044]864 if ( dump ) {
865 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
866 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
867 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
868 _rnx->deepCopy(obs);
869 }
870 _rnx->dumpEpoch(newTime);
[1029]871 }
872 }
[408]873
[1044]874 // Emit new observation signal
875 // ---------------------------
[621]876 bool firstObs = (obs == _decoder->_obsList.first());
[624]877 obs->_status = t_obs::posted;
[621]878 emit newObs(_staID, firstObs, obs);
[249]879 }
880 _decoder->_obsList.clear();
[1030]881
[249]882 }
[1030]883
884 // Timeout, reconnect
885 // ------------------
[249]886 else {
[1299]887 emit(newMessage(_staID + ": Data Timeout, reconnecting", true));
[249]888 tryReconnect();
889 }
[35]890 }
[249]891 catch (const char* msg) {
[1299]892 emit(newMessage(_staID + msg, true));
[136]893 tryReconnect();
[35]894 }
895 }
896}
897
898// Exit
899////////////////////////////////////////////////////////////////////////////
900void bncGetThread::exit(int exitCode) {
901 if (exitCode!= 0) {
[88]902 emit error(_staID);
[35]903 }
904 QThread::exit(exitCode);
[148]905 terminate();
[35]906}
[82]907
[136]908// Try Re-Connect
909////////////////////////////////////////////////////////////////////////////
910void bncGetThread::tryReconnect() {
[408]911 if (_rnx) {
912 _rnx->setReconnectFlag(true);
913 }
[658]914 if ( !_decodeStart.isValid()) {
915 _decodeStop = QDateTime::currentDateTime();
916 }
[138]917 while (1) {
918 delete _socket; _socket = 0;
919 sleep(_nextSleep);
920 if ( initRun() == success ) {
[658]921 if ( !_decodeStop.isValid()) {
922 _decodeStart = QDateTime::currentDateTime();
923 }
[138]924 break;
925 }
926 else {
[658]927
928 // Begin outage threshold
929 // ----------------------
[668]930 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
[658]931 _decodeStop.setDate(QDate());
932 _decodeStop.setTime(QTime());
[686]933 if (_inspSegm>0) {
[699]934 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
935 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
[1299]936 emit(newMessage((_staID + ": Failure threshold exceeded, outage since "
937 + _begDateOut + " " + _begTimeOut).toAscii(), true));
[696]938 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
[686]939 }
[658]940 }
[138]941 _nextSleep *= 2;
[442]942 if (_nextSleep > 256) {
943 _nextSleep = 256;
[152]944 }
[277]945 _nextSleep += rand() % 6;
[138]946 }
947 }
948 _nextSleep = 1;
[136]949}
[658]950
[668]951// Call advisory notice script
[658]952////////////////////////////////////////////////////////////////////////////
953void bncGetThread::callScript(const char* _comment) {
[672]954 QMutexLocker locker(&_mutex);
[668]955 if (!_adviseScript.isEmpty()) {
[672]956 msleep(1);
[658]957#ifdef WIN32
[668]958 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
[658]959#else
[668]960 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
[658]961#endif
962 }
963}
[1044]964
965//
966//////////////////////////////////////////////////////////////////////////////
967void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
968 RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
969
970 if ( decoder ) {
971 QMutexLocker locker(&_mutex);
972
[1218]973 string storedPRN;
974 vector<int> IODs;
975
976 if ( decoder->storeEph(gpseph, storedPRN, IODs) ) {
977#ifdef DEBUG_RTCM2_2021
978 QString msg = _staID + QString(": stored eph %1 IODs").arg(storedPRN.c_str());
979
980 for (unsigned ii = 0; ii < IODs.size(); ii++) {
981 msg += QString(" %1").arg(IODs[ii],4);
982 }
983
[1299]984 emit(newMessage(msg.toAscii(), false));
[1218]985#endif
986 }
[1044]987 }
988}
989
Note: See TracBrowser for help on using the repository browser.