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

Last change on this file since 696 was 696, checked in by weber, 16 years ago

* empty log message *

File size: 19.8 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
43#include <QFile>
44#include <QTextStream>
45#include <QtNetwork>
46#include <QTime>
47
48#include "bncgetthread.h"
49#include "bnctabledlg.h"
50#include "bncapp.h"
51#include "bncutils.h"
52#include "bncrinex.h"
53#include "bnczerodecoder.h"
54
55#include "RTCM/RTCM2Decoder.h"
56#include "RTCM3/RTCM3Decoder.h"
57#include "RTIGS/RTIGSDecoder.h"
58
59using namespace std;
60
61// Constructor
62////////////////////////////////////////////////////////////////////////////
63bncGetThread::bncGetThread(const QUrl& mountPoint,
64 const QByteArray& format,
65 const QByteArray& latitude,
66 const QByteArray& longitude,
67 const QByteArray& nmea, int iMount) {
68
69 setTerminationEnabled(true);
70
71 _decoder = 0;
72 _mountPoint = mountPoint;
73 _staID = mountPoint.path().mid(1).toAscii();
74 _staID_orig = _staID;
75 _format = format;
76 _latitude = latitude;
77 _longitude = longitude;
78 _nmea = nmea;
79 _socket = 0;
80 _timeOut = 20*1000; // 20 seconds
81 _nextSleep = 1; // 1 second
82 _iMount = iMount; // index in mountpoints array
83
84 // Check name conflict
85 // -------------------
86 QSettings settings;
87 QListIterator<QString> it(settings.value("mountPoints").toStringList());
88 int num = 0;
89 int ind = -1;
90 while (it.hasNext()) {
91 ++ind;
92 QStringList hlp = it.next().split(" ");
93 if (hlp.size() <= 1) continue;
94 QUrl url(hlp[0]);
95 if (_mountPoint.path() == url.path()) {
96 if (_iMount > ind) {
97 ++num;
98 }
99 }
100 }
101
102 if (num > 0) {
103 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
104 }
105
106 // Notice threshold
107 // ----------------
108 _inspSegm = 50;
109 if ( settings.value("obsRate").toString().isEmpty() ) { _inspSegm = 0; }
110 if ( settings.value("obsRate").toString().indexOf("5 Hz") != -1 ) { _inspSegm = 2; }
111 if ( settings.value("obsRate").toString().indexOf("1 Hz") != -1 ) { _inspSegm = 10; }
112 if ( settings.value("obsRate").toString().indexOf("0.5 Hz") != -1 ) { _inspSegm = 20; }
113 if ( settings.value("obsRate").toString().indexOf("0.2 Hz") != -1 ) { _inspSegm = 40; }
114 if ( settings.value("obsRate").toString().indexOf("0.1 Hz") != -1 ) { _inspSegm = 50; }
115 _adviseFail = settings.value("adviseFail").toInt();
116 _adviseReco = settings.value("adviseReco").toInt();
117 _adviseScript = settings.value("adviseScript").toString();
118 expandEnvVar(_adviseScript);
119
120 // RINEX writer
121 // ------------
122 _samplingRate = settings.value("rnxSampl").toInt();
123 if ( settings.value("rnxPath").toString().isEmpty() ) {
124 _rnx = 0;
125 }
126 else {
127 _rnx = new bncRinex(_staID, mountPoint, format, latitude, longitude, nmea);
128 }
129
130 msleep(100); //sleep 0.1 sec
131}
132
133// Destructor
134////////////////////////////////////////////////////////////////////////////
135bncGetThread::~bncGetThread() {
136 if (_socket) {
137 _socket->close();
138#if QT_VERSION == 0x040203
139 delete _socket;
140#else
141 _socket->deleteLater();
142#endif
143 }
144 delete _decoder;
145 delete _rnx;
146}
147
148#define AGENTVERSION "1.5"
149// Connect to Caster, send the Request (static)
150////////////////////////////////////////////////////////////////////////////
151QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
152 QByteArray& latitude, QByteArray& longitude,
153 QByteArray& nmea, int timeOut,
154 QString& msg) {
155
156 // Connect the Socket
157 // ------------------
158 QSettings settings;
159 QString proxyHost = settings.value("proxyHost").toString();
160 int proxyPort = settings.value("proxyPort").toInt();
161
162 QTcpSocket* socket = new QTcpSocket();
163 if ( proxyHost.isEmpty() ) {
164 socket->connectToHost(mountPoint.host(), mountPoint.port());
165 }
166 else {
167 socket->connectToHost(proxyHost, proxyPort);
168 }
169 if (!socket->waitForConnected(timeOut)) {
170 msg += "Connect timeout\n";
171 delete socket;
172 return 0;
173 }
174
175 // Send Request
176 // ------------
177 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
178 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
179 QByteArray userAndPwd;
180
181 if(!uName.isEmpty() || !passW.isEmpty())
182 {
183 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
184 passW.toAscii()).toBase64() + "\r\n";
185 }
186
187 QUrl hlp;
188 hlp.setScheme("http");
189 hlp.setHost(mountPoint.host());
190 hlp.setPort(mountPoint.port());
191 hlp.setPath(mountPoint.path());
192
193 QByteArray reqStr;
194 if ( proxyHost.isEmpty() ) {
195 if (hlp.path().indexOf("/") != 0) hlp.setPath("/");
196 reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n";
197 } else {
198 reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n";
199 }
200 reqStr += "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
201 "Host: " + hlp.host().toAscii() + "\r\n"
202 + userAndPwd + "\r\n";
203
204// NMEA string to handle VRS stream
205// --------------------------------
206
207 double lat, lon;
208
209 lat = strtod(latitude,NULL);
210 lon = strtod(longitude,NULL);
211
212 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
213 const char* flagN="N";
214 const char* flagE="E";
215 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
216 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
217 if (lon < -180.) {lon=(lon+360.); flagE="E";}
218 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
219 QTime ttime(QDateTime::currentDateTime().toUTC().time());
220 int lat_deg = (int)lat;
221 double lat_min=(lat-lat_deg)*60.;
222 int lon_deg = (int)lon;
223 double lon_min=(lon-lon_deg)*60.;
224 int hh = 0 , mm = 0;
225 double ss = 0.0;
226 hh=ttime.hour();
227 mm=ttime.minute();
228 ss=(double)ttime.second()+0.001*ttime.msec();
229 QString gga;
230 gga += "GPGGA,";
231 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'));
232 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
233 gga += flagN;
234 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
235 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
236 int xori;
237 char XOR = 0;
238 char *Buff =gga.toAscii().data();
239 int iLen = strlen(Buff);
240 for (xori = 0; xori < iLen; xori++) {
241 XOR ^= (char)Buff[xori];
242 }
243 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
244 reqStr += "$";
245 reqStr += gga;
246 reqStr += "\r\n";
247 }
248
249 msg += reqStr;
250
251 socket->write(reqStr, reqStr.length());
252
253 if (!socket->waitForBytesWritten(timeOut)) {
254 msg += "Write timeout\n";
255 delete socket;
256 return 0;
257 }
258
259 return socket;
260}
261
262// Init Run
263////////////////////////////////////////////////////////////////////////////
264t_irc bncGetThread::initRun() {
265
266 // Initialize Socket
267 // -----------------
268 QString msg;
269 _socket = this->request(_mountPoint, _latitude, _longitude,
270 _nmea, _timeOut, msg);
271 if (!_socket) {
272 return failure;
273 }
274
275 // Read Caster Response
276 // --------------------
277 _socket->waitForReadyRead(_timeOut);
278 if (_socket->canReadLine()) {
279 QString line = _socket->readLine();
280
281 // Skip messages from proxy server
282 // -------------------------------
283 if (line.indexOf("ICY 200 OK") == -1 &&
284 line.indexOf("200 OK") != -1 ) {
285 bool proxyRespond = true;
286 while (true) {
287 if (_socket->canReadLine()) {
288 line = _socket->readLine();
289 if (!proxyRespond) {
290 break;
291 }
292 if (line.trimmed().isEmpty()) {
293 proxyRespond = false;
294 }
295 }
296 else {
297 _socket->waitForReadyRead(_timeOut);
298 if (_socket->bytesAvailable() <= 0) {
299 break;
300 }
301 }
302 }
303 }
304
305 if (line.indexOf("Unauthorized") != -1) {
306 QStringList table;
307 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
308 QString net;
309 QStringListIterator it(table);
310 while (it.hasNext()) {
311 QString line = it.next();
312 if (line.indexOf("STR") == 0) {
313 QStringList tags = line.split(";");
314 if (tags.at(1) == _staID_orig) {
315 net = tags.at(7);
316 break;
317 }
318 }
319 }
320
321 QString reg;
322 it.toFront();
323 while (it.hasNext()) {
324 QString line = it.next();
325 if (line.indexOf("NET") == 0) {
326 QStringList tags = line.split(";");
327 if (tags.at(1) == net) {
328 reg = tags.at(7);
329 break;
330 }
331 }
332 }
333 emit(newMessage((_staID + ": Caster Response: " + line +
334 " Adjust User-ID and Password Register, see"
335 "\n " + reg).toAscii()));
336 return fatal;
337 }
338 if (line.indexOf("ICY 200 OK") != 0) {
339 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
340 return failure;
341 }
342 }
343 else {
344 emit(newMessage(_staID + ": Response Timeout"));
345 return failure;
346 }
347
348 // Instantiate the filter
349 // ----------------------
350 if (!_decoder) {
351 if (_format.indexOf("RTCM_2") != -1) {
352 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
353 _decoder = new RTCM2Decoder();
354 }
355 else if (_format.indexOf("RTCM_3") != -1) {
356 emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
357 _decoder = new RTCM3Decoder();
358 }
359 else if (_format.indexOf("RTIGS") != -1) {
360 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
361 _decoder = new RTIGSDecoder();
362 }
363 else if (_format.indexOf("SP3") != -1 || _format.indexOf("ZERO") != -1) {
364 emit(newMessage("Get Data: " + _staID + " in original format"));
365 _decoder = new bncZeroDecoder(_staID);
366 }
367 else {
368 emit(newMessage(_staID + ": Unknown data format " + _format));
369 return fatal;
370 }
371 }
372 return success;
373}
374
375// Run
376////////////////////////////////////////////////////////////////////////////
377void bncGetThread::run() {
378
379 t_irc irc = initRun();
380
381 if (irc == fatal) {
382 QThread::exit(1);
383 return;
384 }
385 else if (irc != success) {
386 emit(newMessage(_staID + ": initRun failed, reconnecting"));
387 tryReconnect();
388 }
389
390 bool wrongEpoch = false;
391 bool decode = true;
392 int numSucc = 0;
393 int secSucc = 0;
394 int secFail = 0;
395 int initPause = 30;
396 int currPause = 0;
397 bool begCorrupt = false;
398 bool endCorrupt = false;
399 _decodeTime = QDateTime::currentDateTime();
400
401 if (initPause < _inspSegm) {
402 initPause = _inspSegm;
403 }
404 currPause = initPause;
405
406 // Read Incoming Data
407 // ------------------
408 while (true) {
409 try {
410 if (_socket->state() != QAbstractSocket::ConnectedState) {
411 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
412 tryReconnect();
413 }
414
415 QListIterator<p_obs> it(_decoder->_obsList);
416 while (it.hasNext()) {
417 delete it.next();
418 }
419 _decoder->_obsList.clear();
420
421 _socket->waitForReadyRead(_timeOut);
422 qint64 nBytes = _socket->bytesAvailable();
423 if (nBytes > 0) {
424 emit newBytes(_staID, nBytes);
425
426 char* data = new char[nBytes];
427 _socket->read(data, nBytes);
428
429 if (_inspSegm<1) {
430 _decoder->Decode(data, nBytes);
431 }
432 else {
433
434 // Decode data
435 // -----------
436 if (!_decodePause.isValid() ||
437 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
438
439 if (decode) {
440 if ( _decoder->Decode(data, nBytes) == success ) {
441 numSucc += 1;
442 }
443 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
444 decode = false;
445 }
446 }
447
448 // Check - once per inspect segment
449 // --------------------------------
450 if (!decode) {
451 _decodeTime = QDateTime::currentDateTime();
452 if (numSucc>0) {
453 secSucc += _inspSegm;
454 if (secSucc > _adviseReco * 60) {
455 secSucc = _adviseReco * 60 + 1;
456 }
457 numSucc = 0;
458 currPause = initPause;
459 _decodePause.setDate(QDate());
460 _decodePause.setTime(QTime());
461 }
462 else {
463 secFail += _inspSegm;
464 secSucc = 0;
465 if (secFail > _adviseFail * 60) {
466 secFail = _adviseFail * 60 + 1;
467 }
468 if (!_decodePause.isValid()) {
469 _decodePause = QDateTime::currentDateTime();
470 }
471 else {
472 _decodePause.setDate(QDate());
473 _decodePause.setTime(QTime());
474 secFail = secFail + currPause - _inspSegm;
475 currPause = currPause * 2;
476 if (currPause > 960) {
477 currPause = 960;
478 }
479 }
480 }
481
482 // End corrupt threshold
483 // ---------------------
484 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
485 _endDateCor = QDateTime::currentDateTime().toUTC().date().toString("dd-MM-yy");
486 _endTimeCor = QDateTime::currentDateTime().toUTC().time().toString("hh:mm:ss");
487 emit(newMessage(_staID + ": Corrupted recovery threshold exceeded"));
488 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
489 endCorrupt = true;
490 begCorrupt = false;
491 secFail = 0;
492 }
493 else {
494
495 // Begin corrupt threshold
496 // -----------------------
497 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
498 _begDateCor = QDateTime::currentDateTime().toUTC().date().toString("dd-MM-yy");
499 _begTimeCor = QDateTime::currentDateTime().toUTC().time().toString("hh:mm:ss");
500 emit(newMessage(_staID + ": Corrupted failure threshold exceeded"));
501 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
502 begCorrupt = true;
503 endCorrupt = false;
504 secSucc = 0;
505 numSucc = 0;
506 }
507 }
508 decode = true;
509 }
510 }
511 }
512
513 // End outage threshold
514 // --------------------
515 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
516 _decodeStart.setDate(QDate());
517 _decodeStart.setTime(QTime());
518 if (_inspSegm>0) {
519 _endDateOut = QDateTime::currentDateTime().toUTC().date().toString("dd-MM-yy");
520 _endTimeOut = QDateTime::currentDateTime().toUTC().time().toString("hh:mm:ss");
521 emit(newMessage(_staID + ": Outage recovery threshold exceeded"));
522 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
523 }
524 }
525
526 delete [] data;
527
528 QListIterator<p_obs> it(_decoder->_obsList);
529 while (it.hasNext()) {
530 p_obs obs = it.next();
531
532 // Check observation epoch
533 // -----------------------
534 int week;
535 double sec;
536 currentGPSWeeks(week, sec);
537
538 const double secPerWeek = 7.0 * 24.0 * 3600.0;
539 const double maxDt = 600.0;
540
541 if (week < obs->_o.GPSWeek) {
542 week += 1;
543 sec -= secPerWeek;
544 }
545 if (week > obs->_o.GPSWeek) {
546 week -= 1;
547 sec += secPerWeek;
548 }
549 double dt = fabs(sec - obs->_o.GPSWeeks);
550 if (week != obs->_o.GPSWeek || dt > maxDt) {
551 if (!wrongEpoch) {
552 emit( newMessage(_staID + ": Wrong observation epoch") );
553 wrongEpoch = true;
554 }
555 delete obs;
556 continue;
557 }
558 else {
559 wrongEpoch = false;
560 }
561
562 // RINEX Output
563 // ------------
564 if (_rnx) {
565 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
566 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
567 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
568 _rnx->deepCopy(obs);
569 }
570 _rnx->dumpEpoch(newTime);
571 }
572
573 bool firstObs = (obs == _decoder->_obsList.first());
574 obs->_status = t_obs::posted;
575 emit newObs(_staID, firstObs, obs);
576 }
577 _decoder->_obsList.clear();
578 }
579 else {
580 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
581 tryReconnect();
582 }
583 }
584 catch (const char* msg) {
585 emit(newMessage(_staID + msg));
586 tryReconnect();
587 }
588 }
589}
590
591// Exit
592////////////////////////////////////////////////////////////////////////////
593void bncGetThread::exit(int exitCode) {
594 if (exitCode!= 0) {
595 emit error(_staID);
596 }
597 QThread::exit(exitCode);
598 terminate();
599}
600
601// Try Re-Connect
602////////////////////////////////////////////////////////////////////////////
603void bncGetThread::tryReconnect() {
604 if (_rnx) {
605 _rnx->setReconnectFlag(true);
606 }
607 if ( !_decodeStart.isValid()) {
608 _decodeStop = QDateTime::currentDateTime();
609 }
610 while (1) {
611 delete _socket; _socket = 0;
612 sleep(_nextSleep);
613 if ( initRun() == success ) {
614 if ( !_decodeStop.isValid()) {
615 _decodeStart = QDateTime::currentDateTime();
616 }
617 break;
618 }
619 else {
620
621 // Begin outage threshold
622 // ----------------------
623 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
624 _decodeStop.setDate(QDate());
625 _decodeStop.setTime(QTime());
626 if (_inspSegm>0) {
627 _begDateOut = QDateTime::currentDateTime().toUTC().date().toString("dd-MM-yy");
628 _begTimeOut = QDateTime::currentDateTime().toUTC().time().toString("hh:mm:ss");
629 emit(newMessage(_staID + ": Outage failure threshold exceeded"));
630 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
631 }
632 }
633 _nextSleep *= 2;
634 if (_nextSleep > 256) {
635 _nextSleep = 256;
636 }
637 _nextSleep += rand() % 6;
638 }
639 }
640 _nextSleep = 1;
641}
642
643// Call advisory notice script
644////////////////////////////////////////////////////////////////////////////
645void bncGetThread::callScript(const char* _comment) {
646 QMutexLocker locker(&_mutex);
647 if (!_adviseScript.isEmpty()) {
648 msleep(1);
649#ifdef WIN32
650 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
651#else
652 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
653#endif
654 }
655}
Note: See TracBrowser for help on using the repository browser.