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

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