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

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

* empty log message *

File size: 30.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#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//
302////////////////////////////////////////////////////////////////////////////
303void bncGetThread::terminate() {
304 cout << "terminate a" << endl;
305 if (_query) {
306 _query->stop();
307 }
308 cout << "terminate b" << endl;
309 QThread::terminate();
310}
311
312// Init Run
313////////////////////////////////////////////////////////////////////////////
314t_irc bncGetThread::initRun() {
315
316 if (!_rawInpFile) {
317 delete _query;
318 if (_ntripVersion == "2") {
319 _query = new bncNetQueryV2();
320 }
321 else {
322 _query = new bncNetQueryV1();
323 }
324 if (_nmea == "yes") {
325 QByteArray gga = ggaString(_latitude, _longitude);
326 _query->startRequest(_mountPoint, gga);
327 }
328 else {
329 _query->startRequest(_mountPoint, "");
330 }
331 }
332
333 // Instantiate the filter
334 // ----------------------
335 if (!_decoder) {
336 if (_format.indexOf("RTCM_2") != -1) {
337 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format", true));
338 _decoder = new RTCM2Decoder(_staID.data());
339 }
340 else if (_format.indexOf("RTCM_3") != -1) {
341 emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format", true));
342 _decoder = new RTCM3Decoder(_staID);
343 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
344 this, SIGNAL(newMessage(QByteArray,bool)));
345 }
346 else if (_format.indexOf("RTIGS") != -1) {
347 emit(newMessage("Get Data: " + _staID + " in RTIGS format", true));
348 _decoder = new RTIGSDecoder();
349 }
350 else if (_format.indexOf("GPSS") != -1 || _format.indexOf("BNC") != -1) {
351 emit(newMessage("Get Data: " + _staID + " in GPSS format", true));
352 _decoder = new gpssDecoder();
353 }
354 else if (_format.indexOf("ZERO") != -1) {
355 emit(newMessage("Get Data: " + _staID + " in original format", true));
356 _decoder = new bncZeroDecoder(_staID);
357 }
358 else {
359 emit(newMessage(_staID + ": Unknown data format " + _format, true));
360 if (_rawInpFile) {
361 cerr << "Uknown data format" << endl;
362 ::exit(0);
363 }
364 else {
365 return fatal;
366 }
367 }
368 }
369 return success;
370}
371
372// Run
373////////////////////////////////////////////////////////////////////////////
374void bncGetThread::run() {
375
376 const double maxDt = 600.0; // Check observation epoch
377 bool wrongEpoch = false;
378 bool decode = true;
379 int numSucc = 0;
380 int secSucc = 0;
381 int secFail = 0;
382 int initPause = 30; // Initial pause for corrupted streams
383 int currPause = 0;
384 bool begCorrupt = false;
385 bool endCorrupt = false;
386 bool followSec = false;
387 int oldSecGPS= 0;
388 int newSecGPS = 0;
389 int numGaps = 0;
390 int diffSecGPS = 0;
391 int numLat = 0;
392 double sumLat = 0.;
393 double sumLatQ = 0.;
394 double meanDiff = 0.;
395 double minLat = maxDt;
396 double maxLat = -maxDt;
397 double curLat = 0.;
398
399 _decodeTime = QDateTime::currentDateTime();
400 _decodeSucc = QDateTime::currentDateTime();
401 t_irc irc = initRun();
402
403 if (irc == fatal) {
404 QThread::exit(1);
405 return;
406 }
407 else if (irc != success) {
408 emit(newMessage(_staID + ": initRun failed, reconnecting", true));
409 tryReconnect();
410 }
411
412 if (initPause < _inspSegm) {
413 initPause = _inspSegm;
414 }
415 if(!_makePause) {initPause = 0;}
416 currPause = initPause;
417
418 // Read Incoming Data
419 // ------------------
420 while (true) {
421 try {
422 if (_query && _query->status() != bncNetQuery::running) {
423 emit(newMessage(_staID + ": Internet query not running, reconnecting", true));
424 tryReconnect();
425 }
426
427 QListIterator<p_obs> it(_decoder->_obsList);
428 while (it.hasNext()) {
429 delete it.next();
430 }
431 _decoder->_obsList.clear();
432
433 qint64 nBytes = 0;
434
435 QByteArray data;
436
437 if (_query) {
438 _query->waitForReadyRead(data);
439 nBytes = data.size();
440 }
441 else if (_rawInpFile) {
442 const qint64 maxBytes = 1024;
443 nBytes = maxBytes;
444 }
445
446 if (nBytes > 0) {
447 emit newBytes(_staID, nBytes);
448
449 if (_rawInpFile) {
450 data = _rawInpFile->read(nBytes);
451 if (data.isEmpty()) {
452 cout << "no more data" << endl;
453 ::exit(0);
454 }
455 }
456
457 if (_rawOutFile) {
458 _rawOutFile->write(data);
459 _rawOutFile->flush();
460 }
461
462 if (_serialPort) {
463 _serialPort->write(data);
464 }
465
466 if (_inspSegm<1) {
467 vector<string> errmsg;
468 _decoder->Decode(data.data(), data.size(), errmsg);
469#ifdef DEBUG_RTCM2_2021
470 for (unsigned ii = 0; ii < errmsg.size(); ii++) {
471 emit newMessage(_staID + ": " + errmsg[ii].c_str(), false);
472 }
473#endif
474 }
475 else {
476
477 // Decode data
478 // -----------
479 if (!_decodePause.isValid() ||
480 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
481
482 if (decode) {
483 vector<string> errmsg;
484 if ( _decoder->Decode(data.data(), data.size(), errmsg) == success ) {
485 numSucc += 1;
486 }
487 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
488 decode = false;
489 }
490#ifdef DEBUG_RTCM2_2021
491 for (unsigned ii = 0; ii < errmsg.size(); ii++) {
492 emit newMessage(_staID + ": " + errmsg[ii].c_str(), false);
493 }
494#endif
495 }
496
497 // Check - once per inspect segment
498 // --------------------------------
499 if (!decode) {
500 _decodeTime = QDateTime::currentDateTime();
501 if (numSucc>0) {
502 secSucc += _inspSegm;
503 _decodeSucc = QDateTime::currentDateTime();
504 if (secSucc > _adviseReco * 60) {
505 secSucc = _adviseReco * 60 + 1;
506 }
507 numSucc = 0;
508 currPause = initPause;
509 _decodePause.setDate(QDate());
510 _decodePause.setTime(QTime());
511 }
512 else {
513 secFail += _inspSegm;
514 secSucc = 0;
515 if (secFail > _adviseFail * 60) {
516 secFail = _adviseFail * 60 + 1;
517 }
518 if (!_decodePause.isValid() || !_makePause) {
519 _decodePause = QDateTime::currentDateTime();
520 }
521 else {
522 _decodePause.setDate(QDate());
523 _decodePause.setTime(QTime());
524 secFail = secFail + currPause - _inspSegm;
525 currPause = currPause * 2;
526 if (currPause > 960) {
527 currPause = 960;
528 }
529 }
530 }
531
532 // End corrupt threshold
533 // ---------------------
534 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
535 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
536 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
537 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended "
538 + _endDateCor + " " + _endTimeCor).toAscii(), true));
539 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
540 endCorrupt = true;
541 begCorrupt = false;
542 secFail = 0;
543 }
544 else {
545
546 // Begin corrupt threshold
547 // -----------------------
548 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
549 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
550 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
551 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since "
552 + _begDateCor + " " + _begTimeCor).toAscii(), true));
553 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
554 begCorrupt = true;
555 endCorrupt = false;
556 secSucc = 0;
557 numSucc = 0;
558 }
559 }
560 decode = true;
561 }
562 }
563 }
564
565 // End outage threshold
566 // --------------------
567 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
568 _decodeStart.setDate(QDate());
569 _decodeStart.setTime(QTime());
570 if (_inspSegm>0) {
571 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
572 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
573 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended "
574 + _endDateOut + " " + _endTimeOut).toAscii(), true));
575 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
576 }
577 }
578
579 // RTCM scan output
580 // ----------------
581 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
582 QSettings settings;
583 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked) {
584
585 // RTCMv3 message types
586 // --------------------
587 if (0<_decoder->_typeList.size()) {
588 QString type;
589 for (int ii=0;ii<_decoder->_typeList.size();ii++) {
590 type = QString("%1 ").arg(_decoder->_typeList[ii]);
591 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
592 }
593 }
594
595 // RTCMv3 antenna descriptor
596 // -------------------------
597 if (0<_decoder->_antType.size()) {
598 QString ant1;
599 for (int ii=0;ii<_decoder->_antType.size();ii++) {
600 ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
601 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
602 }
603 }
604
605 // Antenna XYZ
606 // ------------------
607 if (0<_decoder->_antList.size()) {
608 for (int ii=0;ii<_decoder->_antList.size();++ii) {
609 QByteArray ant1,ant2,ant3, antT;
610 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
611 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
612 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
613 switch (_decoder->_antList[ii].type) {
614 case GPSDecoder::t_antInfo::ARP: antT = "ARP"; break;
615 case GPSDecoder::t_antInfo::APC: antT = "APC"; break;
616 }
617 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
618 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
619 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
620 if (_decoder->_antList[ii].height_f) {
621 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
622 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
623 }
624 emit(newAntCrd(_staID,
625 _decoder->_antList[ii].xx, _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
626 antT));
627 }
628 }
629 }
630 if ( _checkMountPoint == "ANTCRD_ONLY" && _decoder->_antList.size() ) {
631 for (int ii=0;ii<_decoder->_antList.size();++ii) {
632 QByteArray antT;
633 switch (_decoder->_antList[ii].type) {
634 case GPSDecoder::t_antInfo::ARP: antT = "ARP"; break;
635 case GPSDecoder::t_antInfo::APC: antT = "APC"; break;
636 }
637 emit(newAntCrd(_staID,
638 _decoder->_antList[ii].xx, _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
639 antT));
640 }
641 }
642 }
643
644 _decoder->_typeList.clear();
645 _decoder->_antType.clear();
646 _decoder->_antList.clear();
647
648 // Loop over all observations (observations output)
649 // ------------------------------------------------
650 QListIterator<p_obs> it(_decoder->_obsList);
651 while (it.hasNext()) {
652 p_obs obs = it.next();
653
654 // Check observation epoch
655 // -----------------------
656 if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
657 int week;
658 double sec;
659 currentGPSWeeks(week, sec);
660 const double secPerWeek = 7.0 * 24.0 * 3600.0;
661
662 if (week < obs->_o.GPSWeek) {
663 week += 1;
664 sec -= secPerWeek;
665 }
666 if (week > obs->_o.GPSWeek) {
667 week -= 1;
668 sec += secPerWeek;
669 }
670 double dt = fabs(sec - obs->_o.GPSWeeks);
671 if (week != obs->_o.GPSWeek || dt > maxDt) {
672 if (!wrongEpoch) {
673 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
674 wrongEpoch = true;
675 }
676 delete obs;
677 continue;
678 }
679 else {
680 wrongEpoch = false;
681
682 // Latency and completeness
683 // ------------------------
684 if (_perfIntr>0) {
685 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
686 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
687 if (newSecGPS != oldSecGPS) {
688 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
689 if (numLat>0) {
690 if (meanDiff>0.) {
691 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
692 .arg(_staID.data())
693 .arg(int(sumLat/numLat*100)/100.)
694 .arg(int(minLat*100)/100.)
695 .arg(int(maxLat*100)/100.)
696 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
697 .arg(numLat)
698 .arg(numGaps)
699 .toAscii(), true) );
700 } else {
701 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
702 .arg(_staID.data())
703 .arg(int(sumLat/numLat*100)/100.)
704 .arg(int(minLat*100)/100.)
705 .arg(int(maxLat*100)/100.)
706 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
707 .arg(numLat)
708 .toAscii(), true) );
709 }
710 }
711 meanDiff = diffSecGPS/numLat;
712 diffSecGPS = 0;
713 numGaps = 0;
714 sumLat = 0.;
715 sumLatQ = 0.;
716 numLat = 0;
717 minLat = maxDt;
718 maxLat = -maxDt;
719 }
720 if (followSec) {
721 diffSecGPS += newSecGPS - oldSecGPS;
722 if (meanDiff>0.) {
723 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
724 numGaps += 1;
725 }
726 }
727 }
728 curLat = sec - obs->_o.GPSWeeks;
729 sumLat += curLat;
730 sumLatQ += curLat * curLat;
731 if (curLat < minLat) minLat = curLat;
732 if (curLat >= maxLat) maxLat = curLat;
733 numLat += 1;
734 oldSecGPS = newSecGPS;
735 followSec = true;
736 }
737 }
738 }
739 }
740 }
741
742 // RINEX Output
743 // ------------
744 if (_rnx) {
745 bool dump = true;
746
747 //// // RTCMv2 XYZ
748 //// // ----------
749 //// RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
750 //// if ( decoder2 && !_rnx_set_position ) {
751 //// double stax, stay, staz;
752 //// double dL1[3], dL2[3];
753 //// if ( decoder2->getStaCrd(stax, stay, staz,
754 //// dL1[0], dL1[1], dL1[2],
755 //// dL2[0], dL2[1], dL2[2]) == success ) {
756 ////
757 //// if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
758 //// QString ant1;
759 //// ant1 = QString("%1 ").arg(stax,0,'f',4);
760 //// emit(newMessage(_staID + ": ARP X " + ant1.toAscii() + "m" ));
761 //// ant1 = QString("%1 ").arg(stay,0,'f',4);
762 //// emit(newMessage(_staID + ": ARP Y " + ant1.toAscii() + "m" ));
763 //// ant1 = QString("%1 ").arg(staz,0,'f',4);
764 //// emit(newMessage(_staID + ": ARP Z " + ant1.toAscii() + "m" ));
765 //// ant1 = QString("%1 ").arg(dL1[0],0,'f',4);
766 //// emit(newMessage(_staID + ": L1 APC DX " + ant1.toAscii() + "m" ));
767 //// ant1 = QString("%1 ").arg(dL1[1],0,'f',4);
768 //// emit(newMessage(_staID + ": L1 APC DY " + ant1.toAscii() + "m" ));
769 //// ant1 = QString("%1 ").arg(dL1[2],0,'f',4);
770 //// emit(newMessage(_staID + ": L1 APC DZ " + ant1.toAscii() + "m" ));
771 //// ant1 = QString("%1 ").arg(dL2[0],0,'f',4);
772 //// emit(newMessage(_staID + ": L2 APC DX " + ant1.toAscii() + "m" ));
773 //// ant1 = QString("%1 ").arg(dL2[1],0,'f',4);
774 //// emit(newMessage(_staID + ": L2 APC DY " + ant1.toAscii() + "m" ));
775 //// ant1 = QString("%1 ").arg(dL2[2],0,'f',4);
776 //// emit(newMessage(_staID + ": L2 APC DZ " + ant1.toAscii() + "m" ));
777 //// }
778 //// _rnx_set_position = true;
779 //// }
780 //// }
781
782 if ( dump ) {
783 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
784 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
785 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
786 _rnx->deepCopy(obs);
787 }
788 _rnx->dumpEpoch(newTime);
789 }
790 }
791
792 // Emit new observation signal
793 // ---------------------------
794 bool firstObs = (obs == _decoder->_obsList.first());
795 obs->_status = t_obs::posted;
796 emit newObs(_staID, firstObs, obs);
797 }
798 _decoder->_obsList.clear();
799
800 }
801
802 // Timeout, reconnect
803 // ------------------
804 else {
805 emit(newMessage(_staID + ": Data Timeout, reconnecting", true));
806 tryReconnect();
807 }
808 }
809 catch (const char* msg) {
810 emit(newMessage(_staID + msg, true));
811 tryReconnect();
812 }
813 }
814}
815
816// Exit
817////////////////////////////////////////////////////////////////////////////
818void bncGetThread::exit(int exitCode) {
819 if (exitCode!= 0) {
820 emit error(_staID);
821 }
822 QThread::exit(exitCode);
823 terminate();
824}
825
826// Try Re-Connect
827////////////////////////////////////////////////////////////////////////////
828void bncGetThread::tryReconnect() {
829 if (_rnx) {
830 _rnx->setReconnectFlag(true);
831 }
832 if ( !_decodeStart.isValid()) {
833 _decodeStop = QDateTime::currentDateTime();
834 }
835 while (1) {
836 sleep(_nextSleep);
837 if ( initRun() == success ) {
838 if ( !_decodeStop.isValid()) {
839 _decodeStart = QDateTime::currentDateTime();
840 }
841 break;
842 }
843 else {
844
845 // Begin outage threshold
846 // ----------------------
847 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
848 _decodeStop.setDate(QDate());
849 _decodeStop.setTime(QTime());
850 if (_inspSegm>0) {
851 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
852 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
853 emit(newMessage((_staID + ": Failure threshold exceeded, outage since "
854 + _begDateOut + " " + _begTimeOut).toAscii(), true));
855 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
856 }
857 }
858 _nextSleep *= 2;
859 if (_nextSleep > 256) {
860 _nextSleep = 256;
861 }
862 _nextSleep += rand() % 6;
863 }
864 }
865 _nextSleep = 1;
866}
867
868// Call advisory notice script
869////////////////////////////////////////////////////////////////////////////
870void bncGetThread::callScript(const char* _comment) {
871 QMutexLocker locker(&_mutex);
872 if (!_adviseScript.isEmpty()) {
873 msleep(1);
874#ifdef WIN32
875 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
876#else
877 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
878#endif
879 }
880}
881
882//
883//////////////////////////////////////////////////////////////////////////////
884void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
885 RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
886
887 if ( decoder ) {
888 QMutexLocker locker(&_mutex);
889
890 string storedPRN;
891 vector<int> IODs;
892
893 if ( decoder->storeEph(gpseph, storedPRN, IODs) ) {
894#ifdef DEBUG_RTCM2_2021
895 QString msg = _staID + QString(": stored eph %1 IODs").arg(storedPRN.c_str());
896
897 for (unsigned ii = 0; ii < IODs.size(); ii++) {
898 msg += QString(" %1").arg(IODs[ii],4);
899 }
900
901 emit(newMessage(msg.toAscii(), false));
902#endif
903 }
904 }
905}
Note: See TracBrowser for help on using the repository browser.