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

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

* empty log message *

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