[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 | }
|
---|
[382] | 95 |
|
---|
| 96 | // Start dump epoch loop
|
---|
| 97 | // ---------------------
|
---|
| 98 | _newTime = 0;
|
---|
| 99 | dumpEpochSlot();
|
---|
[35] | 100 | }
|
---|
| 101 |
|
---|
| 102 | // Destructor
|
---|
| 103 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 104 | bncCaster::~bncCaster() {
|
---|
[186] | 105 | QListIterator<bncGetThread*> it(_threads);
|
---|
| 106 | while(it.hasNext()){
|
---|
| 107 | bncGetThread* thread = it.next();
|
---|
| 108 | thread->terminate();
|
---|
[188] | 109 | thread->wait();
|
---|
[230] | 110 | delete thread;
|
---|
[186] | 111 | }
|
---|
[35] | 112 | delete _out;
|
---|
| 113 | delete _outFile;
|
---|
[187] | 114 | delete _server;
|
---|
| 115 | delete _sockets;
|
---|
| 116 | delete _epochs;
|
---|
[35] | 117 | }
|
---|
| 118 |
|
---|
[369] | 119 | // Reconnecting
|
---|
| 120 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 121 | void bncCaster::reconnecting(const QByteArray& staID) {
|
---|
[385] | 122 | QMutexLocker locker(&_mutex);
|
---|
[369] | 123 | if (_rinexWriters.find(staID) != _rinexWriters.end()) {
|
---|
| 124 | bncRinex* rnx = _rinexWriters.find(staID).value();
|
---|
| 125 | rnx->setReconnectFlag(true);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[35] | 129 | // New Observations
|
---|
| 130 | ////////////////////////////////////////////////////////////////////////////
|
---|
[350] | 131 | void bncCaster::newObs(const QByteArray& staID, const QUrl& mountPoint,
|
---|
| 132 | bool firstObs, Observation* obs,
|
---|
[366] | 133 | const QByteArray& format,
|
---|
| 134 | const QByteArray& latitude,
|
---|
| 135 | const QByteArray& longitude,
|
---|
| 136 | const QByteArray& nmea) {
|
---|
[35] | 137 |
|
---|
[243] | 138 | QMutexLocker locker(&_mutex);
|
---|
| 139 |
|
---|
[222] | 140 | long iSec = long(floor(obs->GPSWeeks+0.5));
|
---|
[382] | 141 | _newTime = obs->GPSWeek * 7*24*3600 + iSec;
|
---|
[35] | 142 |
|
---|
[160] | 143 | // Rename the Station
|
---|
| 144 | // ------------------
|
---|
| 145 | strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
|
---|
[333] | 146 | obs->StatID[sizeof(obs->StatID)-1] = '\0';
|
---|
[160] | 147 |
|
---|
[350] | 148 | // Prepare RINEX Output
|
---|
| 149 | // --------------------
|
---|
| 150 | if (_rinexWriters.find(obs->StatID) == _rinexWriters.end()) {
|
---|
| 151 | _rinexWriters.insert(obs->StatID, new bncRinex(obs->StatID,
|
---|
[366] | 152 | mountPoint, format, latitude, longitude, nmea));
|
---|
[350] | 153 | }
|
---|
| 154 | bncRinex* rnx = _rinexWriters.find(obs->StatID).value();
|
---|
| 155 | if (_samplingRate == 0 || iSec % _samplingRate == 0) {
|
---|
| 156 | rnx->deepCopy(obs);
|
---|
| 157 | }
|
---|
[382] | 158 | rnx->dumpEpoch(_newTime);
|
---|
[350] | 159 |
|
---|
[35] | 160 | // First time, set the _lastDumpSec immediately
|
---|
| 161 | // --------------------------------------------
|
---|
| 162 | if (_lastDumpSec == 0) {
|
---|
[382] | 163 | _lastDumpSec = _newTime - 1;
|
---|
[35] | 164 | }
|
---|
| 165 |
|
---|
| 166 | // An old observation - throw it away
|
---|
| 167 | // ----------------------------------
|
---|
[382] | 168 | if (_newTime <= _lastDumpSec) {
|
---|
[257] | 169 | if (firstObs) {
|
---|
| 170 | QSettings settings;
|
---|
| 171 | if ( !settings.value("outFile").toString().isEmpty() ||
|
---|
| 172 | !settings.value("outPort").toString().isEmpty() ) {
|
---|
[258] | 173 | emit( newMessage(QString("Station %1: old epoch %2 thrown away")
|
---|
| 174 | .arg(staID.data()).arg(iSec).toAscii()) );
|
---|
[257] | 175 | }
|
---|
[181] | 176 | }
|
---|
[35] | 177 | delete obs;
|
---|
[350] | 178 | return;
|
---|
[35] | 179 | }
|
---|
| 180 |
|
---|
[160] | 181 | // Save the observation
|
---|
| 182 | // --------------------
|
---|
[382] | 183 | _epochs->insert(_newTime, obs);
|
---|
| 184 | }
|
---|
[35] | 185 |
|
---|
[382] | 186 | // Dump Loop Event
|
---|
| 187 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 188 | void bncCaster::dumpEpochSlot() {
|
---|
[385] | 189 | QMutexLocker locker(&_mutex);
|
---|
[382] | 190 | if (_newTime != 0 && _epochs->size() > 0) {
|
---|
| 191 | dumpEpochs(_lastDumpSec + 1, _newTime - _waitTime);
|
---|
[252] | 192 |
|
---|
[382] | 193 | if (_lastDumpSec < _newTime - _waitTime) {
|
---|
| 194 | _lastDumpSec = _newTime - _waitTime;
|
---|
| 195 | }
|
---|
[252] | 196 | }
|
---|
[382] | 197 | QTimer::singleShot(100, this, SLOT(dumpEpochSlot()));
|
---|
[35] | 198 | }
|
---|
| 199 |
|
---|
| 200 | // New Connection
|
---|
| 201 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 202 | void bncCaster::slotNewConnection() {
|
---|
[385] | 203 | QMutexLocker locker(&_mutex);
|
---|
[35] | 204 | _sockets->push_back( _server->nextPendingConnection() );
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | // Add New Thread
|
---|
| 208 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 209 | void bncCaster::addGetThread(bncGetThread* getThread) {
|
---|
[385] | 210 | QMutexLocker locker(&_mutex);
|
---|
[35] | 211 | connect(getThread, SIGNAL(error(const QByteArray&)),
|
---|
| 212 | this, SLOT(slotGetThreadError(const QByteArray&)));
|
---|
| 213 |
|
---|
[88] | 214 | _staIDs.push_back(getThread->staID());
|
---|
[186] | 215 | _threads.push_back(getThread);
|
---|
[35] | 216 | }
|
---|
| 217 |
|
---|
| 218 | // Error in get thread
|
---|
| 219 | ////////////////////////////////////////////////////////////////////////////
|
---|
[88] | 220 | void bncCaster::slotGetThreadError(const QByteArray& staID) {
|
---|
[243] | 221 | QMutexLocker locker(&_mutex);
|
---|
[88] | 222 | _staIDs.removeAll(staID);
|
---|
[82] | 223 | emit( newMessage(
|
---|
[88] | 224 | QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
|
---|
| 225 | if (_staIDs.size() == 0) {
|
---|
[82] | 226 | emit(newMessage("bncCaster:: last get thread terminated"));
|
---|
[35] | 227 | emit getThreadErrors();
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | // Dump Complete Epochs
|
---|
| 232 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 233 | void bncCaster::dumpEpochs(long minTime, long maxTime) {
|
---|
[385] | 234 | QMutexLocker locker(&_mutex);
|
---|
[35] | 235 |
|
---|
| 236 | const char begEpoch = 'A';
|
---|
| 237 | const char begObs = 'B';
|
---|
| 238 | const char endEpoch = 'C';
|
---|
| 239 |
|
---|
| 240 | for (long sec = minTime; sec <= maxTime; sec++) {
|
---|
[140] | 241 |
|
---|
[35] | 242 | bool first = true;
|
---|
| 243 | QList<Observation*> allObs = _epochs->values(sec);
|
---|
| 244 | QListIterator<Observation*> it(allObs);
|
---|
| 245 | while (it.hasNext()) {
|
---|
| 246 | Observation* obs = it.next();
|
---|
| 247 |
|
---|
[140] | 248 | if (_samplingRate == 0 || sec % _samplingRate == 0) {
|
---|
| 249 |
|
---|
| 250 | // Output into the file
|
---|
| 251 | // --------------------
|
---|
| 252 | if (_out) {
|
---|
[35] | 253 | if (first) {
|
---|
[341] | 254 | _out->setFieldWidth(1); *_out << begEpoch << endl;;
|
---|
[35] | 255 | }
|
---|
[343] | 256 | _out->setFieldWidth(0); *_out << obs->StatID;
|
---|
[342] | 257 | _out->setFieldWidth(1); *_out << " " << obs->satSys;
|
---|
[344] | 258 | _out->setPadChar('0');
|
---|
[342] | 259 | _out->setFieldWidth(2); *_out << obs->satNum;
|
---|
[344] | 260 | _out->setPadChar(' ');
|
---|
[342] | 261 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 262 | _out->setFieldWidth(4); *_out << obs->GPSWeek;
|
---|
| 263 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 264 | _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->GPSWeeks;
|
---|
| 265 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 266 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C1;
|
---|
| 267 | _out->setFieldWidth(1); *_out << " ";
|
---|
[366] | 268 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->C2;
|
---|
| 269 | _out->setFieldWidth(1); *_out << " ";
|
---|
[342] | 270 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P1;
|
---|
| 271 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 272 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->P2;
|
---|
| 273 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 274 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L1;
|
---|
| 275 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 276 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->L2;
|
---|
[367] | 277 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 278 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S1;
|
---|
| 279 | _out->setFieldWidth(1); *_out << " ";
|
---|
| 280 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->S2;
|
---|
[342] | 281 | _out->setFieldWidth(1);
|
---|
| 282 | *_out << " " << obs->SNR1 << " " << obs->SNR2 << endl;
|
---|
[35] | 283 | if (!it.hasNext()) {
|
---|
[341] | 284 | _out->setFieldWidth(1); *_out << endEpoch << endl;
|
---|
[35] | 285 | }
|
---|
[347] | 286 | _out->flush();
|
---|
[35] | 287 | }
|
---|
[140] | 288 |
|
---|
| 289 | // Output into the socket
|
---|
| 290 | // ----------------------
|
---|
| 291 | if (_sockets) {
|
---|
| 292 | int numBytes = sizeof(*obs);
|
---|
| 293 | QListIterator<QTcpSocket*> is(*_sockets);
|
---|
| 294 | while (is.hasNext()) {
|
---|
| 295 | QTcpSocket* sock = is.next();
|
---|
| 296 | if (first) {
|
---|
| 297 | sock->write(&begEpoch, 1);
|
---|
| 298 | }
|
---|
[383] | 299 | sock->write(&begObs, 1);
|
---|
| 300 | sock->write((char*) obs, numBytes);
|
---|
| 301 | if (!it.hasNext()) {
|
---|
| 302 | sock->write(&endEpoch, 1);
|
---|
| 303 | }
|
---|
[140] | 304 | }
|
---|
| 305 | }
|
---|
[73] | 306 | }
|
---|
| 307 |
|
---|
[35] | 308 | delete obs;
|
---|
| 309 | _epochs->remove(sec);
|
---|
| 310 | first = false;
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 | }
|
---|