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

Last change on this file since 6645 was 6645, checked in by weber, 9 years ago

Work on fixing Failure/Recovery Advise bug

File size: 15.2 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
62 connect(this, SIGNAL(newMessage(QByteArray,bool)),
63 BNC_CORE, SLOT(slotMessage(const QByteArray,bool)));
64
65 bncSettings settings;
66
67 // Notice threshold
68 // ----------------
69 QString obsRate = settings.value("obsRate").toString();
70 _inspSegm = 0;
71 if ( obsRate.isEmpty() ) {
72 _inspSegm = 0;
73 }
74 else if ( obsRate.indexOf("5 Hz") != -1 ) {
75 _inspSegm = 20;
76 }
77 else if ( obsRate.indexOf("1 Hz") != -1 ) {
78 _inspSegm = 10;
79 }
80 else if ( obsRate.indexOf("0.5 Hz") != -1 ) {
81 _inspSegm = 20;
82 }
83 else if ( obsRate.indexOf("0.2 Hz") != -1 ) {
84 _inspSegm = 40;
85 }
86 else if ( obsRate.indexOf("0.1 Hz") != -1 ) {
87 _inspSegm = 50;
88 }
89 _adviseFail = settings.value("adviseFail").toInt();
90 _adviseReco = settings.value("adviseReco").toInt();
91 _adviseScript = settings.value("adviseScript").toString();
92 expandEnvVar(_adviseScript);
93
94 // Latency interval/average
95 // ------------------------
96 _perfIntr = 1;
97 QString perfIntr = settings.value("perfIntr").toString();
98 if ( perfIntr.isEmpty() ) {
99 _perfIntr = 1;
100 }
101 else if ( perfIntr.indexOf("2 sec") != -1 ) {
102 _perfIntr = 2;
103 }
104 else if ( perfIntr.indexOf("10 sec") != -1 ) {
105 _perfIntr = 10;
106 }
107 else if ( perfIntr.indexOf("1 min") != -1 ) {
108 _perfIntr = 60;
109 }
110 else if ( perfIntr.left(5).indexOf("5 min") != -1 ) {
111 _perfIntr = 300;
112 }
113 else if ( perfIntr.indexOf("15 min") != -1 ) {
114 _perfIntr = 900;
115 }
116 else if ( perfIntr.indexOf("1 hour") != -1 ) {
117 _perfIntr = 3600;
118 }
119 else if ( perfIntr.indexOf("6 hours") != -1 ) {
120 _perfIntr = 21600;
121 }
122 else if ( perfIntr.indexOf("1 day") != -1 ) {
123 _perfIntr = 86400;
124 }
125
126 // RTCM message types
127 // ------------------
128 _checkMountPoint = settings.value("miscMount").toString();
129
130 // Initialize private members
131 // --------------------------
132 _maxDt = 1000.0;
133 _wrongEpoch = false;
134 _checkSeg = false;
135 _numSucc = 0;
136 _secSucc = 0;
137 _secFail = 0;
138 _initPause = 0;
139 _currPause = 0;
140 _begCorrupt = false;
141 _endCorrupt = false;
142 _followSec = false;
143 _oldSecGPS = 0;
144 _newSecGPS = 0;
145 _numGaps = 0;
146 _diffSecGPS = 0;
147 _numLat = 0;
148 _sumLat = 0.0;
149 _sumLatQ = 0.0;
150 _meanDiff = 0.0;
151 _minLat = _maxDt;
152 _maxLat = -_maxDt;
153 _curLat = 0.0;
154
155 _checkTime = QDateTime::currentDateTime();
156 _decodeSucc = QDateTime::currentDateTime();
157
158 _decodeStop = QDateTime::currentDateTime();
159
160}
161
162// Destructor
163//////////////////////////////////////////////////////////////////////////////
164latencyChecker::~latencyChecker() {
165}
166
167// Perform 'Begin outage' check
168//////////////////////////////////////////////////////////////////////////////
169void latencyChecker::checkReconnect() {
170
171 // Begin outage threshold
172 // ----------------------
173 if ( _decodeStop.isValid() ) {
174 if ( _decodeStop.secsTo(QDateTime::currentDateTime()) > _adviseFail * 60 ) {
175 _decodeStop.setDate(QDate());
176 _decodeStop.setTime(QTime());
177 _begDateOut = _checkTime.toUTC().date().toString("yy-MM-dd");
178 _begTimeOut = _checkTime.toUTC().time().toString("hh:mm:ss");
179 emit(newMessage((_staID
180 + ": Failure threshold exceeded, outage since "
181 + _begDateOut + " " + _begTimeOut).toAscii(), true));
182 callScript(("Begin_Outage "
183 + _begDateOut + " " + _begTimeOut).toAscii());
184 _decodeStart = QDateTime::currentDateTime();
185 }
186 }
187}
188
189// Perform Corrupt and 'End outage' check
190//////////////////////////////////////////////////////////////////////////////
191void latencyChecker::checkOutage(bool decoded) {
192
193 if (_inspSegm == 0) { return;}
194
195 if (decoded) { _numSucc += 1; }
196
197 if (!_checkPause.isValid() || _checkPause.secsTo(QDateTime::currentDateTime()) >= _currPause ) {
198 if (!_checkSeg) {
199 if ( _checkTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
200 _checkSeg = true;
201 }
202 }
203
204 // Check - once per inspect segment
205 // --------------------------------
206 if (_checkSeg) {
207
208 _checkTime = QDateTime::currentDateTime();
209
210 if (_numSucc > 0) {
211 _secSucc += _inspSegm;
212 _decodeSucc = QDateTime::currentDateTime();
213 if (_secSucc > _adviseReco * 60) {
214 _secSucc = _adviseReco * 60 + 1;
215 }
216 _numSucc = 0;
217 _currPause = _initPause;
218 _checkPause.setDate(QDate());
219 _checkPause.setTime(QTime());
220 }
221 else {
222 _secFail += _inspSegm;
223 _secSucc = 0;
224 if (_secFail > _adviseFail * 60) {
225 _secFail = _adviseFail * 60 + 1;
226 }
227 if (!_checkPause.isValid()) {
228 _checkPause = QDateTime::currentDateTime();
229 }
230 else {
231 _checkPause.setDate(QDate());
232 _checkPause.setTime(QTime());
233 _secFail = _secFail + _currPause - _inspSegm;
234 _currPause = _currPause * 2;
235 if (_currPause > 960) {
236 _currPause = 960;
237 }
238 }
239 }
240
241 // End corrupt threshold
242 // ---------------------
243 if ( _begCorrupt && !_endCorrupt && _secSucc > _adviseReco * 60 ) {
244 _endDateCor = QDateTime::currentDateTime()
245 .addSecs(- _adviseReco * 60)
246 .toUTC().date().toString("yy-MM-dd");
247 _endTimeCor = QDateTime::currentDateTime()
248 .addSecs(- _adviseReco * 60)
249 .toUTC().time().toString("hh:mm:ss");
250 emit(newMessage((_staID
251 + ": Recovery threshold exceeded, corruption ended "
252 + _endDateCor + " " + _endTimeCor).toAscii(), true));
253 callScript(("End_Corrupted "
254 + _endDateCor + " " + _endTimeCor + " Begin was "
255 + _begDateCor + " " + _begTimeCor).toAscii());
256 _endCorrupt = true;
257 _begCorrupt = false;
258 _secFail = 0;
259 }
260 else {
261
262 // Begin corrupt threshold
263 // -----------------------
264 if ( !_begCorrupt && _secFail > _adviseFail * 60 ) {
265 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
266 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
267 emit(newMessage((_staID
268 + ": Failure threshold exceeded, corrupted since "
269 + _begDateCor + " " + _begTimeCor).toAscii(), true));
270 callScript(("Begin_Corrupted "
271 + _begDateCor + " " + _begTimeCor).toAscii());
272 _begCorrupt = true;
273 _endCorrupt = false;
274 _secSucc = 0;
275 _numSucc = 0;
276 }
277 }
278 _checkSeg = false;
279 }
280 }
281
282 // End outage threshold
283 // --------------------
284 if ( _decodeStart.isValid() ) {
285 if ( _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
286 _decodeStart.setDate(QDate());
287 _decodeStart.setTime(QTime());
288 _endDateOut = QDateTime::currentDateTime()
289 .addSecs(- _adviseReco * 60)
290 .toUTC().date().toString("yy-MM-dd");
291 _endTimeOut = QDateTime::currentDateTime()
292 .addSecs(- _adviseReco * 60)
293 .toUTC().time().toString("hh:mm:ss");
294 emit(newMessage((_staID
295 + ": Recovery threshold exceeded, outage ended "
296 + _endDateOut + " " + _endTimeOut).toAscii(), true));
297 callScript(("End_Outage "
298 + _endDateOut + " " + _endTimeOut + " Begin was "
299 + _begDateOut + " " + _begTimeOut).toAscii());
300 }
301 }
302 _decodeStop = QDateTime::currentDateTime();
303}
304
305// Perform latency checks (observations)
306//////////////////////////////////////////////////////////////////////////////
307void latencyChecker::checkObsLatency(const QList<t_satObs>& obsList) {
308
309 if (_perfIntr > 0 ) {
310
311 QListIterator<t_satObs> it(obsList);
312 while (it.hasNext()) {
313 const t_satObs& obs = it.next();
314
315 _newSecGPS = static_cast<int>(obs._time.gpssec());
316 if (_newSecGPS != _oldSecGPS) {
317 if (_newSecGPS % _perfIntr < _oldSecGPS % _perfIntr) {
318 if (_numLat > 0) {
319 if (_meanDiff > 0.0) {
320 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
321 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
322 .arg(_staID.data())
323 .arg(int(_sumLat/_numLat*100)/100.)
324 .arg(int(_minLat*100)/100.)
325 .arg(int(_maxLat*100)/100.)
326 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
327 .arg(_numLat)
328 .arg(_numGaps)
329 .toAscii(), true) );
330 }
331 } else {
332 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
333 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
334 .arg(_staID.data())
335 .arg(int(_sumLat/_numLat*100)/100.)
336 .arg(int(_minLat*100)/100.)
337 .arg(int(_maxLat*100)/100.)
338 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
339 .arg(_numLat)
340 .toAscii(), true) );
341 }
342 }
343 }
344 _meanDiff = _diffSecGPS / _numLat;
345 _diffSecGPS = 0;
346 _numGaps = 0;
347 _sumLat = 0.0;
348 _sumLatQ = 0.0;
349 _numLat = 0;
350 _minLat = _maxDt;
351 _maxLat = -_maxDt;
352 }
353 if (_followSec) {
354 _diffSecGPS += _newSecGPS - _oldSecGPS;
355 if (_meanDiff>0.) {
356 if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
357 _numGaps += 1;
358 }
359 }
360 }
361
362 // Compute the observations latency
363 // --------------------------------
364 int week;
365 double sec;
366 currentGPSWeeks(week, sec);
367 const double secPerWeek = 7.0 * 24.0 * 3600.0;
368 if (week < int(obs._time.gpsw())) {
369 week += 1;
370 sec -= secPerWeek;
371 }
372 if (week > int(obs._time.gpsw())) {
373 week -= 1;
374 sec += secPerWeek;
375 }
376 _curLat = sec - obs._time.gpssec();
377 _sumLat += _curLat;
378 _sumLatQ += _curLat * _curLat;
379 if (_curLat < _minLat) {
380 _minLat = _curLat;
381 }
382 if (_curLat >= _maxLat) {
383 _maxLat = _curLat;
384 }
385 _numLat += 1;
386 _oldSecGPS = _newSecGPS;
387 _followSec = true;
388 }
389 }
390 }
391}
392
393// Perform latency checks (corrections)
394//////////////////////////////////////////////////////////////////////////////
395void latencyChecker::checkCorrLatency(int corrGPSEpochTime) {
396
397 if (corrGPSEpochTime < 0) {
398 return;
399 }
400
401 if (_perfIntr > 0) {
402
403 _newSecGPS = corrGPSEpochTime;
404
405 int week;
406 double sec;
407 currentGPSWeeks(week, sec);
408 double dt = fabs(sec - _newSecGPS);
409 const double secPerWeek = 7.0 * 24.0 * 3600.0;
410 if (dt > 0.5 * secPerWeek) {
411 if (sec > _newSecGPS) {
412 sec -= secPerWeek;
413 } else {
414 sec += secPerWeek;
415 }
416 }
417 if (_newSecGPS != _oldSecGPS) {
418 if (int(_newSecGPS) % _perfIntr < int(_oldSecGPS) % _perfIntr) {
419 if (_numLat>0) {
420 QString late;
421 if (_meanDiff>0.) {
422 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs, %6 gaps")
423 .arg(int(_sumLat/_numLat*100)/100.)
424 .arg(int(_minLat*100)/100.)
425 .arg(int(_maxLat*100)/100.)
426 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
427 .arg(_numLat)
428 .arg(_numGaps);
429 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
430 emit(newMessage(QString(_staID + late ).toAscii(), true) );
431 }
432 }
433 else {
434 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs")
435 .arg(int(_sumLat/_numLat*100)/100.)
436 .arg(int(_minLat*100)/100.)
437 .arg(int(_maxLat*100)/100.)
438 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
439 .arg(_numLat);
440 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
441 emit(newMessage(QString(_staID + late ).toAscii(), true) );
442 }
443 }
444 }
445 _meanDiff = int(_diffSecGPS)/_numLat;
446 _diffSecGPS = 0;
447 _numGaps = 0;
448 _sumLat = 0.0;
449 _sumLatQ = 0.0;
450 _numLat = 0;
451 _minLat = 1000.;
452 _maxLat = -1000.;
453 }
454 if (_followSec) {
455 _diffSecGPS += _newSecGPS - _oldSecGPS;
456 if (_meanDiff>0.) {
457 if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
458 _numGaps += 1;
459 }
460 }
461 }
462 _curLat = sec - _newSecGPS;
463 _sumLat += _curLat;
464 _sumLatQ += _curLat * _curLat;
465 if (_curLat < _minLat) {
466 _minLat = _curLat;
467 }
468 if (_curLat >= _maxLat) {
469 _maxLat = _curLat;
470 }
471 _numLat += 1;
472 _oldSecGPS = _newSecGPS;
473 _followSec = true;
474 }
475 }
476}
477
478// Call advisory notice script
479////////////////////////////////////////////////////////////////////////////
480void latencyChecker::callScript(const char* comment) {
481 if (!_adviseScript.isEmpty()) {
482#ifdef WIN32
483 Sleep(1);
484 QProcess::startDetached(_adviseScript, QStringList() << _staID << comment) ;
485#else
486 sleep(1);
487 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << comment) ;
488#endif
489 }
490}
Note: See TracBrowser for help on using the repository browser.