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

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

* empty log message *

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