source: ntrip/trunk/BNC/src/bnccore.cpp@ 11187

Last change on this file since 11187 was 11187, checked in by stuerze, 10 days ago

Structured logging improved

File size: 32.9 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://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: t_bncCore
30 *
31 * Purpose: This class implements the main application
32 *
33 * Author: L. Mervart
34 *
35 * Created: 29-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <sstream>
43#include <QMessageBox>
44#include <cmath>
45#ifndef Q_OS_WIN
46#include <sys/stat.h>
47#endif
48
49#include "bnccore.h"
50#include "ssrQc.h"
51#include "bncutils.h"
52#include "bncrinex.h"
53#include "bncsettings.h"
54#include "bncversion.h"
55#include "ephemeris.h"
56#include "rinex/rnxobsfile.h"
57#include "rinex/rnxnavfile.h"
58#include "pppMain.h"
59#include "combination/bnccomb.h"
60
61using namespace std;
62
63// Singleton
64////////////////////////////////////////////////////////////////////////////
65t_bncCore* t_bncCore::instance() {
66 static t_bncCore _bncCore;
67 return &_bncCore;
68}
69
70// Constructor
71////////////////////////////////////////////////////////////////////////////
72t_bncCore::t_bncCore() : _ephUser(false) {
73 _logFileFlag = 0;
74 _logFile = 0;
75 _logStream = 0;
76 _logFileStructured = 0;
77 _logStructuredInit = false;
78 _logFormatStructured = t_logRecord::json;
79 _logLevelStructured = t_logRecord::debug;
80 _rawFile = 0;
81 _caster = 0;
82
83 // Eph file(s)
84 // -----------
85 _rinexVers = 0;
86 _ephFilePerStation = false;
87
88 _portEph = 0;
89 _serverEph = 0;
90 _socketsEph = 0;
91
92 _portCorr = 0;
93 _serverCorr = 0;
94 _socketsCorr = 0;
95
96 _pgmName = QString(BNCPGMNAME).leftJustified(20, ' ', true);
97#ifdef WIN32
98 _userName = QString("${USERNAME}");
99#else
100 _userName = QString("${USER}");
101#endif
102 expandEnvVar(_userName);
103
104 _userName = _userName.leftJustified(20, ' ', true);
105 _dateAndTimeGPS = 0;
106 _mainWindow = 0;
107
108 _pppMain = new BNC_PPP::t_pppMain();
109 _ssrQc = 0;
110 qRegisterMetaType< QVector<double> > ("QVector<double>");
111 qRegisterMetaType<bncTime> ("bncTime");
112 qRegisterMetaType<t_logRecord> ("t_logRecord");
113 qRegisterMetaType<t_ephGPS> ("t_ephGPS");
114 qRegisterMetaType<t_ephGlo> ("t_ephGlo");
115 qRegisterMetaType<t_ephGal> ("t_ephGal");
116 qRegisterMetaType<t_ephSBAS> ("t_ephSBAS");
117 qRegisterMetaType<t_ephBDS> ("t_ephBDS");
118 qRegisterMetaType<QList<t_orbCorr> > ("QList<t_orbCorr>");
119 qRegisterMetaType<QList<t_clkCorr> > ("QList<t_clkCorr>");
120 qRegisterMetaType<QList<t_satCodeBias> > ("QList<t_satCodeBias>");
121 qRegisterMetaType<QList<t_satPhaseBias> > ("QList<t_satPhaseBias>");
122 qRegisterMetaType<t_vTec> ("t_vTec");
123 qRegisterMetaType<t_metaData> ("t_metaData");
124 qRegisterMetaType<QList<t_satAntenna> > ("QList<t_satAntenna>");
125}
126
127// Destructor
128////////////////////////////////////////////////////////////////////////////
129t_bncCore::~t_bncCore() {
130 delete _logStream; _logStream = 0;
131 delete _logFile; _logFile = 0;
132 delete _logFileStructured; _logFileStructured = 0;
133 qDeleteAll(_ephFiles);
134 delete _serverEph;
135 delete _socketsEph;
136 delete _serverCorr;
137 delete _socketsCorr;
138 delete _dateAndTimeGPS;
139 if (_rawFile) {
140 delete _rawFile;
141 }
142
143 bncComb::destruct();
144 delete _ssrQc;
145
146 delete _pppMain;
147}
148
149// Write a Program Message
150////////////////////////////////////////////////////////////////////////////
151void t_bncCore::slotMessage(QByteArray msg, bool showOnScreen) {
152 if (msg.isEmpty()) {
153 return;
154 }
155 slotLogRecord(t_logRecord::fromMessage(msg), showOnScreen);
156}
157
158// Write a Program Message of a component with an explicit level
159////////////////////////////////////////////////////////////////////////////
160void t_bncCore::message(const QByteArray& msg, bool showOnScreen, const QByteArray& component,
161 t_logRecord::e_level level) {
162 if (msg.isEmpty()) {
163 return;
164 }
165 slotLogRecord(t_logRecord::message(level, msg, component), showOnScreen);
166}
167
168// Write a Program Message of a component
169////////////////////////////////////////////////////////////////////////////
170void t_bncCore::message(const QByteArray& msg, bool showOnScreen, const QByteArray& component) {
171 if (msg.isEmpty()) {
172 return;
173 }
174 slotLogRecord(t_logRecord::fromMessage(msg, component), showOnScreen);
175}
176
177// Write a Program Message with structured fields (structured logfile);
178// the classic logfile and the screen get the text rec._msg
179////////////////////////////////////////////////////////////////////////////
180void t_bncCore::slotLogRecord(t_logRecord rec, bool showOnScreen) {
181 if (rec._msg.isEmpty()) {
182 return;
183 }
184 QMutexLocker locker(&_mutexMessage);
185
186 messagePrivate(rec);
187 emit newMessage(rec._msg, showOnScreen);
188}
189
190// Write a Program Message (private, no lock)
191////////////////////////////////////////////////////////////////////////////
192void t_bncCore::messagePrivate(const t_logRecord& rec) {
193
194 // First time resolve the log file names
195 // -------------------------------------
196 QDateTime currDateTime = currentDateAndTimeGPS();
197 QDate currDate = currDateTime.date();
198 if (_logFileFlag == 0 || _fileDate != currDate) {
199 delete _logStream; _logStream = 0;
200 delete _logFile; _logFile = 0;
201 _logFileFlag = 1;
202 _fileDate = currDate;
203 bncSettings settings;
204 bool append = (Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked);
205 QString logFileName = settings.value("logFile").toString();
206 if ( !logFileName.isEmpty() ) {
207 expandEnvVar(logFileName);
208 _logFile = new QFile(logFileName + "_" +
209 currDate.toString("yyMMdd").toLatin1().data());
210 if (append) {
211 _logFile->open(QIODevice::WriteOnly | QIODevice::Append);
212 }
213 else {
214 _logFile->open(QIODevice::WriteOnly);
215 }
216 _logStream = new QTextStream();
217 _logStream->setDevice(_logFile);
218 }
219 }
220
221 // Structured logfile: one endless file (rotation by external tools)
222 // ---------------------------------------------------------------
223 if (!_logStructuredInit) {
224 _logStructuredInit = true;
225 bncSettings settings;
226 _logFileStructuredName = settings.value("logFileStructured").toString();
227 if (!_logFileStructuredName.isEmpty()) {
228 expandEnvVar(_logFileStructuredName);
229 _logFormatStructured = t_logRecord::formatFromString(
230 settings.value("logFormatStructured").toString());
231 _logLevelStructured = t_logRecord::levelFromString(
232 settings.value("logLevelStructured").toString());
233 openLogFileStructured();
234 }
235 }
236 else if (!_logFileStructuredName.isEmpty() && _logStructuredCheck.hasExpired(1000)) {
237 openLogFileStructured(); // reopens only if the file was moved or removed
238 }
239
240 if (_logStream) {
241 QByteArray msgLocal = rec._msg;
242 if (rec._msg.indexOf('\n') == 0) {
243 *_logStream << "\r\n";
244 msgLocal = rec._msg.mid(1);
245 }
246 *_logStream << currDateTime.toString("yy-MM-dd hh:mm:ss ").toLatin1().data();
247 *_logStream << msgLocal.data() << "\r\n";
248 _logStream->flush();
249 _logFile->flush();
250 }
251
252 if (_logFileStructured && rec._level >= _logLevelStructured) {
253 _logFileStructured->write(rec.encode(_logFormatStructured, currDateTime) + "\n");
254 _logFileStructured->flush();
255 }
256}
257
258// Open the structured logfile, or reopen it after an external rotation
259// (logrotate without copytruncate moves the file and the old one must be
260// released); always appending, also after copytruncate
261////////////////////////////////////////////////////////////////////////////
262void t_bncCore::openLogFileStructured() {
263 _logStructuredCheck.start();
264 if (_logFileStructured) {
265 bool same = _logFileStructured->exists();
266#ifndef Q_OS_WIN
267 if (same) {
268 struct stat stPath, stOpen;
269 same = (stat(QFile::encodeName(_logFileStructuredName).constData(), &stPath) == 0 &&
270 fstat(_logFileStructured->handle(), &stOpen) == 0 &&
271 stPath.st_dev == stOpen.st_dev && stPath.st_ino == stOpen.st_ino);
272 }
273#endif
274 if (same) {
275 return;
276 }
277 delete _logFileStructured; _logFileStructured = 0;
278 }
279 _logFileStructured = new QFile(_logFileStructuredName);
280 if (!_logFileStructured->open(QIODevice::WriteOnly | QIODevice::Append)) {
281 delete _logFileStructured; _logFileStructured = 0;
282 }
283}
284
285//
286////////////////////////////////////////////////////////////////////////////
287t_irc t_bncCore::checkPrintEph(t_eph* eph) {
288 QMutexLocker locker(&_mutex);
289 t_irc ircPut = _ephUser.putNewEph(eph, true);
290#ifdef BNC_DEBUG_BCE
291 if (eph->checkState() == t_eph::unhealthy) {
292 messagePrivate(t_logRecord::debugMessage("bce", QString("%1: UNHEALTHY %2:%3")
293 .arg(eph->receptStaID())
294 .arg(eph->typeStr(eph->type(), eph->prn(), 99.0))
295 .arg(eph->rinexDateStr(eph->TOC(), eph->prn(), 99.0)).toLatin1()));
296 }
297 if (eph->checkState() == t_eph::bad) {
298 messagePrivate(t_logRecord::debugMessage("bce", QString("%1: WRONG %2:%3")
299 .arg(eph->receptStaID())
300 .arg(eph->typeStr(eph->type(), eph->prn(), 99.0))
301 .arg(eph->rinexDateStr(eph->TOC(), eph->prn(), 99.0)).toLatin1()));
302 }
303 if (eph->checkState() == t_eph::outdated) {
304 messagePrivate(t_logRecord::debugMessage("bce", QString("%1: OUTDATED %2:%3")
305 .arg(eph->receptStaID())
306 .arg(eph->typeStr(eph->type(), eph->prn(), 99.0))
307 .arg(eph->rinexDateStr(eph->TOC(), eph->prn(), 99.0)).toLatin1()));
308 }
309#endif
310 t_ephFile* ephFile = printEphHeader(eph->receptStaID());
311 bool printFile = (ircPut == success);
312 if (ephFile && ephFile->_ephUser) {
313 // file per stream: new ephemerides of this stream, independent of other streams;
314 // the check state of the emitted ephemeris stays that of the common check
315 t_eph::e_checkState checkState = eph->checkState();
316 printFile = (ephFile->_ephUser->putNewEph(eph, true) == success);
317 eph->setCheckState(checkState);
318 }
319 printEph(ephFile, *eph, printFile);
320 return success;
321}
322
323// New GPS Ephemeris
324////////////////////////////////////////////////////////////////////////////
325void t_bncCore::slotNewGPSEph(t_ephGPS eph) {
326 if (checkPrintEph(&eph) == success) {
327 emit newGPSEph(eph);
328 }
329}
330
331// New Glonass Ephemeris
332////////////////////////////////////////////////////////////////////////////
333void t_bncCore::slotNewGlonassEph(t_ephGlo eph) {
334 if (checkPrintEph(&eph) == success) {
335 emit newGlonassEph(eph);
336 }
337}
338
339// New Galileo Ephemeris
340////////////////////////////////////////////////////////////////////////////
341void t_bncCore::slotNewGalileoEph(t_ephGal eph) {
342 if (checkPrintEph(&eph) == success) {
343 emit newGalileoEph(eph);
344 }
345}
346
347// New SBAS Ephemeris
348////////////////////////////////////////////////////////////////////////////
349void t_bncCore::slotNewSBASEph(t_ephSBAS eph) {
350 if (checkPrintEph(&eph) == success) {
351 emit newSBASEph(eph);
352 }
353}
354
355// New BDS Ephemeris
356////////////////////////////////////////////////////////////////////////////
357void t_bncCore::slotNewBDSEph(t_ephBDS eph) {
358 if (checkPrintEph(&eph) == success) {
359 emit newBDSEph(eph);
360 }
361}
362
363// Close RINEX Navigation File(s)
364////////////////////////////////////////////////////////////////////////////
365void t_bncCore::t_ephFile::close() {
366 delete _streamN; _streamN = 0;
367 delete _fileN; _fileN = 0;
368 delete _streamG; _streamG = 0;
369 delete _fileG; _fileG = 0;
370}
371
372// Destructor
373////////////////////////////////////////////////////////////////////////////
374t_bncCore::t_ephFile::~t_ephFile() {
375 close();
376 delete _ephUser;
377}
378
379// Station part of a RINEX navigation file name: "BRDC" (+ "00WRD" in RINEX 3/4)
380// for the single file, otherwise derived from the stream like the observation files
381////////////////////////////////////////////////////////////////////////////
382QString t_bncCore::ephFileNameBase(const QString& staID, const QString& country) const {
383 QString ID = staID.isEmpty() ? QString("BRDC") : staID.left(4).toUpper();
384 if (_rinexVers <= 2) {
385 return ID;
386 }
387 if (staID.size() >= 9 &&
388 isdigit((unsigned char) staID[4].toLatin1()) &&
389 isdigit((unsigned char) staID[5].toLatin1())) {
390 return staID.left(9).toUpper(); // e.g. BCEP00BKG0 -> BCEP00BKG
391 }
392 return ID + "00" + ((!staID.isEmpty() && country.size() >= 3) ? country.left(3).toUpper()
393 : QString("WRD")); // WORLD
394}
395
396// Print Header of the output File(s), returns the file of the stream
397////////////////////////////////////////////////////////////////////////////
398t_bncCore::t_ephFile* t_bncCore::printEphHeader(QString receptStaID) {
399
400 bncSettings settings;
401
402 // Initialization
403 // --------------
404 if (_rinexVers == 0) {
405
406 _rinexVers = settings.value("ephVersion").toInt();
407
408 _ephFilePerStation = Qt::CheckState(settings.value("ephFilePerStation").toInt()) == Qt::Checked;
409
410 _ephPath = settings.value("ephPath").toString();
411
412 if ( !_ephPath.isEmpty() ) {
413 if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
414 _ephPath += QDir::separator();
415 }
416 expandEnvVar(_ephPath);
417 }
418 }
419
420 if (_ephPath.isEmpty()) {
421 return 0;
422 }
423
424 QString key = _ephFilePerStation ? receptStaID : QString();
425
426 // Source comments and station part of the file name
427 // --------------------------------------------------
428 QStringList comments;
429 QMap<QString, QString> nameBase; // staID -> station part of its file name
430 QListIterator<QString> it(settings.value("mountPoints").toStringList());
431 while (it.hasNext()) {
432 QStringList hlp = it.next().split(" ");
433 if (hlp.size() < 7)
434 continue;
435 QUrl url(hlp[0]);
436 QString decoder = hlp[1];
437 QString staID = url.path().mid(1);
438 if (key.isEmpty() || staID == key) {
439 comments.append("Source: " + decoder +
440 " " + QUrl::toAce(url.host()) +
441 "/" + url.path().mid(1).toLatin1());
442 }
443 nameBase[staID] = ephFileNameBase(staID, hlp[2]);
444 }
445
446 QString base = (!key.isEmpty() && nameBase.contains(key)) ? nameBase[key]
447 : ephFileNameBase(key, QString());
448 QString distStr;
449 if (!key.isEmpty()) {
450 QMapIterator<QString, QString> im(nameBase);
451 while (im.hasNext()) {
452 im.next();
453 if (im.key() != key && im.value() == base) { // name conflict with another stream
454 distStr = "_" + key;
455 break;
456 }
457 }
458 }
459
460 t_ephFile*& ephFile = _ephFiles[key];
461 if (!ephFile) {
462 ephFile = new t_ephFile;
463 if (_ephFilePerStation) {
464 ephFile->_ephUser = new bncEphUser(false);
465 }
466 }
467
468 // (Re-)Open output File(s)
469 // ------------------------
470 QDateTime datTim = currentDateAndTimeGPS();
471
472 QString hlpStr = bncRinex::nextEpochStr(datTim,
473 settings.value("ephIntr").toString(), _rinexVers);
474
475 QString ephFileNameGPS = _ephPath + base;
476
477 if (_rinexVers > 2) {
478 ephFileNameGPS += "_S_" + // stream
479 QString("%1").arg(datTim.date().year()) +
480 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
481 hlpStr + // HM_period
482 "_MN" + // mixed BRDC
483 distStr + ".rnx";
484 }
485 else { // RNX v2.11
486 ephFileNameGPS += QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
487 hlpStr + distStr + datTim.toString(".yyN");
488 }
489
490 if (ephFile->_name == ephFileNameGPS) {
491 return ephFile;
492 }
493 ephFile->_name = ephFileNameGPS;
494 ephFile->close();
495
496 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
497 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
498
499 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
500 QFile::exists(ephFileNameGPS) ) {
501 appendFlagGPS = QIODevice::Append;
502 }
503
504 ephFile->_fileN = new QFile(ephFileNameGPS);
505 ephFile->_fileN->open(QIODevice::WriteOnly | appendFlagGPS);
506 ephFile->_streamN = new QTextStream();
507 ephFile->_streamN->setDevice(ephFile->_fileN);
508 QTextStream* streamGPS = ephFile->_streamN;
509
510 if (_rinexVers == 2) {
511 QString ephFileNameGlonass = _ephPath + base +
512 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
513 hlpStr + distStr + datTim.toString(".yyG");
514
515 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
516 QFile::exists(ephFileNameGlonass) ) {
517 appendFlagGlonass = QIODevice::Append;
518 }
519
520 ephFile->_fileG = new QFile(ephFileNameGlonass);
521 ephFile->_fileG->open(QIODevice::WriteOnly | appendFlagGlonass);
522 ephFile->_streamG = new QTextStream();
523 ephFile->_streamG->setDevice(ephFile->_fileG);
524 }
525 QTextStream* streamGlonass = ephFile->_streamG;
526
527 // Header - RINEX Version 3
528 // ------------------------
529 if (_rinexVers > 2) {
530 if ( ! (appendFlagGPS & QIODevice::Append)) {
531 double rinexVersion = defaultRnxNavVersion4;
532 if (_rinexVers < 4.0) {
533 rinexVersion = defaultRnxNavVersion3;
534 }
535 QString line = QString().asprintf("%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
536 rinexVersion, "", "");
537 *streamGPS << line;
538 line.clear();
539
540 QString hlp = currentDateAndTimeGPS().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
541 *streamGPS << _pgmName.toLatin1().data()
542 << _userName.toLatin1().data()
543 << hlp.toLatin1().data()
544 << "PGM / RUN BY / DATE\n";
545
546 QStringListIterator it(comments);
547 while (it.hasNext()) {
548 *streamGPS << it.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
549 }
550
551 if (_rinexVers == 4) {
552 int leapSecs = gnumleap(datTim.date().year(), datTim.date().month(), datTim.date().day());
553 *streamGPS << QString("%1").arg(leapSecs, 6, 10, QLatin1Char(' ')).left(60).leftJustified(60)
554 << "LEAP SECONDS\n";
555 }
556
557 line = QString().asprintf("%60sEND OF HEADER\n", "");
558 *streamGPS << line;
559
560 streamGPS->flush();
561 }
562 }
563
564 // Headers - RINEX Version 2
565 // -------------------------
566 else {
567 if (! (appendFlagGPS & QIODevice::Append)) {
568 QString line = QString().asprintf("%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n",
569 defaultRnxNavVersion2, "", "");
570 *streamGPS << line;
571 line.clear();
572
573 QString hlp = currentDateAndTimeGPS().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
574 *streamGPS << _pgmName.toLatin1().data()
575 << _userName.toLatin1().data()
576 << hlp.toLatin1().data()
577 << "PGM / RUN BY / DATE\n";
578
579 QStringListIterator it(comments);
580 while (it.hasNext()) {
581 *streamGPS << it.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
582 }
583
584 line = QString().asprintf("%60sEND OF HEADER\n", "");
585 *streamGPS << line;
586
587 streamGPS->flush();
588 }
589 if (! (appendFlagGlonass & QIODevice::Append)) {
590 QString line = QString().asprintf("%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",
591 defaultRnxNavVersion2, "", "");
592 *streamGlonass << line;
593 line.clear();
594
595 QString hlp = currentDateAndTimeGPS().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
596 *streamGlonass << _pgmName.toLatin1().data()
597 << _userName.toLatin1().data()
598 << hlp.toLatin1().data()
599 << "PGM / RUN BY / DATE\n";
600
601 QStringListIterator it(comments);
602 while (it.hasNext()) {
603 *streamGlonass << it.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
604 }
605
606 line = QString().asprintf("%60sEND OF HEADER\n", "");
607 *streamGlonass << line;
608
609 streamGlonass->flush();
610 }
611 }
612
613 return ephFile;
614}
615
616// Print One Ephemeris
617////////////////////////////////////////////////////////////////////////////
618void t_bncCore::printEph(t_ephFile* ephFile, const t_eph& eph, bool printFile) {
619
620 QString strV2 = eph.toString(defaultRnxNavVersion2);
621 QString strV3 = eph.toString(defaultRnxNavVersion3);
622 QString strV4 = eph.toString(defaultRnxNavVersion4);
623
624 QTextStream* streamN = ephFile ? ephFile->_streamN : 0;
625 QTextStream* streamG = ephFile ? ephFile->_streamG : 0;
626
627 if (_rinexVers == 2 && eph.system() == t_eph::GLONASS) {
628 printOutputEph(printFile, streamG, strV2, strV3, strV4);
629 }
630 else if (_rinexVers == 2 && eph.system() == t_eph::GPS) {
631 printOutputEph(printFile, streamN, strV2, strV3, strV4);
632 }
633 else if (_rinexVers >= 3) {
634 printOutputEph(printFile, streamN, strV2, strV3, strV4);
635 }
636}
637
638// Output
639////////////////////////////////////////////////////////////////////////////
640void t_bncCore::printOutputEph(bool printFile, QTextStream* stream,
641 const QString& strV2, const QString& strV3,
642 const QString& strV4) {
643 QString strVx;
644 if (_rinexVers == 2) {
645 strVx = strV2;
646 }
647 else if (_rinexVers == 3) {
648 strVx = strV3;
649 }
650 else if (_rinexVers == 4) {
651 strVx = strV4;
652 }
653
654 // Output into file
655 // ----------------
656 if (printFile && stream) {
657 *stream << strVx.toLatin1();
658 stream->flush();
659 }
660
661 // Output into the socket
662 // ----------------------
663 if (_socketsEph) {
664 QMutableListIterator<QTcpSocket*> is(*_socketsEph);
665 while (is.hasNext()) {
666 QTcpSocket* sock = is.next();
667 if (sock->state() == QAbstractSocket::ConnectedState) {
668 if (sock->write(strVx.toLatin1()) == -1) {
669 delete sock;
670 is.remove();
671 }
672 }
673 else if (sock->state() != QAbstractSocket::ConnectingState) {
674 delete sock;
675 is.remove();
676 }
677 }
678 }
679}
680
681// Set Port Number
682////////////////////////////////////////////////////////////////////////////
683void t_bncCore::setPortEph(int port) {
684 _portEph = port;
685 if (_portEph != 0) {
686 delete _serverEph;
687 _serverEph = new QTcpServer;
688 _serverEph->setProxy(QNetworkProxy::NoProxy);
689 if ( !_serverEph->listen(QHostAddress::LocalHost, _portEph) ) {
690 QString message = "t_bncCore: Cannot listen on ephemeris port "
691 + QByteArray::number(_portEph) + ": "
692 + _serverEph->errorString();
693 slotLogRecord(t_logRecord::message(t_logRecord::error, message.toLatin1(), "bnc"), true);
694 }
695 connect(_serverEph, SIGNAL(newConnection()), this, SLOT(slotNewConnectionEph()));
696 delete _socketsEph;
697 _socketsEph = new QList<QTcpSocket*>;
698 }
699}
700
701// Set Port Number
702////////////////////////////////////////////////////////////////////////////
703void t_bncCore::setPortCorr(int port) {
704 _portCorr = port;
705 if (_portCorr != 0) {
706 delete _serverCorr;
707 _serverCorr = new QTcpServer;
708 _serverCorr->setProxy(QNetworkProxy::NoProxy);
709 if ( !_serverCorr->listen(QHostAddress::LocalHost, _portCorr) ) {
710 QString message = "t_bncCore: Cannot listen on correction port "
711 + QByteArray::number(_portCorr) + ": "
712 + _serverCorr->errorString();
713 slotLogRecord(t_logRecord::message(t_logRecord::error, message.toLatin1(), "bnc"), true);
714 }
715 connect(_serverCorr, SIGNAL(newConnection()), this, SLOT(slotNewConnectionCorr()));
716 delete _socketsCorr;
717 _socketsCorr = new QList<QTcpSocket*>;
718 }
719}
720
721// New Connection
722////////////////////////////////////////////////////////////////////////////
723void t_bncCore::slotNewConnectionEph() {
724 _socketsEph->push_back( _serverEph->nextPendingConnection() );
725}
726
727// New Connection
728////////////////////////////////////////////////////////////////////////////
729void t_bncCore::slotNewConnectionCorr() {
730 _socketsCorr->push_back( _serverCorr->nextPendingConnection() );
731}
732
733//
734////////////////////////////////////////////////////////////////////////////
735void t_bncCore::slotQuit() {
736 delete _caster; _caster = 0;
737 qApp->quit();
738}
739
740//
741////////////////////////////////////////////////////////////////////////////
742void t_bncCore::slotNewOrbCorrections(QList<t_orbCorr> orbCorrections) {
743 QMutexLocker locker(&_mutex);
744 emit newOrbCorrections(orbCorrections);
745 if (_socketsCorr) {
746 ostringstream out;
747 t_orbCorr::writeEpoch(&out, orbCorrections);
748 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
749 while (is.hasNext()) {
750 QTcpSocket* sock = is.next();
751 if (sock->state() == QAbstractSocket::ConnectedState) {
752 if (sock->write(out.str().c_str()) == -1) {
753 delete sock;
754 is.remove();
755 }
756 }
757 else if (sock->state() != QAbstractSocket::ConnectingState) {
758 delete sock;
759 is.remove();
760 }
761 }
762 }
763}
764
765//
766////////////////////////////////////////////////////////////////////////////
767void t_bncCore::slotNewClkCorrections(QList<t_clkCorr> clkCorrections) {
768 QMutexLocker locker(&_mutex);
769 emit newClkCorrections(clkCorrections);
770 if (_socketsCorr) {
771 ostringstream out;
772 t_clkCorr::writeEpoch(&out, clkCorrections);
773 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
774 while (is.hasNext()) {
775 QTcpSocket* sock = is.next();
776 if (sock->state() == QAbstractSocket::ConnectedState) {
777 if (sock->write(out.str().c_str()) == -1) {
778 delete sock;
779 is.remove();
780 }
781 }
782 else if (sock->state() != QAbstractSocket::ConnectingState) {
783 delete sock;
784 is.remove();
785 }
786 }
787 }
788}
789
790//
791////////////////////////////////////////////////////////////////////////////
792void t_bncCore::slotNewCodeBiases(QList<t_satCodeBias> codeBiases) {
793 QMutexLocker locker(&_mutex);
794 emit newCodeBiases(codeBiases);
795 if (_socketsCorr) {
796 ostringstream out;
797 t_satCodeBias::writeEpoch(&out, codeBiases);
798 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
799 while (is.hasNext()) {
800 QTcpSocket* sock = is.next();
801 if (sock->state() == QAbstractSocket::ConnectedState) {
802 if (sock->write(out.str().c_str()) == -1) {
803 delete sock;
804 is.remove();
805 }
806 }
807 else if (sock->state() != QAbstractSocket::ConnectingState) {
808 delete sock;
809 is.remove();
810 }
811 }
812 }
813}
814
815//
816////////////////////////////////////////////////////////////////////////////
817void t_bncCore::slotNewPhaseBiases(QList<t_satPhaseBias> phaseBiases) {
818 QMutexLocker locker(&_mutex);
819 emit newPhaseBiases(phaseBiases);
820 if (_socketsCorr) {
821 ostringstream out;
822 t_satPhaseBias::writeEpoch(&out, phaseBiases);
823 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
824 while (is.hasNext()) {
825 QTcpSocket* sock = is.next();
826 if (sock->state() == QAbstractSocket::ConnectedState) {
827 if (sock->write(out.str().c_str()) == -1) {
828 delete sock;
829 is.remove();
830 }
831 }
832 else if (sock->state() != QAbstractSocket::ConnectingState) {
833 delete sock;
834 is.remove();
835 }
836 }
837 }
838}
839
840//
841////////////////////////////////////////////////////////////////////////////
842void t_bncCore::slotNewTec(t_vTec vTec) {
843 QMutexLocker locker(&_mutex);
844 emit newTec(vTec);
845 if (_socketsCorr) {
846 ostringstream out;
847 t_vTec::write(&out, vTec);
848 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
849 while (is.hasNext()) {
850 QTcpSocket* sock = is.next();
851 if (sock->state() == QAbstractSocket::ConnectedState) {
852 if (sock->write(out.str().c_str()) == -1) {
853 delete sock;
854 is.remove();
855 }
856 }
857 else if (sock->state() != QAbstractSocket::ConnectingState) {
858 delete sock;
859 is.remove();
860 }
861 }
862 }
863}
864
865//
866////////////////////////////////////////////////////////////////////////////
867void t_bncCore::slotNewMetaData(t_metaData metaData) {
868 QMutexLocker locker(&_mutex);
869 emit newMetaData(metaData);
870 if (_socketsCorr) {
871 ostringstream out;
872 t_metaData::write(&out, metaData);
873 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
874 while (is.hasNext()) {
875 QTcpSocket* sock = is.next();
876 if (sock->state() == QAbstractSocket::ConnectedState) {
877 if (sock->write(out.str().c_str()) == -1) {
878 delete sock;
879 is.remove();
880 }
881 }
882 else if (sock->state() != QAbstractSocket::ConnectingState) {
883 delete sock;
884 is.remove();
885 }
886 }
887 }
888}
889
890//
891////////////////////////////////////////////////////////////////////////////
892void t_bncCore::slotNewSatAntennas(QList<t_satAntenna> satAntennas) {
893 QMutexLocker locker(&_mutex);
894 emit newSatAntennas(satAntennas);
895 if (_socketsCorr) {
896 ostringstream out;
897 t_satAntenna::writeEpoch(&out, satAntennas);
898 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
899 while (is.hasNext()) {
900 QTcpSocket* sock = is.next();
901 if (sock->state() == QAbstractSocket::ConnectedState) {
902 if (sock->write(out.str().c_str()) == -1) {
903 delete sock;
904 is.remove();
905 }
906 }
907 else if (sock->state() != QAbstractSocket::ConnectingState) {
908 delete sock;
909 is.remove();
910 }
911 }
912 }
913}
914
915//
916////////////////////////////////////////////////////////////////////////////
917void t_bncCore::setConfFileName(const QString& confFileName) {
918 if (confFileName.isEmpty()) {
919 _confFileName = QDir::homePath() + QDir::separator()
920 + ".config" + QDir::separator()
921 + qApp->organizationName() + QDir::separator()
922 + qApp->applicationName() + ".bnc";
923 }
924 else {
925 _confFileName = confFileName;
926 }
927}
928
929// Raw Output
930////////////////////////////////////////////////////////////////////////////
931void t_bncCore::writeRawData(const QByteArray& data, const QByteArray& staID,
932 const QByteArray& format) {
933
934 QMutexLocker locker(&_mutex);
935
936 if (!_rawFile) {
937 bncSettings settings;
938 QByteArray fileName = settings.value("rawOutFile").toByteArray();
939 if (!fileName.isEmpty()) {
940 _rawFile = new bncRawFile(fileName, staID, bncRawFile::output);
941 }
942 }
943
944 if (_rawFile) {
945 _rawFile->writeRawData(data, staID, format);
946 }
947}
948
949//
950////////////////////////////////////////////////////////////////////////////
951void t_bncCore::initCombination() {
952 bncComb::getInstance();
953 if (BNC_CMB->nStreams() < 1) {
954 bncComb::destruct();
955 }
956}
957
958//
959////////////////////////////////////////////////////////////////////////////
960void t_bncCore::stopCombination() {
961 bncComb::destruct();
962}
963
964// SSR QC of the decoded correction streams (optional)
965////////////////////////////////////////////////////////////////////////////
966void t_bncCore::initSsrQc() {
967 delete _ssrQc; _ssrQc = 0;
968 bncSettings settings;
969 QString dirName = settings.value("ssrQcPath").toString();
970 if (dirName.isEmpty()) {
971 return;
972 }
973 expandEnvVar(dirName);
974 QStringList streams = settings.value("ssrQcStreams").toString().split(QRegularExpression("[ ,;]+"), Qt::SkipEmptyParts);
975 if (streams.isEmpty()) {
976 streams << "ALL";
977 }
978 QStringList hlp = settings.value("ssrQcIntr").toString().split(' ', Qt::SkipEmptyParts);
979 double interval = 3600.0;
980 if (hlp.size() == 2) {
981 double fac = (hlp[1] == "min") ? 60.0 : (hlp[1] == "hour") ? 3600.0 : (hlp[1] == "day") ? 86400.0 : 0.0;
982 if (hlp[0].toDouble() * fac > 0.0) {
983 interval = hlp[0].toDouble() * fac;
984 }
985 }
986 _ssrQc = new t_ssrQc(dirName, interval, streams, settings.value("ssrQcRef").toString().trimmed());
987}
988
989// Writes the final SSR QC report
990////////////////////////////////////////////////////////////////////////////
991void t_bncCore::stopSsrQc() {
992 delete _ssrQc; _ssrQc = 0;
993}
994
995//
996////////////////////////////////////////////////////////////////////////////
997bool t_bncCore::dateAndTimeGPSSet() const {
998 QMutexLocker locker(&_mutexDateAndTimeGPS);
999 if (_dateAndTimeGPS) {
1000 return true;
1001 }
1002 else {
1003 return false;
1004 }
1005}
1006
1007//
1008////////////////////////////////////////////////////////////////////////////
1009QDateTime t_bncCore::dateAndTimeGPS() const {
1010 QMutexLocker locker(&_mutexDateAndTimeGPS);
1011 if (_dateAndTimeGPS) {
1012 return *_dateAndTimeGPS;
1013 }
1014 else {
1015 return QDateTime();
1016 }
1017}
1018
1019//
1020////////////////////////////////////////////////////////////////////////////
1021void t_bncCore::setDateAndTimeGPS(QDateTime dateTime) {
1022 QMutexLocker locker(&_mutexDateAndTimeGPS);
1023 delete _dateAndTimeGPS;
1024 _dateAndTimeGPS = new QDateTime(dateTime);
1025}
1026
1027//
1028////////////////////////////////////////////////////////////////////////////
1029void t_bncCore::startPPP() {
1030 _pppMain->start();
1031}
1032
1033//
1034////////////////////////////////////////////////////////////////////////////
1035void t_bncCore::stopPPP() {
1036 _pppMain->stop();
1037 emit stopRinexPPP();
1038}
1039
Note: See TracBrowser for help on using the repository browser.