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

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

* empty log message *

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