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

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

* empty log message *

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