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

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

CHANGE: back to the class QextSerialPort because the QT5 internal class QSerialPort seems to have a bug

File size: 37.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 <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 emit getThreadFinished(_staID);
406}
407
408//
409////////////////////////////////////////////////////////////////////////////
410void bncGetThread::terminate() {
411 _isToBeDeleted = true;
412
413 if (_nmeaPortsMap.contains(_staID)) {
414 _nmeaPortsMap.remove(_staID);
415 }
416 if (_nmeaServer) {
417 delete _nmeaServer;
418 }
419 if (_nmeaSockets) {
420 delete _nmeaSockets;
421 }
422
423#ifdef BNC_DEBUG
424 if (BNC_CORE->mode() != t_bncCore::interactive) {
425 while (!isFinished()) {
426 wait();
427 }
428 delete this;
429 } else {
430 if (!isRunning()) {
431 delete this;
432 }
433 }
434#else
435 if (!isRunning()) {delete this;}
436#endif
437
438}
439
440// Run
441////////////////////////////////////////////////////////////////////////////
442void bncGetThread::run() {
443
444 while (true) {
445 try {
446 if (_isToBeDeleted) {
447 emit(newMessage(_staID + ": is to be deleted", true));
448 QThread::exit(4);
449 this->deleteLater();
450 return;
451 }
452
453 if (tryReconnect() != success) {
454 if (_latencyChecker) {
455 _latencyChecker->checkReconnect();
456 }
457 continue;
458 }
459
460 // Delete old observations
461 // -----------------------
462 if (_rawFile) {
463 QMapIterator<QString, GPSDecoder*> itDec(_decodersRaw);
464 while (itDec.hasNext()) {
465 itDec.next();
466 GPSDecoder* decoder = itDec.value();
467 decoder->_obsList.clear();
468 }
469 } else {
470 _decoder->_obsList.clear();
471 }
472
473 // Read Data
474 // ---------
475 QByteArray data;
476 if (_query) {
477 _query->waitForReadyRead(data);
478 } else if (_rawFile) {
479 data = _rawFile->readChunk();
480 _format = _rawFile->format();
481 _staID = _rawFile->staID();
482
483 QCoreApplication::processEvents();
484
485 if (data.isEmpty() || BNC_CORE->sigintReceived) {
486 emit(newMessage("No more data or SIGINT/SIGTERM received", true));
487 BNC_CORE->stopPPP();
488 BNC_CORE->stopCombination();
489 sleep(2);
490 ::exit(5);
491 }
492 }
493
494 qint64 nBytes = data.size();
495
496 // Timeout, reconnect
497 // ------------------
498 if (nBytes == 0) {
499 if (_latencyChecker) {
500 _latencyChecker->checkReconnect();
501 }
502 emit(newMessage(_staID + ": Data timeout, reconnecting", true));
503 msleep(10000); //sleep 10 sec, G. Weber
504 continue;
505 } else {
506 emit newBytes(_staID, nBytes);
507 emit newRawData(_staID, data);
508 }
509
510 // Output Data
511 // -----------
512 if (_rawOutput) {
513 BNC_CORE->writeRawData(data, _staID, _format);
514 }
515
516 if (_serialPort) {
517 slotSerialReadyRead();
518 _serialPort->write(data);
519 }
520
521 // Decode Data
522 // -----------
523 vector<string> errmsg;
524 if (!decoder()) {
525 _isToBeDeleted = true;
526 continue;
527 }
528
529 t_irc irc = decoder()->Decode(data.data(), data.size(), errmsg);
530
531 if (irc != success) {
532 continue;
533 }
534 // Perform various scans and checks
535 // --------------------------------
536 if (_latencyChecker) {
537 _latencyChecker->checkOutage(irc);
538 QListIterator<GPSDecoder::t_typeInfo> it(decoder()->_typeList);
539 _ssrEpoch = static_cast<int>(decoder()->corrGPSEpochTime());
540 if (_ssrEpoch != -1) {
541 if (_rtcmSsrOrb) {
542 _latencyChecker->checkCorrLatency(_ssrEpoch, 1057);
543 _rtcmSsrOrb = false;
544 }
545 if (_rtcmSsrClk) {
546 _latencyChecker->checkCorrLatency(_ssrEpoch, 1058);
547 _rtcmSsrClk = false;
548 }
549 if (_rtcmSsrOrbClk) {
550 _latencyChecker->checkCorrLatency(_ssrEpoch, 1060);
551 _rtcmSsrOrbClk = false;
552 }
553 if (_rtcmSsrCbi) {
554 _latencyChecker->checkCorrLatency(_ssrEpoch, 1059);
555 _rtcmSsrCbi = false;
556 }
557 if (_rtcmSsrPbi) {
558 _latencyChecker->checkCorrLatency(_ssrEpoch, 1265);
559 _rtcmSsrPbi = false;
560 }
561 if (_rtcmSsrVtec) {
562 _latencyChecker->checkCorrLatency(_ssrEpoch, 1264);
563 _rtcmSsrVtec = false;
564 }
565 if (_rtcmSsrUra) {
566 _latencyChecker->checkCorrLatency(_ssrEpoch, 1061);
567 _rtcmSsrUra = false;
568 }
569 if (_rtcmSsrHr) {
570 _latencyChecker->checkCorrLatency(_ssrEpoch, 1062);
571 _rtcmSsrHr = false;
572 }
573 if (_rtcmSsrIgs) {
574 _latencyChecker->checkCorrLatency(_ssrEpoch, 4076);
575 _rtcmSsrIgs = false;
576 }
577 }
578 while (it.hasNext()) {
579 int rtcmType = it.next()._type;
580 if ((rtcmType >= 1001 && rtcmType <= 1004) || // legacy RTCM OBS
581 (rtcmType >= 1009 && rtcmType <= 1012) || // legacy RTCM OBS
582 (rtcmType >= 1070 && rtcmType <= 1137)) { // MSM RTCM OBS
583 _rtcmObs = true;
584 } else if ((rtcmType >= 1057 && rtcmType <= 1068) ||
585 (rtcmType >= 1240 && rtcmType <= 1270) ||
586 (rtcmType == 4076)) {
587 switch (rtcmType) {
588 case 1057: case 1063: case 1240: case 1246: case 1252: case 1258:
589 _rtcmSsrOrb = true;
590 break;
591 case 1058: case 1064: case 1241: case 1247: case 1253: case 1259:
592 _rtcmSsrClk = true;
593 break;
594 case 1060: case 1066: case 1243: case 1249: case 1255: case 1261:
595 _rtcmSsrOrbClk = true;
596 break;
597 case 1059: case 1065: case 1242: case 1248: case 1254: case 1260:
598 _rtcmSsrCbi = true;
599 break;
600 case 1265: case 1266: case 1267: case 1268: case 1269: case 1270:
601 _rtcmSsrPbi = true;
602 break;
603 case 1264:
604 _rtcmSsrVtec = true;
605 break;
606 case 1061: case 1067: case 1244: case 1250: case 1256: case 1262:
607 _rtcmSsrUra = true;
608 break;
609 case 1062: case 1068: case 1245: case 1251: case 1257: case 1263:
610 _rtcmSsrHr = true;
611 break;
612 case 4076:
613 _rtcmSsrIgs = true;
614 break;
615 }
616 }
617 }
618 if (_rtcmObs) {
619 _latencyChecker->checkObsLatency(decoder()->_obsList);
620 }
621 emit newLatency(_staID, _latencyChecker->currentLatency());
622 }
623 miscScanRTCM();
624
625 // Loop over all observations (observations output)
626 // ------------------------------------------------
627 QListIterator<t_satObs> it(decoder()->_obsList);
628
629 QList<t_satObs> obsListHlp;
630
631 while (it.hasNext()) {
632 const t_satObs& obs = it.next();
633
634 // Check observation epoch
635 // -----------------------
636 if (!_rawFile) {
637 bool wrongObservationEpoch = checkForWrongObsEpoch(obs._time);
638 if (wrongObservationEpoch) {
639 QString prn(obs._prn.toString().c_str());
640 QString type = QString("%1").arg(obs._type);
641 emit(newMessage(_staID + " (" + prn.toLatin1() + ")" + ": Wrong observation epoch(s)" + "( MT: " + type.toLatin1() + ")", false));
642 continue;
643 }
644 }
645
646 // Check observations coming twice (e.g. KOUR0 Problem)
647 // ----------------------------------------------------
648 if (!_rawFile) {
649 QString prn(obs._prn.toString().c_str());
650 bncTime obsTime = obs._time;
651 QMap<QString, bncTime>::const_iterator it = _prnLastEpo.find(prn);
652 if (it != _prnLastEpo.end()) {
653 bncTime oldTime = it.value();
654 if (obsTime < oldTime) {
655 emit(newMessage(_staID + ": old observation " + prn.toLatin1(), false));
656 continue;
657 } else if (obsTime == oldTime) {
658 emit(newMessage(_staID + ": observation coming more than once " + prn.toLatin1(), false));
659 continue;
660 }
661 }
662 _prnLastEpo[prn] = obsTime;
663 }
664
665 decoder()->dumpRinexEpoch(obs, _format);
666
667 // Save observations
668 // -----------------
669 obsListHlp.append(obs);
670 }
671
672 // Emit signal
673 // -----------
674 if (!_isToBeDeleted && obsListHlp.size() > 0) {
675 emit newObs(_staID, obsListHlp);
676 }
677
678 }
679 catch (Exception& exc) {
680 emit(newMessage(_staID + " " + exc.what(), true));
681 _isToBeDeleted = true;
682 }
683 catch (std::exception& exc) {
684 emit(newMessage(_staID + " " + exc.what(), true));
685 _isToBeDeleted = true;
686 }
687 catch (const string& error) {
688 emit(newMessage(_staID + " ERROR: " + error.c_str(), true));
689 _isToBeDeleted = true;
690 }
691 catch (const char* error) {
692 emit(newMessage(_staID + " ERROR: " + error, true));
693 _isToBeDeleted = true;
694 }
695 catch (QString error) {
696 emit(newMessage(_staID + " ERROR: " + error.toStdString().c_str(), true));
697 _isToBeDeleted = true;
698 }
699 catch (...) {
700 emit(newMessage(_staID + " bncGetThread: unknown exception", true));
701 _isToBeDeleted = true;
702 }
703 }
704}
705
706// Try Re-Connect
707////////////////////////////////////////////////////////////////////////////
708t_irc bncGetThread::tryReconnect() {
709
710 // Easy Return
711 // -----------
712 if (_query && _query->status() == bncNetQuery::running) {
713 _nextSleep = 0;
714 if (_rawFile) {
715 QMapIterator<QString, GPSDecoder*> itDec(_decodersRaw);
716 while (itDec.hasNext()) {
717 itDec.next();
718 GPSDecoder* decoder = itDec.value();
719 decoder->setRinexReconnectFlag(false);
720 }
721 } else {
722 _decoder->setRinexReconnectFlag(false);
723 }
724 return success;
725 }
726
727 // Start a new query
728 // -----------------
729 if (!_rawFile) {
730
731 sleep(_nextSleep);
732 if (_nextSleep == 0) {
733 _nextSleep = 1;
734 } else {
735 _nextSleep = 2 * _nextSleep;
736 if (_nextSleep > 256) {
737 _nextSleep = 256;
738 }
739#ifdef MLS_SOFTWARE
740 if (_nextSleep > 4) {
741 _nextSleep = 4;
742 }
743#endif
744 }
745 delete _query;
746 if (_ntripVersion == "U") {
747 _query = new bncNetQueryUdp();
748 } else if (_ntripVersion == "R") {
749 _query = new bncNetQueryRtp();
750 } else if (_ntripVersion == "S") {
751 _query = new bncNetQueryS();
752 } else if (_ntripVersion == "N") {
753 _query = new bncNetQueryV0();
754 } else if (_ntripVersion == "UN") {
755 _query = new bncNetQueryUdp0();
756 } else if (_ntripVersion == "2") {
757 _query = new bncNetQueryV2(false);
758 } else if (_ntripVersion == "2s") {
759 _query = new bncNetQueryV2(true);
760 } else {
761 _query = new bncNetQueryV1();
762 }
763 if (_nmea == "yes") {
764 if (_serialNMEA == MANUAL_NMEA) {
765 _query->startRequest(_mountPoint, _manualNMEAString);
766 _lastNMEA = QDateTime::currentDateTime();
767 } else if (_serialNMEA == AUTO_NMEA) {
768 if (_serialPort) {
769 int nb = _serialPort->bytesAvailable();
770 if (nb > 0) {
771 QByteArray data = _serialPort->read(nb);
772 int i1 = data.indexOf("$GPGGA");
773 if (i1 == -1) {
774 i1 = data.indexOf("$GNGGA");
775 }
776 if (i1 != -1) {
777 int i2 = data.indexOf("*", i1);
778 if (i2 != -1 && data.size() > i2 + 1) {
779 QByteArray gga = data.mid(i1, i2 - i1 + 3);
780 _query->startRequest(_mountPoint, gga);
781 _lastNMEA = QDateTime::currentDateTime();
782 }
783 }
784 }
785 }
786 }
787 } else {
788 _query->startRequest(_mountPoint, "");
789 }
790
791 if (_query->status() != bncNetQuery::running) {
792 return failure;
793 }
794 }
795
796 if (_rawFile) {
797 QMapIterator<QString, GPSDecoder*> itDec(_decodersRaw);
798 while (itDec.hasNext()) {
799 itDec.next();
800 GPSDecoder* decoder = itDec.value();
801 decoder->setRinexReconnectFlag(false);
802 }
803 } else {
804 _decoder->setRinexReconnectFlag(false);
805 }
806
807 return success;
808}
809
810// RTCM scan output
811//////////////////////////////////////////////////////////////////////////////
812void bncGetThread::miscScanRTCM() {
813
814 if (!decoder()) {
815 return;
816 }
817
818 bncSettings settings;
819 if (Qt::CheckState(settings.value("miscScanRTCM").toInt()) == Qt::Checked) {
820
821 if (_miscMount == _staID || _miscMount == "ALL") {
822 // RTCM message types
823 // ------------------
824 for (int ii = 0; ii < decoder()->_typeList.size(); ii++) {
825 QString type = QString("%1 ").arg(decoder()->_typeList[ii]._type);
826 QString size = (decoder()->_typeList[ii]._size) ? QString("(size %1)").arg(decoder()->_typeList[ii]._size) : "";
827 emit(newMessage(_staID + ": Received message type " + type.toLatin1() + size.toLatin1(), true));
828 }
829
830 // Check Observation Types
831 // -----------------------
832 for (int ii = 0; ii < decoder()->_obsList.size(); ii++) {
833 t_satObs& obs = decoder()->_obsList[ii];
834 QVector<QString>& rnxTypes = _rnxTypes[obs._prn.system()];
835 bool allFound = true;
836 for (unsigned iFrq = 0; iFrq < obs._obs.size(); iFrq++) {
837 if (obs._obs[iFrq]->_codeValid) {
838 QString rnxStr('C');
839 rnxStr.append(obs._obs[iFrq]->_rnxType2ch.c_str());
840 if (_format.indexOf("RTCM_2") != -1
841 || _format.indexOf("RTCM2") != -1
842 || _format.indexOf("RTCM 2") != -1) {
843 rnxStr = t_rnxObsFile::type3to2(obs._prn.system(), rnxStr);
844 }
845 if (rnxTypes.indexOf(rnxStr) == -1) {
846 rnxTypes.push_back(rnxStr);
847 allFound = false;
848 }
849 }
850 if (obs._obs[iFrq]->_phaseValid) {
851 QString rnxStr('L');
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]->_dopplerValid) {
864 QString rnxStr('D');
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]->_snrValid) {
877 QString rnxStr('S');
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 }
890 if (!allFound) {
891 QString msg;
892 QTextStream str(&msg);
893 str << obs._prn.system() << QString(" %1 ").arg(rnxTypes.size());
894 for (int iType = 0; iType < rnxTypes.size(); iType++) {
895 str << " " << rnxTypes[iType];
896 }
897 emit(newMessage(_staID + ": Observation Types: " + msg.toLatin1(), true));
898 }
899 }
900
901 // RTCMv3 antenna descriptor
902 // -------------------------
903 for (int ii = 0; ii < decoder()->_antType.size(); ii++) {
904 QString ant1 = QString(": Antenna Descriptor: %1 ").arg(decoder()->_antType[ii]._descriptor);
905 emit(newMessage(_staID + ant1.toLatin1(), true));
906 if (strlen(decoder()->_antType[ii]._serialnumber)) {
907 QString ant2 = QString(": Antenna Serial Number: %1 ").arg(decoder()->_antType[ii]._serialnumber);
908 emit(newMessage(_staID + ant2.toLatin1(), true));
909 }
910 }
911
912 // RTCM Antenna Coordinates
913 // ------------------------
914 for (int ii = 0; ii < decoder()->_antList.size(); ii++) {
915 QByteArray antT;
916 if (decoder()->_antList[ii]._type == GPSDecoder::t_antRefPoint::ARP) {
917 antT = "ARP";
918 } else if (decoder()->_antList[ii]._type == GPSDecoder::t_antRefPoint::APC) {
919 antT = "APC";
920 }
921 QByteArray ant1, ant2, ant3;
922 ant1 = QString("%1 ").arg(decoder()->_antList[ii]._xx, 0, 'f', 4).toLatin1();
923 ant2 = QString("%1 ").arg(decoder()->_antList[ii]._yy, 0, 'f', 4).toLatin1();
924 ant3 = QString("%1 ").arg(decoder()->_antList[ii]._zz, 0, 'f', 4).toLatin1();
925 emit(newMessage(_staID + ": " + antT + " (ITRF) X " + ant1 + "m", true));
926 emit(newMessage(_staID + ": " + antT + " (ITRF) Y " + ant2 + "m", true));
927 emit(newMessage(_staID + ": " + antT + " (ITRF) Z " + ant3 + "m", true));
928 double hh = 0.0;
929 if (decoder()->_antList[ii]._height_f) {
930 hh = decoder()->_antList[ii]._height;
931 QByteArray ant4 = QString("%1 ").arg(hh, 0, 'f', 4).toLatin1();
932 emit(newMessage(
933 _staID + ": Antenna height above marker " + ant4 + "m", true));
934 }
935 emit(newAntCrd(_staID, decoder()->_antList[ii]._xx,
936 decoder()->_antList[ii]._yy, decoder()->_antList[ii]._zz, hh, antT));
937 }
938
939 // RTCMv3 receiver descriptor
940 // --------------------------
941 for (int ii = 0; ii < decoder()->_recType.size(); ii++) {
942 QString rec1 = QString(": Receiver Descriptor: %1 ").arg(decoder()->_recType[ii]._descriptor);
943 QString rec2 = QString(": Receiver Firmware Version: %1 ").arg(decoder()->_recType[ii]._firmware);
944 QString rec3 = QString(": Receiver Serial Number: %1 ").arg(decoder()->_recType[ii]._serialnumber);
945 emit(newMessage(_staID + rec1.toLatin1(), true));
946 emit(newMessage(_staID + rec2.toLatin1(), true));
947 emit(newMessage(_staID + rec3.toLatin1(), true));
948 }
949
950 // RTCM GLONASS slots
951 // ------------------
952 if (decoder()->_gloFrq.size()) {
953 bool allFound = true;
954 QString slot = decoder()->_gloFrq;
955 slot.replace(" ", " ").replace(" ", ":");
956 if (_gloSlots.indexOf(slot) == -1) {
957 _gloSlots.append(slot);
958 allFound = false;
959 }
960 if (!allFound) {
961 _gloSlots.sort();
962 emit(newMessage(
963 _staID + ": GLONASS Slot:Freq " + _gloSlots.join(" ").toLatin1(),
964 true));
965 }
966 }
967
968 // RTCM GLONASS Code-Phase biases (MT 1230)
969 // ----------------------------------------
970 if (decoder()->_gloBiasInfo.changed()) {
971 QString gloCodePhaseBiases = decoder()->_gloBiasInfo.toString();
972 emit(newMessage(_staID + gloCodePhaseBiases.toLatin1(), true));
973 decoder()->_gloBiasInfo.setChanged(false);
974 }
975
976 // Service CRS / RTCM CRS
977 // ------------------------
978 if (fmod(decoder()->corrGPSEpochTime(), 60.0) == 0.0) {
979 // Service CRS
980 for (int ii = 0; ii < decoder()->_serviceCrs.size(); ii++) {
981 QString servicecrsname = QString(": Service CRS Name: %1 ").arg(decoder()->_serviceCrs[ii]._name);
982 QString coordinateEpoch = QString(": Service CRS Coordinate Epoch: %1 ").arg(decoder()->_serviceCrs[ii]._coordinateEpoch);
983 //QString ce = QString(": CE: %1 ").arg(decoder()->_serviceCrs[ii]._CE);
984 emit(newMessage(_staID + servicecrsname.toLatin1(), true));
985 emit(newMessage(_staID + coordinateEpoch.toLatin1(), true));
986 //emit(newMessage(_staID + ce.toLatin1(), true));
987 }
988 // RTCM CRS
989 for (int ii = 0; ii < decoder()->_rtcmCrs.size(); ii++) {
990 QString rtcmcrsname = QString(": RTCM CRS Name: %1 ").arg(decoder()->_rtcmCrs[ii]._name);
991 QString anchor = QString(": RTCM CRS Anchor: %1 ").arg(decoder()->_rtcmCrs[ii]._anchor);
992 QString platenumber = QString(": RTCM CRS Plate Number: %1 ").arg(decoder()->_rtcmCrs[ii]._plateNumber);
993 emit(newMessage(_staID + rtcmcrsname.toLatin1(), true));
994 emit(newMessage(_staID + anchor.toLatin1(), true));
995 emit(newMessage(_staID + platenumber.toLatin1(), true));
996 for (int i = 0; i<decoder()->_rtcmCrs[ii]._databaseLinks.size(); i++) {
997 QString dblink = QString(": Database Link: %1 ").arg(decoder()->_rtcmCrs[ii]._databaseLinks[i]);
998 emit(newMessage(_staID + dblink.toLatin1(), true));
999 }
1000 }
1001
1002 // Helmert Parameters
1003 //-------------------
1004 for (int ii = 0; ii < decoder()->_helmertPar.size(); ii++) {
1005 t_helmertPar& helmertPar = decoder()->_helmertPar[ii];
1006 bncTime t; t.setmjd(0, helmertPar._mjd); QString dateStr = QString::fromStdString(t.datestr());
1007 QString sourcename = QString(": MT1301 Source Name: %1 ").arg(helmertPar._sourceName);
1008 QString targetname = QString(": MT1301 Target Name: %1 ").arg(helmertPar._targetName);
1009 QString sysidentnum = QString(": MT1301 Sys Ident Num: %1 ").arg(helmertPar._sysIdentNum);
1010 QString trafomessageind = QString(": MT1301 Trafo Ident Num: %1 ").arg(helmertPar.IndtoString());
1011 QString epoch = QString(": MT1301 t0: MJD %1 (%2) ").arg(helmertPar._mjd).arg(dateStr);
1012 QString partrans = QString(": MT1301 Helmert Par Trans: dx = %1, dy = %2, dz = %3, dxr = %4, dyr = %5, dzr = %6")
1013 .arg(helmertPar._dx).arg(helmertPar._dy).arg(helmertPar._dz)
1014 .arg(helmertPar._dxr).arg(helmertPar._dyr).arg(helmertPar._dzr);
1015 QString parrot = QString(": MT1301 Helmert Par Rot: ox = %1, oy = %2, oz = %3, oxr = %4, oyr = %5, ozr = %6")
1016 .arg(helmertPar._ox).arg(helmertPar._oy).arg(helmertPar._oz)
1017 .arg(helmertPar._oxr).arg(helmertPar._oyr).arg(helmertPar._ozr);
1018 QString parscale = QString(": MT1301 Helmert Par Scale: sc = %1, scr = %2").arg(helmertPar._sc).arg(helmertPar._scr);
1019 emit(newMessage(_staID + sourcename.toLatin1(), true));
1020 emit(newMessage(_staID + targetname.toLatin1(), true));
1021 emit(newMessage(_staID + sysidentnum.toLatin1(), true));
1022 emit(newMessage(_staID + trafomessageind.toLatin1(), true));
1023 emit(newMessage(_staID + epoch.toLatin1(), true));
1024 emit(newMessage(_staID + partrans.toLatin1(), true));
1025 emit(newMessage(_staID + parrot.toLatin1(), true));
1026 emit(newMessage(_staID + parscale.toLatin1(), true));
1027 }
1028 }
1029 }
1030 }
1031
1032#ifdef MLS_SOFTWARE
1033 for (int ii=0; ii <decoder()->_antList.size(); ii++) {
1034 QByteArray antT;
1035 if (decoder()->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
1036 antT = "ARP";
1037 }
1038 else if (decoder()->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
1039 antT = "APC";
1040 }
1041 double hh = 0.0;
1042 if (decoder()->_antList[ii].height_f) {
1043 hh = decoder()->_antList[ii].height;
1044 }
1045 emit(newAntCrd(_staID, decoder()->_antList[ii].xx,
1046 decoder()->_antList[ii].yy, decoder()->_antList[ii].zz,
1047 hh, antT));
1048 }
1049
1050 for (int ii = 0; ii <decoder()->_typeList.size(); ii++) {
1051 emit(newRTCMMessage(_staID, decoder()->_typeList[ii]));
1052 }
1053#endif
1054
1055 decoder()->_gloFrq.clear();
1056 // decoder()->_gloBiases.clear();
1057 decoder()->_typeList.clear();
1058 decoder()->_antType.clear();
1059 decoder()->_recType.clear();
1060 decoder()->_antList.clear();
1061}
1062
1063// Handle Data from Serial Port
1064////////////////////////////////////////////////////////////////////////////
1065void bncGetThread::slotSerialReadyRead() {
1066
1067 if (_serialPort) {
1068
1069 if (_nmea == "yes" && _serialNMEA == MANUAL_NMEA) {
1070 if (_NMEASampl) {
1071 int dt = _lastNMEA.secsTo(QDateTime::currentDateTime());
1072 if (dt && (fmod(double(dt), double(_NMEASampl)) == 0.0)) {
1073 _query->sendNMEA(_manualNMEAString);
1074 _lastNMEA = QDateTime::currentDateTime();
1075 }
1076 }
1077 }
1078
1079 int nb = _serialPort->bytesAvailable();
1080 if (nb > 0) {
1081 QByteArray data = _serialPort->read(nb);
1082
1083 if (_nmea == "yes" && _serialNMEA == AUTO_NMEA) {
1084 int i1 = data.indexOf("$GPGGA");
1085 if (i1 == -1) {
1086 i1 = data.indexOf("$GNGGA");
1087 }
1088 if (i1 != -1) {
1089 int i2 = data.indexOf("*", i1);
1090 if (i2 != -1 && data.size() > i2 + 1) {
1091 QByteArray gga = data.mid(i1, i2 - i1 + 3);
1092 if (_NMEASampl) {
1093 int dt = _lastNMEA.secsTo(QDateTime::currentDateTime());
1094 if (dt && (fmod(double(dt), double(_NMEASampl)) == 0.0)) {
1095 _query->sendNMEA(gga);
1096 _lastNMEA = QDateTime::currentDateTime();
1097 }
1098 }
1099 }
1100 }
1101 }
1102
1103 if (_serialOutFile) {
1104 _serialOutFile->write(data);
1105 _serialOutFile->flush();
1106 }
1107 }
1108 }
1109}
1110
1111void bncGetThread::slotNewNMEAConnection() {
1112 _nmeaSockets->push_back(_nmeaServer->nextPendingConnection());
1113 emit(newMessage(
1114 QString("New PPP client on port: # %1").arg(_nmeaSockets->size()).toLatin1(),
1115 true));
1116}
1117
1118//
1119////////////////////////////////////////////////////////////////////////////
1120void bncGetThread::slotNewNMEAstr(QByteArray staID, QByteArray str) {
1121 if (_nmeaPortsMap.contains(staID)) {
1122 int nmeaPort = _nmeaPortsMap.value(staID);
1123 QMutableListIterator<QTcpSocket*> is(*_nmeaSockets);
1124 while (is.hasNext()) {
1125 QTcpSocket* sock = is.next();
1126 if (sock->localPort() == nmeaPort) {
1127 if (sock->state() == QAbstractSocket::ConnectedState) {
1128 sock->write(str);
1129 } else if (sock->state() != QAbstractSocket::ConnectingState) {
1130 delete sock;
1131 is.remove();
1132 }
1133 }
1134 }
1135 }
1136}
Note: See TracBrowser for help on using the repository browser.