source: ntrip/trunk/BNC/latencychecker.cpp@ 1972

Last change on this file since 1972 was 1972, checked in by weber, 14 years ago

* empty log message *

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