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

Last change on this file since 1322 was 1320, checked in by mervart, 17 years ago

* empty log message *

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