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

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