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

Last change on this file since 1607 was 1607, checked in by mervart, 15 years ago

* empty log message *

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