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

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

* empty log message *

File size: 17.8 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"
[1621]56#include "bncnetqueryv0.h"
[1387]57#include "bncnetqueryv1.h"
58#include "bncnetqueryv2.h"
[1410]59#include "bncnetqueryrtp.h"
[1535]60#include "bncsettings.h"
[1558]61#include "latencychecker.h"
[65]62
[243]63#include "RTCM/RTCM2Decoder.h"
[297]64#include "RTCM3/RTCM3Decoder.h"
[293]65#include "RTIGS/RTIGSDecoder.h"
[1310]66#include "GPSS/gpssDecoder.h"
[1318]67#include "serial/qextserialport.h"
[35]68
69using namespace std;
70
[1143]71// Constructor 1
[35]72////////////////////////////////////////////////////////////////////////////
[1138]73bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
74 const QByteArray& format) {
75
76 _rawInpFile = new QFile(rawInpFileName);
77 _rawInpFile->open(QIODevice::ReadOnly);
[1555]78 _format = format;
79 _staID = rawInpFileName.mid(
80 rawInpFileName.lastIndexOf(QDir::separator())+1,4);
[1147]81
[1555]82 initialize();
[1138]83}
84
[1143]85// Constructor 2
86////////////////////////////////////////////////////////////////////////////
[278]87bncGetThread::bncGetThread(const QUrl& mountPoint,
[366]88 const QByteArray& format,
89 const QByteArray& latitude,
90 const QByteArray& longitude,
[1353]91 const QByteArray& nmea,
92 const QByteArray& ntripVersion, int iMount) {
[1555]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;
[1353]100 _ntripVersion = ntripVersion;
[1555]101 _iMount = iMount; // index in mountpoints array
[1139]102
103 initialize();
104}
105
[1555]106// Initialization (common part of the constructor)
[1143]107////////////////////////////////////////////////////////////////////////////
[1139]108void bncGetThread::initialize() {
109
[1555]110 setTerminationEnabled(true);
111
[1225]112 bncApp* app = (bncApp*) qApp;
113
[1555]114 connect(this, SIGNAL(newMessage(QByteArray,bool)),
115 app, SLOT(slotMessage(const QByteArray,bool)));
116
[1525]117 _isToBeDeleted = false;
[1555]118 _decoder = 0;
119 _query = 0;
120 _nextSleep = 0;
121 _rawOutFile = 0;
[1607]122 _serialOutFile = 0;
[1555]123 _staID_orig = _staID;
[255]124
[1574]125 bncSettings settings;
[1595]126
[1574]127 _miscMount = settings.value("miscMount").toString();
128
[1595]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
[255]136 // Check name conflict
137 // -------------------
138 QListIterator<QString> it(settings.value("mountPoints").toStringList());
139 int num = 0;
[278]140 int ind = -1;
[255]141 while (it.hasNext()) {
[278]142 ++ind;
[255]143 QStringList hlp = it.next().split(" ");
144 if (hlp.size() <= 1) continue;
145 QUrl url(hlp[0]);
146 if (_mountPoint.path() == url.path()) {
[1162]147 if (_iMount > ind || _iMount < 0) {
[278]148 ++num;
[255]149 }
150 }
151 }
[278]152
153 if (num > 0) {
154 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
[255]155 }
[408]156
157 // RINEX writer
158 // ------------
159 _samplingRate = settings.value("rnxSampl").toInt();
160 if ( settings.value("rnxPath").toString().isEmpty() ) {
161 _rnx = 0;
[1555]162 if (_rawInpFile) {
163 cerr << "no RINEX path specified" << endl;
164 ::exit(1);
165 }
[408]166 }
167 else {
[1353]168 _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
[1639]169 _longitude, _nmea, _ntripVersion);
[408]170 }
171
[1555]172 // Serial Port
173 // -----------
[1327]174 if (settings.value("serialMountPoint").toString() == _staID) {
175 _serialPort = new QextSerialPort(
176 settings.value("serialPortName").toString() );
[1330]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 }
[1326]244 _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
[1331]245 if (!_serialPort->isOpen()) {
246 delete _serialPort;
247 _serialPort = 0;
[1451]248 emit(newMessage((_staID + ": Cannot open serial port\n"), true));
[1331]249 }
[1606]250
251 connect(_serialPort, SIGNAL(readyRead()),
252 this, SLOT(slotSerialReadyRead()));
253
254 // Serial File Output
255 // ------------------
256 QString serialFileNMEA = settings.value("serialFileNMEA").toString();
[1625]257 QString serialAutoNMEA = settings.value("serialAutoNMEA").toString();
258 if (!serialFileNMEA.isEmpty() && serialAutoNMEA == "Auto" ) {
[1606]259 _serialOutFile = new QFile(serialFileNMEA);
[1634]260 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
261 _serialOutFile->open(QIODevice::WriteOnly | QIODevice::Append);
262 }
263 else {
264 _serialOutFile->open(QIODevice::WriteOnly);
265 }
[1606]266 }
[1633]267 _serialPort->setTimeout(0,100);
[1636]268 hlp = settings.value("serialFlowControl").toString();
269 if (hlp == "OFF") {
270 _serialPort->setFlowControl(FLOW_OFF);
271 }
272 else if (hlp == "XONXOFF") {
273 _serialPort->setFlowControl(FLOW_XONXOFF);
274 }
275else if (hlp == "HARDWARE") {
276 _serialPort->setFlowControl(FLOW_HARDWARE);
277 }
[1318]278 }
279 else {
280 _serialPort = 0;
281 }
282
[1137]283 // Raw Output
284 // ----------
[1140]285 // QByteArray rawOutFileName = "./" + _staID + ".raw";
286 // _rawOutFile = new QFile(rawOutFileName);
287 // _rawOutFile->open(QIODevice::WriteOnly);
[1137]288
[1555]289
290 // Instantiate the decoder
291 // -----------------------
292 if (_format.indexOf("RTCM_2") != -1) {
293 emit(newMessage(_staID + ": Get data in RTCM 2.x format", true));
294 _decoder = new RTCM2Decoder(_staID.data());
295 }
296 else if (_format.indexOf("RTCM_3") != -1) {
297 emit(newMessage(_staID + ": Get data in RTCM 3.x format", true));
298 _decoder = new RTCM3Decoder(_staID);
299 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
300 this, SIGNAL(newMessage(QByteArray,bool)));
301 }
302 else if (_format.indexOf("RTIGS") != -1) {
303 emit(newMessage(_staID + ": Get data in RTIGS format", true));
304 _decoder = new RTIGSDecoder();
305 }
306 else if (_format.indexOf("GPSS") != -1 || _format.indexOf("BNC") != -1) {
307 emit(newMessage(_staID + ": Get Data in GPSS format", true));
308 _decoder = new gpssDecoder();
309 }
310 else if (_format.indexOf("ZERO") != -1) {
311 emit(newMessage(_staID + ": Get data in original format", true));
312 _decoder = new bncZeroDecoder(_staID);
313 }
314 else {
315 emit(newMessage(_staID + ": Unknown data format " + _format, true));
316 _isToBeDeleted = true;
317 delete this;
318 }
319
[1558]320 _latencyChecker = new latencyChecker(_staID);
[1557]321
[319]322 msleep(100); //sleep 0.1 sec
[35]323}
324
325// Destructor
326////////////////////////////////////////////////////////////////////////////
327bncGetThread::~bncGetThread() {
[1527]328 if (_query) {
329 _query->stop();
330 delete _query;
331 }
[617]332 delete _decoder;
[1044]333 delete _rnx;
[1138]334 delete _rawInpFile;
335 delete _rawOutFile;
[1607]336 delete _serialOutFile;
[1328]337 delete _serialPort;
[1557]338 delete _latencyChecker;
[1556]339 emit getThreadFinished(_staID);
[35]340}
341
[1390]342//
343////////////////////////////////////////////////////////////////////////////
344void bncGetThread::terminate() {
[1525]345 _isToBeDeleted = true;
[1559]346 if (!isRunning()) {
347 delete this;
348 }
[1390]349}
350
[136]351// Run
352////////////////////////////////////////////////////////////////////////////
353void bncGetThread::run() {
354
[35]355 while (true) {
[629]356 try {
[1555]357 if (_isToBeDeleted) {
358 QThread::exit(0);
359 this->deleteLater();
360 return;
[629]361 }
[621]362
[1555]363 if (tryReconnect() != success) {
[1572]364 _latencyChecker->checkReconnect();
[1555]365 continue;
[621]366 }
[1555]367
368 // Delete old observations
369 // -----------------------
370 QListIterator<p_obs> itOld(_decoder->_obsList);
371 while (itOld.hasNext()) {
372 delete itOld.next();
373 }
[621]374 _decoder->_obsList.clear();
375
[1555]376 // Read Data
377 // ---------
[1377]378 QByteArray data;
379 if (_query) {
380 _query->waitForReadyRead(data);
[1138]381 }
382 else if (_rawInpFile) {
[1146]383 const qint64 maxBytes = 1024;
[1555]384 data = _rawInpFile->read(maxBytes);
385 if (data.isEmpty()) {
386 cout << "no more data" << endl;
387 ::exit(0);
388 }
[1138]389 }
[1555]390 qint64 nBytes = data.size();
[1138]391
[1555]392 // Timeout, reconnect
393 // ------------------
394 if (nBytes == 0) {
[1576]395 _latencyChecker->checkReconnect();
[1555]396 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
397 continue;
398 }
399 else {
[567]400 emit newBytes(_staID, nBytes);
[1555]401 }
[567]402
[1555]403 // Output Data
404 // -----------
405 if (_rawOutFile) {
406 _rawOutFile->write(data);
407 _rawOutFile->flush();
408 }
409 if (_serialPort) {
[1633]410 slotSerialReadyRead();
[1555]411 _serialPort->write(data);
412 }
413
414 // Decode Data
415 // -----------
416 vector<string> errmsg;
[1562]417 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
[1137]418
[1555]419 // Perform various scans and checks
420 // --------------------------------
[1562]421 _latencyChecker->checkOutage(irc == success);
422 _latencyChecker->checkObsLatency(_decoder->_obsList);
[1567]423 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
424
[1555]425 scanRTCM();
[1138]426
[1555]427 // Loop over all observations (observations output)
428 // ------------------------------------------------
429 QListIterator<p_obs> it(_decoder->_obsList);
430 while (it.hasNext()) {
431 p_obs obs = it.next();
432
433 // Check observation epoch
434 // -----------------------
435 if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
436 int week;
437 double sec;
438 currentGPSWeeks(week, sec);
439 const double secPerWeek = 7.0 * 24.0 * 3600.0;
440
441 if (week < obs->_o.GPSWeek) {
442 week += 1;
443 sec -= secPerWeek;
[1218]444 }
[1555]445 if (week > obs->_o.GPSWeek) {
446 week -= 1;
447 sec += secPerWeek;
[650]448 }
[1555]449 double dt = fabs(sec - obs->_o.GPSWeeks);
450 const double maxDt = 600.0;
451 if (week != obs->_o.GPSWeek || dt > maxDt) {
452 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
453 delete obs;
454 continue;
[940]455 }
[686]456 }
[1555]457
458 // RINEX Output
459 // ------------
460 if (_rnx) {
461 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
462 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
463 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
464 _rnx->deepCopy(obs);
[1271]465 }
[1555]466 _rnx->dumpEpoch(newTime);
[1271]467 }
[1555]468
469 // Emit new observation signal
470 // ---------------------------
471 bool firstObs = (obs == _decoder->_obsList.first());
472 obs->_status = t_obs::posted;
473 emit newObs(_staID, firstObs, obs);
[249]474 }
[1555]475 _decoder->_obsList.clear();
[35]476 }
[1555]477 catch (...) {
[1559]478 emit(newMessage(_staID + "bncGetThread exception", true));
479 _isToBeDeleted = true;
[1555]480 }
[35]481 }
482}
483
[136]484// Try Re-Connect
485////////////////////////////////////////////////////////////////////////////
[1555]486t_irc bncGetThread::tryReconnect() {
487
[1627]488 bncSettings settings;
[1629]489 bool manual = true;
490 if ( _staID == settings.value("serialMountPoint").toString() &&
491 settings.value("serialAutoNMEA").toString() == "Auto" ) {
492 manual = false;
493 }
[1627]494
[1555]495 // Easy Return
496 // -----------
497 if (_query && _query->status() == bncNetQuery::running) {
498 _nextSleep = 0;
499 return success;
[408]500 }
[1555]501
502 // Start a new query
503 // -----------------
504 if (!_rawInpFile) {
505
[138]506 sleep(_nextSleep);
[1555]507 if (_nextSleep == 0) {
508 _nextSleep = 1;
[138]509 }
510 else {
[1555]511 _nextSleep = 2 * _nextSleep;
[442]512 if (_nextSleep > 256) {
513 _nextSleep = 256;
[152]514 }
[138]515 }
[1555]516
517 delete _query;
518 if (_ntripVersion == "R") {
519 _query = new bncNetQueryRtp();
520 }
[1621]521 else if (_ntripVersion == "N") {
522 _query = new bncNetQueryV0();
523 }
[1555]524 else if (_ntripVersion == "2") {
525 _query = new bncNetQueryV2();
526 }
527 else {
528 _query = new bncNetQueryV1();
529 }
[1629]530 if (_nmea == "yes" && manual ) {
[1595]531 QByteArray gga = ggaString(_latitude, _longitude, _height);
[1555]532 _query->startRequest(_mountPoint, gga);
533 }
534 else {
535 _query->startRequest(_mountPoint, "");
536 }
537 if (_query->status() != bncNetQuery::running) {
538 return failure;
539 }
[138]540 }
[658]541
[1555]542 if (_rnx) {
543 _rnx->setReconnectFlag(true);
[658]544 }
[1555]545
546 return success;
[658]547}
[1044]548
[1555]549// RTCM scan output
[1044]550//////////////////////////////////////////////////////////////////////////////
[1555]551void bncGetThread::scanRTCM() {
[1044]552
[1555]553 bncSettings settings;
[1574]554 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
555
556 if ( _miscMount == _staID || _miscMount == "ALL" ) {
557
[1575]558 // RTCM message types
559 // ------------------
[1574]560 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
561 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
562 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
563 }
[1044]564
[1574]565 // RTCMv3 antenna descriptor
566 // -------------------------
567 for (int ii=0;ii<_decoder->_antType.size();ii++) {
568 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
569 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
570 }
[1555]571
[1575]572 // RTCM Antenna Coordinates
573 // ------------------------
[1574]574 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
575 QByteArray antT;
576 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
577 antT = "ARP";
578 }
579 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
580 antT = "APC";
581 }
[1575]582 QByteArray ant1, ant2, ant3;
583 ant1 = QString("%1 ").arg(_decoder->_antList[ii].xx,0,'f',4).toAscii();
584 ant2 = QString("%1 ").arg(_decoder->_antList[ii].yy,0,'f',4).toAscii();
585 ant3 = QString("%1 ").arg(_decoder->_antList[ii].zz,0,'f',4).toAscii();
586 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
587 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
588 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
589 if (_decoder->_antList[ii].height_f) {
590 QByteArray ant4 = QString("%1 ").arg(_decoder->_antList[ii].height,0,'f',4).toAscii();
591 emit(newMessage(_staID + ": Antenna height above marker " + ant4 + "m", true));
592 }
[1574]593 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
594 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
595 antT));
[1218]596 }
597 }
[1044]598 }
[1575]599
[1555]600 _decoder->_typeList.clear();
601 _decoder->_antType.clear();
602 _decoder->_antList.clear();
[1044]603}
[1555]604
[1606]605// Handle Data from Serial Port
606////////////////////////////////////////////////////////////////////////////
607void bncGetThread::slotSerialReadyRead() {
[1607]608 if (_serialPort) {
[1633]609 int nb = _serialPort->bytesAvailable();
610 if (nb > 0) {
611 QByteArray data = _serialPort->read(nb);
612 if (_serialOutFile) {
613 _serialOutFile->write(data);
614 _serialOutFile->flush();
615 }
[1607]616 }
617 }
[1606]618}
Note: See TracBrowser for help on using the repository browser.