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

Last change on this file since 1310 was 1310, checked in by mervart, 15 years ago

* empty log message *

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