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

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

* empty log message *

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