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

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

* empty log message *

File size: 32.7 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
57#include "RTCM/RTCM2Decoder.h"
58#include "RTCM3/RTCM3Decoder.h"
59#include "RTIGS/RTIGSDecoder.h"
60
61using namespace std;
62
63// Constructor 1
64////////////////////////////////////////////////////////////////////////////
65bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
66 const QByteArray& format) {
67
68 _format = format;
69
70 int iSep = rawInpFileName.lastIndexOf(QDir::separator());
71 _staID = rawInpFileName.mid(iSep+1,4);
72
73 initialize();
74
75 _inspSegm = 0;
76
77 _rawInpFile = new QFile(rawInpFileName);
78 _rawInpFile->open(QIODevice::ReadOnly);
79
80 if (!_rnx) {
81 cerr << "no RINEX path specified" << endl;
82 ::exit(0);
83 }
84}
85
86// Constructor 2
87////////////////////////////////////////////////////////////////////////////
88bncGetThread::bncGetThread(const QUrl& mountPoint,
89 const QByteArray& format,
90 const QByteArray& latitude,
91 const QByteArray& longitude,
92 const QByteArray& nmea, int iMount) {
93
94 setTerminationEnabled(true);
95
96 _mountPoint = mountPoint;
97 _staID = mountPoint.path().mid(1).toAscii();
98 _format = format;
99 _latitude = latitude;
100 _longitude = longitude;
101 _nmea = nmea;
102 _iMount = iMount; // index in mountpoints array
103
104 initialize();
105}
106
107// Initialization
108////////////////////////////////////////////////////////////////////////////
109void bncGetThread::initialize() {
110
111
112 bncApp* app = (bncApp*) qApp;
113 app->connect(this, SIGNAL(newMessage(QByteArray)),
114 app, SLOT(slotMessage(const QByteArray)));
115
116 _decoder = 0;
117 _socket = 0;
118 _timeOut = 20*1000; // 20 seconds
119 _nextSleep = 1; // 1 second
120 _rawInpFile = 0;
121 _rawOutFile = 0;
122 _staID_orig = _staID;
123
124 // Check name conflict
125 // -------------------
126 QSettings settings;
127 QListIterator<QString> it(settings.value("mountPoints").toStringList());
128 int num = 0;
129 int ind = -1;
130 while (it.hasNext()) {
131 ++ind;
132 QStringList hlp = it.next().split(" ");
133 if (hlp.size() <= 1) continue;
134 QUrl url(hlp[0]);
135 if (_mountPoint.path() == url.path()) {
136 if (_iMount > ind || _iMount < 0) {
137 ++num;
138 }
139 }
140 }
141
142 if (num > 0) {
143 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
144 }
145
146 // Notice threshold
147 // ----------------
148 _inspSegm = 50;
149 if ( settings.value("obsRate").toString().isEmpty() ) { _inspSegm = 0; }
150 if ( settings.value("obsRate").toString().indexOf("5 Hz") != -1 ) { _inspSegm = 2; }
151 if ( settings.value("obsRate").toString().indexOf("1 Hz") != -1 ) { _inspSegm = 10; }
152 if ( settings.value("obsRate").toString().indexOf("0.5 Hz") != -1 ) { _inspSegm = 20; }
153 if ( settings.value("obsRate").toString().indexOf("0.2 Hz") != -1 ) { _inspSegm = 40; }
154 if ( settings.value("obsRate").toString().indexOf("0.1 Hz") != -1 ) { _inspSegm = 50; }
155 _adviseFail = settings.value("adviseFail").toInt();
156 _adviseReco = settings.value("adviseReco").toInt();
157 _makePause = false;
158 if ( Qt::CheckState(settings.value("makePause").toInt()) == Qt::Checked) {_makePause = true; }
159 _adviseScript = settings.value("adviseScript").toString();
160 expandEnvVar(_adviseScript);
161
162 // Latency interval/average
163 // ------------------------
164 _perfIntr = 86400;
165 if ( settings.value("perfIntr").toString().isEmpty() ) { _perfIntr = 0; }
166 if ( settings.value("perfIntr").toString().indexOf("1 min") != -1 ) { _perfIntr = 60; }
167 if ( settings.value("perfIntr").toString().indexOf("5 min") != -1 ) { _perfIntr = 300; }
168 if ( settings.value("perfIntr").toString().indexOf("15 min") != -1 ) { _perfIntr = 900; }
169 if ( settings.value("perfIntr").toString().indexOf("1 hour") != -1 ) { _perfIntr = 3600; }
170 if ( settings.value("perfIntr").toString().indexOf("6 hours") != -1 ) { _perfIntr = 21600; }
171 if ( settings.value("perfIntr").toString().indexOf("1 day") != -1 ) { _perfIntr = 86400; }
172
173 // RTCM message types
174 // ------------------
175 _checkMountPoint = settings.value("messTypes").toString();
176
177 // RINEX writer
178 // ------------
179 _samplingRate = settings.value("rnxSampl").toInt();
180 if ( settings.value("rnxPath").toString().isEmpty() ) {
181 _rnx = 0;
182 }
183 else {
184 _rnx = new bncRinex(_staID, _mountPoint,
185 _format, _latitude, _longitude, _nmea);
186 }
187 _rnx_set_position = false;
188
189 connect(((bncApp*)qApp), SIGNAL(newEphGPS(gpsephemeris)),
190 this, SLOT(slotNewEphGPS(gpsephemeris)));
191
192 // Raw Output
193 // ----------
194 // QByteArray rawOutFileName = "./" + _staID + ".raw";
195 // _rawOutFile = new QFile(rawOutFileName);
196 // _rawOutFile->open(QIODevice::WriteOnly);
197
198 msleep(100); //sleep 0.1 sec
199}
200
201// Destructor
202////////////////////////////////////////////////////////////////////////////
203bncGetThread::~bncGetThread() {
204 if (_socket) {
205 _socket->close();
206#if QT_VERSION == 0x040203
207 delete _socket;
208#else
209 _socket->deleteLater();
210#endif
211 }
212 delete _decoder;
213 delete _rnx;
214 delete _rawInpFile;
215 delete _rawOutFile;
216}
217
218#define AGENTVERSION "1.6"
219// Connect to Caster, send the Request (static)
220////////////////////////////////////////////////////////////////////////////
221QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
222 QByteArray& latitude, QByteArray& longitude,
223 QByteArray& nmea, int timeOut,
224 QString& msg) {
225
226 // Connect the Socket
227 // ------------------
228 QTcpSocket* socket = new QTcpSocket();
229
230 socket->connectToHost(mountPoint.host(), mountPoint.port());
231
232 if (!socket->waitForConnected(timeOut)) {
233 msg += "Connect timeout\n";
234 delete socket;
235 return 0;
236 }
237
238 // Set Proxy (application-wide)
239 // ----------------------------
240 QSettings settings;
241 QString proxyHost = settings.value("proxyHost").toString();
242 int proxyPort = settings.value("proxyPort").toInt();
243
244 QNetworkProxy proxy;
245 if (proxyHost.isEmpty()) {
246 proxy.setType(QNetworkProxy::NoProxy);
247 }
248 else {
249 //// proxy.setType(QNetworkProxy::HttpProxy);
250 proxy.setHostName(proxyHost);
251 proxy.setPort(proxyPort);
252 }
253 QNetworkProxy::setApplicationProxy(proxy);
254
255 // Send Request
256 // ------------
257 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
258 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
259 QByteArray userAndPwd;
260
261 if(!uName.isEmpty() || !passW.isEmpty())
262 {
263 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
264 passW.toAscii()).toBase64() + "\r\n";
265 }
266
267 QUrl hlp;
268 hlp.setScheme("http");
269 hlp.setHost(mountPoint.host());
270 hlp.setPort(mountPoint.port());
271 hlp.setPath(mountPoint.path());
272 if (hlp.path().indexOf("/") != 0) {
273 hlp.setPath("/");
274 }
275
276 QByteArray reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n" +
277 "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
278 "Host: " + hlp.host().toAscii() + "\r\n"
279 + userAndPwd + "\r\n";
280
281 // NMEA string to handle VRS stream
282 // --------------------------------
283 double lat, lon;
284
285 lat = strtod(latitude,NULL);
286 lon = strtod(longitude,NULL);
287
288 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
289 const char* flagN="N";
290 const char* flagE="E";
291 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
292 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
293 if (lon < -180.) {lon=(lon+360.); flagE="E";}
294 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
295 QTime ttime(QDateTime::currentDateTime().toUTC().time());
296 int lat_deg = (int)lat;
297 double lat_min=(lat-lat_deg)*60.;
298 int lon_deg = (int)lon;
299 double lon_min=(lon-lon_deg)*60.;
300 int hh = 0 , mm = 0;
301 double ss = 0.0;
302 hh=ttime.hour();
303 mm=ttime.minute();
304 ss=(double)ttime.second()+0.001*ttime.msec();
305 QString gga;
306 gga += "GPGGA,";
307 gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
308 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
309 gga += flagN;
310 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
311 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
312 int xori;
313 char XOR = 0;
314 char *Buff =gga.toAscii().data();
315 int iLen = strlen(Buff);
316 for (xori = 0; xori < iLen; xori++) {
317 XOR ^= (char)Buff[xori];
318 }
319 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
320 reqStr += "$";
321 reqStr += gga;
322 reqStr += "\r\n";
323 }
324
325 msg += reqStr;
326
327 socket->write(reqStr, reqStr.length());
328
329 if (!socket->waitForBytesWritten(timeOut)) {
330 msg += "Write timeout\n";
331 delete socket;
332 return 0;
333 }
334
335 return socket;
336}
337
338// Init Run
339////////////////////////////////////////////////////////////////////////////
340t_irc bncGetThread::initRun() {
341
342 if (!_rawInpFile) {
343
344 // Initialize Socket
345 // -----------------
346 QString msg;
347 _socket = this->request(_mountPoint, _latitude, _longitude,
348 _nmea, _timeOut, msg);
349 if (!_socket) {
350 return failure;
351 }
352
353 // Read Caster Response
354 // --------------------
355 _socket->waitForReadyRead(_timeOut);
356 if (_socket->canReadLine()) {
357 QString line = _socket->readLine();
358
359 // Skip messages from proxy server
360 // -------------------------------
361 if (line.indexOf("ICY 200 OK") == -1 &&
362 line.indexOf("200 OK") != -1 ) {
363 bool proxyRespond = true;
364 while (true) {
365 if (_socket->canReadLine()) {
366 line = _socket->readLine();
367 if (!proxyRespond) {
368 break;
369 }
370 if (line.trimmed().isEmpty()) {
371 proxyRespond = false;
372 }
373 }
374 else {
375 _socket->waitForReadyRead(_timeOut);
376 if (_socket->bytesAvailable() <= 0) {
377 break;
378 }
379 }
380 }
381 }
382
383 if (line.indexOf("Unauthorized") != -1) {
384 QStringList table;
385 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
386 QString net;
387 QStringListIterator it(table);
388 while (it.hasNext()) {
389 QString line = it.next();
390 if (line.indexOf("STR") == 0) {
391 QStringList tags = line.split(";");
392 if (tags.at(1) == _staID_orig) {
393 net = tags.at(7);
394 break;
395 }
396 }
397 }
398
399 QString reg;
400 it.toFront();
401 while (it.hasNext()) {
402 QString line = it.next();
403 if (line.indexOf("NET") == 0) {
404 QStringList tags = line.split(";");
405 if (tags.at(1) == net) {
406 reg = tags.at(7);
407 break;
408 }
409 }
410 }
411 emit(newMessage((_staID + ": Caster Response: " + line +
412 " Adjust User-ID and Password Register, see"
413 "\n " + reg).toAscii()));
414 return fatal;
415 }
416 if (line.indexOf("ICY 200 OK") != 0) {
417 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
418 return failure;
419 }
420 }
421 else {
422 emit(newMessage(_staID + ": Response Timeout"));
423 return failure;
424 }
425 }
426
427 // Instantiate the filter
428 // ----------------------
429 if (!_decoder) {
430 if (_format.indexOf("RTCM_2") != -1) {
431 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
432 _decoder = new RTCM2Decoder(_staID.data());
433 }
434 else if (_format.indexOf("RTCM_3") != -1) {
435 emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
436 _decoder = new RTCM3Decoder(_staID);
437 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray)),
438 this, SIGNAL(newMessage(QByteArray)));
439 }
440 else if (_format.indexOf("RTIGS") != -1) {
441 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
442 _decoder = new RTIGSDecoder();
443 }
444 else if (_format.indexOf("ZERO") != -1) {
445 emit(newMessage("Get Data: " + _staID + " in original format"));
446 _decoder = new bncZeroDecoder(_staID);
447 }
448 else {
449 emit(newMessage(_staID + ": Unknown data format " + _format));
450 if (_rawInpFile) {
451 cerr << "Uknown data format" << endl;
452 ::exit(0);
453 }
454 else {
455 return fatal;
456 }
457 }
458 }
459 return success;
460}
461
462// Run
463////////////////////////////////////////////////////////////////////////////
464void bncGetThread::run() {
465
466 const double maxDt = 600.0; // Check observation epoch
467 bool wrongEpoch = false;
468 bool decode = true;
469 int numSucc = 0;
470 int secSucc = 0;
471 int secFail = 0;
472 int initPause = 30; // Initial pause for corrupted streams
473 int currPause = 0;
474 bool begCorrupt = false;
475 bool endCorrupt = false;
476 bool followSec = false;
477 int oldSecGPS= 0;
478 int newSecGPS = 0;
479 int numGaps = 0;
480 int diffSecGPS = 0;
481 int numLat = 0;
482 double sumLat = 0.;
483 double sumLatQ = 0.;
484 double meanDiff = 0.;
485 double minLat = maxDt;
486 double maxLat = -maxDt;
487 double curLat = 0.;
488
489 _decodeTime = QDateTime::currentDateTime();
490 _decodeSucc = QDateTime::currentDateTime();
491 t_irc irc = initRun();
492
493 if (irc == fatal) {
494 QThread::exit(1);
495 return;
496 }
497 else if (irc != success) {
498 emit(newMessage(_staID + ": initRun failed, reconnecting"));
499 tryReconnect();
500 }
501
502 if (initPause < _inspSegm) {
503 initPause = _inspSegm;
504 }
505 if(!_makePause) {initPause = 0;}
506 currPause = initPause;
507
508 // Read Incoming Data
509 // ------------------
510 while (true) {
511 try {
512 if (_socket && _socket->state() != QAbstractSocket::ConnectedState) {
513 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
514 tryReconnect();
515 }
516
517 QListIterator<p_obs> it(_decoder->_obsList);
518 while (it.hasNext()) {
519 delete it.next();
520 }
521 _decoder->_obsList.clear();
522
523 qint64 nBytes = 0;
524
525 if (_socket) {
526 _socket->waitForReadyRead(_timeOut);
527 nBytes = _socket->bytesAvailable();
528 }
529 else if (_rawInpFile) {
530 const qint64 maxBytes = 1024;
531 nBytes = maxBytes;
532 }
533
534 if (nBytes > 0) {
535 emit newBytes(_staID, nBytes);
536
537 char* data = new char[nBytes];
538
539 if (_socket) {
540 _socket->read(data, nBytes);
541 }
542 else if (_rawInpFile) {
543 nBytes = _rawInpFile->read(data, nBytes);
544 if (nBytes <= 0) {
545 cout << "no more data" << endl;
546 ::exit(0);
547 }
548 }
549
550 if (_rawOutFile) {
551 _rawOutFile->write(data, nBytes);
552 _rawOutFile->flush();
553 }
554
555 if (_inspSegm<1) {
556 vector<string> errmsg;
557 _decoder->Decode(data, nBytes, errmsg);
558#ifdef DEBUG_RTCM2_2021
559 for (unsigned ii = 0; ii < errmsg.size(); ii++) {
560 emit newMessage(_staID + ": " + errmsg[ii].c_str());
561 }
562#endif
563 }
564 else {
565
566 // Decode data
567 // -----------
568 if (!_decodePause.isValid() ||
569 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
570
571 if (decode) {
572 vector<string> errmsg;
573 if ( _decoder->Decode(data, nBytes, errmsg) == success ) {
574 numSucc += 1;
575 }
576 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
577 decode = false;
578 }
579#ifdef DEBUG_RTCM2_2021
580 for (unsigned ii = 0; ii < errmsg.size(); ii++) {
581 emit newMessage(_staID + ": " + errmsg[ii].c_str());
582 }
583#endif
584 }
585
586 // Check - once per inspect segment
587 // --------------------------------
588 if (!decode) {
589 _decodeTime = QDateTime::currentDateTime();
590 if (numSucc>0) {
591 secSucc += _inspSegm;
592 _decodeSucc = QDateTime::currentDateTime();
593 if (secSucc > _adviseReco * 60) {
594 secSucc = _adviseReco * 60 + 1;
595 }
596 numSucc = 0;
597 currPause = initPause;
598 _decodePause.setDate(QDate());
599 _decodePause.setTime(QTime());
600 }
601 else {
602 secFail += _inspSegm;
603 secSucc = 0;
604 if (secFail > _adviseFail * 60) {
605 secFail = _adviseFail * 60 + 1;
606 }
607 if (!_decodePause.isValid() || !_makePause) {
608 _decodePause = QDateTime::currentDateTime();
609 }
610 else {
611 _decodePause.setDate(QDate());
612 _decodePause.setTime(QTime());
613 secFail = secFail + currPause - _inspSegm;
614 currPause = currPause * 2;
615 if (currPause > 960) {
616 currPause = 960;
617 }
618 }
619 }
620
621 // End corrupt threshold
622 // ---------------------
623 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
624 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
625 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
626 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
627 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
628 endCorrupt = true;
629 begCorrupt = false;
630 secFail = 0;
631 }
632 else {
633
634 // Begin corrupt threshold
635 // -----------------------
636 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
637 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
638 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
639 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
640 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
641 begCorrupt = true;
642 endCorrupt = false;
643 secSucc = 0;
644 numSucc = 0;
645 }
646 }
647 decode = true;
648 }
649 }
650 }
651
652 // End outage threshold
653 // --------------------
654 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
655 _decodeStart.setDate(QDate());
656 _decodeStart.setTime(QTime());
657 if (_inspSegm>0) {
658 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
659 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
660 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
661 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
662 }
663 }
664
665 delete [] data;
666
667
668 // RTCM scan output
669 // ----------------
670 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
671
672 // RTCMv3 message types
673 // --------------------
674 if (0<_decoder->_typeList.size()) {
675 QString type;
676 for (int ii=0;ii<_decoder->_typeList.size();ii++) {
677 type = QString("%1 ").arg(_decoder->_typeList[ii]);
678 emit(newMessage(_staID + ": Received message type " + type.toAscii() ));
679 }
680 }
681
682 // RTCMv3 antenna descriptor
683 // -------------------------
684 if (0<_decoder->_antType.size()) {
685 QString ant1;
686 for (int ii=0;ii<_decoder->_antType.size();ii++) {
687 ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
688 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii() ));
689 }
690 }
691
692 // antenna XYZ
693 // ------------------
694 if (0<_decoder->_antList.size()) {
695 for (int ii=0;ii<_decoder->_antList.size();++ii) {
696 QByteArray ant1,ant2,ant3, antT;
697 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
698 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
699 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
700 switch (_decoder->_antList[ii].type) {
701 case GPSDecoder::t_antInfo::ARP: antT = "ARP"; break;
702 case GPSDecoder::t_antInfo::APC: antT = "APC"; break;
703 }
704 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m"));
705 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m"));
706 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m"));
707 if (_decoder->_antList[ii].height_f) {
708 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
709 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m"));
710 }
711 emit(newAntCrd(_staID,
712 _decoder->_antList[ii].xx, _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
713 antT));
714 }
715 }
716 }
717 if ( _checkMountPoint == "ANTCRD_ONLY" && _decoder->_antList.size() ) {
718 for (int ii=0;ii<_decoder->_antList.size();++ii) {
719 QByteArray antT;
720 switch (_decoder->_antList[ii].type) {
721 case GPSDecoder::t_antInfo::ARP: antT = "ARP"; break;
722 case GPSDecoder::t_antInfo::APC: antT = "APC"; break;
723 }
724 emit(newAntCrd(_staID,
725 _decoder->_antList[ii].xx, _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
726 antT));
727 }
728 }
729
730 _decoder->_typeList.clear();
731 _decoder->_antType.clear();
732 _decoder->_antList.clear();
733
734 // Loop over all observations (observations output)
735 // ------------------------------------------------
736 QListIterator<p_obs> it(_decoder->_obsList);
737 while (it.hasNext()) {
738 p_obs obs = it.next();
739
740 // Check observation epoch
741 // -----------------------
742 if (!_rawInpFile) {
743 int week;
744 double sec;
745 currentGPSWeeks(week, sec);
746 const double secPerWeek = 7.0 * 24.0 * 3600.0;
747
748 if (week < obs->_o.GPSWeek) {
749 week += 1;
750 sec -= secPerWeek;
751 }
752 if (week > obs->_o.GPSWeek) {
753 week -= 1;
754 sec += secPerWeek;
755 }
756 double dt = fabs(sec - obs->_o.GPSWeeks);
757 if (week != obs->_o.GPSWeek || dt > maxDt) {
758 if (!wrongEpoch) {
759 emit( newMessage(_staID + ": Wrong observation epoch(s)") );
760 wrongEpoch = true;
761 }
762 delete obs;
763 continue;
764 }
765 else {
766 wrongEpoch = false;
767
768 // Latency and completeness
769 // ------------------------
770 if (_perfIntr>0) {
771 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
772 if (newSecGPS != oldSecGPS) {
773 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
774 if (numLat>0) {
775 if (meanDiff>0.) {
776 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
777 .arg(_staID.data())
778 .arg(int(sumLat/numLat*100)/100.)
779 .arg(int(minLat*100)/100.)
780 .arg(int(maxLat*100)/100.)
781 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
782 .arg(numLat)
783 .arg(numGaps)
784 .toAscii()) );
785 } else {
786 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
787 .arg(_staID.data())
788 .arg(int(sumLat/numLat*100)/100.)
789 .arg(int(minLat*100)/100.)
790 .arg(int(maxLat*100)/100.)
791 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
792 .arg(numLat)
793 .toAscii()) );
794 }
795 }
796 meanDiff = diffSecGPS/numLat;
797 diffSecGPS = 0;
798 numGaps = 0;
799 sumLat = 0.;
800 sumLatQ = 0.;
801 numLat = 0;
802 minLat = maxDt;
803 maxLat = -maxDt;
804 }
805 if (followSec) {
806 diffSecGPS += newSecGPS - oldSecGPS;
807 if (meanDiff>0.) {
808 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
809 numGaps += 1;
810 }
811 }
812 }
813 curLat = sec - obs->_o.GPSWeeks;
814 sumLat += curLat;
815 sumLatQ += curLat * curLat;
816 if (curLat < minLat) minLat = curLat;
817 if (curLat >= maxLat) maxLat = curLat;
818 numLat += 1;
819 oldSecGPS = newSecGPS;
820 followSec = true;
821 }
822 }
823 }
824 }
825
826 // RINEX Output
827 // ------------
828 if (_rnx) {
829 bool dump = true;
830
831 //// // RTCMv2 XYZ
832 //// // ----------
833 //// RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
834 //// if ( decoder2 && !_rnx_set_position ) {
835 //// double stax, stay, staz;
836 //// double dL1[3], dL2[3];
837 //// if ( decoder2->getStaCrd(stax, stay, staz,
838 //// dL1[0], dL1[1], dL1[2],
839 //// dL2[0], dL2[1], dL2[2]) == success ) {
840 ////
841 //// if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
842 //// QString ant1;
843 //// ant1 = QString("%1 ").arg(stax,0,'f',4);
844 //// emit(newMessage(_staID + ": ARP X " + ant1.toAscii() + "m" ));
845 //// ant1 = QString("%1 ").arg(stay,0,'f',4);
846 //// emit(newMessage(_staID + ": ARP Y " + ant1.toAscii() + "m" ));
847 //// ant1 = QString("%1 ").arg(staz,0,'f',4);
848 //// emit(newMessage(_staID + ": ARP Z " + ant1.toAscii() + "m" ));
849 //// ant1 = QString("%1 ").arg(dL1[0],0,'f',4);
850 //// emit(newMessage(_staID + ": L1 APC DX " + ant1.toAscii() + "m" ));
851 //// ant1 = QString("%1 ").arg(dL1[1],0,'f',4);
852 //// emit(newMessage(_staID + ": L1 APC DY " + ant1.toAscii() + "m" ));
853 //// ant1 = QString("%1 ").arg(dL1[2],0,'f',4);
854 //// emit(newMessage(_staID + ": L1 APC DZ " + ant1.toAscii() + "m" ));
855 //// ant1 = QString("%1 ").arg(dL2[0],0,'f',4);
856 //// emit(newMessage(_staID + ": L2 APC DX " + ant1.toAscii() + "m" ));
857 //// ant1 = QString("%1 ").arg(dL2[1],0,'f',4);
858 //// emit(newMessage(_staID + ": L2 APC DY " + ant1.toAscii() + "m" ));
859 //// ant1 = QString("%1 ").arg(dL2[2],0,'f',4);
860 //// emit(newMessage(_staID + ": L2 APC DZ " + ant1.toAscii() + "m" ));
861 //// }
862 //// _rnx_set_position = true;
863 //// }
864 //// }
865
866 if ( dump ) {
867 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
868 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
869 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
870 _rnx->deepCopy(obs);
871 }
872 _rnx->dumpEpoch(newTime);
873 }
874 }
875
876 // Emit new observation signal
877 // ---------------------------
878 bool firstObs = (obs == _decoder->_obsList.first());
879 obs->_status = t_obs::posted;
880 emit newObs(_staID, firstObs, obs);
881 }
882 _decoder->_obsList.clear();
883
884 }
885
886 // Timeout, reconnect
887 // ------------------
888 else {
889 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
890 tryReconnect();
891 }
892 }
893 catch (const char* msg) {
894 emit(newMessage(_staID + msg));
895 tryReconnect();
896 }
897 }
898}
899
900// Exit
901////////////////////////////////////////////////////////////////////////////
902void bncGetThread::exit(int exitCode) {
903 if (exitCode!= 0) {
904 emit error(_staID);
905 }
906 QThread::exit(exitCode);
907 terminate();
908}
909
910// Try Re-Connect
911////////////////////////////////////////////////////////////////////////////
912void bncGetThread::tryReconnect() {
913 if (_rnx) {
914 _rnx->setReconnectFlag(true);
915 }
916 if ( !_decodeStart.isValid()) {
917 _decodeStop = QDateTime::currentDateTime();
918 }
919 while (1) {
920 delete _socket; _socket = 0;
921 sleep(_nextSleep);
922 if ( initRun() == success ) {
923 if ( !_decodeStop.isValid()) {
924 _decodeStart = QDateTime::currentDateTime();
925 }
926 break;
927 }
928 else {
929
930 // Begin outage threshold
931 // ----------------------
932 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
933 _decodeStop.setDate(QDate());
934 _decodeStop.setTime(QTime());
935 if (_inspSegm>0) {
936 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
937 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
938 emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
939 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
940 }
941 }
942 _nextSleep *= 2;
943 if (_nextSleep > 256) {
944 _nextSleep = 256;
945 }
946 _nextSleep += rand() % 6;
947 }
948 }
949 _nextSleep = 1;
950}
951
952// Call advisory notice script
953////////////////////////////////////////////////////////////////////////////
954void bncGetThread::callScript(const char* _comment) {
955 QMutexLocker locker(&_mutex);
956 if (!_adviseScript.isEmpty()) {
957 msleep(1);
958#ifdef WIN32
959 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
960#else
961 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
962#endif
963 }
964}
965
966//
967//////////////////////////////////////////////////////////////////////////////
968void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
969 RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
970
971 if ( decoder ) {
972 QMutexLocker locker(&_mutex);
973
974 string storedPRN;
975 vector<int> IODs;
976
977 if ( decoder->storeEph(gpseph, storedPRN, IODs) ) {
978#ifdef DEBUG_RTCM2_2021
979 QString msg = _staID + QString(": stored eph %1 IODs").arg(storedPRN.c_str());
980
981 for (unsigned ii = 0; ii < IODs.size(); ii++) {
982 msg += QString(" %1").arg(IODs[ii],4);
983 }
984
985 emit(newMessage(msg.toAscii()));
986#endif
987 }
988 }
989}
990
Note: See TracBrowser for help on using the repository browser.