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

Last change on this file since 1628 was 1628, checked in by weber, 15 years ago

* empty log message *

File size: 16.9 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#include "bncnetqueryv0.h"
57#include "bncnetqueryv1.h"
58#include "bncnetqueryv2.h"
59#include "bncnetqueryrtp.h"
60#include "bncsettings.h"
61#include "latencychecker.h"
62
63#include "RTCM/RTCM2Decoder.h"
64#include "RTCM3/RTCM3Decoder.h"
65#include "RTIGS/RTIGSDecoder.h"
66#include "GPSS/gpssDecoder.h"
67#include "serial/qextserialport.h"
68
69using namespace std;
70
71// Constructor 1
72////////////////////////////////////////////////////////////////////////////
73bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
74 const QByteArray& format) {
75
76 _rawInpFile = new QFile(rawInpFileName);
77 _rawInpFile->open(QIODevice::ReadOnly);
78 _format = format;
79 _staID = rawInpFileName.mid(
80 rawInpFileName.lastIndexOf(QDir::separator())+1,4);
81
82 initialize();
83}
84
85// Constructor 2
86////////////////////////////////////////////////////////////////////////////
87bncGetThread::bncGetThread(const QUrl& mountPoint,
88 const QByteArray& format,
89 const QByteArray& latitude,
90 const QByteArray& longitude,
91 const QByteArray& nmea,
92 const QByteArray& ntripVersion, int iMount) {
93 _rawInpFile = 0;
94 _mountPoint = mountPoint;
95 _staID = mountPoint.path().mid(1).toAscii();
96 _format = format;
97 _latitude = latitude;
98 _longitude = longitude;
99 _nmea = nmea;
100 _ntripVersion = ntripVersion;
101 _iMount = iMount; // index in mountpoints array
102
103 initialize();
104}
105
106// Initialization (common part of the constructor)
107////////////////////////////////////////////////////////////////////////////
108void bncGetThread::initialize() {
109
110 setTerminationEnabled(true);
111
112 bncApp* app = (bncApp*) qApp;
113
114 connect(this, SIGNAL(newMessage(QByteArray,bool)),
115 app, SLOT(slotMessage(const QByteArray,bool)));
116
117 _isToBeDeleted = false;
118 _decoder = 0;
119 _query = 0;
120 _nextSleep = 0;
121 _rawOutFile = 0;
122 _serialOutFile = 0;
123 _staID_orig = _staID;
124
125 bncSettings settings;
126
127 _miscMount = settings.value("miscMount").toString();
128
129 if ( settings.value("serialMountPoint").toString() == _staID &&
130 settings.value("serialAutoNMEA").toString() != "Auto" ) {
131 _height = settings.value("serialHeightNMEA").toString().toAscii();
132 } else {
133 _height = "100";
134 }
135
136 // Check name conflict
137 // -------------------
138 QListIterator<QString> it(settings.value("mountPoints").toStringList());
139 int num = 0;
140 int ind = -1;
141 while (it.hasNext()) {
142 ++ind;
143 QStringList hlp = it.next().split(" ");
144 if (hlp.size() <= 1) continue;
145 QUrl url(hlp[0]);
146 if (_mountPoint.path() == url.path()) {
147 if (_iMount > ind || _iMount < 0) {
148 ++num;
149 }
150 }
151 }
152
153 if (num > 0) {
154 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
155 }
156
157 // RINEX writer
158 // ------------
159 _samplingRate = settings.value("rnxSampl").toInt();
160 if ( settings.value("rnxPath").toString().isEmpty() ) {
161 _rnx = 0;
162 if (_rawInpFile) {
163 cerr << "no RINEX path specified" << endl;
164 ::exit(1);
165 }
166 }
167 else {
168 _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
169 _longitude, _nmea);
170 }
171
172 // Serial Port
173 // -----------
174 if (settings.value("serialMountPoint").toString() == _staID) {
175 _serialPort = new QextSerialPort(
176 settings.value("serialPortName").toString() );
177 QString hlp = settings.value("serialBaudRate").toString();
178 if (hlp == "110") {
179 _serialPort->setBaudRate(BAUD110);
180 }
181 else if (hlp == "300") {
182 _serialPort->setBaudRate(BAUD300);
183 }
184 else if (hlp == "600") {
185 _serialPort->setBaudRate(BAUD600);
186 }
187 else if (hlp == "1200") {
188 _serialPort->setBaudRate(BAUD1200);
189 }
190 else if (hlp == "2400") {
191 _serialPort->setBaudRate(BAUD2400);
192 }
193 else if (hlp == "4800") {
194 _serialPort->setBaudRate(BAUD4800);
195 }
196 else if (hlp == "9600") {
197 _serialPort->setBaudRate(BAUD9600);
198 }
199 else if (hlp == "19200") {
200 _serialPort->setBaudRate(BAUD19200);
201 }
202 else if (hlp == "38400") {
203 _serialPort->setBaudRate(BAUD38400);
204 }
205 else if (hlp == "57600") {
206 _serialPort->setBaudRate(BAUD57600);
207 }
208 else if (hlp == "115200") {
209 _serialPort->setBaudRate(BAUD115200);
210 }
211 hlp = settings.value("serialParity").toString();
212 if (hlp == "NONE") {
213 _serialPort->setParity(PAR_NONE);
214 }
215 else if (hlp == "ODD") {
216 _serialPort->setParity(PAR_ODD);
217 }
218 else if (hlp == "EVEN") {
219 _serialPort->setParity(PAR_EVEN);
220 }
221 else if (hlp == "SPACE") {
222 _serialPort->setParity(PAR_SPACE);
223 }
224 hlp = settings.value("serialDataBits").toString();
225 if (hlp == "5") {
226 _serialPort->setDataBits(DATA_5);
227 }
228 else if (hlp == "6") {
229 _serialPort->setDataBits(DATA_6);
230 }
231 else if (hlp == "7") {
232 _serialPort->setDataBits(DATA_7);
233 }
234 else if (hlp == "8") {
235 _serialPort->setDataBits(DATA_8);
236 }
237 hlp = settings.value("serialStopBits").toString();
238 if (hlp == "1") {
239 _serialPort->setStopBits(STOP_1);
240 }
241 else if (hlp == "2") {
242 _serialPort->setStopBits(STOP_2);
243 }
244 _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
245 if (!_serialPort->isOpen()) {
246 delete _serialPort;
247 _serialPort = 0;
248 emit(newMessage((_staID + ": Cannot open serial port\n"), true));
249 }
250
251 connect(_serialPort, SIGNAL(readyRead()),
252 this, SLOT(slotSerialReadyRead()));
253
254 // Serial File Output
255 // ------------------
256 QString serialFileNMEA = settings.value("serialFileNMEA").toString();
257 QString serialAutoNMEA = settings.value("serialAutoNMEA").toString();
258 if (!serialFileNMEA.isEmpty() && serialAutoNMEA == "Auto" ) {
259 _serialOutFile = new QFile(serialFileNMEA);
260 _serialOutFile->open(QIODevice::WriteOnly);
261 }
262 }
263 else {
264 _serialPort = 0;
265 }
266
267 // Raw Output
268 // ----------
269 // QByteArray rawOutFileName = "./" + _staID + ".raw";
270 // _rawOutFile = new QFile(rawOutFileName);
271 // _rawOutFile->open(QIODevice::WriteOnly);
272
273
274 // Instantiate the decoder
275 // -----------------------
276 if (_format.indexOf("RTCM_2") != -1) {
277 emit(newMessage(_staID + ": Get data in RTCM 2.x format", true));
278 _decoder = new RTCM2Decoder(_staID.data());
279 }
280 else if (_format.indexOf("RTCM_3") != -1) {
281 emit(newMessage(_staID + ": Get data in RTCM 3.x format", true));
282 _decoder = new RTCM3Decoder(_staID);
283 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
284 this, SIGNAL(newMessage(QByteArray,bool)));
285 }
286 else if (_format.indexOf("RTIGS") != -1) {
287 emit(newMessage(_staID + ": Get data in RTIGS format", true));
288 _decoder = new RTIGSDecoder();
289 }
290 else if (_format.indexOf("GPSS") != -1 || _format.indexOf("BNC") != -1) {
291 emit(newMessage(_staID + ": Get Data in GPSS format", true));
292 _decoder = new gpssDecoder();
293 }
294 else if (_format.indexOf("ZERO") != -1) {
295 emit(newMessage(_staID + ": Get data in original format", true));
296 _decoder = new bncZeroDecoder(_staID);
297 }
298 else {
299 emit(newMessage(_staID + ": Unknown data format " + _format, true));
300 _isToBeDeleted = true;
301 delete this;
302 }
303
304 _latencyChecker = new latencyChecker(_staID);
305
306 msleep(100); //sleep 0.1 sec
307}
308
309// Destructor
310////////////////////////////////////////////////////////////////////////////
311bncGetThread::~bncGetThread() {
312 if (_query) {
313 _query->stop();
314 delete _query;
315 }
316 delete _decoder;
317 delete _rnx;
318 delete _rawInpFile;
319 delete _rawOutFile;
320 delete _serialOutFile;
321 delete _serialPort;
322 delete _latencyChecker;
323 emit getThreadFinished(_staID);
324}
325
326//
327////////////////////////////////////////////////////////////////////////////
328void bncGetThread::terminate() {
329 _isToBeDeleted = true;
330 if (!isRunning()) {
331 delete this;
332 }
333}
334
335// Run
336////////////////////////////////////////////////////////////////////////////
337void bncGetThread::run() {
338
339 while (true) {
340 try {
341 if (_isToBeDeleted) {
342 QThread::exit(0);
343 this->deleteLater();
344 return;
345 }
346
347 if (tryReconnect() != success) {
348 _latencyChecker->checkReconnect();
349 continue;
350 }
351
352 // Delete old observations
353 // -----------------------
354 QListIterator<p_obs> itOld(_decoder->_obsList);
355 while (itOld.hasNext()) {
356 delete itOld.next();
357 }
358 _decoder->_obsList.clear();
359
360 // Read Data
361 // ---------
362 QByteArray data;
363 if (_query) {
364 _query->waitForReadyRead(data);
365 }
366 else if (_rawInpFile) {
367 const qint64 maxBytes = 1024;
368 data = _rawInpFile->read(maxBytes);
369 if (data.isEmpty()) {
370 cout << "no more data" << endl;
371 ::exit(0);
372 }
373 }
374 qint64 nBytes = data.size();
375
376 // Timeout, reconnect
377 // ------------------
378 if (nBytes == 0) {
379 _latencyChecker->checkReconnect();
380 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
381 continue;
382 }
383 else {
384 emit newBytes(_staID, nBytes);
385 }
386
387 // Output Data
388 // -----------
389 if (_rawOutFile) {
390 _rawOutFile->write(data);
391 _rawOutFile->flush();
392 }
393 if (_serialPort) {
394 _serialPort->write(data);
395 }
396
397 // Decode Data
398 // -----------
399 vector<string> errmsg;
400 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
401
402 // Perform various scans and checks
403 // --------------------------------
404 _latencyChecker->checkOutage(irc == success);
405 _latencyChecker->checkObsLatency(_decoder->_obsList);
406 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
407
408 scanRTCM();
409
410 // Loop over all observations (observations output)
411 // ------------------------------------------------
412 QListIterator<p_obs> it(_decoder->_obsList);
413 while (it.hasNext()) {
414 p_obs obs = it.next();
415
416 // Check observation epoch
417 // -----------------------
418 if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
419 int week;
420 double sec;
421 currentGPSWeeks(week, sec);
422 const double secPerWeek = 7.0 * 24.0 * 3600.0;
423
424 if (week < obs->_o.GPSWeek) {
425 week += 1;
426 sec -= secPerWeek;
427 }
428 if (week > obs->_o.GPSWeek) {
429 week -= 1;
430 sec += secPerWeek;
431 }
432 double dt = fabs(sec - obs->_o.GPSWeeks);
433 const double maxDt = 600.0;
434 if (week != obs->_o.GPSWeek || dt > maxDt) {
435 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
436 delete obs;
437 continue;
438 }
439 }
440
441 // RINEX Output
442 // ------------
443 if (_rnx) {
444 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
445 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
446 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
447 _rnx->deepCopy(obs);
448 }
449 _rnx->dumpEpoch(newTime);
450 }
451
452 // Emit new observation signal
453 // ---------------------------
454 bool firstObs = (obs == _decoder->_obsList.first());
455 obs->_status = t_obs::posted;
456 emit newObs(_staID, firstObs, obs);
457 }
458 _decoder->_obsList.clear();
459 }
460 catch (...) {
461 emit(newMessage(_staID + "bncGetThread exception", true));
462 _isToBeDeleted = true;
463 }
464 }
465}
466
467// Try Re-Connect
468////////////////////////////////////////////////////////////////////////////
469t_irc bncGetThread::tryReconnect() {
470
471 bncSettings settings;
472
473 // Easy Return
474 // -----------
475 if (_query && _query->status() == bncNetQuery::running) {
476 _nextSleep = 0;
477 return success;
478 }
479
480 // Start a new query
481 // -----------------
482 if (!_rawInpFile) {
483
484 sleep(_nextSleep);
485 if (_nextSleep == 0) {
486 _nextSleep = 1;
487 }
488 else {
489 _nextSleep = 2 * _nextSleep;
490 if (_nextSleep > 256) {
491 _nextSleep = 256;
492 }
493 }
494
495 delete _query;
496 if (_ntripVersion == "R") {
497 _query = new bncNetQueryRtp();
498 }
499 else if (_ntripVersion == "N") {
500 _query = new bncNetQueryV0();
501 }
502 else if (_ntripVersion == "2") {
503 _query = new bncNetQueryV2();
504 }
505 else {
506 _query = new bncNetQueryV1();
507 }
508 if (_nmea == "yes") {
509 QByteArray gga = ggaString(_latitude, _longitude, _height);
510 _query->startRequest(_mountPoint, gga);
511 }
512 else {
513 _query->startRequest(_mountPoint, "");
514 }
515 if (_query->status() != bncNetQuery::running) {
516 return failure;
517 }
518 }
519
520 if (_rnx) {
521 _rnx->setReconnectFlag(true);
522 }
523
524 return success;
525}
526
527// RTCM scan output
528//////////////////////////////////////////////////////////////////////////////
529void bncGetThread::scanRTCM() {
530
531 bncSettings settings;
532 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
533
534 if ( _miscMount == _staID || _miscMount == "ALL" ) {
535
536 // RTCM message types
537 // ------------------
538 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
539 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
540 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
541 }
542
543 // RTCMv3 antenna descriptor
544 // -------------------------
545 for (int ii=0;ii<_decoder->_antType.size();ii++) {
546 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
547 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
548 }
549
550 // RTCM Antenna Coordinates
551 // ------------------------
552 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
553 QByteArray antT;
554 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
555 antT = "ARP";
556 }
557 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
558 antT = "APC";
559 }
560 QByteArray ant1, ant2, ant3;
561 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
562 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
563 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
564 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
565 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
566 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
567 if (_decoder->_antList[ii].height_f) {
568 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
569 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
570 }
571 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
572 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
573 antT));
574 }
575 }
576 }
577
578 _decoder->_typeList.clear();
579 _decoder->_antType.clear();
580 _decoder->_antList.clear();
581}
582
583// Handle Data from Serial Port
584////////////////////////////////////////////////////////////////////////////
585void bncGetThread::slotSerialReadyRead() {
586 if (_serialPort) {
587 QByteArray data = _serialPort->readAll();
588 if (_serialOutFile) {
589 _serialOutFile->write(data);
590 _serialOutFile->flush();
591 }
592 }
593}
Note: See TracBrowser for help on using the repository browser.