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

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

* empty log message *

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