source: ntrip/trunk/BNC/bncapp.cpp@ 1000

Last change on this file since 1000 was 995, checked in by mervart, 16 years ago

* empty log message *

File size: 19.7 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: bncApp
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 <QSettings>
43#include <QMessageBox>
44#include <cmath>
45#include <unistd.h>
46
47#include "bncapp.h"
48#include "bncutils.h"
49#include "bncrinex.h"
50
51using namespace std;
52
53struct converttimeinfo {
54 int second; /* seconds of GPS time [0..59] */
55 int minute; /* minutes of GPS time [0..59] */
56 int hour; /* hour of GPS time [0..24] */
57 int day; /* day of GPS time [1..28..30(31)*/
58 int month; /* month of GPS time [1..12]*/
59 int year; /* year of GPS time [1980..] */
60};
61
62extern "C" {
63 void converttime(struct converttimeinfo *c, int week, int tow);
64 void updatetime(int *week, int *tow, int tk, int fixnumleap);
65}
66
67// Constructor
68////////////////////////////////////////////////////////////////////////////
69bncApp::bncApp(int argc, char* argv[], bool GUIenabled) :
70 QApplication(argc, argv, GUIenabled) {
71
72 _bncVersion = "BNC 1.6";
73
74 _logFileFlag = 0;
75 _logFile = 0;
76 _logStream = 0;
77 _caster = 0;
78
79 // Lists of Ephemeris
80 // ------------------
81 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
82 _gpsEph[ii-PRN_GPS_START] = 0;
83 }
84 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
85 _glonassEph[ii-PRN_GLONASS_START] = 0;
86 }
87
88 // Eph file(s)
89 // -----------
90 _rinexVers = 0;
91 _ephFileGPS = 0;
92 _ephStreamGPS = 0;
93 _ephFileGlonass = 0;
94 _ephStreamGlonass = 0;
95
96 _port = 0;
97 _server = 0;
98 _sockets = 0;
99
100 _portCorr = 0;
101 _serverCorr = 0;
102 _socketsCorr = 0;
103
104 _pgmName = _bncVersion.leftJustified(20, ' ', true);
105#ifdef WIN32
106 _userName = QString("${USERNAME}");
107#else
108 _userName = QString("${USER}");
109#endif
110 expandEnvVar(_userName);
111 _userName = _userName.leftJustified(20, ' ', true);
112
113 QSettings settings;
114 _lastDumpCoSec = 0;
115 _waitCoTime = settings.value("corrTime").toInt();
116 if (_waitCoTime < 1) {
117 _waitCoTime = 1;
118 }
119
120 _corrs = new QMultiMap<long, QString>;
121}
122
123// Destructor
124////////////////////////////////////////////////////////////////////////////
125bncApp::~bncApp() {
126 delete _logStream;
127 delete _logFile;
128 delete _ephStreamGPS;
129 delete _ephFileGPS;
130 delete _server;
131 delete _sockets;
132 delete _serverCorr;
133 delete _socketsCorr;
134 if (_rinexVers == 2) {
135 delete _ephStreamGlonass;
136 delete _ephFileGlonass;
137 }
138 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
139 delete _gpsEph[ii-PRN_GPS_START];
140 }
141 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
142 delete _glonassEph[ii-PRN_GLONASS_START];
143 }
144
145 delete _corrs;
146}
147
148// Write a Program Message
149////////////////////////////////////////////////////////////////////////////
150void bncApp::slotMessage(const QByteArray msg) {
151
152 QMutexLocker locker(&_mutex);
153
154 messagePrivate(msg);
155}
156
157// Write a Program Message (private, no lock)
158////////////////////////////////////////////////////////////////////////////
159void bncApp::messagePrivate(const QByteArray& msg) {
160
161 // First time resolve the log file name
162 // ------------------------------------
163 if (_logFileFlag == 0) {
164 _logFileFlag = 1;
165 QSettings settings;
166 QString logFileName = settings.value("logFile").toString();
167 if ( !logFileName.isEmpty() ) {
168 expandEnvVar(logFileName);
169 _logFile = new QFile(logFileName);
170 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
171 _logFile->open(QIODevice::WriteOnly | QIODevice::Append);
172 }
173 else {
174 _logFile->open(QIODevice::WriteOnly);
175 }
176 _logStream = new QTextStream();
177 _logStream->setDevice(_logFile);
178 }
179 }
180
181 if (_logStream) {
182 *_logStream << QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
183 *_logStream << msg.data() << endl;
184 _logStream->flush();
185 }
186}
187
188// New GPS Ephemeris
189////////////////////////////////////////////////////////////////////////////
190void bncApp::slotNewGPSEph(gpsephemeris* gpseph) {
191
192 QMutexLocker locker(&_mutex);
193
194 printEphHeader();
195
196 gpsephemeris** ee = &_gpsEph[gpseph->satellite-1];
197 if ( *ee == 0 ||
198 gpseph->GPSweek > (*ee)->GPSweek ||
199 (gpseph->GPSweek == (*ee)->GPSweek && gpseph->TOC > (*ee)->TOC) ) {
200 delete *ee;
201 *ee = gpseph;
202 printGPSEph(gpseph, true);
203 }
204 else {
205 printGPSEph(gpseph, false);
206 delete gpseph;
207 }
208}
209
210// New Glonass Ephemeris
211////////////////////////////////////////////////////////////////////////////
212void bncApp::slotNewGlonassEph(glonassephemeris* glonasseph) {
213
214 QMutexLocker locker(&_mutex);
215
216 printEphHeader();
217
218 glonassephemeris** ee = &_glonassEph[glonasseph->almanac_number-1];
219
220 int wwOld, towOld, wwNew, towNew;
221 if (*ee != 0) {
222 wwOld = (*ee)->GPSWeek;
223 towOld = (*ee)->GPSTOW;
224 updatetime(&wwOld, &towOld, (*ee)->tb*1000, 0);
225
226 wwNew = glonasseph->GPSWeek;
227 towNew = glonasseph->GPSTOW;
228 updatetime(&wwNew, &towNew, glonasseph->tb*1000, 0);
229 }
230
231 if ( *ee == 0 ||
232 wwNew > wwOld ||
233 (wwNew == wwOld && towNew > towOld) ) {
234 delete *ee;
235 *ee = glonasseph;
236 printGlonassEph(glonasseph, true);
237 }
238 else {
239 printGlonassEph(glonasseph, false);
240 delete glonasseph;
241 }
242}
243
244// Print Header of the output File(s)
245////////////////////////////////////////////////////////////////////////////
246void bncApp::printEphHeader() {
247
248 QSettings settings;
249
250 // Initialization
251 // --------------
252 if (_rinexVers == 0) {
253
254 if ( Qt::CheckState(settings.value("ephV3").toInt()) == Qt::Checked) {
255 _rinexVers = 3;
256 }
257 else {
258 _rinexVers = 2;
259 }
260
261 _ephPath = settings.value("ephPath").toString();
262
263 if ( !_ephPath.isEmpty() ) {
264 if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
265 _ephPath += QDir::separator();
266 }
267 expandEnvVar(_ephPath);
268 }
269 }
270
271 // (Re-)Open output File(s)
272 // ------------------------
273 if (!_ephPath.isEmpty()) {
274
275 QDateTime datTim = QDateTime::currentDateTime().toUTC();
276
277 QString ephFileNameGPS = _ephPath + "BRDC" +
278 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'));
279
280 QString hlpStr = bncRinex::nextEpochStr(datTim,
281 settings.value("ephIntr").toString());
282
283 if (_rinexVers == 3) {
284 ephFileNameGPS += hlpStr + datTim.toString(".yyP");
285 }
286 else {
287 ephFileNameGPS += hlpStr + datTim.toString(".yyN");
288 }
289
290 if (_ephFileNameGPS == ephFileNameGPS) {
291 return;
292 }
293 else {
294 _ephFileNameGPS = ephFileNameGPS;
295 }
296
297 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
298 delete _gpsEph[ii-PRN_GPS_START];
299 _gpsEph[ii-PRN_GPS_START] = 0;
300 }
301 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
302 delete _glonassEph[ii-PRN_GLONASS_START];
303 _glonassEph[ii-PRN_GLONASS_START] = 0;
304 }
305
306 delete _ephStreamGPS;
307 delete _ephFileGPS;
308
309 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
310 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
311
312 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
313 QFile::exists(ephFileNameGPS) ) {
314 appendFlagGPS = QIODevice::Append;
315 }
316
317 _ephFileGPS = new QFile(ephFileNameGPS);
318 _ephFileGPS->open(QIODevice::WriteOnly | appendFlagGPS);
319 _ephStreamGPS = new QTextStream();
320 _ephStreamGPS->setDevice(_ephFileGPS);
321
322 if (_rinexVers == 3) {
323 _ephFileGlonass = _ephFileGPS;
324 _ephStreamGlonass = _ephStreamGPS;
325 }
326 else if (_rinexVers == 2) {
327 QString ephFileNameGlonass = _ephPath + "BRDC" +
328 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
329 hlpStr + datTim.toString(".yyG");
330
331 delete _ephStreamGlonass;
332 delete _ephFileGlonass;
333
334 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
335 QFile::exists(ephFileNameGlonass) ) {
336 appendFlagGlonass = QIODevice::Append;
337 }
338
339 _ephFileGlonass = new QFile(ephFileNameGlonass);
340 _ephFileGlonass->open(QIODevice::WriteOnly | appendFlagGlonass);
341 _ephStreamGlonass = new QTextStream();
342 _ephStreamGlonass->setDevice(_ephFileGlonass);
343 }
344
345 // Header - RINEX Version 3
346 // ------------------------
347 if (_rinexVers == 3) {
348 if ( ! (appendFlagGPS & QIODevice::Append)) {
349 QString line;
350 line.sprintf(
351 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
352 3.0, "", "");
353 *_ephStreamGPS << line;
354
355 QString hlp = QDateTime::currentDateTime().toUTC().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
356 *_ephStreamGPS << _pgmName.toAscii().data()
357 << _userName.toAscii().data()
358 << hlp.toAscii().data()
359 << "PGM / RUN BY / DATE" << endl;
360
361 line.sprintf("%60sEND OF HEADER\n", "");
362 *_ephStreamGPS << line;
363
364 _ephStreamGPS->flush();
365 }
366 }
367
368 // Headers - RINEX Version 2
369 // -------------------------
370 else if (_rinexVers == 2) {
371 if (! (appendFlagGPS & QIODevice::Append)) {
372 QString line;
373 line.sprintf(
374 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.11, "", "");
375 *_ephStreamGPS << line;
376
377 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
378 *_ephStreamGPS << _pgmName.toAscii().data()
379 << _userName.toAscii().data()
380 << hlp.toAscii().data()
381 << "PGM / RUN BY / DATE" << endl;
382
383 line.sprintf("%60sEND OF HEADER\n", "");
384 *_ephStreamGPS << line;
385
386 _ephStreamGPS->flush();
387 }
388 if (! (appendFlagGlonass & QIODevice::Append)) {
389 QString line;
390 line.sprintf(
391 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",2.11,"","");
392 *_ephStreamGlonass << line;
393
394 QString hlp = QDateTime::currentDateTime().toUTC().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
395 *_ephStreamGlonass << _pgmName.toAscii().data()
396 << _userName.toAscii().data()
397 << hlp.toAscii().data()
398 << "PGM / RUN BY / DATE" << endl;
399
400 line.sprintf("%60sEND OF HEADER\n", "");
401 *_ephStreamGlonass << line;
402
403 _ephStreamGlonass->flush();
404 }
405 }
406 }
407}
408
409// Print One GPS Ephemeris
410////////////////////////////////////////////////////////////////////////////
411void bncApp::printGPSEph(gpsephemeris* ep, bool printFile) {
412
413 QString lineV2;
414 QString lineV3;
415
416 struct converttimeinfo cti;
417 converttime(&cti, ep->GPSweek, ep->TOC);
418
419 lineV3.sprintf("G%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
420 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
421 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
422 ep->clock_driftrate);
423
424 lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
425 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
426 cti.minute, (double) cti.second, ep->clock_bias,
427 ep->clock_drift, ep->clock_driftrate);
428
429 QString line;
430 QByteArray allLines;
431
432 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", (double)ep->IODE,
433 ep->Crs, ep->Delta_n, ep->M0);
434 allLines += line;
435
436 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->Cuc,
437 ep->e, ep->Cus, ep->sqrt_A);
438 allLines += line;
439
440 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n",
441 (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
442 allLines += line;
443
444 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->i0,
445 ep->Crc, ep->omega, ep->OMEGADOT);
446 allLines += line;
447
448 double dd = 0;
449 unsigned long ii = ep->flags;
450 if(ii & GPSEPHF_L2CACODE)
451 dd += 2.0;
452 if(ii & GPSEPHF_L2PCODE)
453 dd += 1.0;
454 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->IDOT, dd,
455 (double) ep->GPSweek, ii & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
456 allLines += line;
457
458 if(ep->URAindex <= 6) /* URA index */
459 dd = ceil(10.0*pow(2.0, 1.0+((double)ep->URAindex)/2.0))/10.0;
460 else
461 dd = ceil(10.0*pow(2.0, ((double)ep->URAindex)/2.0))/10.0;
462 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", dd,
463 ((double) ep->SVhealth), ep->TGD, ((double) ep->IODC));
464 allLines += line;
465
466 line.sprintf(" %18.11e %18.11e\n", ((double)ep->TOW), 0.0);
467 allLines += line;
468
469 printOutput(printFile, _ephStreamGPS, lineV2, lineV3, allLines);
470}
471
472// Print One Glonass Ephemeris
473////////////////////////////////////////////////////////////////////////////
474void bncApp::printGlonassEph(glonassephemeris* ep, bool printFile) {
475
476 int ww = ep->GPSWeek;
477 int tow = ep->GPSTOW;
478 struct converttimeinfo cti;
479
480 updatetime(&ww, &tow, ep->tb*1000, 0);
481 converttime(&cti, ww, tow);
482
483 int tk = ep->tk-3*60*60;
484 if (tk < 0) {
485 tk += 86400;
486 }
487
488 QString lineV2;
489 QString lineV3;
490
491 lineV3.sprintf("R%02d %04d %02d %02d %02d %02d %02d %18.11e %18.11e %18.11e\n",
492 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
493 cti.minute, cti.second, -ep->tau, ep->gamma, (double) tk);
494
495 lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f %18.11e %18.11e %18.11e\n",
496 ep->almanac_number, cti.year%100, cti.month, cti.day,
497 cti.hour, cti.minute, (double) cti.second, -ep->tau,
498 ep->gamma, (double) tk);
499
500 QString line;
501 QByteArray allLines;
502
503 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->x_pos,
504 ep->x_velocity, ep->x_acceleration,
505 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
506 allLines += line;
507
508 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->y_pos,
509 ep->y_velocity, ep->y_acceleration,
510 (double) ep->frequency_number);
511 allLines += line;
512
513 line.sprintf(" %18.11e %18.11e %18.11e %18.11e\n", ep->z_pos,
514 ep->z_velocity, ep->z_acceleration, (double) ep->E);
515 allLines += line;
516
517 printOutput(printFile, _ephStreamGlonass, lineV2, lineV3, allLines);
518}
519
520// Output
521////////////////////////////////////////////////////////////////////////////
522void bncApp::printOutput(bool printFile, QTextStream* stream,
523 const QString& lineV2,
524 const QString& lineV3,
525 const QByteArray& allLines) {
526 // Output into file
527 // ----------------
528 if (printFile && stream) {
529 if (_rinexVers == 2) {
530 *stream << lineV2.toAscii();
531 }
532 else {
533 *stream << lineV3.toAscii();
534 }
535 *stream << allLines;
536 stream->flush();
537 }
538
539 // Output into the socket
540 // ----------------------
541 if (_sockets) {
542 QMutableListIterator<QTcpSocket*> is(*_sockets);
543 while (is.hasNext()) {
544 QTcpSocket* sock = is.next();
545 if (sock->state() == QAbstractSocket::ConnectedState) {
546 if (sock->write(lineV3.toAscii()) == -1 ||
547 sock->write(allLines) == -1) {
548 delete sock;
549 is.remove();
550 }
551 }
552 else if (sock->state() != QAbstractSocket::ConnectingState) {
553 delete sock;
554 is.remove();
555 }
556 }
557 }
558}
559
560// Set Port Number
561////////////////////////////////////////////////////////////////////////////
562void bncApp::setPort(int port) {
563 _port = port;
564 if (_port != 0) {
565 delete _server;
566 _server = new QTcpServer;
567 _server->listen(QHostAddress::Any, _port);
568 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
569 delete _sockets;
570 _sockets = new QList<QTcpSocket*>;
571 }
572}
573
574// Set Port Number
575////////////////////////////////////////////////////////////////////////////
576void bncApp::setPortCorr(int port) {
577 _portCorr = port;
578 if (_portCorr != 0) {
579 delete _serverCorr;
580 _serverCorr = new QTcpServer;
581 _serverCorr->listen(QHostAddress::Any, _portCorr);
582 connect(_serverCorr, SIGNAL(newConnection()), this, SLOT(slotNewConnectionCorr()));
583 delete _socketsCorr;
584 _socketsCorr = new QList<QTcpSocket*>;
585 }
586}
587
588// New Connection
589////////////////////////////////////////////////////////////////////////////
590void bncApp::slotNewConnection() {
591 _sockets->push_back( _server->nextPendingConnection() );
592}
593
594// New Connection
595////////////////////////////////////////////////////////////////////////////
596void bncApp::slotNewConnectionCorr() {
597 _socketsCorr->push_back( _serverCorr->nextPendingConnection() );
598}
599
600//
601////////////////////////////////////////////////////////////////////////////
602void bncApp::slotQuit() {
603 cout << "bncApp::slotQuit" << endl;
604 delete _caster;
605 quit();
606}
607
608//
609////////////////////////////////////////////////////////////////////////////
610void bncApp::slotNewCorrLine(QString line, QString staID, long coTime) {
611
612 QMutexLocker locker(&_mutex);
613
614 if (!_socketsCorr) {
615 return;
616 }
617
618 QSettings settings;
619 _waitCoTime = settings.value("corrTime").toInt();
620 if (_waitCoTime < 1) {
621 _waitCoTime = 1;
622 }
623
624 // First time, set the _lastDumpSec immediately
625 // --------------------------------------------
626 if (_lastDumpCoSec == 0) {
627 _lastDumpCoSec = coTime - 1;
628 }
629
630 // An old correction - throw it away
631 // ---------------------------------
632 if (coTime <= _lastDumpCoSec) {
633 QString line = "old correction: " + staID +
634 QString().sprintf(" age %ld",
635 _lastDumpCoSec - coTime + _waitCoTime);
636 messagePrivate(line.toAscii());
637 emit( newMessage(line.toAscii()) );
638 return;
639 }
640
641 _corrs->insert(coTime, QString(line + " " + staID));
642
643 // Dump Corrections
644 // ----------------
645 if (coTime - _waitCoTime > _lastDumpCoSec) {
646 dumpCorrs(_lastDumpCoSec + 1, coTime - _waitCoTime);
647 _lastDumpCoSec = coTime - _waitCoTime;
648 }
649}
650
651// Dump Complete Correction Epochs
652////////////////////////////////////////////////////////////////////////////
653void bncApp::dumpCorrs(long minTime, long maxTime) {
654
655 for (long sec = minTime; sec <= maxTime; sec++) {
656 QList<QString> allCorrs = _corrs->values(sec);
657 QListIterator<QString> it(allCorrs);
658 while (it.hasNext()) {
659 QString corrLine = it.next() + "\n";
660
661 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
662 while (is.hasNext()) {
663 QTcpSocket* sock = is.next();
664 if (sock->state() == QAbstractSocket::ConnectedState) {
665 if (sock->write(corrLine.toAscii()) == -1) {
666 delete sock;
667 is.remove();
668 }
669 }
670 else if (sock->state() != QAbstractSocket::ConnectingState) {
671 delete sock;
672 is.remove();
673 }
674 }
675 }
676 _corrs->remove(sec);
677 }
678}
Note: See TracBrowser for help on using the repository browser.