[280] | 1 | // Part of BNC, a utility for retrieving decoding and
|
---|
| 2 | // converting GNSS data streams from NTRIP broadcasters,
|
---|
| 3 | // written by Leos Mervart.
|
---|
| 4 | //
|
---|
| 5 | // Copyright (C) 2006
|
---|
| 6 | // German Federal Agency for Cartography and Geodesy (BKG)
|
---|
| 7 | // http://www.bkg.bund.de
|
---|
| 8 | // Czech Technical University Prague, Department of Advanced Geodesy
|
---|
| 9 | // http://www.fsv.cvut.cz
|
---|
| 10 | //
|
---|
| 11 | // Email: euref-ip@bkg.bund.de
|
---|
| 12 | //
|
---|
| 13 | // This program is free software; you can redistribute it and/or
|
---|
| 14 | // modify it under the terms of the GNU General Public License
|
---|
| 15 | // as published by the Free Software Foundation, version 2.
|
---|
| 16 | //
|
---|
| 17 | // This program is distributed in the hope that it will be useful,
|
---|
| 18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | // GNU General Public License for more details.
|
---|
| 21 | //
|
---|
| 22 | // You should have received a copy of the GNU General Public License
|
---|
| 23 | // along with this program; if not, write to the Free Software
|
---|
| 24 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
[35] | 25 |
|
---|
| 26 | /* -------------------------------------------------------------------------
|
---|
[93] | 27 | * BKG NTRIP Client
|
---|
[35] | 28 | * -------------------------------------------------------------------------
|
---|
| 29 | *
|
---|
| 30 | * Class: bncCaster
|
---|
| 31 | *
|
---|
| 32 | * Purpose: buffers and disseminates the data
|
---|
| 33 | *
|
---|
| 34 | * Author: L. Mervart
|
---|
| 35 | *
|
---|
| 36 | * Created: 24-Dec-2005
|
---|
| 37 | *
|
---|
| 38 | * Changes:
|
---|
| 39 | *
|
---|
| 40 | * -----------------------------------------------------------------------*/
|
---|
| 41 |
|
---|
[222] | 42 | #include <math.h>
|
---|
| 43 |
|
---|
[35] | 44 | #include "bnccaster.h"
|
---|
| 45 | #include "bncgetthread.h"
|
---|
[134] | 46 | #include "bncutils.h"
|
---|
[207] | 47 | #include "RTCM/GPSDecoder.h"
|
---|
[35] | 48 |
|
---|
| 49 | // Constructor
|
---|
| 50 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 51 | bncCaster::bncCaster(const QString& outFileName, int port) {
|
---|
| 52 |
|
---|
[275] | 53 | QSettings settings;
|
---|
| 54 |
|
---|
[35] | 55 | if ( !outFileName.isEmpty() ) {
|
---|
[134] | 56 | QString lName = outFileName;
|
---|
| 57 | expandEnvVar(lName);
|
---|
| 58 | _outFile = new QFile(lName);
|
---|
[275] | 59 | if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
|
---|
| 60 | _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
|
---|
| 61 | }
|
---|
| 62 | else {
|
---|
| 63 | _outFile->open(QIODevice::WriteOnly);
|
---|
| 64 | }
|
---|
[35] | 65 | _out = new QTextStream(_outFile);
|
---|
| 66 | _out->setRealNumberNotation(QTextStream::FixedNotation);
|
---|
| 67 | }
|
---|
| 68 | else {
|
---|
| 69 | _outFile = 0;
|
---|
| 70 | _out = 0;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | _port = port;
|
---|
| 74 |
|
---|
| 75 | if (_port != 0) {
|
---|
| 76 | _server = new QTcpServer;
|
---|
| 77 | _server->listen(QHostAddress::Any, _port);
|
---|
| 78 | connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
|
---|
| 79 | _sockets = new QList<QTcpSocket*>;
|
---|
| 80 | }
|
---|
| 81 | else {
|
---|
| 82 | _server = 0;
|
---|
| 83 | _sockets = 0;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | _epochs = new QMultiMap<long, Observation*>;
|
---|
| 87 |
|
---|
| 88 | _lastDumpSec = 0;
|
---|
[133] | 89 |
|
---|
| 90 | _samplingRate = settings.value("rnxSampl").toInt();
|
---|
[136] | 91 | _waitTime = settings.value("waitTime").toInt();
|
---|
[139] | 92 | if (_waitTime < 1) {
|
---|
| 93 | _waitTime = 1;
|
---|
[136] | 94 | }
|
---|
[35] | 95 | }
|
---|
| 96 |
|
---|
| 97 | // Destructor
|
---|
| 98 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 99 | bncCaster::~bncCaster() {
|
---|
[186] | 100 | QListIterator<bncGetThread*> it(_threads);
|
---|
| 101 | while(it.hasNext()){
|
---|
| 102 | bncGetThread* thread = it.next();
|
---|
| 103 | thread->terminate();
|
---|
[188] | 104 | thread->wait();
|
---|
[230] | 105 | delete thread;
|
---|
[186] | 106 | }
|
---|
[35] | 107 | delete _out;
|
---|
| 108 | delete _outFile;
|
---|
[187] | 109 | delete _server;
|
---|
| 110 | delete _sockets;
|
---|
| 111 | delete _epochs;
|
---|
[35] | 112 | }
|
---|
| 113 |
|
---|
| 114 | // New Observations
|
---|
| 115 | ////////////////////////////////////////////////////////////////////////////
|
---|
[408] | 116 | void bncCaster::newObs(const QByteArray& staID, bool firstObs,
|
---|
| 117 | Observation* obs) {
|
---|
[35] | 118 |
|
---|
[243] | 119 | QMutexLocker locker(&_mutex);
|
---|
| 120 |
|
---|
[222] | 121 | long iSec = long(floor(obs->GPSWeeks+0.5));
|
---|
[462] | 122 | long newTime = obs->GPSWeek * 7*24*3600 + iSec;
|
---|
[35] | 123 |
|
---|
[160] | 124 | // Rename the Station
|
---|
| 125 | // ------------------
|
---|
| 126 | strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
|
---|
[333] | 127 | obs->StatID[sizeof(obs->StatID)-1] = '\0';
|
---|
[160] | 128 |
|
---|
[35] | 129 | // First time, set the _lastDumpSec immediately
|
---|
| 130 | // --------------------------------------------
|
---|
| 131 | if (_lastDumpSec == 0) {
|
---|
[462] | 132 | _lastDumpSec = newTime - 1;
|
---|
[35] | 133 | }
|
---|
| 134 |
|
---|
| 135 | // An old observation - throw it away
|
---|
| 136 | // ----------------------------------
|
---|
[462] | 137 | if (newTime <= _lastDumpSec) {
|
---|
[257] | 138 | if (firstObs) {
|
---|
| 139 | QSettings settings;
|
---|
| 140 | if ( !settings.value("outFile").toString().isEmpty() ||
|
---|
| 141 | !settings.value("outPort").toString().isEmpty() ) {
|
---|
[258] | 142 | emit( newMessage(QString("Station %1: old epoch %2 thrown away")
|
---|
| 143 | .arg(staID.data()).arg(iSec).toAscii()) );
|
---|
[257] | 144 | }
|
---|
[181] | 145 | }
|
---|
[35] | 146 | delete obs;
|
---|
[350] | 147 | return;
|
---|
[35] | 148 | }
|
---|
| 149 |
|
---|
[160] | 150 | // Save the observation
|
---|
| 151 | // --------------------
|
---|
[462] | 152 | _epochs->insert(newTime, obs);
|
---|
[393] | 153 |
|
---|
[462] | 154 | // Dump Epochs
|
---|
| 155 | // -----------
|
---|
| 156 | if (newTime - _waitTime > _lastDumpSec) {
|
---|
| 157 | dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
|
---|
| 158 | _lastDumpSec = newTime - _waitTime;
|
---|
[252] | 159 | }
|
---|
[35] | 160 | }
|
---|
| 161 |
|
---|
| 162 | // New Connection
|
---|
| 163 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 164 | void bncCaster::slotNewConnection() {
|
---|
| 165 | _sockets->push_back( _server->nextPendingConnection() );
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | // Add New Thread
|
---|
| 169 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 170 | void bncCaster::addGetThread(bncGetThread* getThread) {
|
---|
[458] | 171 | connect(getThread, SIGNAL(error(const QByteArray)),
|
---|
| 172 | this, SLOT(slotGetThreadError(const QByteArray)));
|
---|
[35] | 173 |
|
---|
[88] | 174 | _staIDs.push_back(getThread->staID());
|
---|
[186] | 175 | _threads.push_back(getThread);
|
---|
[35] | 176 | }
|
---|
| 177 |
|
---|
| 178 | // Error in get thread
|
---|
| 179 | ////////////////////////////////////////////////////////////////////////////
|
---|
[458] | 180 | void bncCaster::slotGetThreadError(const QByteArray staID) {
|
---|
[243] | 181 | QMutexLocker locker(&_mutex);
|
---|
[88] | 182 | _staIDs.removeAll(staID);
|
---|
[82] | 183 | emit( newMessage(
|
---|
[88] | 184 | QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
|
---|
| 185 | if (_staIDs.size() == 0) {
|
---|
[82] | 186 | emit(newMessage("bncCaster:: last get thread terminated"));
|
---|
[35] | 187 | emit getThreadErrors();
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | // Dump Complete Epochs
|
---|
| 192 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 193 | void bncCaster::dumpEpochs(long minTime, long maxTime) {
|
---|
| 194 |
|
---|
| 195 | const char begEpoch = 'A';
|
---|
| 196 | const char begObs = 'B';
|
---|
| 197 | const char endEpoch = 'C';
|
---|
| 198 |
|
---|
| 199 | for (long sec = minTime; sec <= maxTime; sec++) {
|
---|
[140] | 200 |
|
---|
[35] | 201 | bool first = true;
|
---|
| 202 | QList<Observation*> allObs = _epochs->values(sec);
|
---|
| 203 | QListIterator<Observation*> it(allObs);
|
---|
| 204 | while (it.hasNext()) {
|
---|
| 205 | Observation* obs = it.next();
|
---|
| 206 |
|
---|
[140] | 207 | if (_samplingRate == 0 || sec % _samplingRate == 0) {
|
---|
| 208 |
|
---|
| 209 | // Output into the file
|
---|
| 210 | // --------------------
|
---|
| 211 | if (_out) {
|
---|
[35] | 212 | if (first) {
|
---|
[341] | 213 | _out->setFieldWidth(1); *_out << begEpoch << endl;;
|
---|
[35] | 214 | }
|
---|
[343] | 215 | _out->setFieldWidth(0); *_out << obs->StatID;
|
---|
[342] | 216 | _out->setFieldWidth(1); *_out << " " << obs->satSys;
|
---|
[344] | 217 | _out->setPadChar('0');
|
---|
[342] | 218 | _out->setFieldWidth(2); *_out << obs->satNum;
|
---|
[344] | 219 | _out->setPadChar(' ');
|
---|
[342] | 220 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 221 | _out->setFieldWidth(4); *_out << obs->GPSWeek;
|
---|
| 222 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 223 | _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->GPSWeeks;
|
---|
| 224 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 225 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C1;
|
---|
| 226 | _out->setFieldWidth(1); *_out << " ";
|
---|
[366] | 227 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C2;
|
---|
| 228 | _out->setFieldWidth(1); *_out << " ";
|
---|
[342] | 229 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P1;
|
---|
| 230 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 231 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P2;
|
---|
| 232 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 233 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L1;
|
---|
| 234 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 235 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L2;
|
---|
[367] | 236 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 237 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S1;
|
---|
| 238 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 239 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S2;
|
---|
[342] | 240 | _out->setFieldWidth(1);
|
---|
| 241 | *_out << " " << obs->SNR1 << " " << obs->SNR2 << endl;
|
---|
[35] | 242 | if (!it.hasNext()) {
|
---|
[341] | 243 | _out->setFieldWidth(1); *_out << endEpoch << endl;
|
---|
[35] | 244 | }
|
---|
[347] | 245 | _out->flush();
|
---|
[35] | 246 | }
|
---|
[140] | 247 |
|
---|
| 248 | // Output into the socket
|
---|
| 249 | // ----------------------
|
---|
| 250 | if (_sockets) {
|
---|
| 251 | int numBytes = sizeof(*obs);
|
---|
| 252 | QListIterator<QTcpSocket*> is(*_sockets);
|
---|
| 253 | while (is.hasNext()) {
|
---|
| 254 | QTcpSocket* sock = is.next();
|
---|
[397] | 255 | if (sock->state() == QAbstractSocket::ConnectedState) {
|
---|
| 256 | if (first) {
|
---|
| 257 | sock->write(&begEpoch, 1);
|
---|
| 258 | }
|
---|
| 259 | sock->write(&begObs, 1);
|
---|
| 260 | sock->write((char*) obs, numBytes);
|
---|
| 261 | if (!it.hasNext()) {
|
---|
| 262 | sock->write(&endEpoch, 1);
|
---|
| 263 | }
|
---|
[140] | 264 | }
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
[73] | 267 | }
|
---|
| 268 |
|
---|
[35] | 269 | delete obs;
|
---|
| 270 | _epochs->remove(sec);
|
---|
| 271 | first = false;
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | }
|
---|