source: ntrip/trunk/BNC/src/latencychecker.cpp@ 10586

Last change on this file since 10586 was 10586, checked in by stuerze, 3 days ago

minor changes

File size: 18.3 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: latencyChecker
30 *
31 * Purpose: Check incoming GNSS data for latencies, gaps etc.
32 *
33 * Author: G. Weber
34 *
35 * Created: 02-Feb-2009
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42
43#ifdef WIN32
44#include <windows.h>
45#else
46#include <unistd.h>
47#endif
48
49#include "latencychecker.h"
50#include "bnccore.h"
51#include "bncutils.h"
52#include "bncsettings.h"
53
54using namespace std;
55
56// Constructor
57//////////////////////////////////////////////////////////////////////////////
58latencyChecker::latencyChecker(QByteArray staID) {
59
60 _staID = staID;
61 _curLat = 0.0;
62
63 connect(this, SIGNAL(newMessage(QByteArray,bool)),
64 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
65
66 bncSettings settings;
67
68 // Notice threshold
69 // ----------------
70 QString adviseObsRate = settings.value("adviseObsRate").toString();
71 _inspSegm = 0;
72 if ( adviseObsRate.isEmpty() ) {
73 _inspSegm = 0;
74 }
75 else if ( adviseObsRate.indexOf("5 Hz") != -1 ) {
76 _inspSegm = 20;
77 }
78 else if ( adviseObsRate.indexOf("1 Hz") != -1 ) {
79 _inspSegm = 10;
80 }
81 else if ( adviseObsRate.indexOf("0.5 Hz") != -1 ) {
82 _inspSegm = 20;
83 }
84 else if ( adviseObsRate.indexOf("0.2 Hz") != -1 ) {
85 _inspSegm = 40;
86 }
87 else if ( adviseObsRate.indexOf("0.1 Hz") != -1 ) {
88 _inspSegm = 50;
89 }
90 _adviseFail = settings.value("adviseFail").toInt();
91 _adviseReco = settings.value("adviseReco").toInt();
92 _adviseScript = settings.value("adviseScript").toString();
93 expandEnvVar(_adviseScript);
94
95 // Latency interval/average
96 // ------------------------
97 _miscIntr = 300;
98 QString miscIntr = settings.value("miscIntr").toString();
99 if ( miscIntr.isEmpty() ) {
100 _miscIntr = 300;
101 }
102 else if ( miscIntr.indexOf("2 sec") != -1 ) {
103 _miscIntr = 2;
104 }
105 else if ( miscIntr.indexOf("10 sec") != -1 ) {
106 _miscIntr = 10;
107 }
108 else if ( miscIntr.indexOf("1 min") != -1 ) {
109 _miscIntr = 60;
110 }
111 else if ( miscIntr.left(5).indexOf("5 min") != -1 ) {
112 _miscIntr = 300;
113 }
114 else if ( miscIntr.indexOf("15 min") != -1 ) {
115 _miscIntr = 900;
116 }
117 else if ( miscIntr.indexOf("1 hour") != -1 ) {
118 _miscIntr = 3600;
119 }
120 else if ( miscIntr.indexOf("6 hours") != -1 ) {
121 _miscIntr = 21600;
122 }
123 else if ( miscIntr.indexOf("1 day") != -1 ) {
124 _miscIntr = 86400;
125 }
126
127 // RTCM message types
128 // ------------------
129 _checkMountPoint = settings.value("miscMount").toString();
130
131 // Initialize private members
132 // --------------------------
133 _wrongEpoch = false;
134 _checkSeg = false;
135 _numSucc = 0;
136 _secSucc = 0;
137 _secFail = 0;
138 _initPause = 0;
139 _currPause = 0;
140 _endCorrupt = false;
141 _begCorrupt = false;
142 _fromCorrupt = false;
143
144 _checkTime = QDateTime::currentDateTime();
145 _decodeSucc = QDateTime::currentDateTime();
146
147 _decodeStop = QDateTime::currentDateTime();
148 _begDateTimeOut = QDateTime::currentDateTime();
149 _endDateTimeOut = QDateTime::currentDateTime();
150 _fromReconnect = false;
151
152 _decodeStopCorr = QDateTime::currentDateTime();
153 _begDateTimeCorr = QDateTime::currentDateTime();
154 _endDateTimeCorr = QDateTime::currentDateTime();
155}
156
157// Destructor
158//////////////////////////////////////////////////////////////////////////////
159latencyChecker::~latencyChecker() {
160}
161
162// Perform 'Begin outage' check
163//////////////////////////////////////////////////////////////////////////////
164void latencyChecker::checkReconnect() {
165
166 if (_inspSegm == 0) { return;}
167
168 // Begin outage threshold
169 // ----------------------
170 if (!_fromReconnect) {
171 _endDateTimeOut = QDateTime::currentDateTime();
172 }
173 _fromReconnect = true;
174
175 if ( _decodeStop.isValid() ) {
176 _begDateTimeOut = QDateTime::currentDateTime();
177 if ( _endDateTimeOut.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
178 _begDateOut = _endDateTimeOut.toUTC().date().toString("yy-MM-dd");
179 _begTimeOut = _endDateTimeOut.toUTC().time().toString("hh:mm:ss");
180 emit(newMessage((_staID + ": Failure threshold exceeded, outage since "
181 + _begDateOut + " " + _begTimeOut + " UTC").toLatin1(), true));
182 callScript(("Begin_Outage "
183 + _begDateOut + " " + _begTimeOut + " UTC").toLatin1());
184 _decodeStop.setDate(QDate());
185 _decodeStop.setTime(QTime());
186 _decodeStart = QDateTime::currentDateTime();
187 }
188 }
189}
190
191// Perform Corrupt and 'End outage' check
192//////////////////////////////////////////////////////////////////////////////
193void latencyChecker::checkOutage(bool decoded) {
194
195 if (_inspSegm == 0) { return;}
196
197 if (decoded) { _numSucc += 1; }
198
199 if (!_checkPause.isValid() || _checkPause.secsTo(QDateTime::currentDateTime()) >= _currPause ) {
200 if (!_checkSeg) {
201 if ( _checkTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
202 _checkSeg = true;
203 }
204 }
205
206 // Check - once per inspect segment
207 // --------------------------------
208 if (_checkSeg) {
209
210 _checkTime = QDateTime::currentDateTime();
211
212 if (_numSucc > 0) {
213 _secSucc += _inspSegm;
214 _secFail = 0;
215 _decodeSucc = QDateTime::currentDateTime();
216 if (_secSucc > _adviseReco * 60) {
217 _secSucc = _adviseReco * 60 + 1;
218 }
219 _numSucc = 0;
220 _currPause = _initPause;
221 _checkPause.setDate(QDate());
222 _checkPause.setTime(QTime());
223 }
224 else {
225 _secFail += _inspSegm;
226 _secSucc = 0;
227 if (_secFail > _adviseFail * 60) {
228 _secFail = _adviseFail * 60 + 1;
229 }
230 if (!_checkPause.isValid()) {
231 _checkPause = QDateTime::currentDateTime();
232 }
233 else {
234 _checkPause.setDate(QDate());
235 _checkPause.setTime(QTime());
236 _secFail = _secFail + _currPause - _inspSegm;
237 _currPause = _currPause * 2;
238 if (_currPause > 960) {
239 _currPause = 960;
240 }
241 }
242 }
243
244 // Begin corrupt threshold
245 // -----------------------
246 if (_secSucc > 0) {
247 _endDateTimeCorr = QDateTime::currentDateTime();
248 }
249
250 if (_secFail > 0) {
251 _begDateTimeCorr = QDateTime::currentDateTime();
252 }
253
254 if ( _decodeStopCorr.isValid() ) {
255 _begDateTimeCorr = QDateTime::currentDateTime();
256 if ( _endDateTimeCorr.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
257 _begDateCorr = _endDateTimeCorr.toUTC().date().toString("yy-MM-dd");
258 _begTimeCorr = _endDateTimeCorr.toUTC().time().toString("hh:mm:ss");
259 emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since "
260 + _begDateCorr + " " + _begTimeCorr + " UTC").toLatin1(), true));
261 callScript(("Begin_Corrupted "
262 + _begDateCorr + " " + _begTimeCorr + " UTC").toLatin1());
263 _secSucc = 0;
264 _numSucc = 0;
265 _decodeStopCorr.setDate(QDate());
266 _decodeStopCorr.setTime(QTime());
267 _decodeStartCorr = QDateTime::currentDateTime();
268 }
269 }
270 else {
271
272 // End corrupt threshold
273 // ---------------------
274 if ( _decodeStartCorr.isValid() ) {
275 _endDateTimeCorr = QDateTime::currentDateTime();
276 if ( _begDateTimeCorr.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
277 _endDateCorr = _begDateTimeCorr.toUTC().date().toString("yy-MM-dd");
278 _endTimeCorr = _begDateTimeCorr.toUTC().time().toString("hh:mm:ss");
279 emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended "
280 + _endDateCorr + " " + _endTimeCorr + " UTC").toLatin1(), true));
281 callScript(("End_Corrupted "
282 + _endDateCorr + " " + _endTimeCorr + " UTC Begin was "
283 + _begDateCorr + " " + _begTimeCorr + " UTC").toLatin1());
284 _decodeStartCorr.setDate(QDate());
285 _decodeStartCorr.setTime(QTime());
286 _decodeStopCorr = QDateTime::currentDateTime();
287 _secFail = 0;
288 }
289 }
290 }
291 _checkSeg = false;
292 }
293 }
294
295 // End outage threshold
296 // --------------------
297 if (_fromReconnect) {
298 _begDateTimeOut = QDateTime::currentDateTime();
299 }
300 _fromReconnect = false;
301
302 if ( _decodeStart.isValid() ) {
303 _endDateTimeOut = QDateTime::currentDateTime();
304 if ( _begDateTimeOut.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
305 _endDateOut = _begDateTimeOut.toUTC().date().toString("yy-MM-dd");
306 _endTimeOut = _begDateTimeOut.toUTC().time().toString("hh:mm:ss");
307 emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended "
308 + _endDateOut + " " + _endTimeOut + " UTC").toLatin1(), true));
309 callScript(("End_Outage "
310 + _endDateOut + " " + _endTimeOut + " UTC Begin was "
311 + _begDateOut + " " + _begTimeOut + " UTC").toLatin1());
312 _decodeStart.setDate(QDate());
313 _decodeStart.setTime(QTime());
314 _decodeStop = QDateTime::currentDateTime();
315 }
316 }
317}
318
319// Perform latency checks (observations)
320//////////////////////////////////////////////////////////////////////////////
321void latencyChecker::checkObsLatency(const QList<t_satObs>& obsList) {
322
323 if (_miscIntr > 0 ) {
324 t_latency& l = _lObs;
325 l._type = "Observations";
326 QListIterator<t_satObs> it(obsList);
327 while (it.hasNext()) {
328 const t_satObs& obs = it.next();
329 bool wrongObservationEpoch = checkForWrongObsEpoch(obs._time);
330 l._newSec = static_cast<int>(nint(obs._time.gpssec()*10));
331 if (l._newSec > l._oldSec && !wrongObservationEpoch) {
332 if (l._newSec % (_miscIntr * 10) < l._oldSec % (_miscIntr * 10)) {
333 if (l._numLat > 0) {
334 if (l._meanDiff > 0.0) {
335 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
336 emit( newMessage(QString("%1 %2: Mean latency %3 sec, min %4, max %5, rms %6, %7 epochs, %8 gaps")
337 .arg(_staID.data())
338 .arg(l._type.data())
339 .arg(int(l._sumLat/l._numLat*100)/100., 4, 'f', 2)
340 .arg(int(l._minLat*100)/100., 4, 'f', 2)
341 .arg(int(l._maxLat*100)/100., 4, 'f', 2)
342 .arg(int((sqrt(l._sumLatQ / l._numLat))*100)/100., 4, 'f', 2)
343 .arg(l._numLat)
344 .arg(l._numGaps)
345 .toLatin1(), true) );
346 }
347 } else {
348 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
349 emit( newMessage(QString("%1 %2: Mean latency %3 sec, min %4, max %5, rms %6, %7 epochs")
350 .arg(_staID.data())
351 .arg(l._type.data())
352 .arg(int(l._sumLat/l._numLat*100)/100., 4, 'f', 2)
353 .arg(int(l._minLat*100)/100., 4, 'f', 2)
354 .arg(int(l._maxLat*100)/100., 4, 'f', 2)
355 .arg(int((sqrt(l._sumLatQ / l._numLat))*100)/100., 4, 'f', 2)
356 .arg(l._numLat)
357 .toLatin1(), true) );
358 }
359 }
360 l._meanDiff = l._diffSec / l._numLat;
361 l.init();
362 }
363 }
364 if (l._followSec) {
365 l._diffSec += l._newSec - l._oldSec;
366 if (l._meanDiff > 0.0) {
367 if (l._newSec - l._oldSec > 1.5 * l._meanDiff) {
368 l._numGaps += 1;
369 }
370 }
371 }
372
373 // Compute the observations latency
374 // --------------------------------
375 int week;
376 double sec;
377 currentGPSWeeks(week, sec);
378 const double secPerWeek = 7.0 * 24.0 * 3600.0;
379 if (week < int(obs._time.gpsw())) {
380 week += 1;
381 sec -= secPerWeek;
382 }
383 if (week > int(obs._time.gpsw())) {
384 week -= 1;
385 sec += secPerWeek;
386 }
387 l._curLat = sec - obs._time.gpssec();
388 l._sumLat += l._curLat;
389 l._sumLatQ += l._curLat * l._curLat;
390 if (l._curLat < l._minLat) {
391 l._minLat = l._curLat;
392 }
393 if (l._curLat >= l._maxLat) {
394 l._maxLat = l._curLat;
395 }
396 l._numLat += 1;
397 l._followSec = true;
398 }
399 l._oldSec = l._newSec;
400 }
401 _lObs = l;
402 setCurrentLatency(l._curLat);
403 }
404}
405
406// Perform latency checks (corrections)
407//////////////////////////////////////////////////////////////////////////////
408void latencyChecker::checkCorrLatency(int corrGPSEpochTime, int type) {
409 if (corrGPSEpochTime < 0) {
410 return;
411 }
412 t_latency& l = _lOrb; // init
413 switch (type) {
414 case 1057: case 1063: case 1240: case 1246: case 1252: case 1258:
415 l = _lOrb;
416 l._type = "RtcmSsrOrbit";
417 break;
418 case 1058: case 1064: case 1241: case 1247: case 1253: case 1259:
419 l = _lClk;
420 l._type = "RtcmSsrClock";
421 break;
422 case 1060: case 1066: case 1243: case 1249: case 1255: case 1261:
423 l = _lClkOrb;
424 l._type = "RtcmSsrClock&Orbit";
425 break;
426 case 1059: case 1065: case 1242: case 1248: case 1254: case 1260:
427 l = _lCb;
428 l._type = "RtcmSsrCodeBiases";
429 break;
430 case 1265: case 1266: case 1267: case 1268: case 1269: case 1270:
431 l = _lPb;
432 l._type = "RtcmSsrPhaseBiases";
433 break;
434 case 1264:
435 l = _lVtec;
436 l._type = "RtcmSsrVTEC";
437 break;
438 case 1061: case 1067: case 1244: case 1250: case 1256: case 1262:
439 l = _lUra;
440 l._type = "RtcmSsrURA";
441 break;
442 case 1062: case 1068: case 1245: case 1251: case 1257: case 1263:
443 l = _lHr;
444 l._type = "RtcmSsrHrClock";
445 break;
446 case 4076:
447 l = _lSsrIgs;
448 l._type = "RtcmSsrIgs4076";
449 break;
450 default:
451 return;
452 }
453
454 if (_miscIntr > 0) {
455 l._newSec = corrGPSEpochTime;
456 if (l._newSec > l._oldSec) {
457 if (int(l._newSec) % _miscIntr < int(l._oldSec) % _miscIntr) {
458 if (l._numLat>0) {
459 QString late;
460 if (l._meanDiff>0.) {
461 late = QString(" %1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
462 .arg(l._type.data())
463 .arg(int(l._sumLat/l._numLat*100)/100., 4, 'f', 2)
464 .arg(int(l._minLat*100)/100., 4, 'f', 2)
465 .arg(int(l._maxLat*100)/100., 4, 'f', 2)
466 .arg(int((sqrt(l._sumLatQ / l._numLat))*100)/100., 4, 'f', 2)
467 .arg(l._numLat)
468 .arg(l._numGaps);
469 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
470 emit(newMessage(QString(_staID + late ).toLatin1(), true) );
471 }
472 }
473 else {
474 late = QString(" %1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
475 .arg(l._type.data())
476 .arg(int(l._sumLat/l._numLat*100)/100., 4, 'f', 2)
477 .arg(int(l._minLat*100)/100., 4, 'f', 2)
478 .arg(int(l._maxLat*100)/100., 4, 'f', 2)
479 .arg(int((sqrt(l._sumLatQ / l._numLat))*100)/100., 4, 'f', 2)
480 .arg(l._numLat);
481 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
482 emit(newMessage(QString(_staID + late ).toLatin1(), true) );
483 }
484 }
485 }
486 l._meanDiff = int(l._diffSec)/l._numLat;
487 l.init();
488 }
489 if (l._followSec) {
490 l._diffSec += l._newSec - l._oldSec;
491 if (l._meanDiff>0.) {
492 if (l._newSec - l._oldSec > 1.5 * l._meanDiff) {
493 l._numGaps += 1;
494 }
495 }
496 }
497
498 // Compute the observations latency
499 // --------------------------------
500 int week;
501 double sec;
502 currentGPSWeeks(week, sec);
503 double dt = fabs(sec - l._newSec);
504 const double secPerWeek = 7.0 * 24.0 * 3600.0;
505 if (dt > 0.5 * secPerWeek) {
506 if (sec > l._newSec) {
507 sec -= secPerWeek;
508 } else {
509 sec += secPerWeek;
510 }
511 }
512 l._curLat = sec - l._newSec;
513 l._sumLat += l._curLat;
514 l._sumLatQ += l._curLat * l._curLat;
515 if (l._curLat < l._minLat) {
516 l._minLat = l._curLat;
517 }
518 if (l._curLat >= l._maxLat) {
519 l._maxLat = l._curLat;
520 }
521 l._numLat += 1;
522 l._followSec = true;
523 setCurrentLatency(l._curLat);
524 }
525 l._oldSec = l._newSec;
526 }
527 switch (type) {
528 case 1057: case 1063: case 1240: case 1246: case 1252: case 1258:
529 _lOrb = l;
530 break;
531 case 1058: case 1064: case 1241: case 1247: case 1253: case 1259:
532 _lClk = l;
533 break;
534 case 1060: case 1066: case 1243: case 1249: case 1255: case 1261:
535 _lClkOrb = l;
536 break;
537 case 1059: case 1065: case 1242: case 1248: case 1254: case 1260:
538 _lCb = l;
539 break;
540 case 1265: case 1266: case 1267: case 1268: case 1269: case 1270:
541 _lPb = l;
542 break;
543 case 1264:
544 _lVtec = l;
545 break;
546 case 1061: case 1067: case 1244: case 1250: case 1256: case 1262:
547 _lUra = l;
548 break;
549 case 1062: case 1068: case 1245: case 1251: case 1257: case 1263:
550 _lHr = l;
551 break;
552 case 4076:
553 _lSsrIgs = l;
554 break;
555 }
556}
557
558// Call advisory notice script
559////////////////////////////////////////////////////////////////////////////
560void latencyChecker::callScript(const char* comment) {
561 if (!_adviseScript.isEmpty()) {
562#ifdef WIN32
563 Sleep(1);
564 QProcess::startDetached(_adviseScript, QStringList() << _staID << comment) ;
565#else
566 sleep(1);
567 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << comment) ;
568#endif
569 }
570}
Note: See TracBrowser for help on using the repository browser.