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

Last change on this file since 1554 was 1554, checked in by mervart, 14 years ago

* empty log message *

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