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

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

* empty log message *

File size: 15.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 _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 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
357 continue;
358 }
359 else {
360 emit newBytes(_staID, nBytes);
361 }
362
363 // Output Data
364 // -----------
365 if (_rawOutFile) {
366 _rawOutFile->write(data);
367 _rawOutFile->flush();
368 }
369 if (_serialPort) {
370 _serialPort->write(data);
371 }
372
373 // Decode Data
374 // -----------
375 vector<string> errmsg;
376 t_irc irc = _decoder->Decode(data.data(), data.size(), errmsg);
377
378 // Perform various scans and checks
379 // --------------------------------
380 _latencyChecker->checkOutage(irc == success);
381 _latencyChecker->checkObsLatency(_decoder->_obsList);
382 _latencyChecker->checkCorrLatency(_decoder->corrGPSEpochTime());
383
384 scanRTCM();
385
386 // Loop over all observations (observations output)
387 // ------------------------------------------------
388 QListIterator<p_obs> it(_decoder->_obsList);
389 while (it.hasNext()) {
390 p_obs obs = it.next();
391
392 // Check observation epoch
393 // -----------------------
394 if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
395 int week;
396 double sec;
397 currentGPSWeeks(week, sec);
398 const double secPerWeek = 7.0 * 24.0 * 3600.0;
399
400 if (week < obs->_o.GPSWeek) {
401 week += 1;
402 sec -= secPerWeek;
403 }
404 if (week > obs->_o.GPSWeek) {
405 week -= 1;
406 sec += secPerWeek;
407 }
408 double dt = fabs(sec - obs->_o.GPSWeeks);
409 const double maxDt = 600.0;
410 if (week != obs->_o.GPSWeek || dt > maxDt) {
411 emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
412 delete obs;
413 continue;
414 }
415 }
416
417 // RINEX Output
418 // ------------
419 if (_rnx) {
420 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
421 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
422 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
423 _rnx->deepCopy(obs);
424 }
425 _rnx->dumpEpoch(newTime);
426 }
427
428 // Emit new observation signal
429 // ---------------------------
430 bool firstObs = (obs == _decoder->_obsList.first());
431 obs->_status = t_obs::posted;
432 emit newObs(_staID, firstObs, obs);
433 }
434 _decoder->_obsList.clear();
435 }
436 catch (...) {
437 emit(newMessage(_staID + "bncGetThread exception", true));
438 _isToBeDeleted = true;
439 }
440 }
441}
442
443// Try Re-Connect
444////////////////////////////////////////////////////////////////////////////
445t_irc bncGetThread::tryReconnect() {
446
447 // Easy Return
448 // -----------
449 if (_query && _query->status() == bncNetQuery::running) {
450 _nextSleep = 0;
451 return success;
452 }
453
454 // Start a new query
455 // -----------------
456 if (!_rawInpFile) {
457
458 sleep(_nextSleep);
459 if (_nextSleep == 0) {
460 _nextSleep = 1;
461 }
462 else {
463 _nextSleep = 2 * _nextSleep;
464 if (_nextSleep > 256) {
465 _nextSleep = 256;
466 }
467 }
468
469 delete _query;
470 if (_ntripVersion == "R") {
471 _query = new bncNetQueryRtp();
472 }
473 else if (_ntripVersion == "2") {
474 _query = new bncNetQueryV2();
475 }
476 else {
477 _query = new bncNetQueryV1();
478 }
479 if (_nmea == "yes") {
480 QByteArray gga = ggaString(_latitude, _longitude);
481 _query->startRequest(_mountPoint, gga);
482 }
483 else {
484 _query->startRequest(_mountPoint, "");
485 }
486 if (_query->status() != bncNetQuery::running) {
487 return failure;
488 }
489 }
490
491 if (_rnx) {
492 _rnx->setReconnectFlag(true);
493 }
494
495 return success;
496}
497
498// RTCM scan output
499//////////////////////////////////////////////////////////////////////////////
500void bncGetThread::scanRTCM() {
501
502 bncSettings settings;
503 if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked ) {
504
505 if ( _miscMount == _staID || _miscMount == "ALL" ) {
506
507 // RTCMv3 message types
508 // --------------------
509 for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
510 QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
511 emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
512 }
513
514 // RTCMv3 antenna descriptor
515 // -------------------------
516 for (int ii=0;ii<_decoder->_antType.size();ii++) {
517 QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
518 emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
519 }
520
521 // Antenna Coordinates
522 // -------------------
523 for (int ii=0; ii <_decoder->_antList.size(); ii++) {
524 QByteArray antT;
525 if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
526 antT = "ARP";
527 }
528 else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
529 antT = "APC";
530 }
531 emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
532 _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
533 antT));
534 }
535 }
536 }
537
538 _decoder->_typeList.clear();
539 _decoder->_antType.clear();
540 _decoder->_antList.clear();
541}
542
Note: See TracBrowser for help on using the repository browser.