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

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

* empty log message *

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