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

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