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

Last change on this file since 1143 was 1143, checked in by mervart, 16 years ago

* empty log message *

File size: 28.2 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: bncGetThread
30 *
31 * Purpose: Thread that retrieves data from NTRIP caster
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Dec-2005
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdlib.h>
42#include <iomanip>
43
44#include <QFile>
45#include <QTextStream>
46#include <QtNetwork>
47#include <QTime>
48
49#include "bncgetthread.h"
50#include "bnctabledlg.h"
51#include "bncapp.h"
52#include "bncutils.h"
53#include "bncrinex.h"
54#include "bnczerodecoder.h"
55
56#include "RTCM/RTCM2Decoder.h"
57#include "RTCM3/RTCM3Decoder.h"
58#include "RTIGS/RTIGSDecoder.h"
59
60using namespace std;
61
62// Constructor 1
63////////////////////////////////////////////////////////////////////////////
64bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
65 const QByteArray& format) {
66
67 _format = format;
68 _staID = rawInpFileName.left(4);
69
70 initialize();
71
72 _inspSegm = 0;
73
74 _rawInpFile = new QFile(rawInpFileName);
75 _rawInpFile->open(QIODevice::ReadOnly);
76}
77
78// Constructor 2
79////////////////////////////////////////////////////////////////////////////
80bncGetThread::bncGetThread(const QUrl& mountPoint,
81 const QByteArray& format,
82 const QByteArray& latitude,
83 const QByteArray& longitude,
84 const QByteArray& nmea, int iMount) {
85
86 setTerminationEnabled(true);
87
88 _mountPoint = mountPoint;
89 _staID = mountPoint.path().mid(1).toAscii();
90 _format = format;
91 _latitude = latitude;
92 _longitude = longitude;
93 _nmea = nmea;
94 _iMount = iMount; // index in mountpoints array
95
96 initialize();
97}
98
99// Initialization
100////////////////////////////////////////////////////////////////////////////
101void bncGetThread::initialize() {
102
103 _decoder = 0;
104 _socket = 0;
105 _timeOut = 20*1000; // 20 seconds
106 _nextSleep = 1; // 1 second
107 _rawInpFile = 0;
108 _rawOutFile = 0;
109 _staID_orig = _staID;
110
111 // Check name conflict
112 // -------------------
113 QSettings settings;
114 QListIterator<QString> it(settings.value("mountPoints").toStringList());
115 int num = 0;
116 int ind = -1;
117 while (it.hasNext()) {
118 ++ind;
119 QStringList hlp = it.next().split(" ");
120 if (hlp.size() <= 1) continue;
121 QUrl url(hlp[0]);
122 if (_mountPoint.path() == url.path()) {
123 if (_iMount > ind) {
124 ++num;
125 }
126 }
127 }
128
129 if (num > 0) {
130 _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
131 }
132
133 // Notice threshold
134 // ----------------
135 _inspSegm = 50;
136 if ( settings.value("obsRate").toString().isEmpty() ) { _inspSegm = 0; }
137 if ( settings.value("obsRate").toString().indexOf("5 Hz") != -1 ) { _inspSegm = 2; }
138 if ( settings.value("obsRate").toString().indexOf("1 Hz") != -1 ) { _inspSegm = 10; }
139 if ( settings.value("obsRate").toString().indexOf("0.5 Hz") != -1 ) { _inspSegm = 20; }
140 if ( settings.value("obsRate").toString().indexOf("0.2 Hz") != -1 ) { _inspSegm = 40; }
141 if ( settings.value("obsRate").toString().indexOf("0.1 Hz") != -1 ) { _inspSegm = 50; }
142 _adviseFail = settings.value("adviseFail").toInt();
143 _adviseReco = settings.value("adviseReco").toInt();
144 _makePause = false;
145 if ( Qt::CheckState(settings.value("makePause").toInt()) == Qt::Checked) {_makePause = true; }
146 _adviseScript = settings.value("adviseScript").toString();
147 expandEnvVar(_adviseScript);
148
149 // Latency interval/average
150 // ------------------------
151 _perfIntr = 86400;
152 if ( settings.value("perfIntr").toString().isEmpty() ) { _perfIntr = 0; }
153 if ( settings.value("perfIntr").toString().indexOf("1 min") != -1 ) { _perfIntr = 60; }
154 if ( settings.value("perfIntr").toString().indexOf("5 min") != -1 ) { _perfIntr = 300; }
155 if ( settings.value("perfIntr").toString().indexOf("15 min") != -1 ) { _perfIntr = 900; }
156 if ( settings.value("perfIntr").toString().indexOf("1 hour") != -1 ) { _perfIntr = 3600; }
157 if ( settings.value("perfIntr").toString().indexOf("6 hours") != -1 ) { _perfIntr = 21600; }
158 if ( settings.value("perfIntr").toString().indexOf("1 day") != -1 ) { _perfIntr = 86400; }
159
160 // RTCM message types
161 // ------------------
162 _checkMountPoint = settings.value("messTypes").toString();
163
164 // RINEX writer
165 // ------------
166 _samplingRate = settings.value("rnxSampl").toInt();
167 if ( settings.value("rnxPath").toString().isEmpty() ) {
168 _rnx = 0;
169 }
170 else {
171 _rnx = new bncRinex(_staID, _mountPoint,
172 _format, _latitude, _longitude, _nmea);
173 }
174 _rnx_set_position = false;
175
176 // Raw Output
177 // ----------
178 // QByteArray rawOutFileName = "./" + _staID + ".raw";
179 // _rawOutFile = new QFile(rawOutFileName);
180 // _rawOutFile->open(QIODevice::WriteOnly);
181
182 msleep(100); //sleep 0.1 sec
183}
184
185// Destructor
186////////////////////////////////////////////////////////////////////////////
187bncGetThread::~bncGetThread() {
188 if (_socket) {
189 _socket->close();
190#if QT_VERSION == 0x040203
191 delete _socket;
192#else
193 _socket->deleteLater();
194#endif
195 }
196 delete _decoder;
197 delete _rnx;
198 delete _rawInpFile;
199 delete _rawOutFile;
200}
201
202#define AGENTVERSION "1.6"
203// Connect to Caster, send the Request (static)
204////////////////////////////////////////////////////////////////////////////
205QTcpSocket* bncGetThread::request(const QUrl& mountPoint,
206 QByteArray& latitude, QByteArray& longitude,
207 QByteArray& nmea, int timeOut,
208 QString& msg) {
209
210 // Connect the Socket
211 // ------------------
212 QSettings settings;
213 QString proxyHost = settings.value("proxyHost").toString();
214 int proxyPort = settings.value("proxyPort").toInt();
215
216 QTcpSocket* socket = new QTcpSocket();
217 if ( proxyHost.isEmpty() ) {
218 socket->connectToHost(mountPoint.host(), mountPoint.port());
219 }
220 else {
221 socket->connectToHost(proxyHost, proxyPort);
222 }
223 if (!socket->waitForConnected(timeOut)) {
224 msg += "Connect timeout\n";
225 delete socket;
226 return 0;
227 }
228
229 // Send Request
230 // ------------
231 QString uName = QUrl::fromPercentEncoding(mountPoint.userName().toAscii());
232 QString passW = QUrl::fromPercentEncoding(mountPoint.password().toAscii());
233 QByteArray userAndPwd;
234
235 if(!uName.isEmpty() || !passW.isEmpty())
236 {
237 userAndPwd = "Authorization: Basic " + (uName.toAscii() + ":" +
238 passW.toAscii()).toBase64() + "\r\n";
239 }
240
241 QUrl hlp;
242 hlp.setScheme("http");
243 hlp.setHost(mountPoint.host());
244 hlp.setPort(mountPoint.port());
245 hlp.setPath(mountPoint.path());
246
247 QByteArray reqStr;
248 if ( proxyHost.isEmpty() ) {
249 if (hlp.path().indexOf("/") != 0) {
250 hlp.setPath("/");
251 }
252 reqStr = "GET " + hlp.path().toAscii() + " HTTP/1.0\r\n";
253 } else {
254 reqStr = "GET " + hlp.toEncoded() + " HTTP/1.0\r\n";
255 }
256 reqStr += "User-Agent: NTRIP BNC/" AGENTVERSION "\r\n"
257 "Host: " + hlp.host().toAscii() + "\r\n"
258 + userAndPwd + "\r\n";
259
260 // NMEA string to handle VRS stream
261 // --------------------------------
262 double lat, lon;
263
264 lat = strtod(latitude,NULL);
265 lon = strtod(longitude,NULL);
266
267 if ((nmea == "yes") && (hlp.path().length() > 2) && (hlp.path().indexOf(".skl") < 0)) {
268 const char* flagN="N";
269 const char* flagE="E";
270 if (lon >180.) {lon=(lon-360.)*(-1.); flagE="W";}
271 if ((lon < 0.) && (lon >= -180.)) {lon=lon*(-1.); flagE="W";}
272 if (lon < -180.) {lon=(lon+360.); flagE="E";}
273 if (lat < 0.) {lat=lat*(-1.); flagN="S";}
274 QTime ttime(QDateTime::currentDateTime().toUTC().time());
275 int lat_deg = (int)lat;
276 double lat_min=(lat-lat_deg)*60.;
277 int lon_deg = (int)lon;
278 double lon_min=(lon-lon_deg)*60.;
279 int hh = 0 , mm = 0;
280 double ss = 0.0;
281 hh=ttime.hour();
282 mm=ttime.minute();
283 ss=(double)ttime.second()+0.001*ttime.msec();
284 QString gga;
285 gga += "GPGGA,";
286 gga += QString("%1%2%3,").arg((int)hh, 2, 10, QLatin1Char('0')).arg((int)mm, 2, 10, QLatin1Char('0')).arg((int)ss, 2, 10, QLatin1Char('0'));
287 gga += QString("%1%2,").arg((int)lat_deg,2, 10, QLatin1Char('0')).arg(lat_min, 7, 'f', 4, QLatin1Char('0'));
288 gga += flagN;
289 gga += QString(",%1%2,").arg((int)lon_deg,3, 10, QLatin1Char('0')).arg(lon_min, 7, 'f', 4, QLatin1Char('0'));
290 gga += flagE + QString(",1,05,1.00,+00100,M,10.000,M,,");
291 int xori;
292 char XOR = 0;
293 char *Buff =gga.toAscii().data();
294 int iLen = strlen(Buff);
295 for (xori = 0; xori < iLen; xori++) {
296 XOR ^= (char)Buff[xori];
297 }
298 gga += QString("*%1").arg(XOR, 2, 16, QLatin1Char('0'));
299 reqStr += "$";
300 reqStr += gga;
301 reqStr += "\r\n";
302 }
303
304 msg += reqStr;
305
306 socket->write(reqStr, reqStr.length());
307
308 if (!socket->waitForBytesWritten(timeOut)) {
309 msg += "Write timeout\n";
310 delete socket;
311 return 0;
312 }
313
314 return socket;
315}
316
317// Init Run
318////////////////////////////////////////////////////////////////////////////
319t_irc bncGetThread::initRun() {
320
321 if (!_rawInpFile) {
322
323 // Initialize Socket
324 // -----------------
325 QString msg;
326 _socket = this->request(_mountPoint, _latitude, _longitude,
327 _nmea, _timeOut, msg);
328 if (!_socket) {
329 return failure;
330 }
331
332 // Read Caster Response
333 // --------------------
334 _socket->waitForReadyRead(_timeOut);
335 if (_socket->canReadLine()) {
336 QString line = _socket->readLine();
337
338 // Skip messages from proxy server
339 // -------------------------------
340 if (line.indexOf("ICY 200 OK") == -1 &&
341 line.indexOf("200 OK") != -1 ) {
342 bool proxyRespond = true;
343 while (true) {
344 if (_socket->canReadLine()) {
345 line = _socket->readLine();
346 if (!proxyRespond) {
347 break;
348 }
349 if (line.trimmed().isEmpty()) {
350 proxyRespond = false;
351 }
352 }
353 else {
354 _socket->waitForReadyRead(_timeOut);
355 if (_socket->bytesAvailable() <= 0) {
356 break;
357 }
358 }
359 }
360 }
361
362 if (line.indexOf("Unauthorized") != -1) {
363 QStringList table;
364 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(), table);
365 QString net;
366 QStringListIterator it(table);
367 while (it.hasNext()) {
368 QString line = it.next();
369 if (line.indexOf("STR") == 0) {
370 QStringList tags = line.split(";");
371 if (tags.at(1) == _staID_orig) {
372 net = tags.at(7);
373 break;
374 }
375 }
376 }
377
378 QString reg;
379 it.toFront();
380 while (it.hasNext()) {
381 QString line = it.next();
382 if (line.indexOf("NET") == 0) {
383 QStringList tags = line.split(";");
384 if (tags.at(1) == net) {
385 reg = tags.at(7);
386 break;
387 }
388 }
389 }
390 emit(newMessage((_staID + ": Caster Response: " + line +
391 " Adjust User-ID and Password Register, see"
392 "\n " + reg).toAscii()));
393 return fatal;
394 }
395 if (line.indexOf("ICY 200 OK") != 0) {
396 emit(newMessage((_staID + ": Wrong Caster Response:\n" + line).toAscii()));
397 return failure;
398 }
399 }
400 else {
401 emit(newMessage(_staID + ": Response Timeout"));
402 return failure;
403 }
404 }
405
406 // Instantiate the filter
407 // ----------------------
408 if (!_decoder) {
409 if (_format.indexOf("RTCM_2") != -1) {
410 emit(newMessage("Get Data: " + _staID + " in RTCM 2.x format"));
411 _decoder = new RTCM2Decoder(_staID.data());
412 }
413 else if (_format.indexOf("RTCM_3") != -1) {
414 emit(newMessage("Get Data: " + _staID + " in RTCM 3.x format"));
415 _decoder = new RTCM3Decoder(_staID);
416 connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray)),
417 this, SIGNAL(newMessage(QByteArray)));
418 }
419 else if (_format.indexOf("RTIGS") != -1) {
420 emit(newMessage("Get Data: " + _staID + " in RTIGS format"));
421 _decoder = new RTIGSDecoder();
422 }
423 else if (_format.indexOf("ZERO") != -1) {
424 emit(newMessage("Get Data: " + _staID + " in original format"));
425 _decoder = new bncZeroDecoder(_staID);
426 }
427 else {
428 emit(newMessage(_staID + ": Unknown data format " + _format));
429 return fatal;
430 }
431 }
432 return success;
433}
434
435// Run
436////////////////////////////////////////////////////////////////////////////
437void bncGetThread::run() {
438
439 const double maxDt = 600.0; // Check observation epoch
440 bool wrongEpoch = false;
441 bool decode = true;
442 int numSucc = 0;
443 int secSucc = 0;
444 int secFail = 0;
445 int initPause = 30; // Initial pause for corrupted streams
446 int currPause = 0;
447 bool begCorrupt = false;
448 bool endCorrupt = false;
449 bool followSec = false;
450 int oldSecGPS= 0;
451 int newSecGPS = 0;
452 int numGaps = 0;
453 int diffSecGPS = 0;
454 int numLat = 0;
455 double sumLat = 0.;
456 double sumLatQ = 0.;
457 double meanDiff = 0.;
458 double minLat = maxDt;
459 double maxLat = -maxDt;
460 double curLat = 0.;
461
462 _decodeTime = QDateTime::currentDateTime();
463 _decodeSucc = QDateTime::currentDateTime();
464 t_irc irc = initRun();
465
466 if (irc == fatal) {
467 QThread::exit(1);
468 return;
469 }
470 else if (irc != success) {
471 emit(newMessage(_staID + ": initRun failed, reconnecting"));
472 tryReconnect();
473 }
474
475 if (initPause < _inspSegm) {
476 initPause = _inspSegm;
477 }
478 if(!_makePause) {initPause = 0;}
479 currPause = initPause;
480
481 // Read Incoming Data
482 // ------------------
483 while (true) {
484 try {
485 if (_socket && _socket->state() != QAbstractSocket::ConnectedState) {
486 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
487 tryReconnect();
488 }
489
490 QListIterator<p_obs> it(_decoder->_obsList);
491 while (it.hasNext()) {
492 delete it.next();
493 }
494 _decoder->_obsList.clear();
495
496 qint64 nBytes = 0;
497
498 if (_socket) {
499 _socket->waitForReadyRead(_timeOut);
500 nBytes = _socket->bytesAvailable();
501 }
502 else if (_rawInpFile) {
503 const qint64 maxBytes = 10000;
504 nBytes = maxBytes;
505 }
506
507 if (nBytes > 0) {
508 emit newBytes(_staID, nBytes);
509
510 char* data = new char[nBytes];
511
512 if (_socket) {
513 _socket->read(data, nBytes);
514 }
515 else if (_rawInpFile) {
516 nBytes = _rawInpFile->read(data, nBytes);
517 cout << "nBytes = " << nBytes << endl;
518 if (nBytes <= 0) {
519 cout << "no more data" << endl;
520 ::exit(0);
521 }
522 }
523
524 if (_rawOutFile) {
525 _rawOutFile->write(data, nBytes);
526 _rawOutFile->flush();
527 }
528
529 if (_inspSegm<1) {
530 _decoder->Decode(data, nBytes);
531 }
532 else {
533
534 // Decode data
535 // -----------
536 if (!_decodePause.isValid() ||
537 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
538
539 if (decode) {
540 if ( _decoder->Decode(data, nBytes) == success ) {
541 numSucc += 1;
542 }
543 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
544 decode = false;
545 }
546 }
547
548 // Check - once per inspect segment
549 // --------------------------------
550 if (!decode) {
551 _decodeTime = QDateTime::currentDateTime();
552 if (numSucc>0) {
553 secSucc += _inspSegm;
554 _decodeSucc = QDateTime::currentDateTime();
555 if (secSucc > _adviseReco * 60) {
556 secSucc = _adviseReco * 60 + 1;
557 }
558 numSucc = 0;
559 currPause = initPause;
560 _decodePause.setDate(QDate());
561 _decodePause.setTime(QTime());
562 }
563 else {
564 secFail += _inspSegm;
565 secSucc = 0;
566 if (secFail > _adviseFail * 60) {
567 secFail = _adviseFail * 60 + 1;
568 }
569 if (!_decodePause.isValid() || !_makePause) {
570 _decodePause = QDateTime::currentDateTime();
571 }
572 else {
573 _decodePause.setDate(QDate());
574 _decodePause.setTime(QTime());
575 secFail = secFail + currPause - _inspSegm;
576 currPause = currPause * 2;
577 if (currPause > 960) {
578 currPause = 960;
579 }
580 }
581 }
582
583 // End corrupt threshold
584 // ---------------------
585 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
586 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
587 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
588 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
589 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
590 endCorrupt = true;
591 begCorrupt = false;
592 secFail = 0;
593 }
594 else {
595
596 // Begin corrupt threshold
597 // -----------------------
598 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
599 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
600 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
601 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
602 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
603 begCorrupt = true;
604 endCorrupt = false;
605 secSucc = 0;
606 numSucc = 0;
607 }
608 }
609 decode = true;
610 }
611 }
612 }
613
614 // End outage threshold
615 // --------------------
616 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
617 _decodeStart.setDate(QDate());
618 _decodeStart.setTime(QTime());
619 if (_inspSegm>0) {
620 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
621 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
622 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
623 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
624 }
625 }
626
627 delete [] data;
628
629 QListIterator<p_obs> it(_decoder->_obsList);
630 while (it.hasNext()) {
631 p_obs obs = it.next();
632
633 // Check observation epoch
634 // -----------------------
635 if (!_rawInpFile) {
636 int week;
637 double sec;
638 leapsecGPSWeeks(week, sec);
639 const double secPerWeek = 7.0 * 24.0 * 3600.0;
640
641 if (week < obs->_o.GPSWeek) {
642 week += 1;
643 sec -= secPerWeek;
644 }
645 if (week > obs->_o.GPSWeek) {
646 week -= 1;
647 sec += secPerWeek;
648 }
649 double dt = fabs(sec - obs->_o.GPSWeeks);
650 if (week != obs->_o.GPSWeek || dt > maxDt) {
651 if (!wrongEpoch) {
652 emit( newMessage(_staID + ": Wrong observation epoch(s)") );
653 wrongEpoch = true;
654 }
655 delete obs;
656 continue;
657 }
658 else {
659 wrongEpoch = false;
660
661 // Latency and completeness
662 // ------------------------
663 if (_perfIntr>0) {
664 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
665 if (newSecGPS != oldSecGPS) {
666 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
667 if (numLat>0) {
668 if (meanDiff>0.) {
669 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
670 .arg(_staID.data())
671 .arg(int(sumLat/numLat*100)/100.)
672 .arg(int(minLat*100)/100.)
673 .arg(int(maxLat*100)/100.)
674 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
675 .arg(numLat)
676 .arg(numGaps)
677 .toAscii()) );
678 } else {
679 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
680 .arg(_staID.data())
681 .arg(int(sumLat/numLat*100)/100.)
682 .arg(int(minLat*100)/100.)
683 .arg(int(maxLat*100)/100.)
684 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
685 .arg(numLat)
686 .toAscii()) );
687 }
688 }
689 meanDiff = diffSecGPS/numLat;
690 diffSecGPS = 0;
691 numGaps = 0;
692 sumLat = 0.;
693 sumLatQ = 0.;
694 numLat = 0;
695 minLat = maxDt;
696 maxLat = -maxDt;
697 }
698 if (followSec) {
699 diffSecGPS += newSecGPS - oldSecGPS;
700 if (meanDiff>0.) {
701 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
702 numGaps += 1;
703 }
704 }
705 }
706 curLat = sec - obs->_o.GPSWeeks;
707 sumLat += curLat;
708 sumLatQ += curLat * curLat;
709 if (curLat < minLat) minLat = curLat;
710 if (curLat >= maxLat) maxLat = curLat;
711 numLat += 1;
712 oldSecGPS = newSecGPS;
713 followSec = true;
714 }
715 }
716 }
717 }
718
719 // RINEX Output
720 // ------------
721 if (_rnx) {
722 bool dump = true;
723
724 ////RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
725 ////if ( decoder2 && !_rnx_set_position ) {
726 //// double stax, stay, staz;
727 //// if ( decoder2->getStaCrd(stax, stay, staz) == success ) {
728 //// _rnx->setApproxPos(stax, stay, staz);
729 //// _rnx_set_position = true;
730 //// }
731 //// else {
732 //// dump = false;
733 //// }
734 ////}
735
736 if ( dump ) {
737 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
738 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
739 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
740 _rnx->deepCopy(obs);
741 }
742 _rnx->dumpEpoch(newTime);
743 }
744 }
745
746 // Emit new observation signal
747 // ---------------------------
748 bool firstObs = (obs == _decoder->_obsList.first());
749 obs->_status = t_obs::posted;
750 emit newObs(_staID, firstObs, obs);
751 }
752 _decoder->_obsList.clear();
753
754 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
755
756 // RTCM message types
757 // ------------------
758 if (0<_decoder->_typeList.size()) {
759 QString type;
760 for (int ii=0;ii<_decoder->_typeList.size();ii++) {
761 type = QString("%1 ").arg(_decoder->_typeList[ii]);
762 emit(newMessage(_staID + ": Received message type " + type.toAscii() ));
763 }
764 }
765 _decoder->_typeList.clear();
766
767 // Antenna XYZ & H
768 // ---------------
769 if (0<_decoder->_antList.size()) {
770 QString ant1,ant2,ant3,ant4;
771 for (int ii=0;ii<_decoder->_antList.size();ii+=4) {
772 ant1 = QString("%1 ").arg(_decoder->_antList[ii+0]*0.0001,0,'f',4);
773 ant2 = QString("%1 ").arg(_decoder->_antList[ii+1]*0.0001,0,'f',4);
774 ant3 = QString("%1 ").arg(_decoder->_antList[ii+2]*0.0001,0,'f',4);
775 ant4 = QString("%1 ").arg(_decoder->_antList[ii+3]*0.0001,0,'f',4);
776 emit(newMessage(_staID + ": ARP (ITRF) X " + ant1.toAscii() + "m" ));
777 emit(newMessage(_staID + ": ARP (ITRF) Y " + ant2.toAscii() + "m"));
778 emit(newMessage(_staID + ": ARP (ITRF) Z " + ant3.toAscii() + "m"));
779 emit(newMessage(_staID + ": Antenna height " + ant4.toAscii() + "m"));
780 }
781 }
782 _decoder->_antList.clear();
783 }
784 }
785
786 // Timeout, reconnect
787 // ------------------
788 else {
789 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
790 tryReconnect();
791 }
792 }
793 catch (const char* msg) {
794 emit(newMessage(_staID + msg));
795 tryReconnect();
796 }
797 }
798}
799
800// Exit
801////////////////////////////////////////////////////////////////////////////
802void bncGetThread::exit(int exitCode) {
803 if (exitCode!= 0) {
804 emit error(_staID);
805 }
806 QThread::exit(exitCode);
807 terminate();
808}
809
810// Try Re-Connect
811////////////////////////////////////////////////////////////////////////////
812void bncGetThread::tryReconnect() {
813 if (_rnx) {
814 _rnx->setReconnectFlag(true);
815 }
816 if ( !_decodeStart.isValid()) {
817 _decodeStop = QDateTime::currentDateTime();
818 }
819 while (1) {
820 delete _socket; _socket = 0;
821 sleep(_nextSleep);
822 if ( initRun() == success ) {
823 if ( !_decodeStop.isValid()) {
824 _decodeStart = QDateTime::currentDateTime();
825 }
826 break;
827 }
828 else {
829
830 // Begin outage threshold
831 // ----------------------
832 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
833 _decodeStop.setDate(QDate());
834 _decodeStop.setTime(QTime());
835 if (_inspSegm>0) {
836 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
837 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
838 emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
839 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
840 }
841 }
842 _nextSleep *= 2;
843 if (_nextSleep > 256) {
844 _nextSleep = 256;
845 }
846 _nextSleep += rand() % 6;
847 }
848 }
849 _nextSleep = 1;
850}
851
852// Call advisory notice script
853////////////////////////////////////////////////////////////////////////////
854void bncGetThread::callScript(const char* _comment) {
855 QMutexLocker locker(&_mutex);
856 if (!_adviseScript.isEmpty()) {
857 msleep(1);
858#ifdef WIN32
859 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
860#else
861 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
862#endif
863 }
864}
865
866//
867//////////////////////////////////////////////////////////////////////////////
868void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
869 RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
870
871 if ( decoder ) {
872 QMutexLocker locker(&_mutex);
873
874 decoder->storeEph(gpseph);
875 }
876}
877
Note: See TracBrowser for help on using the repository browser.