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

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

* empty log message *

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