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

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