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

Last change on this file since 1618 was 1607, checked in by mervart, 17 years ago

* empty log message *

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