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

Last change on this file since 3260 was 2711, checked in by mervart, 14 years ago
File size: 15.1 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 = 1;
98 QString perfIntr = settings.value("perfIntr").toString();
99 if ( perfIntr.isEmpty() ) {
100 _perfIntr = 1;
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.left(5).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<t_obs>& obsList) {
310
311 if (_perfIntr > 0 ) {
312
313 QListIterator<t_obs> it(obsList);
314 while (it.hasNext()) {
315 const t_obs& obs = it.next();
316
317 _newSecGPS = static_cast<int>(obs.GPSWeeks);
318 if (_newSecGPS != _oldSecGPS) {
319 if (_newSecGPS % _perfIntr < _oldSecGPS % _perfIntr) {
320 if (_numLat > 0) {
321 if (_meanDiff > 0.0) {
322 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
323 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
324 .arg(_staID.data())
325 .arg(int(_sumLat/_numLat*100)/100.)
326 .arg(int(_minLat*100)/100.)
327 .arg(int(_maxLat*100)/100.)
328 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
329 .arg(_numLat)
330 .arg(_numGaps)
331 .toAscii(), true) );
332 }
333 } else {
334 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
335 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
336 .arg(_staID.data())
337 .arg(int(_sumLat/_numLat*100)/100.)
338 .arg(int(_minLat*100)/100.)
339 .arg(int(_maxLat*100)/100.)
340 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
341 .arg(_numLat)
342 .toAscii(), true) );
343 }
344 }
345 }
346 _meanDiff = _diffSecGPS / _numLat;
347 _diffSecGPS = 0;
348 _numGaps = 0;
349 _sumLat = 0.0;
350 _sumLatQ = 0.0;
351 _numLat = 0;
352 _minLat = _maxDt;
353 _maxLat = -_maxDt;
354 }
355 if (_followSec) {
356 _diffSecGPS += _newSecGPS - _oldSecGPS;
357 if (_meanDiff>0.) {
358 if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
359 _numGaps += 1;
360 }
361 }
362 }
363
364 // Compute the observations latency
365 // --------------------------------
366 int week;
367 double sec;
368 currentGPSWeeks(week, sec);
369 const double secPerWeek = 7.0 * 24.0 * 3600.0;
370 if (week < obs.GPSWeek) {
371 week += 1;
372 sec -= secPerWeek;
373 }
374 if (week > obs.GPSWeek) {
375 week -= 1;
376 sec += secPerWeek;
377 }
378 _curLat = sec - obs.GPSWeeks;
379 _sumLat += _curLat;
380 _sumLatQ += _curLat * _curLat;
381 if (_curLat < _minLat) {
382 _minLat = _curLat;
383 }
384 if (_curLat >= _maxLat) {
385 _maxLat = _curLat;
386 }
387 _numLat += 1;
388 _oldSecGPS = _newSecGPS;
389 _followSec = true;
390 }
391 }
392 }
393}
394
395// Perform latency checks (corrections)
396//////////////////////////////////////////////////////////////////////////////
397void latencyChecker::checkCorrLatency(int corrGPSEpochTime) {
398
399 if (corrGPSEpochTime < 0) {
400 return;
401 }
402
403 if (_perfIntr > 0) {
404
405 _newSecGPS = corrGPSEpochTime;
406
407 int week;
408 double sec;
409 currentGPSWeeks(week, sec);
410 double dt = fabs(sec - _newSecGPS);
411 const double secPerWeek = 7.0 * 24.0 * 3600.0;
412 if (dt > 0.5 * secPerWeek) {
413 if (sec > _newSecGPS) {
414 sec -= secPerWeek;
415 } else {
416 sec += secPerWeek;
417 }
418 }
419 if (_newSecGPS != _oldSecGPS) {
420 if (int(_newSecGPS) % _perfIntr < int(_oldSecGPS) % _perfIntr) {
421 if (_numLat>0) {
422 QString late;
423 if (_meanDiff>0.) {
424 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs, %6 gaps")
425 .arg(int(_sumLat/_numLat*100)/100.)
426 .arg(int(_minLat*100)/100.)
427 .arg(int(_maxLat*100)/100.)
428 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
429 .arg(_numLat)
430 .arg(_numGaps);
431 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
432 emit(newMessage(QString(_staID + late ).toAscii(), true) );
433 }
434 }
435 else {
436 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs")
437 .arg(int(_sumLat/_numLat*100)/100.)
438 .arg(int(_minLat*100)/100.)
439 .arg(int(_maxLat*100)/100.)
440 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
441 .arg(_numLat);
442 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
443 emit(newMessage(QString(_staID + late ).toAscii(), true) );
444 }
445 }
446 }
447 _meanDiff = int(_diffSecGPS)/_numLat;
448 _diffSecGPS = 0;
449 _numGaps = 0;
450 _sumLat = 0.0;
451 _sumLatQ = 0.0;
452 _numLat = 0;
453 _minLat = 1000.;
454 _maxLat = -1000.;
455 }
456 if (_followSec) {
457 _diffSecGPS += _newSecGPS - _oldSecGPS;
458 if (_meanDiff>0.) {
459 if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
460 _numGaps += 1;
461 }
462 }
463 }
464 _curLat = sec - _newSecGPS;
465 _sumLat += _curLat;
466 _sumLatQ += _curLat * _curLat;
467 if (_curLat < _minLat) {
468 _minLat = _curLat;
469 }
470 if (_curLat >= _maxLat) {
471 _maxLat = _curLat;
472 }
473 _numLat += 1;
474 _oldSecGPS = _newSecGPS;
475 _followSec = true;
476 }
477 }
478}
479
480// Call advisory notice script
481////////////////////////////////////////////////////////////////////////////
482void latencyChecker::callScript(const char* comment) {
483 if (!_adviseScript.isEmpty()) {
484#ifdef WIN32
485 Sleep(1);
486 QProcess::startDetached(_adviseScript, QStringList() << _staID << comment) ;
487#else
488 sleep(1);
489 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << comment) ;
490#endif
491 }
492}
Note: See TracBrowser for help on using the repository browser.