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

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

* empty log message *

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