source: ntrip/trunk/BNC/src/bncgetthread.cpp@ 11182

Last change on this file since 11182 was 11179, checked in by stuerze, 6 days ago

structuered logging is added and bug is fixed in SP3 file writing

File size: 38.3 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://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 <iostream>
43#include <iomanip>
44#include <sstream>
45
46#include <QComboBox>
47#include <QDialog>
48#include <QFile>
49#include <QTextStream>
50#include <QMutex>
51#include <QPushButton>
52#include <QTableWidget>
53#include <QTime>
54
55#include "bncgetthread.h"
56#include "bnctabledlg.h"
57#include "bnccore.h"
58#include "bncutils.h"
59#include "bnctime.h"
60#include "bnczerodecoder.h"
61#include "bncnetqueryv0.h"
62#include "bncnetqueryv1.h"
63#include "bncnetqueryv2.h"
64#include "bncnetqueryrtp.h"
65#include "bncnetqueryudp.h"
66#include "bncnetqueryudp0.h"
67#include "bnccredentials.h"
68#include "bncnetquerys.h"
69#include "bncsettings.h"
70#include "latencychecker.h"
71#include "upload/bncrtnetdecoder.h"
72#include "RTCM/RTCM2Decoder.h"
73#include "RTCM3/RTCM3Decoder.h"
74#include "serial/qextserialport.h"
75
76using namespace std;
77
78// Constructor 1
79////////////////////////////////////////////////////////////////////////////
80bncGetThread::bncGetThread(bncRawFile* rawFile) {
81
82 _rawFile = rawFile;
83 _format = rawFile->format();
84 _staID = rawFile->staID();
85 _rawOutput = false;
86 _ntripVersion = "N";
87
88 initialize();
89}
90
91// Constructor 2
92////////////////////////////////////////////////////////////////////////////
93bncGetThread::bncGetThread(const QUrl& mountPoint, const QByteArray& format,
94 const QByteArray& latitude, const QByteArray& longitude,
95 const QByteArray& nmea, const QByteArray& ntripVersion) {
96 _rawFile = 0;
97 _mountPoint = mountPoint;
98 _requestUrl = mountPoint;
99 bncCredentials::complete(_requestUrl); // password from a credentials file
100 _staID = mountPoint.path().mid(1).toLatin1();
101 _format = format;
102 _latitude = latitude;
103 _longitude = longitude;
104 _nmea = nmea;
105 _ntripVersion = ntripVersion;
106
107 bncSettings settings;
108 if (!settings.value("rawOutFile").toString().isEmpty()) {
109 _rawOutput = true;
110 }
111 else {
112 _rawOutput = false;
113 }
114
115 _latencycheck = true; // in order to allow at least to check for reconnect
116
117 _NMEASampl = settings.value("serialNMEASampling").toInt();
118
119 initialize();
120 initDecoder();
121}
122
123// Initialization (common part of the constructor)
124////////////////////////////////////////////////////////////////////////////
125void bncGetThread::initialize() {
126
127 bncSettings settings;
128
129 setTerminationEnabled(true);
130
131 connect(this, SIGNAL(newMessage(QByteArray,bool)),
132 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
133 connect(this, SIGNAL(newLogRecord(t_logRecord,bool)),
134 BNC_CORE, SLOT(slotLogRecord(t_logRecord,bool)));
135 t_logRecord::registerStation(_staID);
136
137 _isToBeDeleted = false;
138 _query = 0;
139 _nextSleep = 0;
140 _miscMount = settings.value("miscMount").toString();
141 _decoder = 0;
142
143 // NMEA Port
144 // -----------
145 QListIterator<QString> iSta(settings.value("PPP/staTable").toStringList());
146 int nmeaPort = 0;
147 while (iSta.hasNext()) {
148 QStringList hlp = iSta.next().split(",");
149 if (hlp.size() < 10) {
150 continue;
151 }
152 QByteArray mp = hlp[0].toLatin1();
153 if (_staID == mp) {
154 nmeaPort = hlp[9].toInt();
155 }
156 }
157 if (nmeaPort != 0) {
158 _nmeaServer = new QTcpServer;
159 _nmeaServer->setProxy(QNetworkProxy::NoProxy);
160 if (!_nmeaServer->listen(QHostAddress::LocalHost, nmeaPort)) {
161 QString message = "bncCaster: Cannot listen on port "
162 + QByteArray::number(nmeaPort) + ": "
163 + _nmeaServer->errorString();
164 emit newMessage(message.toLatin1(), true);
165 }
166 else {
167 connect(_nmeaServer, SIGNAL(newConnection()), this, SLOT(slotNewNMEAConnection()));
168 connect(BNC_CORE, SIGNAL(newNMEAstr(QByteArray, QByteArray)), this, SLOT(slotNewNMEAstr(QByteArray, QByteArray)));
169 _nmeaSockets = new QList<QTcpSocket*>;
170 _nmeaPortsMap[_staID] = nmeaPort;
171 }
172 } else {
173 _nmeaServer = 0;
174 _nmeaSockets = 0;
175 }
176
177 // Serial Port
178 // -----------
179 _serialNMEA = NO_NMEA;
180 _serialOutFile = 0;
181 _serialPort = 0;
182 QString portString = settings.value("serialPortName").toString();
183
184 if (!_staID.isEmpty()
185 && settings.value("serialMountPoint").toString() == _staID) {
186 _serialPort = new QextSerialPort(portString);
187 _serialPort->setTimeout(0, 100);
188
189 // Baud Rate
190 // ---------
191 QString hlp = settings.value("serialBaudRate").toString();
192 if (hlp == "110") {
193 _serialPort->setBaudRate(BAUD110);
194 } else if (hlp == "300") {
195 _serialPort->setBaudRate(BAUD300);
196 } else if (hlp == "600") {
197 _serialPort->setBaudRate(BAUD600);
198 } else if (hlp == "1200") {
199 _serialPort->setBaudRate(BAUD1200);
200 } else if (hlp == "2400") {
201 _serialPort->setBaudRate(BAUD2400);
202 } else if (hlp == "4800") {
203 _serialPort->setBaudRate(BAUD4800);
204 } else if (hlp == "9600") {
205 _serialPort->setBaudRate(BAUD9600);
206 } else if (hlp == "19200") {
207 _serialPort->setBaudRate(BAUD19200);
208 } else if (hlp == "38400") {
209 _serialPort->setBaudRate(BAUD38400);
210 } else if (hlp == "57600") {
211 _serialPort->setBaudRate(BAUD57600);
212 } else if (hlp == "115200") {
213 _serialPort->setBaudRate(BAUD115200);
214 }
215
216 // Parity
217 // ------
218 hlp = settings.value("serialParity").toString();
219 if (hlp == "NONE") {
220 _serialPort->setParity(PAR_NONE);
221 } else if (hlp == "ODD") {
222 _serialPort->setParity(PAR_ODD);
223 } else if (hlp == "EVEN") {
224 _serialPort->setParity(PAR_EVEN);
225 } else if (hlp == "SPACE") {
226 _serialPort->setParity(PAR_SPACE);
227 }
228
229 // Data Bits
230 // ---------
231 hlp = settings.value("serialDataBits").toString();
232 if (hlp == "5") {
233 _serialPort->setDataBits(DATA_5);
234 } else if (hlp == "6") {
235 _serialPort->setDataBits(DATA_6);
236 } else if (hlp == "7") {
237 _serialPort->setDataBits(DATA_7);
238 } else if (hlp == "8") {
239 _serialPort->setDataBits(DATA_8);
240 }
241 // Stop Bits
242 // ---------
243 hlp = settings.value("serialStopBits").toString();
244 if (hlp == "1") {
245 _serialPort->setStopBits(STOP_1);
246 } else if (hlp == "2") {
247 _serialPort->setStopBits(STOP_2);
248 }
249
250 // Flow Control
251 // ------------
252 hlp = settings.value("serialFlowControl").toString();
253 if (hlp == "XONXOFF") {
254 _serialPort->setFlowControl(FLOW_XONXOFF);
255 } else if (hlp == "HARDWARE") {
256 _serialPort->setFlowControl(FLOW_HARDWARE);
257 } else {
258 _serialPort->setFlowControl(FLOW_OFF);
259 }
260
261 // Open Serial Port
262 // ----------------
263 _serialPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
264 msleep(100); //sleep 0.1 sec
265 if (!_serialPort->isOpen()) {
266 emit(newMessage(_staID + ": Cannot open serial port " + portString.toLatin1()
267 + ": " + _serialPort->errorString().toLatin1(), true));
268 delete _serialPort;
269 _serialPort = 0;
270 }
271 else {
272 emit(newMessage(_staID + ": Serial port " + portString.toLatin1()
273 + "is connected: " + _serialPort->errorString().toLatin1(), true));
274 connect(_serialPort, SIGNAL(readyRead()), this, SLOT(slotSerialReadyRead()));
275 }
276
277 // Automatic NMEA
278 // --------------
279 QString nmeaMode = settings.value("serialAutoNMEA").toString();
280 if (nmeaMode == "Auto") {
281 _serialNMEA = AUTO_NMEA;
282 QString fName = settings.value("serialFileNMEA").toString();
283 if (!fName.isEmpty()) {
284 _serialOutFile = new QFile(fName);
285 if (Qt::CheckState(settings.value("rnxAppend").toInt())
286 == Qt::Checked) {
287 _serialOutFile->open(QIODevice::WriteOnly | QIODevice::Append);
288 } else {
289 _serialOutFile->open(QIODevice::WriteOnly);
290 }
291 }
292 }
293 // Manual NMEA
294 // -----------
295 if ((nmeaMode == "Manual GPGGA") ||
296 (nmeaMode == "Manual GNGGA")) {
297 _serialNMEA = MANUAL_NMEA;
298 bncSettings settings;
299 QString hlp = settings.value("serialHeightNMEA").toString();
300 if (hlp.isEmpty()) {
301 hlp = "0.0";
302 }
303 QByteArray _serialHeightNMEA = hlp.toLatin1();
304 _manualNMEAString = ggaString(_latitude, _longitude, _serialHeightNMEA, nmeaMode);
305 }
306 }
307
308 if (!_staID.isEmpty() && _latencycheck) {
309 _latencyChecker = new latencyChecker(_staID);
310 _rtcmObs = false;
311 _rtcmSsrOrb = false;
312 _rtcmSsrClk = false;
313 _rtcmSsrOrbClk = false;
314 _rtcmSsrCbi = false;
315 _rtcmSsrPbi = false;
316 _rtcmSsrVtec = false;
317 _rtcmSsrUra = false;
318 _rtcmSsrHr = false;
319 _rtcmSsrIgs = false;
320 _ssrEpoch = 0;
321 } else {
322 _latencyChecker = 0;
323 }
324}
325
326// Instantiate the decoder
327//////////////////////////////////////////////////////////////////////////////
328t_irc bncGetThread::initDecoder() {
329
330 _decoder = 0;
331
332 if (_format.indexOf("RTCM_2") != -1 ||
333 _format.indexOf("RTCM2") != -1 ||
334 _format.indexOf("RTCM 2") != -1) {
335 emitFormat(": Get data in RTCM 2.x format", "RTCM2");
336 _decoder = new RTCM2Decoder(_staID.data());
337 } else if (_format.indexOf("RTCM_3") != -1 ||
338 _format.indexOf("RTCM3") != -1 ||
339 _format.indexOf("RTCM 3") != -1) {
340 emitFormat(": Get data in RTCM 3.x format", "RTCM3");
341 RTCM3Decoder* newDecoder = new RTCM3Decoder(_staID, _rawFile);
342 _decoder = newDecoder;
343 connect((RTCM3Decoder*) newDecoder, SIGNAL(newMessage(QByteArray,bool)),
344 this, SIGNAL(newMessage(QByteArray,bool)));
345 } else if (_format == "ZERO") {
346 emitFormat(": Forward data in original format", "ZERO");
347 _decoder = new bncZeroDecoder(_staID, false);
348 }
349 else if (_format == "ZERO2FILE") {
350 emitFormat(": Get data in original format and store it", "ZERO2FILE");
351 _decoder = new bncZeroDecoder(_staID, true);
352 }
353 else if (_format.indexOf("RTNET") != -1) {
354 emitFormat(": Get data in RTNet format", "RTNET");
355 _decoder = new bncRtnetDecoder();
356 } else {
357 emit(newMessage(_staID + ": Unknown data format " + _format + ". Please change the format entry to ZERO or ZERO2FILE to forward unchanged data.", true));
358 _isToBeDeleted = true;
359 return failure;
360 }
361
362 msleep(100); //sleep 0.1 sec
363
364 _decoder->initRinex(_staID, _requestUrl, _latitude, _longitude, _nmea,
365 _ntripVersion);
366
367 if (_rawFile) {
368 _decodersRaw[_staID] = _decoder;
369 }
370
371 return success;
372}
373
374// Current decoder in use
375////////////////////////////////////////////////////////////////////////////
376GPSDecoder* bncGetThread::decoder() {
377 if (!_rawFile) {
378 return _decoder;
379 } else {
380 if (_decodersRaw.contains(_staID) || initDecoder() == success) {
381 return _decodersRaw[_staID];
382 }
383 }
384 return 0;
385}
386
387// Destructor
388////////////////////////////////////////////////////////////////////////////
389bncGetThread::~bncGetThread() {
390 if (isRunning()) {
391 wait();
392 }
393 if (_query) {
394 _query->stop();
395 _query->deleteLater();
396 }
397 if (_rawFile) {
398 QMapIterator<QString, GPSDecoder*> it(_decodersRaw);
399 while (it.hasNext()) {
400 it.next();
401 delete it.value();
402 }
403 _decodersRaw.clear();
404 } else {
405 delete _decoder;
406 }
407 delete _rawFile;
408 delete _serialOutFile;
409 delete _serialPort;
410 delete _latencyChecker;
411 delete _nmeaServer;
412 delete _nmeaSockets;
413 emit getThreadFinished(_staID);
414}
415
416//
417////////////////////////////////////////////////////////////////////////////
418void bncGetThread::terminate() {
419 _isToBeDeleted = true;
420
421 if (_nmeaPortsMap.contains(_staID)) {
422 _nmeaPortsMap.remove(_staID);
423 }
424 delete _nmeaServer; _nmeaServer = nullptr;
425 delete _nmeaSockets; _nmeaSockets = nullptr;
426
427#ifdef BNC_DEBUG
428 if (BNC_CORE->mode() != t_bncCore::interactive) {
429 while (!isFinished()) {
430 wait();
431 }
432 delete this;
433 } else {
434 if (!isRunning()) {
435 delete this;
436 }
437 }
438#else
439 if (!isRunning()) {delete this;}
440#endif
441
442}
443
444// Run
445////////////////////////////////////////////////////////////////////////////
446void bncGetThread::run() {
447
448 while (true) {
449 try {
450 if (_isToBeDeleted) {
451 emit(newMessage(_staID + ": is to be deleted", true));
452 QThread::exit(4);
453 this->deleteLater();
454 return;
455 }
456
457 if (tryReconnect() != success) {
458 if (_latencyChecker) {
459 _latencyChecker->checkReconnect();
460 }
461 continue;
462 }
463
464 // Delete old observations
465 // -----------------------
466 if (_rawFile) {
467 QMapIterator<QString, GPSDecoder*> itDec(_decodersRaw);
468 while (itDec.hasNext()) {
469 itDec.next();
470 GPSDecoder* decoder = itDec.value();
471 decoder->_obsList.clear();
472 }
473 } else {
474 _decoder->_obsList.clear();
475 }
476
477 // Read Data
478 // ---------
479 QByteArray data;
480 if (_query) {
481 _query->waitForReadyRead(data);
482 } else if (_rawFile) {
483 data = _rawFile->readChunk();
484 _format = _rawFile->format();
485 _staID = _rawFile->staID();
486 t_logRecord::registerStation(_staID);
487
488 QCoreApplication::processEvents();
489
490 if (data.isEmpty() || BNC_CORE->sigintReceived) {
491 emit(newMessage("No more data or SIGINT/SIGTERM received", true));
492 // pass on data the decoders still hold back (last epoch)
493 QMapIterator<QString, GPSDecoder*> itFlush(_decodersRaw);
494 while (itFlush.hasNext()) {
495 itFlush.next();
496 itFlush.value()->flush();
497 }
498 BNC_CORE->stopPPP();
499 BNC_CORE->stopCombination();
500 BNC_CORE->stopSsrQc();
501 sleep(2);
502 ::exit(5);
503 }
504 }
505
506 qint64 nBytes = data.size();
507
508 // Timeout, reconnect
509 // ------------------
510 if (nBytes == 0) {
511 if (_latencyChecker) {
512 _latencyChecker->checkReconnect();
513 }
514 emit newLogRecord(t_logRecord(t_logRecord::warning, "stream", "stream_timeout", _staID,
515 _staID + ": Data timeout, reconnecting"), true);
516 msleep(10000); //sleep 10 sec, G. Weber
517 continue;
518 } else {
519 emit newBytes(_staID, nBytes);
520 emit newRawData(_staID, data);
521 }
522
523 // Output Data
524 // -----------
525 if (_rawOutput) {
526 BNC_CORE->writeRawData(data, _staID, _format);
527 }
528
529 if (_serialPort) {
530 slotSerialReadyRead();
531 _serialPort->write(data);
532 }
533
534 // Decode Data
535 // -----------
536 vector<string> errmsg;
537 if (!decoder()) {
538 _isToBeDeleted = true;
539 continue;
540 }
541
542 t_irc irc = decoder()->Decode(data.data(), data.size(), errmsg);
543
544 if (irc != success) {
545 continue;
546 }
547 // Perform various scans and checks
548 // --------------------------------
549 if (_latencyChecker) {
550 _latencyChecker->checkOutage(irc);
551 QListIterator<GPSDecoder::t_typeInfo> it(decoder()->_typeList);
552 _ssrEpoch = static_cast<int>(decoder()->corrGPSEpochTime());
553 if (_ssrEpoch != -1) {
554 if (_rtcmSsrOrb) {
555 _latencyChecker->checkCorrLatency(_ssrEpoch, 1057);
556 _rtcmSsrOrb = false;
557 }
558 if (_rtcmSsrClk) {
559 _latencyChecker->checkCorrLatency(_ssrEpoch, 1058);
560 _rtcmSsrClk = false;
561 }
562 if (_rtcmSsrOrbClk) {
563 _latencyChecker->checkCorrLatency(_ssrEpoch, 1060);
564 _rtcmSsrOrbClk = false;
565 }
566 if (_rtcmSsrCbi) {
567 _latencyChecker->checkCorrLatency(_ssrEpoch, 1059);
568 _rtcmSsrCbi = false;
569 }
570 if (_rtcmSsrPbi) {
571 _latencyChecker->checkCorrLatency(_ssrEpoch, 1265);
572 _rtcmSsrPbi = false;
573 }
574 if (_rtcmSsrVtec) {
575 _latencyChecker->checkCorrLatency(_ssrEpoch, 1264);
576 _rtcmSsrVtec = false;
577 }
578 if (_rtcmSsrUra) {
579 _latencyChecker->checkCorrLatency(_ssrEpoch, 1061);
580 _rtcmSsrUra = false;
581 }
582 if (_rtcmSsrHr) {
583 _latencyChecker->checkCorrLatency(_ssrEpoch, 1062);
584 _rtcmSsrHr = false;
585 }
586 if (_rtcmSsrIgs) {
587 _latencyChecker->checkCorrLatency(_ssrEpoch, 4076);
588 _rtcmSsrIgs = false;
589 }
590 }
591 while (it.hasNext()) {
592 int rtcmType = it.next()._type;
593 if ((rtcmType >= 1001 && rtcmType <= 1004) || // legacy RTCM OBS
594 (rtcmType >= 1009 && rtcmType <= 1012) || // legacy RTCM OBS
595 (rtcmType >= 1070 && rtcmType <= 1137)) { // MSM RTCM OBS
596 _rtcmObs = true;
597 } else if ((rtcmType >= 1057 && rtcmType <= 1068) ||
598 (rtcmType >= 1240 && rtcmType <= 1270) ||
599 (rtcmType == 4076)) {
600 switch (rtcmType) {
601 case 1057: case 1063: case 1240: case 1246: case 1252: case 1258:
602 _rtcmSsrOrb = true;
603 break;
604 case 1058: case 1064: case 1241: case 1247: case 1253: case 1259:
605 _rtcmSsrClk = true;
606 break;
607 case 1060: case 1066: case 1243: case 1249: case 1255: case 1261:
608 _rtcmSsrOrbClk = true;
609 break;
610 case 1059: case 1065: case 1242: case 1248: case 1254: case 1260:
611 _rtcmSsrCbi = true;
612 break;
613 case 1265: case 1266: case 1267: case 1268: case 1269: case 1270:
614 _rtcmSsrPbi = true;
615 break;
616 case 1264:
617 _rtcmSsrVtec = true;
618 break;
619 case 1061: case 1067: case 1244: case 1250: case 1256: case 1262:
620 _rtcmSsrUra = true;
621 break;
622 case 1062: case 1068: case 1245: case 1251: case 1257: case 1263:
623 _rtcmSsrHr = true;
624 break;
625 case 4076:
626 _rtcmSsrIgs = true;
627 break;
628 }
629 }
630 }
631 if (_rtcmObs) {
632 _latencyChecker->checkObsLatency(decoder()->_obsList);
633 }
634 emit newLatency(_staID, _latencyChecker->currentLatency());
635 }
636 miscScanRTCM();
637
638 // Loop over all observations (observations output)
639 // ------------------------------------------------
640 QListIterator<t_satObs> it(decoder()->_obsList);
641
642 QList<t_satObs> obsListHlp;
643
644 while (it.hasNext()) {
645 const t_satObs& obs = it.next();
646
647 // Check observation epoch
648 // -----------------------
649 if (!_rawFile) {
650 bool wrongObservationEpoch = checkForWrongObsEpoch(obs._time);
651 if (wrongObservationEpoch) {
652 QString prn(obs._prn.toString().c_str());
653 QString type = QString("%1").arg(obs._type);
654 emit(newMessage(_staID + " (" + prn.toLatin1() + ")" + ": Wrong observation epoch(s)" + "( MT: " + type.toLatin1() + ")", false));
655 continue;
656 }
657 }
658
659 // Check observations coming twice (e.g. KOUR0 Problem)
660 // ----------------------------------------------------
661 if (!_rawFile) {
662 QString prn(obs._prn.toString().c_str());
663 bncTime obsTime = obs._time;
664 QMap<QString, bncTime>::const_iterator it = _prnLastEpo.find(prn);
665 if (it != _prnLastEpo.end()) {
666 bncTime oldTime = it.value();
667 if (obsTime < oldTime) {
668 emit(newMessage(_staID + ": old observation " + prn.toLatin1(), false));
669 continue;
670 } else if (obsTime == oldTime) {
671 emit(newMessage(_staID + ": observation coming more than once " + prn.toLatin1(), false));
672 continue;
673 }
674 }
675 _prnLastEpo[prn] = obsTime;
676 }
677
678 decoder()->dumpRinexEpoch(obs, _format);
679
680 // Save observations
681 // -----------------
682 obsListHlp.append(obs);
683 }
684
685 // Emit signal
686 // -----------
687 if (!_isToBeDeleted && obsListHlp.size() > 0) {
688 emit newObs(_staID, obsListHlp);
689 }
690
691 }
692 catch (Exception& exc) {
693 emit(newMessage(_staID + " " + exc.what(), true));
694 _isToBeDeleted = true;
695 }
696 catch (std::exception& exc) {
697 emit(newMessage(_staID + " " + exc.what(), true));
698 _isToBeDeleted = true;
699 }
700 catch (const string& error) {
701 emit(newMessage(_staID + " ERROR: " + error.c_str(), true));
702 _isToBeDeleted = true;
703 }
704 catch (const char* error) {
705 emit(newMessage(_staID + " ERROR: " + error, true));
706 _isToBeDeleted = true;
707 }
708 catch (QString error) {
709 emit(newMessage(_staID + " ERROR: " + error.toStdString().c_str(), true));
710 _isToBeDeleted = true;
711 }
712 catch (...) {
713 emit(newMessage(_staID + " bncGetThread: unknown exception", true));
714 _isToBeDeleted = true;
715 }
716 }
717}
718
719// Try Re-Connect
720////////////////////////////////////////////////////////////////////////////
721t_irc bncGetThread::tryReconnect() {
722
723 // Easy Return
724 // -----------
725 if (_query && _query->status() == bncNetQuery::running) {
726 _nextSleep = 0;
727 if (_rawFile) {
728 QMapIterator<QString, GPSDecoder*> itDec(_decodersRaw);
729 while (itDec.hasNext()) {
730 itDec.next();
731 GPSDecoder* decoder = itDec.value();
732 decoder->setRinexReconnectFlag(false);
733 }
734 } else {
735 _decoder->setRinexReconnectFlag(false);
736 }
737 return success;
738 }
739
740 // Start a new query
741 // -----------------
742 if (!_rawFile) {
743
744 sleep(_nextSleep);
745 if (_nextSleep == 0) {
746 _nextSleep = 1;
747 } else {
748 _nextSleep = 2 * _nextSleep;
749 if (_nextSleep > 256) {
750 _nextSleep = 256;
751 }
752#ifdef MLS_SOFTWARE
753 if (_nextSleep > 4) {
754 _nextSleep = 4;
755 }
756#endif
757 }
758 delete _query;
759 if (_ntripVersion == "U") {
760 _query = new bncNetQueryUdp();
761 } else if (_ntripVersion == "R") {
762 _query = new bncNetQueryRtp();
763 } else if (_ntripVersion == "S") {
764 _query = new bncNetQueryS();
765 } else if (_ntripVersion == "N") {
766 _query = new bncNetQueryV0();
767 } else if (_ntripVersion == "UN") {
768 _query = new bncNetQueryUdp0();
769 } else if (_ntripVersion == "2") {
770 _query = new bncNetQueryV2(false);
771 } else if (_ntripVersion == "2s") {
772 _query = new bncNetQueryV2(true);
773 } else {
774 _query = new bncNetQueryV1();
775 }
776 if (_nmea == "yes") {
777 if (_serialNMEA == MANUAL_NMEA) {
778 _query->startRequest(_requestUrl, _manualNMEAString);
779 _lastNMEA = QDateTime::currentDateTime();
780 } else if (_serialNMEA == AUTO_NMEA) {
781 if (_serialPort) {
782 int nb = _serialPort->bytesAvailable();
783 if (nb > 0) {
784 QByteArray data = _serialPort->read(nb);
785 int i1 = data.indexOf("$GPGGA");
786 if (i1 == -1) {
787 i1 = data.indexOf("$GNGGA");
788 }
789 if (i1 != -1) {
790 int i2 = data.indexOf("*", i1);
791 if (i2 != -1 && data.size() > i2 + 1) {
792 QByteArray gga = data.mid(i1, i2 - i1 + 3);
793 _query->startRequest(_requestUrl, gga);
794 _lastNMEA = QDateTime::currentDateTime();
795 }
796 }
797 }
798 }
799 }
800 } else {
801 _query->startRequest(_requestUrl, "");
802 }
803
804 if (_query->status() != bncNetQuery::running) {
805 return failure;
806 }
807 }
808
809 if (_rawFile) {
810 QMapIterator<QString, GPSDecoder*> itDec(_decodersRaw);
811 while (itDec.hasNext()) {
812 itDec.next();
813 GPSDecoder* decoder = itDec.value();
814 decoder->setRinexReconnectFlag(false);
815 }
816 } else {
817 _decoder->setRinexReconnectFlag(false);
818 }
819
820 return success;
821}
822
823// RTCM scan output
824//////////////////////////////////////////////////////////////////////////////
825void bncGetThread::miscScanRTCM() {
826
827 if (!decoder()) {
828 return;
829 }
830
831 bncSettings settings;
832 if (Qt::CheckState(settings.value("miscScanRTCM").toInt()) == Qt::Checked) {
833
834 if (_miscMount == _staID || _miscMount == "ALL") {
835 // RTCM message types
836 // ------------------
837 for (int ii = 0; ii < decoder()->_typeList.size(); ii++) {
838 QString type = QString("%1 ").arg(decoder()->_typeList[ii]._type);
839 QString size = (decoder()->_typeList[ii]._size) ? QString("(size %1)").arg(decoder()->_typeList[ii]._size) : "";
840 emit(newMessage(_staID + ": Received message type " + type.toLatin1() + size.toLatin1(), true));
841 }
842
843 // Check Observation Types
844 // -----------------------
845 for (int ii = 0; ii < decoder()->_obsList.size(); ii++) {
846 t_satObs& obs = decoder()->_obsList[ii];
847 QVector<QString>& rnxTypes = _rnxTypes[obs._prn.system()];
848 bool allFound = true;
849 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
850 if (obs._obs[iFrq]->_codeValid) {
851 QString rnxStr('C');
852 rnxStr.append(obs._obs[iFrq]->_rnxType2ch.c_str());
853 if (_format.indexOf("RTCM_2") != -1
854 || _format.indexOf("RTCM2") != -1
855 || _format.indexOf("RTCM 2") != -1) {
856 rnxStr = t_rnxObsFile::type3to2(obs._prn.system(), rnxStr);
857 }
858 if (rnxTypes.indexOf(rnxStr) == -1) {
859 rnxTypes.push_back(rnxStr);
860 allFound = false;
861 }
862 }
863 if (obs._obs[iFrq]->_phaseValid) {
864 QString rnxStr('L');
865 rnxStr.append(obs._obs[iFrq]->_rnxType2ch.c_str());
866 if (_format.indexOf("RTCM_2") != -1
867 || _format.indexOf("RTCM2") != -1
868 || _format.indexOf("RTCM 2") != -1) {
869 rnxStr = t_rnxObsFile::type3to2(obs._prn.system(), rnxStr);
870 }
871 if (rnxTypes.indexOf(rnxStr) == -1) {
872 rnxTypes.push_back(rnxStr);
873 allFound = false;
874 }
875 }
876 if (obs._obs[iFrq]->_dopplerValid) {
877 QString rnxStr('D');
878 rnxStr.append(obs._obs[iFrq]->_rnxType2ch.c_str());
879 if (_format.indexOf("RTCM_2") != -1
880 || _format.indexOf("RTCM2") != -1
881 || _format.indexOf("RTCM 2") != -1) {
882 rnxStr = t_rnxObsFile::type3to2(obs._prn.system(), rnxStr);
883 }
884 if (rnxTypes.indexOf(rnxStr) == -1) {
885 rnxTypes.push_back(rnxStr);
886 allFound = false;
887 }
888 }
889 if (obs._obs[iFrq]->_snrValid) {
890 QString rnxStr('S');
891 rnxStr.append(obs._obs[iFrq]->_rnxType2ch.c_str());
892 if (_format.indexOf("RTCM_2") != -1
893 || _format.indexOf("RTCM2") != -1
894 || _format.indexOf("RTCM 2") != -1) {
895 rnxStr = t_rnxObsFile::type3to2(obs._prn.system(), rnxStr);
896 }
897 if (rnxTypes.indexOf(rnxStr) == -1) {
898 rnxTypes.push_back(rnxStr);
899 allFound = false;
900 }
901 }
902 }
903 if (!allFound) {
904 QString msg;
905 QTextStream str(&msg);
906 str << obs._prn.system() << QString(" %1 ").arg(rnxTypes.size());
907 for (int iType = 0; iType < rnxTypes.size(); iType++) {
908 str << " " << rnxTypes[iType];
909 }
910 emit(newMessage(_staID + ": Observation Types: " + msg.toLatin1(), true));
911 }
912 }
913
914 // RTCMv3 antenna descriptor
915 // -------------------------
916 for (int ii = 0; ii < decoder()->_antType.size(); ii++) {
917 QString ant1 = QString(": Antenna Descriptor: %1 ").arg(decoder()->_antType[ii]._descriptor);
918 emit(newMessage(_staID + ant1.toLatin1(), true));
919 if (strlen(decoder()->_antType[ii]._serialnumber)) {
920 QString ant2 = QString(": Antenna Serial Number: %1 ").arg(decoder()->_antType[ii]._serialnumber);
921 emit(newMessage(_staID + ant2.toLatin1(), true));
922 }
923 }
924
925 // RTCM Antenna Coordinates
926 // ------------------------
927 for (int ii = 0; ii < decoder()->_antList.size(); ii++) {
928 QByteArray antT;
929 if (decoder()->_antList[ii]._type == GPSDecoder::t_antRefPoint::ARP) {
930 antT = "ARP";
931 } else if (decoder()->_antList[ii]._type == GPSDecoder::t_antRefPoint::APC) {
932 antT = "APC";
933 }
934 QByteArray ant1, ant2, ant3;
935 ant1 = QString("%1 ").arg(decoder()->_antList[ii]._xx, 0, 'f', 4).toLatin1();
936 ant2 = QString("%1 ").arg(decoder()->_antList[ii]._yy, 0, 'f', 4).toLatin1();
937 ant3 = QString("%1 ").arg(decoder()->_antList[ii]._zz, 0, 'f', 4).toLatin1();
938 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
939 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
940 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
941 double hh = 0.0;
942 if (decoder()->_antList[ii]._height_f) {
943 hh = decoder()->_antList[ii]._height;
944 QByteArray ant4 = QString("%1 ").arg(hh, 0, 'f', 4).toLatin1();
945 emit(newMessage(
946 _staID + ": Antenna height above marker " + ant4 + "m", true));
947 }
948 emit(newAntCrd(_staID, decoder()->_antList[ii]._xx,
949 decoder()->_antList[ii]._yy, decoder()->_antList[ii]._zz, hh, antT));
950 }
951
952 // RTCMv3 receiver descriptor
953 // --------------------------
954 for (int ii = 0; ii < decoder()->_recType.size(); ii++) {
955 QString rec1 = QString(": Receiver Descriptor: %1 ").arg(decoder()->_recType[ii]._descriptor);
956 QString rec2 = QString(": Receiver Firmware Version: %1 ").arg(decoder()->_recType[ii]._firmware);
957 QString rec3 = QString(": Receiver Serial Number: %1 ").arg(decoder()->_recType[ii]._serialnumber);
958 emit(newMessage(_staID + rec1.toLatin1(), true));
959 emit(newMessage(_staID + rec2.toLatin1(), true));
960 emit(newMessage(_staID + rec3.toLatin1(), true));
961 }
962
963 // RTCM GLONASS slots
964 // ------------------
965 if (decoder()->_gloFrq.size()) {
966 bool allFound = true;
967 QString slot = decoder()->_gloFrq;
968 slot.replace(" ", " ").replace(" ", ":");
969 if (_gloSlots.indexOf(slot) == -1) {
970 _gloSlots.append(slot);
971 allFound = false;
972 }
973 if (!allFound) {
974 _gloSlots.sort();
975 emit(newMessage(
976 _staID + ": GLONASS Slot:Freq " + _gloSlots.join(" ").toLatin1(),
977 true));
978 }
979 }
980
981 // RTCM GLONASS Code-Phase biases (MT 1230)
982 // ----------------------------------------
983 if (decoder()->_gloBiasInfo.changed()) {
984 QString gloCodePhaseBiases = decoder()->_gloBiasInfo.toString();
985 emit(newMessage(_staID + gloCodePhaseBiases.toLatin1(), true));
986 decoder()->_gloBiasInfo.setChanged(false);
987 }
988
989 // Service CRS / RTCM CRS
990 // ------------------------
991 if (fmod(decoder()->corrGPSEpochTime(), 60.0) == 0.0) {
992 // Service CRS
993 for (int ii = 0; ii < decoder()->_serviceCrs.size(); ii++) {
994 QString servicecrsname = QString(": Service CRS Name: %1 ").arg(decoder()->_serviceCrs[ii]._name);
995 QString coordinateEpoch = QString(": Service CRS Coordinate Epoch: %1 ").arg(decoder()->_serviceCrs[ii]._coordinateEpoch);
996 //QString ce = QString(": CE: %1 ").arg(decoder()->_serviceCrs[ii]._CE);
997 emit(newMessage(_staID + servicecrsname.toLatin1(), true));
998 emit(newMessage(_staID + coordinateEpoch.toLatin1(), true));
999 //emit(newMessage(_staID + ce.toLatin1(), true));
1000 }
1001 // RTCM CRS
1002 for (int ii = 0; ii < decoder()->_rtcmCrs.size(); ii++) {
1003 QString rtcmcrsname = QString(": RTCM CRS Name: %1 ").arg(decoder()->_rtcmCrs[ii]._name);
1004 QString anchor = QString(": RTCM CRS Anchor: %1 ").arg(decoder()->_rtcmCrs[ii]._anchor);
1005 QString platenumber = QString(": RTCM CRS Plate Number: %1 ").arg(decoder()->_rtcmCrs[ii]._plateNumber);
1006 emit(newMessage(_staID + rtcmcrsname.toLatin1(), true));
1007 emit(newMessage(_staID + anchor.toLatin1(), true));
1008 emit(newMessage(_staID + platenumber.toLatin1(), true));
1009 for (int i = 0; i<decoder()->_rtcmCrs[ii]._databaseLinks.size(); i++) {
1010 QString dblink = QString(": Database Link: %1 ").arg(decoder()->_rtcmCrs[ii]._databaseLinks[i]);
1011 emit(newMessage(_staID + dblink.toLatin1(), true));
1012 }
1013 }
1014
1015 // Helmert Parameters
1016 //-------------------
1017 for (int ii = 0; ii < decoder()->_helmertPar.size(); ii++) {
1018 t_helmertPar& helmertPar = decoder()->_helmertPar[ii];
1019 bncTime t; t.setmjd(0, helmertPar._mjd); QString dateStr = QString::fromStdString(t.datestr());
1020 QString sourcename = QString(": MT1301 Source Name: %1 ").arg(helmertPar._sourceName);
1021 QString targetname = QString(": MT1301 Target Name: %1 ").arg(helmertPar._targetName);
1022 QString sysidentnum = QString(": MT1301 Sys Ident Num: %1 ").arg(helmertPar._sysIdentNum);
1023 QString trafomessageind = QString(": MT1301 Trafo Ident Num: %1 ").arg(helmertPar.IndtoString());
1024 QString epoch = QString(": MT1301 t0: MJD %1 (%2) ").arg(helmertPar._mjd).arg(dateStr);
1025 QString partrans = QString(": MT1301 Helmert Par Trans: dx = %1, dy = %2, dz = %3, dxr = %4, dyr = %5, dzr = %6")
1026 .arg(helmertPar._dx).arg(helmertPar._dy).arg(helmertPar._dz)
1027 .arg(helmertPar._dxr).arg(helmertPar._dyr).arg(helmertPar._dzr);
1028 QString parrot = QString(": MT1301 Helmert Par Rot: ox = %1, oy = %2, oz = %3, oxr = %4, oyr = %5, ozr = %6")
1029 .arg(helmertPar._ox).arg(helmertPar._oy).arg(helmertPar._oz)
1030 .arg(helmertPar._oxr).arg(helmertPar._oyr).arg(helmertPar._ozr);
1031 QString parscale = QString(": MT1301 Helmert Par Scale: sc = %1, scr = %2").arg(helmertPar._sc).arg(helmertPar._scr);
1032 emit(newMessage(_staID + sourcename.toLatin1(), true));
1033 emit(newMessage(_staID + targetname.toLatin1(), true));
1034 emit(newMessage(_staID + sysidentnum.toLatin1(), true));
1035 emit(newMessage(_staID + trafomessageind.toLatin1(), true));
1036 emit(newMessage(_staID + epoch.toLatin1(), true));
1037 emit(newMessage(_staID + partrans.toLatin1(), true));
1038 emit(newMessage(_staID + parrot.toLatin1(), true));
1039 emit(newMessage(_staID + parscale.toLatin1(), true));
1040 }
1041 }
1042 }
1043 }
1044
1045#ifdef MLS_SOFTWARE
1046 for (int ii=0; ii <decoder()->_antList.size(); ii++) {
1047 QByteArray antT;
1048 if (decoder()->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
1049 antT = "ARP";
1050 }
1051 else if (decoder()->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
1052 antT = "APC";
1053 }
1054 double hh = 0.0;
1055 if (decoder()->_antList[ii].height_f) {
1056 hh = decoder()->_antList[ii].height;
1057 }
1058 emit(newAntCrd(_staID, decoder()->_antList[ii].xx,
1059 decoder()->_antList[ii].yy, decoder()->_antList[ii].zz,
1060 hh, antT));
1061 }
1062
1063 for (int ii = 0; ii <decoder()->_typeList.size(); ii++) {
1064 emit(newRTCMMessage(_staID, decoder()->_typeList[ii]));
1065 }
1066#endif
1067
1068 decoder()->_gloFrq.clear();
1069 // decoder()->_gloBiases.clear();
1070 decoder()->_typeList.clear();
1071 decoder()->_antType.clear();
1072 decoder()->_recType.clear();
1073 decoder()->_antList.clear();
1074}
1075
1076// Handle Data from Serial Port
1077////////////////////////////////////////////////////////////////////////////
1078void bncGetThread::slotSerialReadyRead() {
1079
1080 if (_serialPort) {
1081
1082 if (_nmea == "yes" && _serialNMEA == MANUAL_NMEA) {
1083 if (_NMEASampl) {
1084 int dt = _lastNMEA.secsTo(QDateTime::currentDateTime());
1085 if (dt && (fmod(double(dt), double(_NMEASampl)) == 0.0)) {
1086 _query->sendNMEA(_manualNMEAString);
1087 _lastNMEA = QDateTime::currentDateTime();
1088 }
1089 }
1090 }
1091
1092 int nb = _serialPort->bytesAvailable();
1093 if (nb > 0) {
1094 QByteArray data = _serialPort->read(nb);
1095
1096 if (_nmea == "yes" && _serialNMEA == AUTO_NMEA) {
1097 int i1 = data.indexOf("$GPGGA");
1098 if (i1 == -1) {
1099 i1 = data.indexOf("$GNGGA");
1100 }
1101 if (i1 != -1) {
1102 int i2 = data.indexOf("*", i1);
1103 if (i2 != -1 && data.size() > i2 + 1) {
1104 QByteArray gga = data.mid(i1, i2 - i1 + 3);
1105 if (_NMEASampl) {
1106 int dt = _lastNMEA.secsTo(QDateTime::currentDateTime());
1107 if (dt && (fmod(double(dt), double(_NMEASampl)) == 0.0)) {
1108 _query->sendNMEA(gga);
1109 _lastNMEA = QDateTime::currentDateTime();
1110 }
1111 }
1112 }
1113 }
1114 }
1115
1116 if (_serialOutFile) {
1117 _serialOutFile->write(data);
1118 _serialOutFile->flush();
1119 }
1120 }
1121 }
1122}
1123
1124void bncGetThread::slotNewNMEAConnection() {
1125 _nmeaSockets->push_back(_nmeaServer->nextPendingConnection());
1126 emit(newMessage(
1127 QString("New PPP client on port: # %1").arg(_nmeaSockets->size()).toLatin1(),
1128 true));
1129}
1130
1131//
1132////////////////////////////////////////////////////////////////////////////
1133void bncGetThread::slotNewNMEAstr(QByteArray staID, QByteArray str) {
1134 if (_nmeaPortsMap.contains(staID)) {
1135 int nmeaPort = _nmeaPortsMap.value(staID);
1136 QMutableListIterator<QTcpSocket*> is(*_nmeaSockets);
1137 while (is.hasNext()) {
1138 QTcpSocket* sock = is.next();
1139 if (sock->localPort() == nmeaPort) {
1140 if (sock->state() == QAbstractSocket::ConnectedState) {
1141 sock->write(str);
1142 } else if (sock->state() != QAbstractSocket::ConnectingState) {
1143 delete sock;
1144 is.remove();
1145 }
1146 }
1147 }
1148 }
1149}
1150
1151// Message on the data format of the stream
1152////////////////////////////////////////////////////////////////////////////
1153void bncGetThread::emitFormat(const char* text, const char* decoder) {
1154 t_logRecord rec(t_logRecord::info, "stream", "stream_format", _staID, _staID + text);
1155 rec.add("decoder", QByteArray(decoder))
1156 .add("format", _format);
1157 emit newLogRecord(rec, true);
1158}
Note: See TracBrowser for help on using the repository browser.