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

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

* empty log message *

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