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

Last change on this file since 884 was 880, checked in by mervart, 17 years ago

* empty log message *

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