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

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

* empty log message *

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