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

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

* empty log message *

File size: 16.0 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 "bncnetqueryv1.h"
57#include "bncnetqueryv2.h"
58#include "bncnetqueryrtp.h"
59#include "bncsettings.h"
60#include "latencychecker.h"
61
62#include "RTCM/RTCM2Decoder.h"
63#include "RTCM3/RTCM3Decoder.h"
64#include "RTIGS/RTIGSDecoder.h"
65#include "GPSS/gpssDecoder.h"
66#include "serial/qextserialport.h"
67
68using namespace std;
69
70// Constructor 1
71////////////////////////////////////////////////////////////////////////////
72bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
73 const QByteArray& format) {
74
75 _rawInpFile = new QFile(rawInpFileName);
76 _rawInpFile->open(QIODevice::ReadOnly);
77 _format = format;
78 _staID = rawInpFileName.mid(
79 rawInpFileName.lastIndexOf(QDir::separator())+1,4);
80
81 initialize();
82}
83
84// Constructor 2
85////////////////////////////////////////////////////////////////////////////
86bncGetThread::bncGetThread(const QUrl& mountPoint,
87 const QByteArray& format,
88 const QByteArray& latitude,
89 const QByteArray& longitude,
90 const QByteArray& nmea,
91 const QByteArray& ntripVersion, int iMount) {
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;
99 _ntripVersion = ntripVersion;
100 _iMount = iMount; // index in mountpoints array
101
102 initialize();
103}
104
105// Initialization (common part of the constructor)
106////////////////////////////////////////////////////////////////////////////
107void bncGetThread::initialize() {
108
109 setTerminationEnabled(true);
110
111 bncApp* app = (bncApp*) qApp;
112
113 connect(this, SIGNAL(newMessage(QByteArray,bool)),
114 app, SLOT(slotMessage(const QByteArray,bool)));
115
116 _isToBeDeleted = false;
117 _decoder = 0;
118 _query = 0;
119 _nextSleep = 0;
120 _rawOutFile = 0;
121 _staID_orig = _staID;
122
123 bncSettings settings;
124
125 _miscMount = settings.value("miscMount").toString();
126
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
134 // Check name conflict
135 // -------------------
136 QListIterator<QString> it(settings.value("mountPoints").toStringList());
137 int num = 0;
138 int ind = -1;
139 while (it.hasNext()) {
140 ++ind;
141 QStringList hlp = it.next().split(" ");
142 if (hlp.size() <= 1) continue;
143 QUrl url(hlp[0]);
144 if (_mountPoint.path() == url.path()) {
145 if (_iMount > ind || _iMount < 0) {
146 ++num;
147 }
148 }
149 }
150
151 if (num > 0) {
152 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
153 }
154
155 // RINEX writer
156 // ------------
157 _samplingRate = settings.value("rnxSampl").toInt();
158 if ( settings.value("rnxPath").toString().isEmpty() ) {
159 _rnx = 0;
160 if (_rawInpFile) {
161 cerr << "no RINEX path specified" << endl;
162 ::exit(1);
163 }
164 }
165 else {
166 _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
167 _longitude, _nmea);
168 }
169
170 // Serial Port
171 // -----------
172 if (settings.value("serialMountPoint").toString() == _staID) {
173 _serialPort = new QextSerialPort(
174 settings.value("serialPortName").toString() );
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 }
242 _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
243 if (!_serialPort->isOpen()) {
244 delete _serialPort;
245 _serialPort = 0;
246 emit(newMessage((_staID + ": Cannot open serial port\n"), true));
247 }
248 }
249 else {
250 _serialPort = 0;
251 }
252
253 // Raw Output
254 // ----------
255 // QByteArray rawOutFileName = "./" + _staID + ".raw";
256 // _rawOutFile = new QFile(rawOutFileName);
257 // _rawOutFile->open(QIODevice::WriteOnly);
258
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
290 _latencyChecker = new latencyChecker(_staID);
291
292 msleep(100); //sleep 0.1 sec
293}
294
295// Destructor
296////////////////////////////////////////////////////////////////////////////
297bncGetThread::~bncGetThread() {
298 if (_query) {
299 _query->stop();
300 delete _query;
301 }
302 delete _decoder;
303 delete _rnx;
304 delete _rawInpFile;
305 delete _rawOutFile;
306 delete _serialPort;
307 delete _latencyChecker;
308 emit getThreadFinished(_staID);
309}
310
311//
312////////////////////////////////////////////////////////////////////////////
313void bncGetThread::terminate() {
314 _isToBeDeleted = true;
315 if (!isRunning()) {
316 delete this;
317 }
318}
319
320// Run
321////////////////////////////////////////////////////////////////////////////
322void bncGetThread::run() {
323
324 while (true) {
325 try {
326 if (_isToBeDeleted) {
327 QThread::exit(0);
328 this->deleteLater();
329 return;
330 }
331
332 if (tryReconnect() != success) {
333 _latencyChecker->checkReconnect();
334 continue;
335 }
336
337 // Delete old observations
338 // -----------------------
339 QListIterator<p_obs> itOld(_decoder->_obsList);
340 while (itOld.hasNext()) {
341 delete itOld.next();
342 }
343 _decoder->_obsList.clear();
344
345 // Read Data
346 // ---------
347 QByteArray data;
348 if (_query) {
349 _query->waitForReadyRead(data);
350 }
351 else if (_rawInpFile) {
352 const qint64 maxBytes = 1024;
353 data = _rawInpFile->read(maxBytes);
354 if (data.isEmpty()) {
355 cout << "no more data" << endl;
356 ::exit(0);
357 }
358 }
359 qint64 nBytes = data.size();
360
361 // Timeout, reconnect
362 // ------------------
363 if (nBytes == 0) {
364 _latencyChecker->checkReconnect();
365 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
366 continue;
367 }
368 else {
369 emit newBytes(_staID, nBytes);
370 }
371
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;
385 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
386
387 // Perform various scans and checks
388 // --------------------------------
389 _latencyChecker->checkOutage(irc == success);
390 _latencyChecker->checkObsLatency(_decoder->_obsList);
391 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
392
393 scanRTCM();
394
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;
412 }
413 if (week > obs->_o.GPSWeek) {
414 week -= 1;
415 sec += secPerWeek;
416 }
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;
423 }
424 }
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);
433 }
434 _rnx->dumpEpoch(newTime);
435 }
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);
442 }
443 _decoder->_obsList.clear();
444 }
445 catch (...) {
446 emit(newMessage(_staID + "bncGetThread exception", true));
447 _isToBeDeleted = true;
448 }
449 }
450}
451
452// Try Re-Connect
453////////////////////////////////////////////////////////////////////////////
454t_irc bncGetThread::tryReconnect() {
455
456 // Easy Return
457 // -----------
458 if (_query && _query->status() == bncNetQuery::running) {
459 _nextSleep = 0;
460 return success;
461 }
462
463 // Start a new query
464 // -----------------
465 if (!_rawInpFile) {
466
467 sleep(_nextSleep);
468 if (_nextSleep == 0) {
469 _nextSleep = 1;
470 }
471 else {
472 _nextSleep = 2 * _nextSleep;
473 if (_nextSleep > 256) {
474 _nextSleep = 256;
475 }
476 }
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") {
489 QByteArray gga = ggaString(_latitude, _longitude, _height);
490 _query->startRequest(_mountPoint, gga);
491 }
492 else {
493 _query->startRequest(_mountPoint, "");
494 }
495 if (_query->status() != bncNetQuery::running) {
496 return failure;
497 }
498 }
499
500 if (_rnx) {
501 _rnx->setReconnectFlag(true);
502 }
503
504 return success;
505}
506
507// RTCM scan output
508//////////////////////////////////////////////////////////////////////////////
509void bncGetThread::scanRTCM() {
510
511 bncSettings settings;
512 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
513
514 if ( _miscMount == _staID || _miscMount == "ALL" ) {
515
516 // RTCM message types
517 // ------------------
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 }
522
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 }
529
530 // RTCM Antenna Coordinates
531 // ------------------------
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 }
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 }
551 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
552 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
553 antT));
554 }
555 }
556 }
557
558 _decoder->_typeList.clear();
559 _decoder->_antType.clear();
560 _decoder->_antList.clear();
561}
562
Note: See TracBrowser for help on using the repository browser.