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

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

* empty log message *

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