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

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

* empty log message *

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