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

Last change on this file since 10936 was 10936, checked in by stuerze, 5 weeks ago

minor changes

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