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

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

* empty log message *

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