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

Last change on this file since 1144 was 1144, 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 if (_rawInpFile) {
430 ::exit(0);
431 }
432 else {
433 return fatal;
434 }
435 }
436 }
437 return success;
438}
439
440// Run
441////////////////////////////////////////////////////////////////////////////
442void bncGetThread::run() {
443
444 const double maxDt = 600.0; // Check observation epoch
445 bool wrongEpoch = false;
446 bool decode = true;
447 int numSucc = 0;
448 int secSucc = 0;
449 int secFail = 0;
450 int initPause = 30; // Initial pause for corrupted streams
451 int currPause = 0;
452 bool begCorrupt = false;
453 bool endCorrupt = false;
454 bool followSec = false;
455 int oldSecGPS= 0;
456 int newSecGPS = 0;
457 int numGaps = 0;
458 int diffSecGPS = 0;
459 int numLat = 0;
460 double sumLat = 0.;
461 double sumLatQ = 0.;
462 double meanDiff = 0.;
463 double minLat = maxDt;
464 double maxLat = -maxDt;
465 double curLat = 0.;
466
467 _decodeTime = QDateTime::currentDateTime();
468 _decodeSucc = QDateTime::currentDateTime();
469 t_irc irc = initRun();
470
471 if (irc == fatal) {
472 QThread::exit(1);
473 return;
474 }
475 else if (irc != success) {
476 emit(newMessage(_staID + ": initRun failed, reconnecting"));
477 tryReconnect();
478 }
479
480 if (initPause < _inspSegm) {
481 initPause = _inspSegm;
482 }
483 if(!_makePause) {initPause = 0;}
484 currPause = initPause;
485
486 // Read Incoming Data
487 // ------------------
488 while (true) {
489 try {
490 if (_socket && _socket->state() != QAbstractSocket::ConnectedState) {
491 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
492 tryReconnect();
493 }
494
495 QListIterator<p_obs> it(_decoder->_obsList);
496 while (it.hasNext()) {
497 delete it.next();
498 }
499 _decoder->_obsList.clear();
500
501 qint64 nBytes = 0;
502
503 if (_socket) {
504 _socket->waitForReadyRead(_timeOut);
505 nBytes = _socket->bytesAvailable();
506 }
507 else if (_rawInpFile) {
508 const qint64 maxBytes = 10000;
509 nBytes = maxBytes;
510 }
511
512 if (nBytes > 0) {
513 emit newBytes(_staID, nBytes);
514
515 char* data = new char[nBytes];
516
517 if (_socket) {
518 _socket->read(data, nBytes);
519 }
520 else if (_rawInpFile) {
521 nBytes = _rawInpFile->read(data, nBytes);
522 cout << "nBytes = " << nBytes << endl;
523 if (nBytes <= 0) {
524 cout << "no more data" << endl;
525 ::exit(0);
526 }
527 }
528
529 if (_rawOutFile) {
530 _rawOutFile->write(data, nBytes);
531 _rawOutFile->flush();
532 }
533
534 if (_inspSegm<1) {
535 _decoder->Decode(data, nBytes);
536 }
537 else {
538
539 // Decode data
540 // -----------
541 if (!_decodePause.isValid() ||
542 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
543
544 if (decode) {
545 if ( _decoder->Decode(data, nBytes) == success ) {
546 numSucc += 1;
547 }
548 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
549 decode = false;
550 }
551 }
552
553 // Check - once per inspect segment
554 // --------------------------------
555 if (!decode) {
556 _decodeTime = QDateTime::currentDateTime();
557 if (numSucc>0) {
558 secSucc += _inspSegm;
559 _decodeSucc = QDateTime::currentDateTime();
560 if (secSucc > _adviseReco * 60) {
561 secSucc = _adviseReco * 60 + 1;
562 }
563 numSucc = 0;
564 currPause = initPause;
565 _decodePause.setDate(QDate());
566 _decodePause.setTime(QTime());
567 }
568 else {
569 secFail += _inspSegm;
570 secSucc = 0;
571 if (secFail > _adviseFail * 60) {
572 secFail = _adviseFail * 60 + 1;
573 }
574 if (!_decodePause.isValid() || !_makePause) {
575 _decodePause = QDateTime::currentDateTime();
576 }
577 else {
578 _decodePause.setDate(QDate());
579 _decodePause.setTime(QTime());
580 secFail = secFail + currPause - _inspSegm;
581 currPause = currPause * 2;
582 if (currPause > 960) {
583 currPause = 960;
584 }
585 }
586 }
587
588 // End corrupt threshold
589 // ---------------------
590 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
591 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
592 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
593 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
594 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
595 endCorrupt = true;
596 begCorrupt = false;
597 secFail = 0;
598 }
599 else {
600
601 // Begin corrupt threshold
602 // -----------------------
603 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
604 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
605 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
606 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
607 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
608 begCorrupt = true;
609 endCorrupt = false;
610 secSucc = 0;
611 numSucc = 0;
612 }
613 }
614 decode = true;
615 }
616 }
617 }
618
619 // End outage threshold
620 // --------------------
621 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
622 _decodeStart.setDate(QDate());
623 _decodeStart.setTime(QTime());
624 if (_inspSegm>0) {
625 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
626 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
627 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
628 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
629 }
630 }
631
632 delete [] data;
633
634 QListIterator<p_obs> it(_decoder->_obsList);
635 while (it.hasNext()) {
636 p_obs obs = it.next();
637
638 // Check observation epoch
639 // -----------------------
640 if (!_rawInpFile) {
641 int week;
642 double sec;
643 leapsecGPSWeeks(week, sec);
644 const double secPerWeek = 7.0 * 24.0 * 3600.0;
645
646 if (week < obs->_o.GPSWeek) {
647 week += 1;
648 sec -= secPerWeek;
649 }
650 if (week > obs->_o.GPSWeek) {
651 week -= 1;
652 sec += secPerWeek;
653 }
654 double dt = fabs(sec - obs->_o.GPSWeeks);
655 if (week != obs->_o.GPSWeek || dt > maxDt) {
656 if (!wrongEpoch) {
657 emit( newMessage(_staID + ": Wrong observation epoch(s)") );
658 wrongEpoch = true;
659 }
660 delete obs;
661 continue;
662 }
663 else {
664 wrongEpoch = false;
665
666 // Latency and completeness
667 // ------------------------
668 if (_perfIntr>0) {
669 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
670 if (newSecGPS != oldSecGPS) {
671 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
672 if (numLat>0) {
673 if (meanDiff>0.) {
674 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
675 .arg(_staID.data())
676 .arg(int(sumLat/numLat*100)/100.)
677 .arg(int(minLat*100)/100.)
678 .arg(int(maxLat*100)/100.)
679 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
680 .arg(numLat)
681 .arg(numGaps)
682 .toAscii()) );
683 } else {
684 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
685 .arg(_staID.data())
686 .arg(int(sumLat/numLat*100)/100.)
687 .arg(int(minLat*100)/100.)
688 .arg(int(maxLat*100)/100.)
689 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
690 .arg(numLat)
691 .toAscii()) );
692 }
693 }
694 meanDiff = diffSecGPS/numLat;
695 diffSecGPS = 0;
696 numGaps = 0;
697 sumLat = 0.;
698 sumLatQ = 0.;
699 numLat = 0;
700 minLat = maxDt;
701 maxLat = -maxDt;
702 }
703 if (followSec) {
704 diffSecGPS += newSecGPS - oldSecGPS;
705 if (meanDiff>0.) {
706 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
707 numGaps += 1;
708 }
709 }
710 }
711 curLat = sec - obs->_o.GPSWeeks;
712 sumLat += curLat;
713 sumLatQ += curLat * curLat;
714 if (curLat < minLat) minLat = curLat;
715 if (curLat >= maxLat) maxLat = curLat;
716 numLat += 1;
717 oldSecGPS = newSecGPS;
718 followSec = true;
719 }
720 }
721 }
722 }
723
724 // RINEX Output
725 // ------------
726 if (_rnx) {
727 bool dump = true;
728
729 ////RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
730 ////if ( decoder2 && !_rnx_set_position ) {
731 //// double stax, stay, staz;
732 //// if ( decoder2->getStaCrd(stax, stay, staz) == success ) {
733 //// _rnx->setApproxPos(stax, stay, staz);
734 //// _rnx_set_position = true;
735 //// }
736 //// else {
737 //// dump = false;
738 //// }
739 ////}
740
741 if ( dump ) {
742 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
743 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
744 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
745 _rnx->deepCopy(obs);
746 }
747 _rnx->dumpEpoch(newTime);
748 }
749 }
750
751 // Emit new observation signal
752 // ---------------------------
753 bool firstObs = (obs == _decoder->_obsList.first());
754 obs->_status = t_obs::posted;
755 emit newObs(_staID, firstObs, obs);
756 }
757 _decoder->_obsList.clear();
758
759 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
760
761 // RTCM message types
762 // ------------------
763 if (0<_decoder->_typeList.size()) {
764 QString type;
765 for (int ii=0;ii<_decoder->_typeList.size();ii++) {
766 type = QString("%1 ").arg(_decoder->_typeList[ii]);
767 emit(newMessage(_staID + ": Received message type " + type.toAscii() ));
768 }
769 }
770 _decoder->_typeList.clear();
771
772 // Antenna XYZ & H
773 // ---------------
774 if (0<_decoder->_antList.size()) {
775 QString ant1,ant2,ant3,ant4;
776 for (int ii=0;ii<_decoder->_antList.size();ii+=4) {
777 ant1 = QString("%1 ").arg(_decoder->_antList[ii+0]*0.0001,0,'f',4);
778 ant2 = QString("%1 ").arg(_decoder->_antList[ii+1]*0.0001,0,'f',4);
779 ant3 = QString("%1 ").arg(_decoder->_antList[ii+2]*0.0001,0,'f',4);
780 ant4 = QString("%1 ").arg(_decoder->_antList[ii+3]*0.0001,0,'f',4);
781 emit(newMessage(_staID + ": ARP (ITRF) X " + ant1.toAscii() + "m" ));
782 emit(newMessage(_staID + ": ARP (ITRF) Y " + ant2.toAscii() + "m"));
783 emit(newMessage(_staID + ": ARP (ITRF) Z " + ant3.toAscii() + "m"));
784 emit(newMessage(_staID + ": Antenna height " + ant4.toAscii() + "m"));
785 }
786 }
787 _decoder->_antList.clear();
788 }
789 }
790
791 // Timeout, reconnect
792 // ------------------
793 else {
794 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
795 tryReconnect();
796 }
797 }
798 catch (const char* msg) {
799 emit(newMessage(_staID + msg));
800 tryReconnect();
801 }
802 }
803}
804
805// Exit
806////////////////////////////////////////////////////////////////////////////
807void bncGetThread::exit(int exitCode) {
808 if (exitCode!= 0) {
809 emit error(_staID);
810 }
811 QThread::exit(exitCode);
812 terminate();
813}
814
815// Try Re-Connect
816////////////////////////////////////////////////////////////////////////////
817void bncGetThread::tryReconnect() {
818 if (_rnx) {
819 _rnx->setReconnectFlag(true);
820 }
821 if ( !_decodeStart.isValid()) {
822 _decodeStop = QDateTime::currentDateTime();
823 }
824 while (1) {
825 delete _socket; _socket = 0;
826 sleep(_nextSleep);
827 if ( initRun() == success ) {
828 if ( !_decodeStop.isValid()) {
829 _decodeStart = QDateTime::currentDateTime();
830 }
831 break;
832 }
833 else {
834
835 // Begin outage threshold
836 // ----------------------
837 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
838 _decodeStop.setDate(QDate());
839 _decodeStop.setTime(QTime());
840 if (_inspSegm>0) {
841 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
842 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
843 emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
844 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
845 }
846 }
847 _nextSleep *= 2;
848 if (_nextSleep > 256) {
849 _nextSleep = 256;
850 }
851 _nextSleep += rand() % 6;
852 }
853 }
854 _nextSleep = 1;
855}
856
857// Call advisory notice script
858////////////////////////////////////////////////////////////////////////////
859void bncGetThread::callScript(const char* _comment) {
860 QMutexLocker locker(&_mutex);
861 if (!_adviseScript.isEmpty()) {
862 msleep(1);
863#ifdef WIN32
864 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
865#else
866 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
867#endif
868 }
869}
870
871//
872//////////////////////////////////////////////////////////////////////////////
873void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
874 RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
875
876 if ( decoder ) {
877 QMutexLocker locker(&_mutex);
878
879 decoder->storeEph(gpseph);
880 }
881}
882
Note: See TracBrowser for help on using the repository browser.