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

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

* empty log message *

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