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

Last change on this file since 1271 was 1271, checked in by zdenek, 15 years ago

* empty log message *

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