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

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

* empty log message *

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