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

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