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

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

* empty log message *

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