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

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

* empty log message *

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