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

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

* empty log message *

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