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