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

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

* empty log message *

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