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

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