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

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

* empty log message *

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