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

Last change on this file since 1559 was 1559, checked in by mervart, 15 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 if (!isRunning()) {
306 delete this;
307 }
308}
309
310// Run
311////////////////////////////////////////////////////////////////////////////
312void bncGetThread::run() {
313
314 while (true) {
315 try {
316 if (_isToBeDeleted) {
317 QThread::exit(0);
318 this->deleteLater();
319 return;
320 }
321
322 if (tryReconnect() != success) {
323 continue;
324 }
325
326 // Delete old observations
327 // -----------------------
328 QListIterator<p_obs> itOld(_decoder->_obsList);
329 while (itOld.hasNext()) {
330 delete itOld.next();
331 }
332 _decoder->_obsList.clear();
333
334 // Read Data
335 // ---------
336 QByteArray data;
337 if (_query) {
338 _query->waitForReadyRead(data);
339 }
340 else if (_rawInpFile) {
341 const qint64 maxBytes = 1024;
342 data = _rawInpFile->read(maxBytes);
343 if (data.isEmpty()) {
344 cout << "no more data" << endl;
345 ::exit(0);
346 }
347 }
348 qint64 nBytes = data.size();
349
350 // Timeout, reconnect
351 // ------------------
352 if (nBytes == 0) {
353 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
354 continue;
355 }
356 else {
357 emit newBytes(_staID, nBytes);
358 }
359
360 // Output Data
361 // -----------
362 if (_rawOutFile) {
363 _rawOutFile->write(data);
364 _rawOutFile->flush();
365 }
366 if (_serialPort) {
367 _serialPort->write(data);
368 }
369
370 // Decode Data
371 // -----------
372 vector<string> errmsg;
373 _decoder->Decode(data.data(), data.size(), errmsg);
374
375 // Perform various scans and checks
376 // --------------------------------
377 scanRTCM();
378 _latencyChecker->check(_decoder);
379
380 // Loop over all observations (observations output)
381 // ------------------------------------------------
382 QListIterator<p_obs> it(_decoder->_obsList);
383 while (it.hasNext()) {
384 p_obs obs = it.next();
385
386 // Check observation epoch
387 // -----------------------
388 if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
389 int week;
390 double sec;
391 currentGPSWeeks(week, sec);
392 const double secPerWeek = 7.0 * 24.0 * 3600.0;
393
394 if (week < obs->_o.GPSWeek) {
395 week += 1;
396 sec -= secPerWeek;
397 }
398 if (week > obs->_o.GPSWeek) {
399 week -= 1;
400 sec += secPerWeek;
401 }
402 double dt = fabs(sec - obs->_o.GPSWeeks);
403 const double maxDt = 600.0;
404 if (week != obs->_o.GPSWeek || dt > maxDt) {
405 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
406 delete obs;
407 continue;
408 }
409 }
410
411 // RINEX Output
412 // ------------
413 if (_rnx) {
414 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
415 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
416 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
417 _rnx->deepCopy(obs);
418 }
419 _rnx->dumpEpoch(newTime);
420 }
421
422 // Emit new observation signal
423 // ---------------------------
424 bool firstObs = (obs == _decoder->_obsList.first());
425 obs->_status = t_obs::posted;
426 emit newObs(_staID, firstObs, obs);
427 }
428 _decoder->_obsList.clear();
429 }
430 catch (...) {
431 emit(newMessage(_staID + "bncGetThread exception", true));
432 _isToBeDeleted = true;
433 }
434 }
435}
436
437// Try Re-Connect
438////////////////////////////////////////////////////////////////////////////
439t_irc bncGetThread::tryReconnect() {
440
441 // Easy Return
442 // -----------
443 if (_query && _query->status() == bncNetQuery::running) {
444 _nextSleep = 0;
445 return success;
446 }
447
448 // Start a new query
449 // -----------------
450 if (!_rawInpFile) {
451
452 sleep(_nextSleep);
453 if (_nextSleep == 0) {
454 _nextSleep = 1;
455 }
456 else {
457 _nextSleep = 2 * _nextSleep;
458 if (_nextSleep > 256) {
459 _nextSleep = 256;
460 }
461 }
462
463 delete _query;
464 if (_ntripVersion == "R") {
465 _query = new bncNetQueryRtp();
466 }
467 else if (_ntripVersion == "2") {
468 _query = new bncNetQueryV2();
469 }
470 else {
471 _query = new bncNetQueryV1();
472 }
473 if (_nmea == "yes") {
474 QByteArray gga = ggaString(_latitude, _longitude);
475 _query->startRequest(_mountPoint, gga);
476 }
477 else {
478 _query->startRequest(_mountPoint, "");
479 }
480 if (_query->status() != bncNetQuery::running) {
481 return failure;
482 }
483 }
484
485 if (_rnx) {
486 _rnx->setReconnectFlag(true);
487 }
488
489 return success;
490}
491
492// RTCM scan output
493//////////////////////////////////////////////////////////////////////////////
494void bncGetThread::scanRTCM() {
495
496 bncSettings settings;
497 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked) {
498
499 // RTCMv3 message types
500 // --------------------
501 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
502 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
503 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
504 }
505
506 // RTCMv3 antenna descriptor
507 // -------------------------
508 for (int ii=0;ii<_decoder->_antType.size();ii++) {
509 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
510 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
511 }
512
513 // Antenna Coordinates
514 // -------------------
515 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
516 QByteArray antT;
517 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
518 antT = "ARP";
519 }
520 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
521 antT = "APC";
522 }
523 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
524 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
525 antT));
526 }
527 }
528
529 _decoder->_typeList.clear();
530 _decoder->_antType.clear();
531 _decoder->_antList.clear();
532}
533
Note: See TracBrowser for help on using the repository browser.