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

Last change on this file since 1558 was 1558, checked in by mervart, 14 years ago

* empty log message *

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