[35] | 1 |
|
---|
| 2 | /* -------------------------------------------------------------------------
|
---|
[93] | 3 | * BKG NTRIP Client
|
---|
[35] | 4 | * -------------------------------------------------------------------------
|
---|
| 5 | *
|
---|
| 6 | * Class: bncCaster
|
---|
| 7 | *
|
---|
| 8 | * Purpose: buffers and disseminates the data
|
---|
| 9 | *
|
---|
| 10 | * Author: L. Mervart
|
---|
| 11 | *
|
---|
| 12 | * Created: 24-Dec-2005
|
---|
| 13 | *
|
---|
| 14 | * Changes:
|
---|
| 15 | *
|
---|
| 16 | * -----------------------------------------------------------------------*/
|
---|
| 17 |
|
---|
| 18 | #include "bnccaster.h"
|
---|
| 19 | #include "bncgetthread.h"
|
---|
[134] | 20 | #include "bncutils.h"
|
---|
[35] | 21 | #include "RTCM/RTCM.h"
|
---|
| 22 |
|
---|
| 23 | // Constructor
|
---|
| 24 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 25 | bncCaster::bncCaster(const QString& outFileName, int port) {
|
---|
| 26 |
|
---|
| 27 | if ( !outFileName.isEmpty() ) {
|
---|
[134] | 28 | QString lName = outFileName;
|
---|
| 29 | expandEnvVar(lName);
|
---|
| 30 | _outFile = new QFile(lName);
|
---|
[35] | 31 | _outFile->open(QIODevice::WriteOnly);
|
---|
| 32 | _out = new QTextStream(_outFile);
|
---|
| 33 | _out->setRealNumberNotation(QTextStream::FixedNotation);
|
---|
| 34 | _out->setRealNumberPrecision(5);
|
---|
| 35 | }
|
---|
| 36 | else {
|
---|
| 37 | _outFile = 0;
|
---|
| 38 | _out = 0;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | _port = port;
|
---|
| 42 |
|
---|
| 43 | if (_port != 0) {
|
---|
| 44 | _server = new QTcpServer;
|
---|
| 45 | _server->listen(QHostAddress::Any, _port);
|
---|
| 46 | connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
|
---|
| 47 | _sockets = new QList<QTcpSocket*>;
|
---|
| 48 | }
|
---|
| 49 | else {
|
---|
| 50 | _server = 0;
|
---|
| 51 | _sockets = 0;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | _epochs = new QMultiMap<long, Observation*>;
|
---|
| 55 |
|
---|
| 56 | _lastDumpSec = 0;
|
---|
[133] | 57 |
|
---|
| 58 | QSettings settings;
|
---|
| 59 | _samplingRate = settings.value("rnxSampl").toInt();
|
---|
[136] | 60 | _waitTime = settings.value("waitTime").toInt();
|
---|
[139] | 61 | if (_waitTime < 1) {
|
---|
| 62 | _waitTime = 1;
|
---|
[136] | 63 | }
|
---|
[35] | 64 | }
|
---|
| 65 |
|
---|
| 66 | // Destructor
|
---|
| 67 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 68 | bncCaster::~bncCaster() {
|
---|
[186] | 69 | QListIterator<bncGetThread*> it(_threads);
|
---|
| 70 | while(it.hasNext()){
|
---|
| 71 | bncGetThread* thread = it.next();
|
---|
| 72 | thread->terminate();
|
---|
[188] | 73 | thread->wait();
|
---|
[191] | 74 | //// delete thread;
|
---|
[186] | 75 | }
|
---|
[35] | 76 | delete _out;
|
---|
| 77 | delete _outFile;
|
---|
[187] | 78 | delete _server;
|
---|
| 79 | delete _sockets;
|
---|
| 80 | delete _epochs;
|
---|
[35] | 81 | }
|
---|
| 82 |
|
---|
| 83 | // Run
|
---|
| 84 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 85 | void bncCaster::run() {
|
---|
| 86 | exec();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | // New Observations
|
---|
| 90 | ////////////////////////////////////////////////////////////////////////////
|
---|
[88] | 91 | void bncCaster::slotNewObs(const QByteArray& staID, Observation* obs) {
|
---|
[35] | 92 |
|
---|
| 93 | long newTime = obs->GPSWeek * 7*24*3600 + obs->GPSWeeks;
|
---|
| 94 |
|
---|
[160] | 95 | // Rename the Station
|
---|
| 96 | // ------------------
|
---|
| 97 | strncpy(obs->StatID, staID.constData(),sizeof(obs->StatID));
|
---|
| 98 |
|
---|
| 99 | // Prepare RINEX Output
|
---|
| 100 | // --------------------
|
---|
| 101 | if (_rinexWriters.find(obs->StatID) == _rinexWriters.end()) {
|
---|
| 102 | _rinexWriters.insert(obs->StatID, new bncRinex(obs->StatID));
|
---|
| 103 | }
|
---|
| 104 | bncRinex* rnx = _rinexWriters.find(obs->StatID).value();
|
---|
[165] | 105 | if (_samplingRate == 0 || obs->GPSWeeks % _samplingRate == 0) {
|
---|
| 106 | rnx->deepCopy(obs);
|
---|
| 107 | }
|
---|
[160] | 108 | rnx->dumpEpoch(newTime);
|
---|
| 109 |
|
---|
[35] | 110 | // First time, set the _lastDumpSec immediately
|
---|
| 111 | // --------------------------------------------
|
---|
| 112 | if (_lastDumpSec == 0) {
|
---|
| 113 | _lastDumpSec = newTime - 1;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | // An old observation - throw it away
|
---|
| 117 | // ----------------------------------
|
---|
| 118 | if (newTime <= _lastDumpSec) {
|
---|
[181] | 119 | QSettings settings;
|
---|
| 120 | if ( !settings.value("outFile").toString().isEmpty() ||
|
---|
| 121 | !settings.value("outPort").toString().isEmpty() ) {
|
---|
| 122 | emit( newMessage(QString("Station %1: old epoch %2 thrown away")
|
---|
| 123 | .arg(staID.data()).arg(obs->GPSWeeks).toAscii()) );
|
---|
| 124 | }
|
---|
[35] | 125 | delete obs;
|
---|
| 126 | return;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[160] | 129 | // Save the observation
|
---|
| 130 | // --------------------
|
---|
[35] | 131 | _epochs->insert(newTime, obs);
|
---|
| 132 |
|
---|
| 133 | // Dump older epochs
|
---|
| 134 | // -----------------
|
---|
[136] | 135 | dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
|
---|
| 136 | _lastDumpSec = newTime - _waitTime;
|
---|
[35] | 137 | }
|
---|
| 138 |
|
---|
| 139 | // New Connection
|
---|
| 140 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 141 | void bncCaster::slotNewConnection() {
|
---|
| 142 | _sockets->push_back( _server->nextPendingConnection() );
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | // Add New Thread
|
---|
| 146 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 147 | void bncCaster::addGetThread(bncGetThread* getThread) {
|
---|
| 148 | connect(getThread, SIGNAL(newObs(const QByteArray&, Observation*)),
|
---|
| 149 | this, SLOT(slotNewObs(const QByteArray&, Observation*)));
|
---|
| 150 |
|
---|
| 151 | connect(getThread, SIGNAL(error(const QByteArray&)),
|
---|
| 152 | this, SLOT(slotGetThreadError(const QByteArray&)));
|
---|
| 153 |
|
---|
[88] | 154 | _staIDs.push_back(getThread->staID());
|
---|
[186] | 155 | _threads.push_back(getThread);
|
---|
[35] | 156 | }
|
---|
| 157 |
|
---|
| 158 | // Error in get thread
|
---|
| 159 | ////////////////////////////////////////////////////////////////////////////
|
---|
[88] | 160 | void bncCaster::slotGetThreadError(const QByteArray& staID) {
|
---|
| 161 | _staIDs.removeAll(staID);
|
---|
[82] | 162 | emit( newMessage(
|
---|
[88] | 163 | QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
|
---|
| 164 | if (_staIDs.size() == 0) {
|
---|
[82] | 165 | emit(newMessage("bncCaster:: last get thread terminated"));
|
---|
[35] | 166 | emit getThreadErrors();
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | // Dump Complete Epochs
|
---|
| 171 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 172 | void bncCaster::dumpEpochs(long minTime, long maxTime) {
|
---|
| 173 |
|
---|
| 174 | const char begEpoch = 'A';
|
---|
| 175 | const char begObs = 'B';
|
---|
| 176 | const char endEpoch = 'C';
|
---|
| 177 |
|
---|
| 178 | for (long sec = minTime; sec <= maxTime; sec++) {
|
---|
[140] | 179 |
|
---|
[35] | 180 | bool first = true;
|
---|
| 181 | QList<Observation*> allObs = _epochs->values(sec);
|
---|
| 182 | QListIterator<Observation*> it(allObs);
|
---|
| 183 | while (it.hasNext()) {
|
---|
| 184 | Observation* obs = it.next();
|
---|
| 185 |
|
---|
[140] | 186 | if (_samplingRate == 0 || sec % _samplingRate == 0) {
|
---|
| 187 |
|
---|
| 188 | // Output into the file
|
---|
| 189 | // --------------------
|
---|
| 190 | if (_out) {
|
---|
[35] | 191 | if (first) {
|
---|
[140] | 192 | *_out << begEpoch << endl;;
|
---|
[35] | 193 | }
|
---|
[140] | 194 | *_out << obs->StatID << " "
|
---|
| 195 | << (int) obs->SVPRN << " "
|
---|
| 196 | << (int) obs->GPSWeek << " "
|
---|
| 197 | << obs->GPSWeeks << " "
|
---|
| 198 | << obs->sec << " "
|
---|
| 199 | << obs->pCodeIndicator << " "
|
---|
| 200 | << obs->cumuLossOfCont << " "
|
---|
| 201 | << obs->C1 << " "
|
---|
| 202 | << obs->P2 << " "
|
---|
| 203 | << obs->L1 << " "
|
---|
| 204 | << obs->L2 << endl;
|
---|
[35] | 205 | if (!it.hasNext()) {
|
---|
[140] | 206 | *_out << endEpoch << endl;
|
---|
[35] | 207 | }
|
---|
| 208 | }
|
---|
[140] | 209 |
|
---|
| 210 | // Output into the socket
|
---|
| 211 | // ----------------------
|
---|
| 212 | if (_sockets) {
|
---|
| 213 | int numBytes = sizeof(*obs);
|
---|
| 214 | QListIterator<QTcpSocket*> is(*_sockets);
|
---|
| 215 | while (is.hasNext()) {
|
---|
| 216 | QTcpSocket* sock = is.next();
|
---|
| 217 | if (first) {
|
---|
| 218 | sock->write(&begEpoch, 1);
|
---|
| 219 | }
|
---|
| 220 | sock->write(&begObs, 1);
|
---|
| 221 | sock->write((char*) obs, numBytes);
|
---|
| 222 | if (!it.hasNext()) {
|
---|
| 223 | sock->write(&endEpoch, 1);
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
[73] | 227 | }
|
---|
| 228 |
|
---|
[35] | 229 | delete obs;
|
---|
| 230 | _epochs->remove(sec);
|
---|
| 231 | first = false;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | }
|
---|