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: bncGetThread
|
---|
30 | *
|
---|
31 | * Purpose: Thread that retrieves data from NTRIP caster
|
---|
32 | *
|
---|
33 | * Author: L. Mervart
|
---|
34 | *
|
---|
35 | * Created: 24-Dec-2005
|
---|
36 | *
|
---|
37 | * Changes:
|
---|
38 | *
|
---|
39 | * -----------------------------------------------------------------------*/
|
---|
40 |
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <iomanip>
|
---|
43 | #include <sstream>
|
---|
44 |
|
---|
45 | #include <QFile>
|
---|
46 | #include <QTextStream>
|
---|
47 | #include <QtNetwork>
|
---|
48 | #include <QTime>
|
---|
49 |
|
---|
50 | #include "bncgetthread.h"
|
---|
51 | #include "bnctabledlg.h"
|
---|
52 | #include "bncapp.h"
|
---|
53 | #include "bncutils.h"
|
---|
54 | #include "bncrinex.h"
|
---|
55 | #include "bnczerodecoder.h"
|
---|
56 | #include "bncnetqueryv1.h"
|
---|
57 | #include "bncnetqueryv2.h"
|
---|
58 | #include "bncnetqueryrtp.h"
|
---|
59 | #include "bncsettings.h"
|
---|
60 |
|
---|
61 | #include "RTCM/RTCM2Decoder.h"
|
---|
62 | #include "RTCM3/RTCM3Decoder.h"
|
---|
63 | #include "RTIGS/RTIGSDecoder.h"
|
---|
64 | #include "GPSS/gpssDecoder.h"
|
---|
65 | #include "serial/qextserialport.h"
|
---|
66 |
|
---|
67 | using namespace std;
|
---|
68 |
|
---|
69 | // Constructor 1
|
---|
70 | ////////////////////////////////////////////////////////////////////////////
|
---|
71 | bncGetThread::bncGetThread(const QByteArray& rawInpFileName,
|
---|
72 | const QByteArray& format) {
|
---|
73 |
|
---|
74 | _rawInpFile = new QFile(rawInpFileName);
|
---|
75 | _rawInpFile->open(QIODevice::ReadOnly);
|
---|
76 | _format = format;
|
---|
77 | _staID = rawInpFileName.mid(
|
---|
78 | rawInpFileName.lastIndexOf(QDir::separator())+1,4);
|
---|
79 |
|
---|
80 | initialize();
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Constructor 2
|
---|
84 | ////////////////////////////////////////////////////////////////////////////
|
---|
85 | bncGetThread::bncGetThread(const QUrl& mountPoint,
|
---|
86 | const QByteArray& format,
|
---|
87 | const QByteArray& latitude,
|
---|
88 | const QByteArray& longitude,
|
---|
89 | const QByteArray& nmea,
|
---|
90 | const QByteArray& ntripVersion, int iMount) {
|
---|
91 | _rawInpFile = 0;
|
---|
92 | _mountPoint = mountPoint;
|
---|
93 | _staID = mountPoint.path().mid(1).toAscii();
|
---|
94 | _format = format;
|
---|
95 | _latitude = latitude;
|
---|
96 | _longitude = longitude;
|
---|
97 | _nmea = nmea;
|
---|
98 | _ntripVersion = ntripVersion;
|
---|
99 | _iMount = iMount; // index in mountpoints array
|
---|
100 |
|
---|
101 | initialize();
|
---|
102 | }
|
---|
103 |
|
---|
104 | // Initialization (common part of the constructor)
|
---|
105 | ////////////////////////////////////////////////////////////////////////////
|
---|
106 | void bncGetThread::initialize() {
|
---|
107 |
|
---|
108 | setTerminationEnabled(true);
|
---|
109 |
|
---|
110 | bncApp* app = (bncApp*) qApp;
|
---|
111 |
|
---|
112 | connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
113 | app, SLOT(slotMessage(const QByteArray,bool)));
|
---|
114 |
|
---|
115 | _isToBeDeleted = false;
|
---|
116 | _decoder = 0;
|
---|
117 | _query = 0;
|
---|
118 | _nextSleep = 0;
|
---|
119 | _rawOutFile = 0;
|
---|
120 | _staID_orig = _staID;
|
---|
121 |
|
---|
122 | // Check name conflict
|
---|
123 | // -------------------
|
---|
124 | bncSettings settings;
|
---|
125 | QListIterator<QString> it(settings.value("mountPoints").toStringList());
|
---|
126 | int num = 0;
|
---|
127 | int ind = -1;
|
---|
128 | while (it.hasNext()) {
|
---|
129 | ++ind;
|
---|
130 | QStringList hlp = it.next().split(" ");
|
---|
131 | if (hlp.size() <= 1) continue;
|
---|
132 | QUrl url(hlp[0]);
|
---|
133 | if (_mountPoint.path() == url.path()) {
|
---|
134 | if (_iMount > ind || _iMount < 0) {
|
---|
135 | ++num;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (num > 0) {
|
---|
141 | _staID = _staID.left(_staID.length()-1) + QString("%1").arg(num).toAscii();
|
---|
142 | }
|
---|
143 |
|
---|
144 | // RINEX writer
|
---|
145 | // ------------
|
---|
146 | _samplingRate = settings.value("rnxSampl").toInt();
|
---|
147 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
---|
148 | _rnx = 0;
|
---|
149 | if (_rawInpFile) {
|
---|
150 | cerr << "no RINEX path specified" << endl;
|
---|
151 | ::exit(1);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | else {
|
---|
155 | _rnx = new bncRinex(_staID, _mountPoint, _format, _latitude,
|
---|
156 | _longitude, _nmea);
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Serial Port
|
---|
160 | // -----------
|
---|
161 | if (settings.value("serialMountPoint").toString() == _staID) {
|
---|
162 | _serialPort = new QextSerialPort(
|
---|
163 | settings.value("serialPortName").toString() );
|
---|
164 | QString hlp = settings.value("serialBaudRate").toString();
|
---|
165 | if (hlp == "110") {
|
---|
166 | _serialPort->setBaudRate(BAUD110);
|
---|
167 | }
|
---|
168 | else if (hlp == "300") {
|
---|
169 | _serialPort->setBaudRate(BAUD300);
|
---|
170 | }
|
---|
171 | else if (hlp == "600") {
|
---|
172 | _serialPort->setBaudRate(BAUD600);
|
---|
173 | }
|
---|
174 | else if (hlp == "1200") {
|
---|
175 | _serialPort->setBaudRate(BAUD1200);
|
---|
176 | }
|
---|
177 | else if (hlp == "2400") {
|
---|
178 | _serialPort->setBaudRate(BAUD2400);
|
---|
179 | }
|
---|
180 | else if (hlp == "4800") {
|
---|
181 | _serialPort->setBaudRate(BAUD4800);
|
---|
182 | }
|
---|
183 | else if (hlp == "9600") {
|
---|
184 | _serialPort->setBaudRate(BAUD9600);
|
---|
185 | }
|
---|
186 | else if (hlp == "19200") {
|
---|
187 | _serialPort->setBaudRate(BAUD19200);
|
---|
188 | }
|
---|
189 | else if (hlp == "38400") {
|
---|
190 | _serialPort->setBaudRate(BAUD38400);
|
---|
191 | }
|
---|
192 | else if (hlp == "57600") {
|
---|
193 | _serialPort->setBaudRate(BAUD57600);
|
---|
194 | }
|
---|
195 | else if (hlp == "115200") {
|
---|
196 | _serialPort->setBaudRate(BAUD115200);
|
---|
197 | }
|
---|
198 | hlp = settings.value("serialParity").toString();
|
---|
199 | if (hlp == "NONE") {
|
---|
200 | _serialPort->setParity(PAR_NONE);
|
---|
201 | }
|
---|
202 | else if (hlp == "ODD") {
|
---|
203 | _serialPort->setParity(PAR_ODD);
|
---|
204 | }
|
---|
205 | else if (hlp == "EVEN") {
|
---|
206 | _serialPort->setParity(PAR_EVEN);
|
---|
207 | }
|
---|
208 | else if (hlp == "SPACE") {
|
---|
209 | _serialPort->setParity(PAR_SPACE);
|
---|
210 | }
|
---|
211 | hlp = settings.value("serialDataBits").toString();
|
---|
212 | if (hlp == "5") {
|
---|
213 | _serialPort->setDataBits(DATA_5);
|
---|
214 | }
|
---|
215 | else if (hlp == "6") {
|
---|
216 | _serialPort->setDataBits(DATA_6);
|
---|
217 | }
|
---|
218 | else if (hlp == "7") {
|
---|
219 | _serialPort->setDataBits(DATA_7);
|
---|
220 | }
|
---|
221 | else if (hlp == "8") {
|
---|
222 | _serialPort->setDataBits(DATA_8);
|
---|
223 | }
|
---|
224 | hlp = settings.value("serialStopBits").toString();
|
---|
225 | if (hlp == "1") {
|
---|
226 | _serialPort->setStopBits(STOP_1);
|
---|
227 | }
|
---|
228 | else if (hlp == "2") {
|
---|
229 | _serialPort->setStopBits(STOP_2);
|
---|
230 | }
|
---|
231 | _serialPort->open(QIODevice::ReadWrite|QIODevice::Unbuffered);
|
---|
232 | if (!_serialPort->isOpen()) {
|
---|
233 | delete _serialPort;
|
---|
234 | _serialPort = 0;
|
---|
235 | emit(newMessage((_staID + ": Cannot open serial port\n"), true));
|
---|
236 | }
|
---|
237 | }
|
---|
238 | else {
|
---|
239 | _serialPort = 0;
|
---|
240 | }
|
---|
241 |
|
---|
242 | // Raw Output
|
---|
243 | // ----------
|
---|
244 | // QByteArray rawOutFileName = "./" + _staID + ".raw";
|
---|
245 | // _rawOutFile = new QFile(rawOutFileName);
|
---|
246 | // _rawOutFile->open(QIODevice::WriteOnly);
|
---|
247 |
|
---|
248 |
|
---|
249 | // Instantiate the decoder
|
---|
250 | // -----------------------
|
---|
251 | if (_format.indexOf("RTCM_2") != -1) {
|
---|
252 | emit(newMessage(_staID + ": Get data in RTCM 2.x format", true));
|
---|
253 | _decoder = new RTCM2Decoder(_staID.data());
|
---|
254 | }
|
---|
255 | else if (_format.indexOf("RTCM_3") != -1) {
|
---|
256 | emit(newMessage(_staID + ": Get data in RTCM 3.x format", true));
|
---|
257 | _decoder = new RTCM3Decoder(_staID);
|
---|
258 | connect((RTCM3Decoder*) _decoder, SIGNAL(newMessage(QByteArray,bool)),
|
---|
259 | this, SIGNAL(newMessage(QByteArray,bool)));
|
---|
260 | }
|
---|
261 | else if (_format.indexOf("RTIGS") != -1) {
|
---|
262 | emit(newMessage(_staID + ": Get data in RTIGS format", true));
|
---|
263 | _decoder = new RTIGSDecoder();
|
---|
264 | }
|
---|
265 | else if (_format.indexOf("GPSS") != -1 || _format.indexOf("BNC") != -1) {
|
---|
266 | emit(newMessage(_staID + ": Get Data in GPSS format", true));
|
---|
267 | _decoder = new gpssDecoder();
|
---|
268 | }
|
---|
269 | else if (_format.indexOf("ZERO") != -1) {
|
---|
270 | emit(newMessage(_staID + ": Get data in original format", true));
|
---|
271 | _decoder = new bncZeroDecoder(_staID);
|
---|
272 | }
|
---|
273 | else {
|
---|
274 | emit(newMessage(_staID + ": Unknown data format " + _format, true));
|
---|
275 | _isToBeDeleted = true;
|
---|
276 | delete this;
|
---|
277 | }
|
---|
278 |
|
---|
279 | _latencyChecker = new bncGetThread::latencyChecker(_staID);
|
---|
280 |
|
---|
281 | msleep(100); //sleep 0.1 sec
|
---|
282 | }
|
---|
283 |
|
---|
284 | // Destructor
|
---|
285 | ////////////////////////////////////////////////////////////////////////////
|
---|
286 | bncGetThread::~bncGetThread() {
|
---|
287 | if (_query) {
|
---|
288 | _query->stop();
|
---|
289 | delete _query;
|
---|
290 | }
|
---|
291 | delete _decoder;
|
---|
292 | delete _rnx;
|
---|
293 | delete _rawInpFile;
|
---|
294 | delete _rawOutFile;
|
---|
295 | delete _serialPort;
|
---|
296 | delete _latencyChecker;
|
---|
297 | emit getThreadFinished(_staID);
|
---|
298 | }
|
---|
299 |
|
---|
300 | //
|
---|
301 | ////////////////////////////////////////////////////////////////////////////
|
---|
302 | void bncGetThread::terminate() {
|
---|
303 | _isToBeDeleted = true;
|
---|
304 | }
|
---|
305 |
|
---|
306 | // Run
|
---|
307 | ////////////////////////////////////////////////////////////////////////////
|
---|
308 | void bncGetThread::run() {
|
---|
309 |
|
---|
310 | while (true) {
|
---|
311 | try {
|
---|
312 | if (_isToBeDeleted) {
|
---|
313 | QThread::exit(0);
|
---|
314 | this->deleteLater();
|
---|
315 | return;
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (tryReconnect() != success) {
|
---|
319 | continue;
|
---|
320 | }
|
---|
321 |
|
---|
322 | // Delete old observations
|
---|
323 | // -----------------------
|
---|
324 | QListIterator<p_obs> itOld(_decoder->_obsList);
|
---|
325 | while (itOld.hasNext()) {
|
---|
326 | delete itOld.next();
|
---|
327 | }
|
---|
328 | _decoder->_obsList.clear();
|
---|
329 |
|
---|
330 | // Read Data
|
---|
331 | // ---------
|
---|
332 | QByteArray data;
|
---|
333 | if (_query) {
|
---|
334 | _query->waitForReadyRead(data);
|
---|
335 | }
|
---|
336 | else if (_rawInpFile) {
|
---|
337 | const qint64 maxBytes = 1024;
|
---|
338 | data = _rawInpFile->read(maxBytes);
|
---|
339 | if (data.isEmpty()) {
|
---|
340 | cout << "no more data" << endl;
|
---|
341 | ::exit(0);
|
---|
342 | }
|
---|
343 | }
|
---|
344 | qint64 nBytes = data.size();
|
---|
345 |
|
---|
346 | // Timeout, reconnect
|
---|
347 | // ------------------
|
---|
348 | if (nBytes == 0) {
|
---|
349 | emit(newMessage(_staID + ": Data timeout, reconnecting", true));
|
---|
350 | continue;
|
---|
351 | }
|
---|
352 | else {
|
---|
353 | emit newBytes(_staID, nBytes);
|
---|
354 | }
|
---|
355 |
|
---|
356 | // Output Data
|
---|
357 | // -----------
|
---|
358 | if (_rawOutFile) {
|
---|
359 | _rawOutFile->write(data);
|
---|
360 | _rawOutFile->flush();
|
---|
361 | }
|
---|
362 | if (_serialPort) {
|
---|
363 | _serialPort->write(data);
|
---|
364 | }
|
---|
365 |
|
---|
366 | // Decode Data
|
---|
367 | // -----------
|
---|
368 | vector<string> errmsg;
|
---|
369 | _decoder->Decode(data.data(), data.size(), errmsg);
|
---|
370 |
|
---|
371 | // Perform various scans and checks
|
---|
372 | // --------------------------------
|
---|
373 | scanRTCM();
|
---|
374 | _latencyChecker->check(_decoder);
|
---|
375 |
|
---|
376 | // Loop over all observations (observations output)
|
---|
377 | // ------------------------------------------------
|
---|
378 | QListIterator<p_obs> it(_decoder->_obsList);
|
---|
379 | while (it.hasNext()) {
|
---|
380 | p_obs obs = it.next();
|
---|
381 |
|
---|
382 | // Check observation epoch
|
---|
383 | // -----------------------
|
---|
384 | if (!_rawInpFile && !dynamic_cast<gpssDecoder*>(_decoder)) {
|
---|
385 | int week;
|
---|
386 | double sec;
|
---|
387 | currentGPSWeeks(week, sec);
|
---|
388 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
389 |
|
---|
390 | if (week < obs->_o.GPSWeek) {
|
---|
391 | week += 1;
|
---|
392 | sec -= secPerWeek;
|
---|
393 | }
|
---|
394 | if (week > obs->_o.GPSWeek) {
|
---|
395 | week -= 1;
|
---|
396 | sec += secPerWeek;
|
---|
397 | }
|
---|
398 | double dt = fabs(sec - obs->_o.GPSWeeks);
|
---|
399 | const double maxDt = 600.0;
|
---|
400 | if (week != obs->_o.GPSWeek || dt > maxDt) {
|
---|
401 | emit( newMessage(_staID + ": Wrong observation epoch(s)", true) );
|
---|
402 | delete obs;
|
---|
403 | continue;
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | // RINEX Output
|
---|
408 | // ------------
|
---|
409 | if (_rnx) {
|
---|
410 | long iSec = long(floor(obs->_o.GPSWeeks+0.5));
|
---|
411 | long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
|
---|
412 | if (_samplingRate == 0 || iSec % _samplingRate == 0) {
|
---|
413 | _rnx->deepCopy(obs);
|
---|
414 | }
|
---|
415 | _rnx->dumpEpoch(newTime);
|
---|
416 | }
|
---|
417 |
|
---|
418 | // Emit new observation signal
|
---|
419 | // ---------------------------
|
---|
420 | bool firstObs = (obs == _decoder->_obsList.first());
|
---|
421 | obs->_status = t_obs::posted;
|
---|
422 | emit newObs(_staID, firstObs, obs);
|
---|
423 | }
|
---|
424 | _decoder->_obsList.clear();
|
---|
425 | }
|
---|
426 | catch (const char* msg) {
|
---|
427 | emit(newMessage(_staID + msg, true));
|
---|
428 | tryReconnect();
|
---|
429 | }
|
---|
430 | catch (...) {
|
---|
431 | emit(newMessage(_staID + "unknown exception", true));
|
---|
432 | tryReconnect();
|
---|
433 | }
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | // Try Re-Connect
|
---|
438 | ////////////////////////////////////////////////////////////////////////////
|
---|
439 | t_irc bncGetThread::tryReconnect() {
|
---|
440 |
|
---|
441 | // Easy Return
|
---|
442 | // -----------
|
---|
443 | if (_query && _query->status() == bncNetQuery::running) {
|
---|
444 | _nextSleep = 0;
|
---|
445 | return success;
|
---|
446 | }
|
---|
447 |
|
---|
448 | // Start a new query
|
---|
449 | // -----------------
|
---|
450 | if (!_rawInpFile) {
|
---|
451 |
|
---|
452 | sleep(_nextSleep);
|
---|
453 | if (_nextSleep == 0) {
|
---|
454 | _nextSleep = 1;
|
---|
455 | }
|
---|
456 | else {
|
---|
457 | _nextSleep = 2 * _nextSleep;
|
---|
458 | if (_nextSleep > 256) {
|
---|
459 | _nextSleep = 256;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | delete _query;
|
---|
464 | if (_ntripVersion == "R") {
|
---|
465 | _query = new bncNetQueryRtp();
|
---|
466 | }
|
---|
467 | else if (_ntripVersion == "2") {
|
---|
468 | _query = new bncNetQueryV2();
|
---|
469 | }
|
---|
470 | else {
|
---|
471 | _query = new bncNetQueryV1();
|
---|
472 | }
|
---|
473 | if (_nmea == "yes") {
|
---|
474 | QByteArray gga = ggaString(_latitude, _longitude);
|
---|
475 | _query->startRequest(_mountPoint, gga);
|
---|
476 | }
|
---|
477 | else {
|
---|
478 | _query->startRequest(_mountPoint, "");
|
---|
479 | }
|
---|
480 | if (_query->status() != bncNetQuery::running) {
|
---|
481 | return failure;
|
---|
482 | }
|
---|
483 | }
|
---|
484 |
|
---|
485 | if (_rnx) {
|
---|
486 | _rnx->setReconnectFlag(true);
|
---|
487 | }
|
---|
488 |
|
---|
489 | return success;
|
---|
490 | }
|
---|
491 |
|
---|
492 | // RTCM scan output
|
---|
493 | //////////////////////////////////////////////////////////////////////////////
|
---|
494 | void bncGetThread::scanRTCM() {
|
---|
495 |
|
---|
496 | bncSettings settings;
|
---|
497 | if ( Qt::CheckState(settings.value("scanRTCM").toInt()) == Qt::Checked) {
|
---|
498 |
|
---|
499 | // RTCMv3 message types
|
---|
500 | // --------------------
|
---|
501 | for (int ii = 0; ii <_decoder->_typeList.size(); ii++) {
|
---|
502 | QString type = QString("%1 ").arg(_decoder->_typeList[ii]);
|
---|
503 | emit(newMessage(_staID + ": Received message type " + type.toAscii(), true));
|
---|
504 | }
|
---|
505 |
|
---|
506 | // RTCMv3 antenna descriptor
|
---|
507 | // -------------------------
|
---|
508 | for (int ii=0;ii<_decoder->_antType.size();ii++) {
|
---|
509 | QString ant1 = QString("%1 ").arg(_decoder->_antType[ii]);
|
---|
510 | emit(newMessage(_staID + ": Antenna descriptor " + ant1.toAscii(), true));
|
---|
511 | }
|
---|
512 |
|
---|
513 | // Antenna Coordinates
|
---|
514 | // -------------------
|
---|
515 | for (int ii=0; ii <_decoder->_antList.size(); ii++) {
|
---|
516 | QByteArray antT;
|
---|
517 | if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::ARP) {
|
---|
518 | antT = "ARP";
|
---|
519 | }
|
---|
520 | else if (_decoder->_antList[ii].type == GPSDecoder::t_antInfo::APC) {
|
---|
521 | antT = "APC";
|
---|
522 | }
|
---|
523 | emit(newAntCrd(_staID, _decoder->_antList[ii].xx,
|
---|
524 | _decoder->_antList[ii].yy, _decoder->_antList[ii].zz,
|
---|
525 | antT));
|
---|
526 | }
|
---|
527 | }
|
---|
528 |
|
---|
529 | _decoder->_typeList.clear();
|
---|
530 | _decoder->_antType.clear();
|
---|
531 | _decoder->_antList.clear();
|
---|
532 | }
|
---|
533 |
|
---|
534 | // Constructor
|
---|
535 | //////////////////////////////////////////////////////////////////////////////
|
---|
536 | bncGetThread::latencyChecker::latencyChecker(QByteArray staID) {
|
---|
537 |
|
---|
538 | _staID = staID;
|
---|
539 |
|
---|
540 | bncApp* app = (bncApp*) qApp;
|
---|
541 | connect(this, SIGNAL(newMessage(QByteArray,bool)),
|
---|
542 | app, SLOT(slotMessage(const QByteArray,bool)));
|
---|
543 |
|
---|
544 | bncSettings settings;
|
---|
545 |
|
---|
546 | // Notice threshold
|
---|
547 | // ----------------
|
---|
548 | QString obsRate = settings.value("obsRate").toString();
|
---|
549 | _inspSegm = 0;
|
---|
550 | if ( obsRate.isEmpty() ) {
|
---|
551 | _inspSegm = 0;
|
---|
552 | }
|
---|
553 | else if ( obsRate.indexOf("5 Hz") != -1 ) {
|
---|
554 | _inspSegm = 20;
|
---|
555 | }
|
---|
556 | else if ( obsRate.indexOf("1 Hz") != -1 ) {
|
---|
557 | _inspSegm = 10;
|
---|
558 | }
|
---|
559 | else if ( obsRate.indexOf("0.5 Hz") != -1 ) {
|
---|
560 | _inspSegm = 20;
|
---|
561 | }
|
---|
562 | else if ( obsRate.indexOf("0.2 Hz") != -1 ) {
|
---|
563 | _inspSegm = 40;
|
---|
564 | }
|
---|
565 | else if ( obsRate.indexOf("0.1 Hz") != -1 ) {
|
---|
566 | _inspSegm = 50;
|
---|
567 | }
|
---|
568 | _adviseFail = settings.value("adviseFail").toInt();
|
---|
569 | _adviseReco = settings.value("adviseReco").toInt();
|
---|
570 | if ( Qt::CheckState(settings.value("makePause").toInt()) == Qt::Checked) {
|
---|
571 | _makePause = true;
|
---|
572 | }
|
---|
573 | else {
|
---|
574 | _makePause = false;
|
---|
575 | }
|
---|
576 | _adviseScript = settings.value("adviseScript").toString();
|
---|
577 | expandEnvVar(_adviseScript);
|
---|
578 |
|
---|
579 | // Latency interval/average
|
---|
580 | // ------------------------
|
---|
581 | _perfIntr = 0;
|
---|
582 | QString perfIntr = settings.value("perfIntr").toString();
|
---|
583 | if ( perfIntr.isEmpty() ) {
|
---|
584 | _perfIntr = 0;
|
---|
585 | }
|
---|
586 | else if ( perfIntr.indexOf("2 sec") != -1 ) {
|
---|
587 | _perfIntr = 2;
|
---|
588 | }
|
---|
589 | else if ( perfIntr.indexOf("10 sec") != -1 ) {
|
---|
590 | _perfIntr = 10;
|
---|
591 | }
|
---|
592 | else if ( perfIntr.indexOf("1 min") != -1 ) {
|
---|
593 | _perfIntr = 60;
|
---|
594 | }
|
---|
595 | else if ( perfIntr.indexOf("5 min") != -1 ) {
|
---|
596 | _perfIntr = 300;
|
---|
597 | }
|
---|
598 | else if ( perfIntr.indexOf("15 min") != -1 ) {
|
---|
599 | _perfIntr = 900;
|
---|
600 | }
|
---|
601 | else if ( perfIntr.indexOf("1 hour") != -1 ) {
|
---|
602 | _perfIntr = 3600;
|
---|
603 | }
|
---|
604 | else if ( perfIntr.indexOf("6 hours") != -1 ) {
|
---|
605 | _perfIntr = 21600;
|
---|
606 | }
|
---|
607 | else if ( perfIntr.indexOf("1 day") != -1 ) {
|
---|
608 | _perfIntr = 86400;
|
---|
609 | }
|
---|
610 |
|
---|
611 | // RTCM message types
|
---|
612 | // ------------------
|
---|
613 | _checkMountPoint = settings.value("miscMount").toString();
|
---|
614 |
|
---|
615 | // Initialize private members
|
---|
616 | // --------------------------
|
---|
617 | _maxDt = 600.0; // Check observation epoch
|
---|
618 | _wrongEpoch = false;
|
---|
619 | _decode = true;
|
---|
620 | _numSucc = 0;
|
---|
621 | _secSucc = 0;
|
---|
622 | _secFail = 0;
|
---|
623 | _initPause = 30; // Initial pause for corrupted streams
|
---|
624 | _currPause = 0;
|
---|
625 | _begCorrupt = false;
|
---|
626 | _endCorrupt = false;
|
---|
627 | _followSec = false;
|
---|
628 | _oldSecGPS = 0;
|
---|
629 | _newSecGPS = 0;
|
---|
630 | _numGaps = 0;
|
---|
631 | _diffSecGPS = 0;
|
---|
632 | _numLat = 0;
|
---|
633 | _sumLat = 0.0;
|
---|
634 | _sumLatQ = 0.0;
|
---|
635 | _meanDiff = 0.0;
|
---|
636 | _minLat = _maxDt;
|
---|
637 | _maxLat = -_maxDt;
|
---|
638 | _curLat = 0.0;
|
---|
639 |
|
---|
640 | _decodeTime = QDateTime::currentDateTime();
|
---|
641 | _decodeSucc = QDateTime::currentDateTime();
|
---|
642 | }
|
---|
643 |
|
---|
644 | // Destructor
|
---|
645 | //////////////////////////////////////////////////////////////////////////////
|
---|
646 | bncGetThread::latencyChecker::~latencyChecker() {
|
---|
647 |
|
---|
648 | }
|
---|
649 |
|
---|
650 | // Perform latency checks
|
---|
651 | //////////////////////////////////////////////////////////////////////////////
|
---|
652 | void bncGetThread::latencyChecker::check(GPSDecoder* decoder) {
|
---|
653 |
|
---|
654 | // Check - once per inspect segment
|
---|
655 | // --------------------------------
|
---|
656 | if (decoder->_obsList.size() > 0) {
|
---|
657 |
|
---|
658 | _decodeTime = QDateTime::currentDateTime();
|
---|
659 |
|
---|
660 | if (_numSucc > 0) {
|
---|
661 | _secSucc += _inspSegm;
|
---|
662 | _decodeSucc = QDateTime::currentDateTime();
|
---|
663 | if (_secSucc > _adviseReco * 60) {
|
---|
664 | _secSucc = _adviseReco * 60 + 1;
|
---|
665 | }
|
---|
666 | _numSucc = 0;
|
---|
667 | _currPause = _initPause;
|
---|
668 | _decodePause.setDate(QDate());
|
---|
669 | _decodePause.setTime(QTime());
|
---|
670 | }
|
---|
671 | else {
|
---|
672 | _secFail += _inspSegm;
|
---|
673 | _secSucc = 0;
|
---|
674 | if (_secFail > _adviseFail * 60) {
|
---|
675 | _secFail = _adviseFail * 60 + 1;
|
---|
676 | }
|
---|
677 | if (!_decodePause.isValid() || !_makePause) {
|
---|
678 | _decodePause = QDateTime::currentDateTime();
|
---|
679 | }
|
---|
680 | else {
|
---|
681 | _decodePause.setDate(QDate());
|
---|
682 | _decodePause.setTime(QTime());
|
---|
683 | _secFail = _secFail + _currPause - _inspSegm;
|
---|
684 | _currPause = _currPause * 2;
|
---|
685 | if (_currPause > 960) {
|
---|
686 | _currPause = 960;
|
---|
687 | }
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 | // End corrupt threshold
|
---|
692 | // ---------------------
|
---|
693 | if ( _begCorrupt && !_endCorrupt && _secSucc > _adviseReco * 60 ) {
|
---|
694 | _endDateCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
695 | _endTimeCor = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
696 | emit(newMessage((_staID + ": Recovery threshold exceeded, corruption ended "
|
---|
697 | + _endDateCor + " " + _endTimeCor).toAscii(), true));
|
---|
698 | callScript(("End_Corrupted " + _endDateCor + " " + _endTimeCor + " Begin was " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
699 | _endCorrupt = true;
|
---|
700 | _begCorrupt = false;
|
---|
701 | _secFail = 0;
|
---|
702 | }
|
---|
703 | else {
|
---|
704 |
|
---|
705 | // Begin corrupt threshold
|
---|
706 | // -----------------------
|
---|
707 | if ( !_begCorrupt && _secFail > _adviseFail * 60 ) {
|
---|
708 | _begDateCor = _decodeSucc.toUTC().date().toString("yy-MM-dd");
|
---|
709 | _begTimeCor = _decodeSucc.toUTC().time().toString("hh:mm:ss");
|
---|
710 | emit(newMessage((_staID + ": Failure threshold exceeded, corrupted since "
|
---|
711 | + _begDateCor + " " + _begTimeCor).toAscii(), true));
|
---|
712 | callScript(("Begin_Corrupted " + _begDateCor + " " + _begTimeCor).toAscii());
|
---|
713 | _begCorrupt = true;
|
---|
714 | _endCorrupt = false;
|
---|
715 | _secSucc = 0;
|
---|
716 | _numSucc = 0;
|
---|
717 | }
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | // End outage threshold
|
---|
722 | // --------------------
|
---|
723 | if ( _decodeStart.isValid() && _decodeStart.secsTo(QDateTime::currentDateTime()) > _adviseReco * 60 ) {
|
---|
724 | _decodeStart.setDate(QDate());
|
---|
725 | _decodeStart.setTime(QTime());
|
---|
726 | if (_inspSegm > 0) {
|
---|
727 | _endDateOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().date().toString("yy-MM-dd");
|
---|
728 | _endTimeOut = QDateTime::currentDateTime().addSecs(- _adviseReco * 60).toUTC().time().toString("hh:mm:ss");
|
---|
729 | emit(newMessage((_staID + ": Recovery threshold exceeded, outage ended "
|
---|
730 | + _endDateOut + " " + _endTimeOut).toAscii(), true));
|
---|
731 | callScript(("End_Outage " + _endDateOut + " " + _endTimeOut + " Begin was " + _begDateOut + " " + _begTimeOut).toAscii());
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | // Latency and completeness
|
---|
736 | // ------------------------
|
---|
737 | if ( _checkMountPoint == _staID || _checkMountPoint == "ALL" ) {
|
---|
738 | if (_perfIntr > 0 ) {
|
---|
739 |
|
---|
740 | QListIterator<p_obs> it(decoder->_obsList);
|
---|
741 | while (it.hasNext()) {
|
---|
742 | p_obs obs = it.next();
|
---|
743 |
|
---|
744 | _newSecGPS = static_cast<int>(obs->_o.GPSWeeks);
|
---|
745 | if (_newSecGPS != _oldSecGPS) {
|
---|
746 | if (_newSecGPS % _perfIntr < _oldSecGPS % _perfIntr) {
|
---|
747 | if (_numLat > 0) {
|
---|
748 | if (_meanDiff > 0.0) {
|
---|
749 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs, %7 gaps")
|
---|
750 | .arg(_staID.data())
|
---|
751 | .arg(int(_sumLat/_numLat*100)/100.)
|
---|
752 | .arg(int(_minLat*100)/100.)
|
---|
753 | .arg(int(_maxLat*100)/100.)
|
---|
754 | .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
|
---|
755 | .arg(_numLat)
|
---|
756 | .arg(_numGaps)
|
---|
757 | .toAscii(), true) );
|
---|
758 | } else {
|
---|
759 | emit( newMessage(QString("%1: Mean latency %2 sec, min %3, max %4, rms %5, %6 epochs")
|
---|
760 | .arg(_staID.data())
|
---|
761 | .arg(int(_sumLat/_numLat*100)/100.)
|
---|
762 | .arg(int(_minLat*100)/100.)
|
---|
763 | .arg(int(_maxLat*100)/100.)
|
---|
764 | .arg(int((sqrt((_sumLatQ - _sumLat * _sumLat / _numLat)/_numLat))*100)/100.)
|
---|
765 | .arg(_numLat)
|
---|
766 | .toAscii(), true) );
|
---|
767 | }
|
---|
768 | }
|
---|
769 | _meanDiff = _diffSecGPS / _numLat;
|
---|
770 | _diffSecGPS = 0;
|
---|
771 | _numGaps = 0;
|
---|
772 | _sumLat = 0.0;
|
---|
773 | _sumLatQ = 0.0;
|
---|
774 | _numLat = 0;
|
---|
775 | _minLat = _maxDt;
|
---|
776 | _maxLat = -_maxDt;
|
---|
777 | }
|
---|
778 | if (_followSec) {
|
---|
779 | _diffSecGPS += _newSecGPS - _oldSecGPS;
|
---|
780 | if (_meanDiff>0.) {
|
---|
781 | if (_newSecGPS - _oldSecGPS > 1.5 * _meanDiff) {
|
---|
782 | _numGaps += 1;
|
---|
783 | }
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | // Compute the observations latency
|
---|
788 | // --------------------------------
|
---|
789 | int week;
|
---|
790 | double sec;
|
---|
791 | currentGPSWeeks(week, sec);
|
---|
792 | const double secPerWeek = 7.0 * 24.0 * 3600.0;
|
---|
793 | if (week < obs->_o.GPSWeek) {
|
---|
794 | week += 1;
|
---|
795 | sec -= secPerWeek;
|
---|
796 | }
|
---|
797 | if (week > obs->_o.GPSWeek) {
|
---|
798 | week -= 1;
|
---|
799 | sec += secPerWeek;
|
---|
800 | }
|
---|
801 |
|
---|
802 | _curLat = sec - obs->_o.GPSWeeks;
|
---|
803 | _sumLat += _curLat;
|
---|
804 | _sumLatQ += _curLat * _curLat;
|
---|
805 | if (_curLat < _minLat) {
|
---|
806 | _minLat = _curLat;
|
---|
807 | }
|
---|
808 | if (_curLat >= _maxLat) {
|
---|
809 | _maxLat = _curLat;
|
---|
810 | }
|
---|
811 | _numLat += 1;
|
---|
812 | _oldSecGPS = _newSecGPS;
|
---|
813 | _followSec = true;
|
---|
814 | }
|
---|
815 | }
|
---|
816 | }
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | // Call advisory notice script
|
---|
821 | ////////////////////////////////////////////////////////////////////////////
|
---|
822 | void bncGetThread::latencyChecker::callScript(const char* comment) {
|
---|
823 | if (!_adviseScript.isEmpty()) {
|
---|
824 | msleep(1);
|
---|
825 | #ifdef WIN32
|
---|
826 | QProcess::startDetached(_adviseScript, QStringList() << _staID << comment) ;
|
---|
827 | #else
|
---|
828 | QProcess::startDetached("nohup", QStringList() << _adviseScript << _staID << comment) ;
|
---|
829 | #endif
|
---|
830 | }
|
---|
831 | }
|
---|