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

Last change on this file since 2912 was 2909, checked in by mervart, 14 years ago
File size: 25.2 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 <QMessageBox>
43#include <cmath>
44
45#include "bncapp.h"
46#include "bncutils.h"
47#include "bncrinex.h"
48#include "bncsettings.h"
49#include "bncversion.h"
50
51#ifdef USE_COMBINATION
52#include "combination/bnccomb.h"
53#endif
54
55using namespace std;
56
57// Constructor
58////////////////////////////////////////////////////////////////////////////
59bncApp::bncApp(int& argc, char* argv[], bool GUIenabled) :
60 QApplication(argc, argv, GUIenabled) {
61
62 _logFileFlag = 0;
63 _logFile = 0;
64 _logStream = 0;
65 _caster = 0;
66 _rawFile = 0;
67
68 // Lists of Ephemeris
69 // ------------------
70 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
71 _gpsEph[ii-PRN_GPS_START] = 0;
72 }
73 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
74 _glonassEph[ii-PRN_GLONASS_START] = 0;
75 }
76 for (int ii = PRN_GALILEO_START; ii <= PRN_GALILEO_END; ii++) {
77 _galileoEph[ii-PRN_GALILEO_START] = 0;
78 }
79
80 // Eph file(s)
81 // -----------
82 _rinexVers = 0;
83 _ephFileGPS = 0;
84 _ephStreamGPS = 0;
85 _ephFileGlonass = 0;
86 _ephStreamGlonass = 0;
87 _ephFileGalileo = 0;
88 _ephStreamGalileo = 0;
89
90 _port = 0;
91 _server = 0;
92 _sockets = 0;
93
94 _portCorr = 0;
95 _serverCorr = 0;
96 _socketsCorr = 0;
97
98 _pgmName = QString(BNCPGMNAME).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 _lastDumpCoSec = 0;
108
109 _corrs = new QMultiMap<long, QString>;
110
111 _currentDateAndTimeGPS = 0;
112
113 for (int ii = 0; ii < PRN_GLONASS_NUM; ++ii) {
114 _GLOFreq[ii] = 0;
115 }
116}
117
118// Destructor
119////////////////////////////////////////////////////////////////////////////
120bncApp::~bncApp() {
121 delete _logStream;
122 delete _logFile;
123 delete _ephStreamGPS;
124 delete _ephFileGPS;
125 delete _server;
126 delete _sockets;
127 delete _serverCorr;
128 delete _socketsCorr;
129 if (_rinexVers == 2) {
130 delete _ephStreamGlonass;
131 delete _ephFileGlonass;
132 }
133 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
134 delete _gpsEph[ii-PRN_GPS_START];
135 }
136 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
137 delete _glonassEph[ii-PRN_GLONASS_START];
138 }
139 for (int ii = PRN_GALILEO_START; ii <= PRN_GALILEO_END; ii++) {
140 delete _galileoEph[ii-PRN_GALILEO_START];
141 }
142
143 delete _corrs;
144
145 delete _currentDateAndTimeGPS;
146
147 delete _rawFile;
148
149#ifdef USE_COMBINATION
150 delete _bncComb;
151#endif
152}
153
154// Write a Program Message
155////////////////////////////////////////////////////////////////////////////
156void bncApp::slotMessage(QByteArray msg, bool showOnScreen) {
157
158 QMutexLocker locker(&_mutexMessage);
159
160 messagePrivate(msg);
161 emit newMessage(msg, showOnScreen);
162}
163
164// Write a Program Message (private, no lock)
165////////////////////////////////////////////////////////////////////////////
166void bncApp::messagePrivate(const QByteArray& msg) {
167
168 // First time resolve the log file name
169 // ------------------------------------
170 QDate currDate = currentDateAndTimeGPS().date();
171 if (_logFileFlag == 0 || _fileDate != currDate) {
172 delete _logFile;
173 _logFileFlag = 1;
174 bncSettings settings;
175 QString logFileName = settings.value("logFile").toString();
176 if ( !logFileName.isEmpty() ) {
177 expandEnvVar(logFileName);
178 _logFile = new QFile(logFileName + "_" +
179 currDate.toString("yyMMdd").toAscii().data());
180 _fileDate = currDate;
181 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
182 _logFile->open(QIODevice::WriteOnly | QIODevice::Append);
183 }
184 else {
185 _logFile->open(QIODevice::WriteOnly);
186 }
187 _logStream = new QTextStream();
188 _logStream->setDevice(_logFile);
189 }
190 }
191
192 if (_logStream) {
193 *_logStream << currentDateAndTimeGPS().toString("yy-MM-dd hh:mm:ss ").toAscii().data();
194 *_logStream << msg.data() << endl;
195 _logStream->flush();
196 }
197}
198
199// New GPS Ephemeris
200////////////////////////////////////////////////////////////////////////////
201void bncApp::slotNewGPSEph(gpsephemeris* gpseph) {
202
203 QMutexLocker locker(&_mutex);
204
205 gpsephemeris copy_gpseph = *gpseph;
206 emit newEphGPS(copy_gpseph);
207
208 printEphHeader();
209
210 gpsephemeris** ee = &_gpsEph[gpseph->satellite-1];
211
212 if ( *ee == 0 ||
213 gpseph->GPSweek > (*ee)->GPSweek ||
214 (gpseph->GPSweek == (*ee)->GPSweek && gpseph->TOC > (*ee)->TOC) ) {
215 delete *ee;
216 *ee = gpseph;
217 printGPSEph(gpseph, true);
218 }
219 else {
220 printGPSEph(gpseph, false);
221 delete gpseph;
222 }
223}
224
225// New Glonass Ephemeris
226////////////////////////////////////////////////////////////////////////////
227void bncApp::slotNewGlonassEph(glonassephemeris* glonasseph) {
228
229 QMutexLocker locker(&_mutex);
230
231 glonassephemeris copy_glonasseph = *glonasseph;
232 emit newEphGlonass(copy_glonasseph);
233
234 printEphHeader();
235
236 glonassephemeris** ee = &_glonassEph[glonasseph->almanac_number-1];
237
238 int wwOld, towOld, wwNew, towNew;
239 if (*ee != 0) {
240 wwOld = (*ee)->GPSWeek;
241 towOld = (*ee)->GPSTOW;
242 updatetime(&wwOld, &towOld, (*ee)->tb*1000, 0); // Moscow -> GPS
243
244 wwNew = glonasseph->GPSWeek;
245 towNew = glonasseph->GPSTOW;
246 updatetime(&wwNew, &towNew, glonasseph->tb*1000, 0); // Moscow -> GPS
247 }
248
249 if ( *ee == 0 ||
250 wwNew > wwOld ||
251 (wwNew == wwOld && towNew > towOld) ) {
252 delete *ee;
253 *ee = glonasseph;
254 printGlonassEph(glonasseph, true);
255 }
256 else {
257 printGlonassEph(glonasseph, false);
258 delete glonasseph;
259 }
260}
261
262// New Galileo Ephemeris
263////////////////////////////////////////////////////////////////////////////
264void bncApp::slotNewGalileoEph(galileoephemeris* galileoeph) {
265
266 QMutexLocker locker(&_mutex);
267
268 galileoephemeris copy_galileoeph = *galileoeph;
269 emit newEphGalileo(copy_galileoeph);
270
271 printEphHeader();
272
273 int galIndex = galileoeph->satellite - 51;
274 if (galIndex < 0 || galIndex > PRN_GALILEO_END - PRN_GALILEO_START) {
275 emit( newMessage("Wrong Galileo Satellite Number", true) );
276 exit(1);
277 }
278
279 galileoephemeris** ee = &_galileoEph[galIndex];
280
281 if ( *ee == 0 ||
282 galileoeph->Week > (*ee)->Week ||
283 (galileoeph->Week == (*ee)->Week && galileoeph->TOC > (*ee)->TOC) ) {
284 delete *ee;
285 *ee = galileoeph;
286 printGalileoEph(galileoeph, true);
287 }
288 else {
289 printGalileoEph(galileoeph, false);
290 delete galileoeph;
291 }
292}
293
294// Print Header of the output File(s)
295////////////////////////////////////////////////////////////////////////////
296void bncApp::printEphHeader() {
297
298 bncSettings settings;
299
300 // Initialization
301 // --------------
302 if (_rinexVers == 0) {
303
304 if ( Qt::CheckState(settings.value("ephV3").toInt()) == Qt::Checked) {
305 _rinexVers = 3;
306 }
307 else {
308 _rinexVers = 2;
309 }
310
311 _ephPath = settings.value("ephPath").toString();
312
313 if ( !_ephPath.isEmpty() ) {
314 if ( _ephPath[_ephPath.length()-1] != QDir::separator() ) {
315 _ephPath += QDir::separator();
316 }
317 expandEnvVar(_ephPath);
318 }
319 }
320
321 // (Re-)Open output File(s)
322 // ------------------------
323 if (!_ephPath.isEmpty()) {
324
325 QDateTime datTim = currentDateAndTimeGPS();
326
327 QString ephFileNameGPS = _ephPath + "BRDC" +
328 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'));
329
330 QString hlpStr = bncRinex::nextEpochStr(datTim,
331 settings.value("ephIntr").toString());
332
333 if (_rinexVers == 3) {
334 ephFileNameGPS += hlpStr + datTim.toString(".yyP");
335 }
336 else {
337 ephFileNameGPS += hlpStr + datTim.toString(".yyN");
338 }
339
340 if (_ephFileNameGPS == ephFileNameGPS) {
341 return;
342 }
343 else {
344 _ephFileNameGPS = ephFileNameGPS;
345 }
346
347 for (int ii = PRN_GPS_START; ii <= PRN_GPS_END; ii++) {
348 delete _gpsEph[ii-PRN_GPS_START];
349 _gpsEph[ii-PRN_GPS_START] = 0;
350 }
351 for (int ii = PRN_GLONASS_START; ii <= PRN_GLONASS_END; ii++) {
352 delete _glonassEph[ii-PRN_GLONASS_START];
353 _glonassEph[ii-PRN_GLONASS_START] = 0;
354 }
355 for (int ii = PRN_GALILEO_START; ii <= PRN_GALILEO_END; ii++) {
356 delete _galileoEph[ii-PRN_GALILEO_START];
357 _galileoEph[ii-PRN_GALILEO_START] = 0;
358 }
359
360 delete _ephStreamGPS;
361 delete _ephFileGPS;
362
363 QFlags<QIODevice::OpenModeFlag> appendFlagGPS;
364 QFlags<QIODevice::OpenModeFlag> appendFlagGlonass;
365 QFlags<QIODevice::OpenModeFlag> appendFlagGalileo;
366
367 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
368 QFile::exists(ephFileNameGPS) ) {
369 appendFlagGPS = QIODevice::Append;
370 }
371
372 _ephFileGPS = new QFile(ephFileNameGPS);
373 _ephFileGPS->open(QIODevice::WriteOnly | appendFlagGPS);
374 _ephStreamGPS = new QTextStream();
375 _ephStreamGPS->setDevice(_ephFileGPS);
376
377 if (_rinexVers == 3) {
378 _ephFileGlonass = _ephFileGPS;
379 _ephStreamGlonass = _ephStreamGPS;
380 _ephFileGalileo = _ephFileGPS;
381 _ephStreamGalileo = _ephStreamGPS;
382 }
383 else if (_rinexVers == 2) {
384 QString ephFileNameGlonass = _ephPath + "BRDC" +
385 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
386 hlpStr + datTim.toString(".yyG");
387
388 delete _ephStreamGlonass;
389 delete _ephFileGlonass;
390
391 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked &&
392 QFile::exists(ephFileNameGlonass) ) {
393 appendFlagGlonass = QIODevice::Append;
394 }
395
396 _ephFileGlonass = new QFile(ephFileNameGlonass);
397 _ephFileGlonass->open(QIODevice::WriteOnly | appendFlagGlonass);
398 _ephStreamGlonass = new QTextStream();
399 _ephStreamGlonass->setDevice(_ephFileGlonass);
400 }
401
402 // Header - RINEX Version 3
403 // ------------------------
404 if (_rinexVers == 3) {
405 if ( ! (appendFlagGPS & QIODevice::Append)) {
406 QString line;
407 line.sprintf(
408 "%9.2f%11sN: GNSS NAV DATA M: Mixed%12sRINEX VERSION / TYPE\n",
409 3.0, "", "");
410 *_ephStreamGPS << line;
411
412 QString hlp = currentDateAndTimeGPS().toString("yyyyMMdd hhmmss UTC").leftJustified(20, ' ', true);
413 *_ephStreamGPS << _pgmName.toAscii().data()
414 << _userName.toAscii().data()
415 << hlp.toAscii().data()
416 << "PGM / RUN BY / DATE" << endl;
417
418 line.sprintf("%60sEND OF HEADER\n", "");
419 *_ephStreamGPS << line;
420
421 _ephStreamGPS->flush();
422 }
423 }
424
425 // Headers - RINEX Version 2
426 // -------------------------
427 else if (_rinexVers == 2) {
428 if (! (appendFlagGPS & QIODevice::Append)) {
429 QString line;
430 line.sprintf(
431 "%9.2f%11sN: GPS NAV DATA%25sRINEX VERSION / TYPE\n", 2.10, "", "");
432 *_ephStreamGPS << line;
433
434 QString hlp = currentDateAndTimeGPS().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
435 *_ephStreamGPS << _pgmName.toAscii().data()
436 << _userName.toAscii().data()
437 << hlp.toAscii().data()
438 << "PGM / RUN BY / DATE" << endl;
439
440 line.sprintf("%60sEND OF HEADER\n", "");
441 *_ephStreamGPS << line;
442
443 _ephStreamGPS->flush();
444 }
445 if (! (appendFlagGlonass & QIODevice::Append)) {
446 QString line;
447 line.sprintf(
448 "%9.2f%11sG: GLONASS NAV DATA%21sRINEX VERSION / TYPE\n",2.10,"","");
449 *_ephStreamGlonass << line;
450
451 QString hlp = currentDateAndTimeGPS().date().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
452 *_ephStreamGlonass << _pgmName.toAscii().data()
453 << _userName.toAscii().data()
454 << hlp.toAscii().data()
455 << "PGM / RUN BY / DATE" << endl;
456
457 line.sprintf("%60sEND OF HEADER\n", "");
458 *_ephStreamGlonass << line;
459
460 _ephStreamGlonass->flush();
461 }
462 }
463 }
464}
465
466// Print One GPS Ephemeris
467////////////////////////////////////////////////////////////////////////////
468void bncApp::printGPSEph(gpsephemeris* ep, bool printFile) {
469
470 QString lineV2;
471 QString lineV3;
472
473 struct converttimeinfo cti;
474 converttime(&cti, ep->GPSweek, ep->TOC);
475
476 lineV3.sprintf("G%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e\n",
477 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
478 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
479 ep->clock_driftrate);
480
481 lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e\n",
482 ep->satellite, cti.year%100, cti.month, cti.day, cti.hour,
483 cti.minute, (double) cti.second, ep->clock_bias,
484 ep->clock_drift, ep->clock_driftrate);
485
486 QString line;
487 QByteArray allLines;
488
489 QByteArray fmt;
490 QByteArray fmt2;
491 if (_rinexVers == 2) {
492 fmt = " %19.12e%19.12e%19.12e%19.12e\n";
493 fmt2 = " %19.12e%19.12e\n";
494 }
495 else {
496 fmt = " %19.12e%19.12e%19.12e%19.12e\n";
497 fmt2 = " %19.12e%19.12e\n";
498 }
499
500 line.sprintf(fmt.data(), (double)ep->IODE, ep->Crs, ep->Delta_n, ep->M0);
501 allLines += line;
502
503 line.sprintf(fmt.data(), ep->Cuc, ep->e, ep->Cus, ep->sqrt_A);
504 allLines += line;
505
506 line.sprintf(fmt.data(), (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
507 allLines += line;
508
509 line.sprintf(fmt.data(), ep->i0, ep->Crc, ep->omega, ep->OMEGADOT);
510 allLines += line;
511
512 double dd = 0;
513 unsigned long ii = ep->flags;
514 if(ii & GPSEPHF_L2CACODE)
515 dd += 2.0;
516 if(ii & GPSEPHF_L2PCODE)
517 dd += 1.0;
518 line.sprintf(fmt.data(), ep->IDOT, dd, (double) ep->GPSweek,
519 ii & GPSEPHF_L2PCODEDATA ? 1.0 : 0.0);
520 allLines += line;
521
522 if(ep->URAindex <= 6) /* URA index */
523 dd = ceil(10.0*pow(2.0, 1.0+((double)ep->URAindex)/2.0))/10.0;
524 else
525 dd = ceil(10.0*pow(2.0, ((double)ep->URAindex)/2.0))/10.0;
526 line.sprintf(fmt.data(), dd, ((double) ep->SVhealth), ep->TGD,
527 ((double) ep->IODC));
528 allLines += line;
529
530 line.sprintf(fmt2.data(), ((double)ep->TOW), 0.0);
531 allLines += line;
532
533 printOutput(printFile, _ephStreamGPS, lineV2, lineV3, allLines);
534}
535
536// Print One Glonass Ephemeris
537////////////////////////////////////////////////////////////////////////////
538void bncApp::printGlonassEph(glonassephemeris* ep, bool printFile) {
539
540 int ww = ep->GPSWeek;
541 int tow = ep->GPSTOW;
542 struct converttimeinfo cti;
543
544 updatetime(&ww, &tow, ep->tb*1000, 1); // Moscow -> UTC
545 converttime(&cti, ww, tow);
546
547 int tk = ep->tk-3*60*60;
548 if (tk < 0) {
549 tk += 86400;
550 }
551
552 QString lineV2;
553 QString lineV3;
554
555 lineV3.sprintf("R%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e\n",
556 ep->almanac_number, cti.year, cti.month, cti.day, cti.hour,
557 cti.minute, cti.second, -ep->tau, ep->gamma, (double) tk);
558
559 lineV2.sprintf("%02d %02d %02d %02d %02d %02d%5.1f%19.12e%19.12e%19.12e\n",
560 ep->almanac_number, cti.year%100, cti.month, cti.day,
561 cti.hour, cti.minute, (double) cti.second, -ep->tau,
562 ep->gamma, (double) tk);
563
564 QString line;
565 QByteArray allLines;
566
567 QByteArray fmt;
568 if (_rinexVers == 2) {
569 fmt = " %19.12e%19.12e%19.12e%19.12e\n";
570 }
571 else {
572 fmt = " %19.12e%19.12e%19.12e%19.12e\n";
573 }
574
575 line.sprintf(fmt.data(), ep->x_pos, ep->x_velocity, ep->x_acceleration,
576 (ep->flags & GLOEPHF_UNHEALTHY) ? 1.0 : 0.0);
577 allLines += line;
578
579 line.sprintf(fmt.data(), ep->y_pos, ep->y_velocity, ep->y_acceleration,
580 (double) ep->frequency_number);
581 allLines += line;
582
583 line.sprintf(fmt.data(), ep->z_pos, ep->z_velocity, ep->z_acceleration,
584 (double) ep->E);
585 allLines += line;
586
587 printOutput(printFile, _ephStreamGlonass, lineV2, lineV3, allLines);
588}
589
590// Print One Galileo Ephemeris
591////////////////////////////////////////////////////////////////////////////
592void bncApp::printGalileoEph(galileoephemeris* ep, bool printFile) {
593
594 QString lineV2;
595 QString lineV3;
596
597 struct converttimeinfo cti;
598 converttime(&cti, ep->Week, ep->TOC);
599
600 lineV3.sprintf("E%02d %04d %02d %02d %02d %02d %02d%19.12e%19.12e%19.12e\n",
601 ep->satellite, cti.year, cti.month, cti.day, cti.hour,
602 cti.minute, cti.second, ep->clock_bias, ep->clock_drift,
603 ep->clock_driftrate);
604
605 QString line;
606 QByteArray allLines;
607
608 const QByteArray fmt4 = " %19.12e%19.12e%19.12e%19.12e\n";
609 const QByteArray fmt3 = " %19.12e%19.12e%19.12e\n";
610 const QByteArray fmt1 = " %19.12e\n";
611
612 line.sprintf(fmt4.data(), (double)ep->IODnav, ep->Crs, ep->Delta_n, ep->M0);
613 allLines += line;
614
615 line.sprintf(fmt4.data(), ep->Cuc, ep->e, ep->Cus, ep->sqrt_A);
616 allLines += line;
617
618 line.sprintf(fmt4.data(), (double) ep->TOE, ep->Cic, ep->OMEGA0, ep->Cis);
619 allLines += line;
620
621 line.sprintf(fmt4.data(), ep->i0, ep->Crc, ep->omega, ep->OMEGADOT);
622 allLines += line;
623
624 double dataSources = 0.0; // TODO
625 line.sprintf(fmt3.data(), ep->IDOT, dataSources, (double) ep->Week);
626 allLines += line;
627
628 double health = 0.0; // TODO
629 double BGD_1_5B = ep->BGD_1_5A; // TODO
630 line.sprintf(fmt4.data(), (double) ep->SISA, health, ep->BGD_1_5A, BGD_1_5B);
631 allLines += line;
632
633 double transmissionTimeOfMessage = 0.9999e9; // unknown (Rinex v3 standard)
634 line.sprintf(fmt1.data(), transmissionTimeOfMessage);
635 allLines += line;
636
637 printOutput(printFile, _ephStreamGalileo, lineV2, lineV3, allLines);
638}
639
640// Output
641////////////////////////////////////////////////////////////////////////////
642void bncApp::printOutput(bool printFile, QTextStream* stream,
643 const QString& lineV2,
644 const QString& lineV3,
645 const QByteArray& allLines) {
646 // Output into file
647 // ----------------
648 if (printFile && stream) {
649 if (_rinexVers == 2) {
650 *stream << lineV2.toAscii();
651 }
652 else {
653 *stream << lineV3.toAscii();
654 }
655 *stream << allLines;
656 stream->flush();
657 }
658
659 // Output into the socket
660 // ----------------------
661 if (_sockets) {
662 QMutableListIterator<QTcpSocket*> is(*_sockets);
663 while (is.hasNext()) {
664 QTcpSocket* sock = is.next();
665 if (sock->state() == QAbstractSocket::ConnectedState) {
666 if (sock->write(lineV3.toAscii()) == -1 ||
667 sock->write(allLines) == -1) {
668 delete sock;
669 is.remove();
670 }
671 }
672 else if (sock->state() != QAbstractSocket::ConnectingState) {
673 delete sock;
674 is.remove();
675 }
676 }
677 }
678}
679
680// Set Port Number
681////////////////////////////////////////////////////////////////////////////
682void bncApp::setPort(int port) {
683 _port = port;
684 if (_port != 0) {
685 delete _server;
686 _server = new QTcpServer;
687 if ( !_server->listen(QHostAddress::Any, _port) ) {
688 slotMessage("bncApp: Cannot listen on ephemeris port", true);
689 }
690 connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
691 delete _sockets;
692 _sockets = new QList<QTcpSocket*>;
693 }
694}
695
696// Set Port Number
697////////////////////////////////////////////////////////////////////////////
698void bncApp::setPortCorr(int port) {
699 _portCorr = port;
700 if (_portCorr != 0) {
701 delete _serverCorr;
702 _serverCorr = new QTcpServer;
703 if ( !_serverCorr->listen(QHostAddress::Any, _portCorr) ) {
704 slotMessage("bncApp: Cannot listen on correction port", true);
705 }
706 connect(_serverCorr, SIGNAL(newConnection()), this, SLOT(slotNewConnectionCorr()));
707 delete _socketsCorr;
708 _socketsCorr = new QList<QTcpSocket*>;
709 }
710}
711
712// New Connection
713////////////////////////////////////////////////////////////////////////////
714void bncApp::slotNewConnection() {
715 _sockets->push_back( _server->nextPendingConnection() );
716}
717
718// New Connection
719////////////////////////////////////////////////////////////////////////////
720void bncApp::slotNewConnectionCorr() {
721 _socketsCorr->push_back( _serverCorr->nextPendingConnection() );
722}
723
724//
725////////////////////////////////////////////////////////////////////////////
726void bncApp::slotQuit() {
727 cout << "bncApp::slotQuit" << endl;
728 delete _caster;
729 quit();
730}
731
732//
733////////////////////////////////////////////////////////////////////////////
734void bncApp::slotNewCorrLine(QString line, QString staID, long coTime) {
735
736 QMutexLocker locker(&_mutex);
737
738 // Combination of Corrections
739 // --------------------------
740#ifdef USE_COMBINATION
741 if (_bncComb) {
742 _bncComb->processCorrLine(staID, line);
743 }
744#endif
745
746 bncSettings settings;
747 _waitCoTime = settings.value("corrTime").toInt();
748 if (_waitCoTime < 1) {
749 _waitCoTime = 1;
750 }
751
752 // First time, set the _lastDumpSec immediately
753 // --------------------------------------------
754 if (_lastDumpCoSec == 0) {
755 _lastDumpCoSec = coTime - 1;
756 }
757
758 // An old correction - throw it away
759 // ---------------------------------
760 if (coTime <= _lastDumpCoSec) {
761 QString line = staID + ": Correction for one sat neglected because overaged by " +
762 QString().sprintf(" %ld sec",
763 _lastDumpCoSec - coTime + _waitCoTime);
764 messagePrivate(line.toAscii());
765 emit( newMessage(line.toAscii(), true) );
766 return;
767 }
768
769 _corrs->insert(coTime, QString(line + " " + staID));
770
771 // Dump Corrections
772 // ----------------
773 if (coTime - _waitCoTime > _lastDumpCoSec) {
774 dumpCorrs(_lastDumpCoSec + 1, coTime - _waitCoTime);
775 _lastDumpCoSec = coTime - _waitCoTime;
776 }
777}
778
779// Dump Complete Correction Epochs
780////////////////////////////////////////////////////////////////////////////
781void bncApp::dumpCorrs(long minTime, long maxTime) {
782
783 for (long sec = minTime; sec <= maxTime; sec++) {
784 QList<QString> allCorrs = _corrs->values(sec);
785 emit newCorrections(allCorrs);
786 if (_socketsCorr) {
787 QListIterator<QString> it(allCorrs);
788 while (it.hasNext()) {
789 QString corrLine = it.next() + "\n";
790
791 QMutableListIterator<QTcpSocket*> is(*_socketsCorr);
792 while (is.hasNext()) {
793 QTcpSocket* sock = is.next();
794 if (sock->state() == QAbstractSocket::ConnectedState) {
795 if (sock->write(corrLine.toAscii()) == -1) {
796 delete sock;
797 is.remove();
798 }
799 }
800 else if (sock->state() != QAbstractSocket::ConnectingState) {
801 delete sock;
802 is.remove();
803 }
804 }
805 }
806 }
807 _corrs->remove(sec);
808 }
809}
810
811//
812////////////////////////////////////////////////////////////////////////////
813void bncApp::setConfFileName(const QString& confFileName) {
814 if (confFileName.isEmpty()) {
815 _confFileName = QDir::homePath() + QDir::separator()
816 + ".config" + QDir::separator()
817 + organizationName() + QDir::separator()
818 + applicationName() + ".ini";
819 }
820 else {
821 _confFileName = confFileName;
822 }
823}
824
825// Raw Output
826////////////////////////////////////////////////////////////////////////////
827void bncApp::writeRawData(const QByteArray& data, const QByteArray& staID,
828 const QByteArray& format) {
829
830 QMutexLocker locker(&_mutex);
831
832 if (!_rawFile) {
833 bncSettings settings;
834 QByteArray fileName = settings.value("rawOutFile").toByteArray();
835 if (!fileName.isEmpty()) {
836 _rawFile = new bncRawFile(fileName, format, bncRawFile::output);
837 }
838 }
839
840 if (_rawFile) {
841 _rawFile->writeRawData(data, staID, format);
842 }
843}
844
845// Get Glonass Slot Numbers from Global Array
846////////////////////////////////////////////////////////////////////////////
847void bncApp::getGlonassSlotNums(int GLOFreq[]) {
848
849 QMutexLocker locker(&_mutex);
850
851 for (int ii = 0; ii < PRN_GLONASS_NUM; ++ii) {
852 if (_GLOFreq[ii] != 0) {
853 GLOFreq[ii] = _GLOFreq[ii];
854 }
855 }
856}
857
858// Store Glonass Slot Numbers to Global Array
859////////////////////////////////////////////////////////////////////////////
860void bncApp::storeGlonassSlotNums(const int GLOFreq[]) {
861
862 QMutexLocker locker(&_mutex);
863
864 for (int ii = 0; ii < PRN_GLONASS_NUM; ++ii) {
865 if (GLOFreq[ii] != 0) {
866 _GLOFreq[ii] = GLOFreq[ii];
867 }
868 }
869}
870
871//
872////////////////////////////////////////////////////////////////////////////
873void bncApp::initCombination() {
874#ifdef USE_COMBINATION
875 _bncComb = new bncComb();
876 if (_bncComb->nStreams() < 2) {
877 delete _bncComb;
878 _bncComb = 0;
879 }
880#endif
881}
Note: See TracBrowser for help on using the repository browser.