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

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

* empty log message *

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