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

Last change on this file since 590 was 590, checked in by mervart, 17 years ago

* empty log message *

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