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

Last change on this file since 1024 was 1024, checked in by zdenek, 16 years ago

Zdenek Lukes: added logic for decoding of message 20/21 from RTCM 2.3 streams

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