source: ntrip/branches/BNC_2.11.0/src/latencychecker.cpp@ 10212

Last change on this file since 10212 was 5070, checked in by mervart, 12 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 "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 }
185 _decodeStart = QDateTime::currentDateTime();
186 }
187
188}
189
190// Perform Corrupt and 'End outage' check
191//////////////////////////////////////////////////////////////////////////////
192void latencyChecker::checkOutage(bool decoded) {
193
194 if (_inspSegm == 0) { return;}
195
196 if (decoded) { _numSucc += 1; }
197
198 if (!_checkPause.isValid() || _checkPause.secsTo(QDateTime::currentDateTime()) >= _currPause ) {
199 if (!_checkSeg) {
200 if ( _checkTime.secsTo(QDateTime::currentDateTime()) > _inspSegm ) {
201 _checkSeg = true;
202 }
203 }
204
205 // Check - once per inspect segment
206 // --------------------------------
207 if (_checkSeg) {
208
209 _checkTime = QDateTime::currentDateTime();
210
211 if (_numSucc > 0) {
212 _secSucc += _inspSegm;
213 _decodeSucc = QDateTime::currentDateTime();
214 if (_secSucc > _adviseReco * 60) {
215 _secSucc = _adviseReco * 60 + 1;
216 }
217 _numSucc = 0;
218 _currPause = _initPause;
219 _checkPause.setDate(QDate());
220 _checkPause.setTime(QTime());
221 }
222 else {
223 _secFail += _inspSegm;
224 _secSucc = 0;
225 if (_secFail > _adviseFail * 60) {
226 _secFail = _adviseFail * 60 + 1;
227 }
228 if (!_checkPause.isValid()) {
229 _checkPause = QDateTime::currentDateTime();
230 }
231 else {
232 _checkPause.setDate(QDate());
233 _checkPause.setTime(QTime());
234 _secFail = _secFail + _currPause - _inspSegm;
235 _currPause = _currPause * 2;
236 if (_currPause > 960) {
237 _currPause = 960;
238 }
239 }
240 }
241
242 // End corrupt threshold
243 // ---------------------
244 if ( _begCorrupt && !_endCorrupt && _secSucc > _adviseReco * 60 ) {
245 _endDateCor = QDateTime::currentDateTime()
246 .addSecs(- _adviseReco * 60)
247 .toUTC().date().toString("yy-MM-dd");
248 _endTimeCor = QDateTime::currentDateTime()
249 .addSecs(- _adviseReco * 60)
250 .toUTC().time().toString("hh:mm:ss");
251 emit(newMessage((_staID
252 + ": Recovery threshold exceeded, corruption ended "
253 + _endDateCor + " " + _endTimeCor).toAscii(), true));
254 callScript(("End_Corrupted "
255 + _endDateCor + " " + _endTimeCor + " Begin was "
256 + _begDateCor + " " + _begTimeCor).toAscii());
257 _endCorrupt = true;
258 _begCorrupt = false;
259 _secFail = 0;
260 }
261 else {
262
263 // Begin corrupt threshold
264 // -----------------------
265 if ( !_begCorrupt && _secFail > _adviseFail * 60 ) {
266 _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
267 _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
268 emit(newMessage((_staID
269 + ": Failure threshold exceeded, corrupted since "
270 + _begDateCor + " " + _begTimeCor).toAscii(), true));
271 callScript(("Begin_Corrupted "
272 + _begDateCor + " " + _begTimeCor).toAscii());
273 _begCorrupt = true;
274 _endCorrupt = false;
275 _secSucc = 0;
276 _numSucc = 0;
277 }
278 }
279 _checkSeg = false;
280 }
281 }
282
283 // End outage threshold
284 // --------------------
285 if ( _decodeStart.isValid() ) {
286 if ( _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
287 _decodeStart.setDate(QDate());
288 _decodeStart.setTime(QTime());
289 _endDateOut = QDateTime::currentDateTime()
290 .addSecs(- _adviseReco * 60)
291 .toUTC().date().toString("yy-MM-dd");
292 _endTimeOut = QDateTime::currentDateTime()
293 .addSecs(- _adviseReco * 60)
294 .toUTC().time().toString("hh:mm:ss");
295 emit(newMessage((_staID
296 + ": Recovery threshold exceeded, outage ended "
297 + _endDateOut + " " + _endTimeOut).toAscii(), true));
298 callScript(("End_Outage "
299 + _endDateOut + " " + _endTimeOut + " Begin was "
300 + _begDateOut + " " + _begTimeOut).toAscii());
301 _decodeStop = QDateTime::currentDateTime();
302 }
303 }
304}
305
306// Perform latency checks (observations)
307//////////////////////////////////////////////////////////////////////////////
308void latencyChecker::checkObsLatency(const QList<t_obs>& obsList) {
309
310 if (_perfIntr > 0 ) {
311
312 QListIterator<t_obs> it(obsList);
313 while (it.hasNext()) {
314 const t_obs& obs = it.next();
315
316 _newSecGPS = static_cast<int>(obs.GPSWeeks);
317 if (_newSecGPS != _oldSecGPS) {
318 if (_newSecGPS % _perfIntr < _oldSecGPS % _perfIntr) {
319 if (_numLat > 0) {
320 if (_meanDiff > 0.0) {
321 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
322 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
323 .arg(_staID.data())
324 .arg(int(_sumLat/_numLat*100)/100.)
325 .arg(int(_minLat*100)/100.)
326 .arg(int(_maxLat*100)/100.)
327 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
328 .arg(_numLat)
329 .arg(_numGaps)
330 .toAscii(), true) );
331 }
332 } else {
333 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
334 emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
335 .arg(_staID.data())
336 .arg(int(_sumLat/_numLat*100)/100.)
337 .arg(int(_minLat*100)/100.)
338 .arg(int(_maxLat*100)/100.)
339 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
340 .arg(_numLat)
341 .toAscii(), true) );
342 }
343 }
344 }
345 _meanDiff = _diffSecGPS / _numLat;
346 _diffSecGPS = 0;
347 _numGaps = 0;
348 _sumLat = 0.0;
349 _sumLatQ = 0.0;
350 _numLat = 0;
351 _minLat = _maxDt;
352 _maxLat = -_maxDt;
353 }
354 if (_followSec) {
355 _diffSecGPS += _newSecGPS - _oldSecGPS;
356 if (_meanDiff>0.) {
357 if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
358 _numGaps += 1;
359 }
360 }
361 }
362
363 // Compute the observations latency
364 // --------------------------------
365 int week;
366 double sec;
367 currentGPSWeeks(week, sec);
368 const double secPerWeek = 7.0 * 24.0 * 3600.0;
369 if (week < obs.GPSWeek) {
370 week += 1;
371 sec -= secPerWeek;
372 }
373 if (week > obs.GPSWeek) {
374 week -= 1;
375 sec += secPerWeek;
376 }
377 _curLat = sec - obs.GPSWeeks;
378 _sumLat += _curLat;
379 _sumLatQ += _curLat * _curLat;
380 if (_curLat < _minLat) {
381 _minLat = _curLat;
382 }
383 if (_curLat >= _maxLat) {
384 _maxLat = _curLat;
385 }
386 _numLat += 1;
387 _oldSecGPS = _newSecGPS;
388 _followSec = true;
389 }
390 }
391 }
392}
393
394// Perform latency checks (corrections)
395//////////////////////////////////////////////////////////////////////////////
396void latencyChecker::checkCorrLatency(int corrGPSEpochTime) {
397
398 if (corrGPSEpochTime < 0) {
399 return;
400 }
401
402 if (_perfIntr > 0) {
403
404 _newSecGPS = corrGPSEpochTime;
405
406 int week;
407 double sec;
408 currentGPSWeeks(week, sec);
409 double dt = fabs(sec - _newSecGPS);
410 const double secPerWeek = 7.0 * 24.0 * 3600.0;
411 if (dt > 0.5 * secPerWeek) {
412 if (sec > _newSecGPS) {
413 sec -= secPerWeek;
414 } else {
415 sec += secPerWeek;
416 }
417 }
418 if (_newSecGPS != _oldSecGPS) {
419 if (int(_newSecGPS) % _perfIntr < int(_oldSecGPS) % _perfIntr) {
420 if (_numLat>0) {
421 QString late;
422 if (_meanDiff>0.) {
423 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs, %6 gaps")
424 .arg(int(_sumLat/_numLat*100)/100.)
425 .arg(int(_minLat*100)/100.)
426 .arg(int(_maxLat*100)/100.)
427 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
428 .arg(_numLat)
429 .arg(_numGaps);
430 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
431 emit(newMessage(QString(_staID + late ).toAscii(), true) );
432 }
433 }
434 else {
435 late = QString(": Mean latency %1 sec, min %2, max %3, rms %4, %5 epochs")
436 .arg(int(_sumLat/_numLat*100)/100.)
437 .arg(int(_minLat*100)/100.)
438 .arg(int(_maxLat*100)/100.)
439 .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
440 .arg(_numLat);
441 if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
442 emit(newMessage(QString(_staID + late ).toAscii(), true) );
443 }
444 }
445 }
446 _meanDiff = int(_diffSecGPS)/_numLat;
447 _diffSecGPS = 0;
448 _numGaps = 0;
449 _sumLat = 0.0;
450 _sumLatQ = 0.0;
451 _numLat = 0;
452 _minLat = 1000.;
453 _maxLat = -1000.;
454 }
455 if (_followSec) {
456 _diffSecGPS += _newSecGPS - _oldSecGPS;
457 if (_meanDiff>0.) {
458 if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
459 _numGaps += 1;
460 }
461 }
462 }
463 _curLat = sec - _newSecGPS;
464 _sumLat += _curLat;
465 _sumLatQ += _curLat * _curLat;
466 if (_curLat < _minLat) {
467 _minLat = _curLat;
468 }
469 if (_curLat >= _maxLat) {
470 _maxLat = _curLat;
471 }
472 _numLat += 1;
473 _oldSecGPS = _newSecGPS;
474 _followSec = true;
475 }
476 }
477}
478
479// Call advisory notice script
480////////////////////////////////////////////////////////////////////////////
481void latencyChecker::callScript(const char* comment) {
482 if (!_adviseScript.isEmpty()) {
483#ifdef WIN32
484 Sleep(1);
485 QProcess::startDetached(_adviseScript, QStringList() << _staID << comment) ;
486#else
487 sleep(1);
488 QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << comment) ;
489#endif
490 }
491}
Note: See TracBrowser for help on using the repository browser.