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