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

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

* empty log message *

File size: 24.4 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
421 _decodeTime = QDateTime::currentDateTime();
422 _decodeSucc = QDateTime::currentDateTime();
423 t_irc irc = initRun();
424
425 if (irc == fatal) {
426 QThread::exit(1);
427 return;
428 }
429 else if (irc != success) {
430 emit(newMessage(_staID + ": initRun failed, reconnecting"));
431 tryReconnect();
432 }
433
434 if (initPause < _inspSegm) {
435 initPause = _inspSegm;
436 }
437 if(!_makePause) {initPause = 0;}
438 currPause = initPause;
439
440 // Read Incoming Data
441 // ------------------
442 while (true) {
443 try {
444 if (_socket->state() != QAbstractSocket::ConnectedState) {
445 emit(newMessage(_staID + ": Socket not connected, reconnecting"));
446 tryReconnect();
447 }
448
449 QListIterator<p_obs> it(_decoder->_obsList);
450 while (it.hasNext()) {
451 delete it.next();
452 }
453 _decoder->_obsList.clear();
454
455 _socket->waitForReadyRead(_timeOut);
456 qint64 nBytes = _socket->bytesAvailable();
457 if (nBytes > 0) {
458 emit newBytes(_staID, nBytes);
459
460 char* data = new char[nBytes];
461 _socket->read(data, nBytes);
462
463 if (_inspSegm<1) {
464 _decoder->Decode(data, nBytes);
465 }
466 else {
467
468 // Decode data
469 // -----------
470 if (!_decodePause.isValid() ||
471 _decodePause.secsTo(QDateTime::currentDateTime()) >= currPause ) {
472
473 if (decode) {
474 if ( _decoder->Decode(data, nBytes) == success ) {
475 numSucc += 1;
476 }
477 if ( _decodeTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
478 decode = false;
479 }
480 }
481
482 // Check - once per inspect segment
483 // --------------------------------
484 if (!decode) {
485 _decodeTime = QDateTime::currentDateTime();
486 if (numSucc>0) {
487 secSucc += _inspSegm;
488 _decodeSucc = QDateTime::currentDateTime();
489 if (secSucc > _adviseReco * 60) {
490 secSucc = _adviseReco * 60 + 1;
491 }
492 numSucc = 0;
493 currPause = initPause;
494 _decodePause.setDate(QDate());
495 _decodePause.setTime(QTime());
496 }
497 else {
498 secFail += _inspSegm;
499 secSucc = 0;
500 if (secFail > _adviseFail * 60) {
501 secFail = _adviseFail * 60 + 1;
502 }
503 if (!_decodePause.isValid() || !_makePause) {
504 _decodePause = QDateTime::currentDateTime();
505 }
506 else {
507 _decodePause.setDate(QDate());
508 _decodePause.setTime(QTime());
509 secFail = secFail + currPause - _inspSegm;
510 currPause = currPause * 2;
511 if (currPause > 960) {
512 currPause = 960;
513 }
514 }
515 }
516
517 // End corrupt threshold
518 // ---------------------
519 if ( begCorrupt && !endCorrupt && secSucc > _adviseReco * 60 ) {
520 _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
521 _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
522 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended " + _endDateCor + " " + _endTimeCor).toAscii()));
523 callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
524 endCorrupt = true;
525 begCorrupt = false;
526 secFail = 0;
527 }
528 else {
529
530 // Begin corrupt threshold
531 // -----------------------
532 if ( !begCorrupt && secFail > _adviseFail * 60 ) {
533 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
534 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
535 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since " + _begDateCor + " " + _begTimeCor).toAscii()));
536 callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
537 begCorrupt = true;
538 endCorrupt = false;
539 secSucc = 0;
540 numSucc = 0;
541 }
542 }
543 decode = true;
544 }
545 }
546 }
547
548 // End outage threshold
549 // --------------------
550 if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
551 _decodeStart.setDate(QDate());
552 _decodeStart.setTime(QTime());
553 if (_inspSegm>0) {
554 _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
555 _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
556 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended " + _endDateOut + " " + _endTimeOut).toAscii()));
557 callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
558 }
559 }
560
561 delete [] data;
562
563 QListIterator<p_obs> it(_decoder->_obsList);
564 while (it.hasNext()) {
565 p_obs obs = it.next();
566
567 // Check observation epoch
568 // -----------------------
569 int week;
570 double sec;
571 leapsecGPSWeeks(week, sec);
572 const double secPerWeek = 7.0 * 24.0 * 3600.0;
573
574 if (week < obs->_o.GPSWeek) {
575 week += 1;
576 sec -= secPerWeek;
577 }
578 if (week > obs->_o.GPSWeek) {
579 week -= 1;
580 sec += secPerWeek;
581 }
582 double dt = fabs(sec - obs->_o.GPSWeeks);
583 if (week != obs->_o.GPSWeek || dt > maxDt) {
584 if (!wrongEpoch) {
585 emit( newMessage(_staID + ": Wrong observation epoch(s)") );
586 wrongEpoch = true;
587 }
588 delete obs;
589 continue;
590 }
591 else {
592 wrongEpoch = false;
593
594 // Latency and completeness
595 // ------------------------
596 if (_perfIntr>0) {
597 newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
598 if (newSecGPS != oldSecGPS) {
599 if (newSecGPS % _perfIntr < oldSecGPS % _perfIntr) {
600 if (numLat>0) {
601 if (meanDiff>0.) {
602 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs, %6 gaps")
603 .arg(_staID.data())
604 .arg(int(sumLat/numLat*100)/100.)
605 .arg(int(minLat*100)/100.)
606 .arg(int(maxLat*100)/100.)
607 .arg(numLat)
608 .arg(numGaps)
609 .toAscii()) );
610 } else {
611 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, %5 epochs")
612 .arg(_staID.data())
613 .arg(int(sumLat/numLat*100)/100.)
614 .arg(int(minLat*100)/100.)
615 .arg(int(maxLat*100)/100.)
616 .arg(numLat)
617 .toAscii()) );
618 }
619 }
620 meanDiff = diffSecGPS/numLat;
621 diffSecGPS = 0;
622 numGaps = 0;
623 sumLat = 0.;
624 numLat = 0;
625 minLat = maxDt;
626 maxLat = -maxDt;
627 }
628 if (followSec) {
629 diffSecGPS += newSecGPS - oldSecGPS;
630 if (meanDiff>0.) {
631 if (newSecGPS - oldSecGPS > 1.5 * meanDiff) {
632 numGaps += 1;
633 }
634 }
635 }
636 curLat = sec - obs->_o.GPSWeeks;
637 sumLat += curLat;
638 if (curLat < minLat) minLat = curLat;
639 if (curLat >= maxLat) maxLat = curLat;
640 numLat += 1;
641 oldSecGPS = newSecGPS;
642 followSec = true;
643 }
644 }
645 }
646
647 // RINEX Output
648 // ------------
649 if (_rnx) {
650 long iSec = long(floor(obs->_o.GPSWeeks+0.5));
651 long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
652 if (_samplingRate == 0 || iSec % _samplingRate == 0) {
653 _rnx->deepCopy(obs);
654 }
655 _rnx->dumpEpoch(newTime);
656 }
657
658 bool firstObs = (obs == _decoder->_obsList.first());
659 obs->_status = t_obs::posted;
660 emit newObs(_staID, firstObs, obs);
661 }
662 _decoder->_obsList.clear();
663
664 // RTCM message types
665 // ------------------
666 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
667 if (0<_decoder->_typeList.size()) {
668 QString type;
669 for (int ii=0;ii<_decoder->_typeList.size();ii++) {
670 type = QString("%1 ").arg(_decoder->_typeList[ii]);
671 if (type != "") {
672 emit(newMessage(_staID + ": Received message type " + type.toAscii() ));
673 }
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.