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

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

Zdenek Lukes: a) added logic for RTCM 2.3 messages 20/21 decoding

b) added logic for cycle slip flags (slip counters or lock time indicators) handling

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