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

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

* empty log message *

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