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

Last change on this file since 1052 was 1052, checked in by weber, 16 years ago

* empty log message *

File size: 25.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#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 sumLatQ = 0.;
419 double meanDiff = 0.;
420 double minLat = maxDt;
421 double maxLat = -maxDt;
422 double curLat = 0.;
423
424 _decodeTime = QDateTime::currentDateTime();
425 _decodeSucc = QDateTime::currentDateTime();
426 t_irc irc = initRun();
427
428 if (irc == fatal) {
429 QThread::exit(1);
430 return;
431 }
432 else if (irc != success) {
433 emit(newMessage(_staID + ": initRun failed, reconnecting"));
434 tryReconnect();
435 }
436
437 if (initPause < _inspSegm) {
438 initPause = _inspSegm;
439 }
440 if(!_makePause) {initPause = 0;}
441 currPause = initPause;
442
443 // Read Incoming Data
444 // ------------------
445 while (true) {
446 try {
447 if (_socket->state() != QAbstractSocket::ConnectedState) {
448 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
449 tryReconnect();
450 }
451
452 QListIterator<p_obs> it(_decoder->_obsList);
453 while (it.hasNext()) {
454 delete it.next();
455 }
456 _decoder->_obsList.clear();
457
458 _socket->waitForReadyRead(_timeOut);
459 qint64 nBytes = _socket->bytesAvailable();
460 if (nBytes > 0) {
461 emit newBytes(_staID, nBytes);
462
463 char* data = new char[nBytes];
464 _socket->read(data, nBytes);
465
466 if (_inspSegm<1) {
467 _decoder->Decode(data, nBytes);
468 }
469 else {
470
471 // Decode data
472 // -----------
473 if (!_decodePause.isValid() ||
474 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
475
476 if (decode) {
477 if ( _decoder->Decode(data, nBytes) == success ) {
478 numSucc += 1;
479 }
480 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
481 decode = false;
482 }
483 }
484
485 // Check - once per inspect segment
486 // --------------------------------
487 if (!decode) {
488 _decodeTime = QDateTime::currentDateTime();
489 if (numSucc>0) {
490 secSucc += _inspSegm;
491 _decodeSucc = QDateTime::currentDateTime();
492 if (secSucc > _adviseReco * 60) {
493 secSucc = _adviseReco * 60 + 1;
494 }
495 numSucc = 0;
496 currPause = initPause;
497 _decodePause.setDate(QDate());
498 _decodePause.setTime(QTime());
499 }
500 else {
501 secFail += _inspSegm;
502 secSucc = 0;
503 if (secFail > _adviseFail * 60) {
504 secFail = _adviseFail * 60 + 1;
505 }
506 if (!_decodePause.isValid() || !_makePause) {
507 _decodePause = QDateTime::currentDateTime();
508 }
509 else {
510 _decodePause.setDate(QDate());
511 _decodePause.setTime(QTime());
512 secFail = secFail + currPause - _inspSegm;
513 currPause = currPause * 2;
514 if (currPause > 960) {
515 currPause = 960;
516 }
517 }
518 }
519
520 // End corrupt threshold
521 // ---------------------
522 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
523 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
524 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
525 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
526 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
527 endCorrupt = true;
528 begCorrupt = false;
529 secFail = 0;
530 }
531 else {
532
533 // Begin corrupt threshold
534 // -----------------------
535 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
536 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
537 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
538 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
539 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
540 begCorrupt = true;
541 endCorrupt = false;
542 secSucc = 0;
543 numSucc = 0;
544 }
545 }
546 decode = true;
547 }
548 }
549 }
550
551 // End outage threshold
552 // --------------------
553 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
554 _decodeStart.setDate(QDate());
555 _decodeStart.setTime(QTime());
556 if (_inspSegm>0) {
557 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
558 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
559 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
560 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
561 }
562 }
563
564 delete [] data;
565
566 QListIterator<p_obs> it(_decoder->_obsList);
567 while (it.hasNext()) {
568 p_obs obs = it.next();
569
570 // Check observation epoch
571 // -----------------------
572 int week;
573 double sec;
574 leapsecGPSWeeks(week, sec);
575 const double secPerWeek = 7.0 * 24.0 * 3600.0;
576
577 if (week < obs->_o.GPSWeek) {
578 week += 1;
579 sec -= secPerWeek;
580 }
581 if (week > obs->_o.GPSWeek) {
582 week -= 1;
583 sec += secPerWeek;
584 }
585 double dt = fabs(sec - obs->_o.GPSWeeks);
586 if (week != obs->_o.GPSWeek || dt > maxDt) {
587 if (!wrongEpoch) {
588 emit( newMessage(_staID + ": Wrong observation epoch(s)") );
589 wrongEpoch = true;
590 }
591 delete obs;
592 continue;
593 }
594 else {
595 wrongEpoch = false;
596
597 // Latency and completeness
598 // ------------------------
599 if (_perfIntr>0) {
600 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
601 if (newSecGPS != oldSecGPS) {
602 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
603 if (numLat>0) {
604 if (meanDiff>0.) {
605 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
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(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
611 .arg(numLat)
612 .arg(numGaps)
613 .toAscii()) );
614 } else {
615 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
616 .arg(_staID.data())
617 .arg(int(sumLat/numLat*100)/100.)
618 .arg(int(minLat*100)/100.)
619 .arg(int(maxLat*100)/100.)
620 .arg(int((sqrt((sumLatQ - sumLat * sumLat / numLat)/numLat))*100)/100.)
621 .arg(numLat)
622 .toAscii()) );
623 }
624 }
625 meanDiff = diffSecGPS/numLat;
626 diffSecGPS = 0;
627 numGaps = 0;
628 sumLat = 0.;
629 sumLatQ = 0.;
630 numLat = 0;
631 minLat = maxDt;
632 maxLat = -maxDt;
633 }
634 if (followSec) {
635 diffSecGPS += newSecGPS - oldSecGPS;
636 if (meanDiff>0.) {
637 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
638 numGaps += 1;
639 }
640 }
641 }
642 curLat = sec - obs->_o.GPSWeeks;
643 sumLat += curLat;
644 sumLatQ += curLat * curLat;
645 if (curLat < minLat) minLat = curLat;
646 if (curLat >= maxLat) maxLat = curLat;
647 numLat += 1;
648 oldSecGPS = newSecGPS;
649 followSec = true;
650 }
651 }
652 }
653
654 // RINEX Output
655 // ------------
656 if (_rnx) {
657 bool dump = true;
658
659 ////RTCM2Decoder* decoder2 = dynamic_cast<RTCM2Decoder*>(_decoder);
660 ////if ( decoder2 && !_rnx_set_position ) {
661 //// double stax, stay, staz;
662 //// if ( decoder2->getStaCrd(stax, stay, staz) == success ) {
663 //// _rnx->setApproxPos(stax, stay, staz);
664 //// _rnx_set_position = true;
665 //// }
666 //// else {
667 //// dump = false;
668 //// }
669 ////}
670
671 if ( dump ) {
672 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
673 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
674 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
675 _rnx->deepCopy(obs);
676 }
677 _rnx->dumpEpoch(newTime);
678 }
679 }
680
681 // Emit new observation signal
682 // ---------------------------
683 bool firstObs = (obs == _decoder->_obsList.first());
684 obs->_status = t_obs::posted;
685 emit newObs(_staID, firstObs, obs);
686 }
687 _decoder->_obsList.clear();
688
689 // RTCM message types
690 // ------------------
691 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
692 if (0<_decoder->_typeList.size()) {
693 QString type;
694 for (int ii=0;ii<_decoder->_typeList.size();ii++) {
695 type = QString("%1 ").arg(_decoder->_typeList[ii]);
696 if (type != "") {
697 emit(newMessage(_staID + ": Received message type " + type.toAscii() ));
698 }
699 }
700 }
701 }
702 _decoder->_typeList.clear();
703 }
704
705 // Timeout, reconnect
706 // ------------------
707 else {
708 emit(newMessage(_staID + ": Data Timeout, reconnecting"));
709 tryReconnect();
710 }
711 }
712 catch (const char* msg) {
713 emit(newMessage(_staID + msg));
714 tryReconnect();
715 }
716 }
717}
718
719// Exit
720////////////////////////////////////////////////////////////////////////////
721void bncGetThread::exit(int exitCode) {
722 if (exitCode!= 0) {
723 emit error(_staID);
724 }
725 QThread::exit(exitCode);
726 terminate();
727}
728
729// Try Re-Connect
730////////////////////////////////////////////////////////////////////////////
731void bncGetThread::tryReconnect() {
732 if (_rnx) {
733 _rnx->setReconnectFlag(true);
734 }
735 if ( !_decodeStart.isValid()) {
736 _decodeStop = QDateTime::currentDateTime();
737 }
738 while (1) {
739 delete _socket; _socket = 0;
740 sleep(_nextSleep);
741 if ( initRun() == success ) {
742 if ( !_decodeStop.isValid()) {
743 _decodeStart = QDateTime::currentDateTime();
744 }
745 break;
746 }
747 else {
748
749 // Begin outage threshold
750 // ----------------------
751 if ( _decodeStop.isValid() && _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
752 _decodeStop.setDate(QDate());
753 _decodeStop.setTime(QTime());
754 if (_inspSegm>0) {
755 _begDateOut = _decodeTime.toUTC().date().toString("yy-MM-dd");
756 _begTimeOut = _decodeTime.toUTC().time().toString("hh:mm:ss");
757 emit(newMessage((_staID + ": Failure threshold exceeded, outage since " + _begDateOut + " " + _begTimeOut).toAscii()));
758 callScript(("Begin_Outage " + _begDateOut + " " + _begTimeOut).toAscii());
759 }
760 }
761 _nextSleep *= 2;
762 if (_nextSleep > 256) {
763 _nextSleep = 256;
764 }
765 _nextSleep += rand() % 6;
766 }
767 }
768 _nextSleep = 1;
769}
770
771// Call advisory notice script
772////////////////////////////////////////////////////////////////////////////
773void bncGetThread::callScript(const char* _comment) {
774 QMutexLocker locker(&_mutex);
775 if (!_adviseScript.isEmpty()) {
776 msleep(1);
777#ifdef WIN32
778 QProcess::startDetached(_adviseScript, QStringList() << _staID << _comment) ;
779#else
780 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << _comment) ;
781#endif
782 }
783}
784
785//
786//////////////////////////////////////////////////////////////////////////////
787void bncGetThread::slotNewEphGPS(gpsephemeris gpseph) {
788 RTCM2Decoder* decoder = dynamic_cast<RTCM2Decoder*>(_decoder);
789
790 if ( decoder ) {
791 QMutexLocker locker(&_mutex);
792
793 decoder->storeEph(gpseph);
794 }
795}
796
Note: See TracBrowser for help on using the repository browser.