[756] | 1 | /* -------------------------------------------------------------------------
|
---|
| 2 | * BKG NTRIP Server
|
---|
| 3 | * -------------------------------------------------------------------------
|
---|
| 4 | *
|
---|
| 5 | * Class: bns
|
---|
| 6 | *
|
---|
| 7 | * Purpose: This class implements the main application behaviour
|
---|
| 8 | *
|
---|
| 9 | * Author: L. Mervart
|
---|
| 10 | *
|
---|
| 11 | * Created: 29-Mar-2008
|
---|
| 12 | *
|
---|
| 13 | * Changes:
|
---|
| 14 | *
|
---|
| 15 | * -----------------------------------------------------------------------*/
|
---|
| 16 |
|
---|
[858] | 17 | #include <math.h>
|
---|
[756] | 18 | #include <iostream>
|
---|
[800] | 19 | #include <newmatio.h>
|
---|
[756] | 20 |
|
---|
| 21 | #include "bns.h"
|
---|
[799] | 22 | #include "bnsutils.h"
|
---|
[847] | 23 | #include "bnsrinex.h"
|
---|
[848] | 24 | #include "bnssp3.h"
|
---|
[756] | 25 |
|
---|
| 26 | using namespace std;
|
---|
| 27 |
|
---|
| 28 | // Constructor
|
---|
| 29 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 30 | t_bns::t_bns(QObject* parent) : QThread(parent) {
|
---|
[760] | 31 |
|
---|
[764] | 32 | this->setTerminationEnabled(true);
|
---|
[828] | 33 |
|
---|
[836] | 34 | connect(this, SIGNAL(moveSocket(QThread*)),
|
---|
| 35 | this, SLOT(slotMoveSocket(QThread*)));
|
---|
| 36 |
|
---|
[828] | 37 | // Thread that handles broadcast ephemeris
|
---|
| 38 | // ---------------------------------------
|
---|
| 39 | _bnseph = new t_bnseph(parent);
|
---|
[827] | 40 |
|
---|
[884] | 41 | connect(_bnseph, SIGNAL(newEph(t_eph*)), this, SLOT(slotNewEph(t_eph*)));
|
---|
[828] | 42 | connect(_bnseph, SIGNAL(newMessage(QByteArray)),
|
---|
| 43 | this, SLOT(slotMessage(const QByteArray)));
|
---|
| 44 | connect(_bnseph, SIGNAL(error(QByteArray)),
|
---|
| 45 | this, SLOT(slotError(const QByteArray)));
|
---|
[760] | 46 |
|
---|
[827] | 47 | // Server listening for rtnet results
|
---|
| 48 | // ----------------------------------
|
---|
[786] | 49 | QSettings settings;
|
---|
[828] | 50 | _clkSocket = 0;
|
---|
[827] | 51 | _clkServer = new QTcpServer;
|
---|
| 52 | _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
|
---|
[828] | 53 | connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
|
---|
[827] | 54 |
|
---|
[828] | 55 | // Socket and file for outputting the results
|
---|
| 56 | // -------------------------------------------
|
---|
[858] | 57 | _outSocket = 0;
|
---|
| 58 | _outSocketOpenTrial = 0;
|
---|
[827] | 59 |
|
---|
[816] | 60 | QString outFileName = settings.value("outFile").toString();
|
---|
| 61 | if (outFileName.isEmpty()) {
|
---|
[833] | 62 | _outFile = 0;
|
---|
| 63 | _outStream = 0;
|
---|
[811] | 64 | }
|
---|
[816] | 65 | else {
|
---|
| 66 | _outFile = new QFile(outFileName);
|
---|
[817] | 67 | if (_outFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
---|
[816] | 68 | _outStream = new QTextStream(_outFile);
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[812] | 71 |
|
---|
| 72 | // Log File
|
---|
| 73 | // --------
|
---|
[816] | 74 | QString logFileName = settings.value("logFile").toString();
|
---|
| 75 | if (logFileName.isEmpty()) {
|
---|
[947] | 76 | _logFile = 0;
|
---|
| 77 | _logStream = 0;
|
---|
[812] | 78 | }
|
---|
[816] | 79 | else {
|
---|
| 80 | _logFile = new QFile(logFileName);
|
---|
[817] | 81 | if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
---|
[816] | 82 | _logStream = new QTextStream(_logFile);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[847] | 85 |
|
---|
| 86 | // RINEX writer
|
---|
| 87 | // ------------
|
---|
| 88 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
---|
| 89 | _rnx = 0;
|
---|
| 90 | }
|
---|
| 91 | else {
|
---|
[850] | 92 | QString prep = "BNS";
|
---|
[857] | 93 | QString ext = ".clk";
|
---|
[850] | 94 | QString path = settings.value("rnxPath").toString();
|
---|
| 95 | QString intr = settings.value("rnxIntr").toString();
|
---|
| 96 | int sampl = settings.value("rnxSampl").toInt();
|
---|
| 97 | _rnx = new bnsRinex(prep, ext, path, intr, sampl);
|
---|
[847] | 98 | }
|
---|
[848] | 99 |
|
---|
| 100 | // SP3 writer
|
---|
| 101 | // ----------
|
---|
| 102 | if ( settings.value("sp3Path").toString().isEmpty() ) {
|
---|
| 103 | _sp3 = 0;
|
---|
| 104 | }
|
---|
| 105 | else {
|
---|
[850] | 106 | QString prep = "BNS";
|
---|
[857] | 107 | QString ext = ".sp3";
|
---|
[850] | 108 | QString path = settings.value("sp3Path").toString();
|
---|
| 109 | QString intr = settings.value("sp3Intr").toString();
|
---|
| 110 | int sampl = settings.value("sp3Sampl").toInt();
|
---|
| 111 | _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
|
---|
[848] | 112 | }
|
---|
[756] | 113 | }
|
---|
| 114 |
|
---|
| 115 | // Destructor
|
---|
| 116 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 117 | t_bns::~t_bns() {
|
---|
[763] | 118 | deleteBnsEph();
|
---|
[769] | 119 | delete _clkServer;
|
---|
[837] | 120 | delete _clkSocket;
|
---|
[770] | 121 | delete _outSocket;
|
---|
[816] | 122 | delete _outStream;
|
---|
| 123 | delete _logStream;
|
---|
[812] | 124 | delete _outFile;
|
---|
| 125 | delete _logFile;
|
---|
[779] | 126 | QMapIterator<QString, t_ephPair*> it(_ephList);
|
---|
| 127 | while (it.hasNext()) {
|
---|
| 128 | it.next();
|
---|
| 129 | delete it.value();
|
---|
| 130 | }
|
---|
[849] | 131 | delete _rnx;
|
---|
| 132 | delete _sp3;
|
---|
[756] | 133 | }
|
---|
| 134 |
|
---|
[763] | 135 | // Delete bns thread
|
---|
| 136 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 137 | void t_bns::deleteBnsEph() {
|
---|
| 138 | if (_bnseph) {
|
---|
| 139 | _bnseph->terminate();
|
---|
[764] | 140 | _bnseph->wait(100);
|
---|
[763] | 141 | delete _bnseph;
|
---|
| 142 | _bnseph = 0;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[756] | 146 | // Write a Program Message
|
---|
| 147 | ////////////////////////////////////////////////////////////////////////////
|
---|
[758] | 148 | void t_bns::slotMessage(const QByteArray msg) {
|
---|
[816] | 149 | if (_logStream) {
|
---|
| 150 | *_logStream << msg << endl;
|
---|
[818] | 151 | _logStream->flush();
|
---|
[812] | 152 | }
|
---|
[757] | 153 | emit(newMessage(msg));
|
---|
[756] | 154 | }
|
---|
| 155 |
|
---|
[760] | 156 | // Write a Program Message
|
---|
| 157 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 158 | void t_bns::slotError(const QByteArray msg) {
|
---|
[816] | 159 | if (_logStream) {
|
---|
| 160 | *_logStream << msg << endl;
|
---|
[818] | 161 | _logStream->flush();
|
---|
[812] | 162 | }
|
---|
[763] | 163 | deleteBnsEph();
|
---|
[760] | 164 | emit(error(msg));
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[769] | 167 | // New Connection
|
---|
| 168 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 169 | void t_bns::slotNewConnection() {
|
---|
[786] | 170 | slotMessage("t_bns::slotNewConnection");
|
---|
[787] | 171 | delete _clkSocket;
|
---|
[769] | 172 | _clkSocket = _clkServer->nextPendingConnection();
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[770] | 175 | // Start the Communication with NTRIP Caster
|
---|
| 176 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 177 | void t_bns::openCaster() {
|
---|
[858] | 178 |
|
---|
| 179 | delete _outSocket; _outSocket = 0;
|
---|
| 180 |
|
---|
| 181 | double minDt = exp2(_outSocketOpenTrial);
|
---|
| 182 | if (++_outSocketOpenTrial > 8) {
|
---|
| 183 | _outSocketOpenTrial = 8;
|
---|
| 184 | }
|
---|
| 185 | if (_outSocketOpenTime.isValid() &&
|
---|
| 186 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 187 | return;
|
---|
| 188 | }
|
---|
| 189 | else {
|
---|
| 190 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[770] | 193 | QSettings settings;
|
---|
| 194 | _outSocket = new QTcpSocket();
|
---|
[811] | 195 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
---|
| 196 | settings.value("outPort").toInt());
|
---|
[770] | 197 |
|
---|
[819] | 198 | const int timeOut = 100; // 0.1 seconds
|
---|
| 199 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 200 | delete _outSocket;
|
---|
| 201 | _outSocket = 0;
|
---|
| 202 | emit(error("bns::openCaster Connect Timeout"));
|
---|
[840] | 203 | return;
|
---|
[819] | 204 | }
|
---|
| 205 |
|
---|
[770] | 206 | QString mountpoint = settings.value("mountpoint").toString();
|
---|
| 207 | QString password = settings.value("password").toString();
|
---|
| 208 |
|
---|
| 209 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
| 210 | mountpoint.toAscii() + "\r\n" +
|
---|
| 211 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
| 212 |
|
---|
| 213 | _outSocket->write(msg);
|
---|
[820] | 214 | _outSocket->waitForBytesWritten();
|
---|
[770] | 215 |
|
---|
[820] | 216 | _outSocket->waitForReadyRead();
|
---|
[770] | 217 | QByteArray ans = _outSocket->readLine();
|
---|
| 218 |
|
---|
| 219 | if (ans.indexOf("OK") == -1) {
|
---|
| 220 | delete _outSocket;
|
---|
| 221 | _outSocket = 0;
|
---|
[831] | 222 | slotMessage("bns::openCaster socket deleted");
|
---|
[770] | 223 | }
|
---|
[831] | 224 | else {
|
---|
| 225 | slotMessage("bns::openCaster socket OK");
|
---|
[858] | 226 | _outSocketOpenTrial = 0;
|
---|
[831] | 227 | }
|
---|
[770] | 228 | }
|
---|
| 229 |
|
---|
[784] | 230 | //
|
---|
| 231 | ////////////////////////////////////////////////////////////////////////////
|
---|
[884] | 232 | void t_bns::slotNewEph(t_eph* ep) {
|
---|
[784] | 233 |
|
---|
| 234 | QMutexLocker locker(&_mutex);
|
---|
| 235 |
|
---|
| 236 | t_ephPair* pair;
|
---|
[884] | 237 | if ( !_ephList.contains(ep->prn()) ) {
|
---|
[784] | 238 | pair = new t_ephPair();
|
---|
[884] | 239 | _ephList.insert(ep->prn(), pair);
|
---|
[784] | 240 | }
|
---|
| 241 | else {
|
---|
[884] | 242 | pair = _ephList[ep->prn()];
|
---|
[784] | 243 | }
|
---|
| 244 |
|
---|
| 245 | if (pair->eph == 0) {
|
---|
| 246 | pair->eph = ep;
|
---|
| 247 | }
|
---|
| 248 | else {
|
---|
[884] | 249 | if (ep->isNewerThan(pair->eph)) {
|
---|
[784] | 250 | delete pair->oldEph;
|
---|
| 251 | pair->oldEph = pair->eph;
|
---|
| 252 | pair->eph = ep;
|
---|
| 253 | }
|
---|
| 254 | else {
|
---|
| 255 | delete ep;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[756] | 260 | // Start
|
---|
| 261 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 262 | void t_bns::run() {
|
---|
[769] | 263 |
|
---|
[758] | 264 | slotMessage("============ Start BNS ============");
|
---|
[770] | 265 |
|
---|
[828] | 266 | // Start Thread that retrieves broadcast Ephemeris
|
---|
| 267 | // -----------------------------------------------
|
---|
[758] | 268 | _bnseph->start();
|
---|
[769] | 269 |
|
---|
[770] | 270 | // Endless loop
|
---|
| 271 | // ------------
|
---|
[769] | 272 | while (true) {
|
---|
[836] | 273 |
|
---|
| 274 | if (_clkSocket && _clkSocket->thread() != currentThread()) {
|
---|
| 275 | emit(moveSocket(currentThread()));
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[796] | 278 | if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 279 | if ( _clkSocket->canReadLine()) {
|
---|
[832] | 280 | if (_outSocket == 0 ||
|
---|
| 281 | _outSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
[830] | 282 | openCaster();
|
---|
| 283 | }
|
---|
[796] | 284 | readEpoch();
|
---|
| 285 | }
|
---|
[809] | 286 | else {
|
---|
| 287 | _clkSocket->waitForReadyRead(10);
|
---|
| 288 | }
|
---|
[769] | 289 | }
|
---|
| 290 | else {
|
---|
[794] | 291 | msleep(10);
|
---|
[769] | 292 | }
|
---|
| 293 | }
|
---|
[756] | 294 | }
|
---|
| 295 |
|
---|
[778] | 296 | //
|
---|
| 297 | ////////////////////////////////////////////////////////////////////////////
|
---|
[784] | 298 | void t_bns::readEpoch() {
|
---|
[778] | 299 |
|
---|
[784] | 300 | QByteArray line = _clkSocket->readLine();
|
---|
[786] | 301 |
|
---|
[784] | 302 | if (line.indexOf('*') == -1) {
|
---|
| 303 | return;
|
---|
[778] | 304 | }
|
---|
| 305 |
|
---|
[784] | 306 | QTextStream in(line);
|
---|
| 307 |
|
---|
| 308 | QString hlp;
|
---|
[798] | 309 | int GPSweek, numSat;
|
---|
| 310 | double GPSweeks;
|
---|
[784] | 311 |
|
---|
[798] | 312 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
---|
[784] | 313 |
|
---|
[874] | 314 | if (numSat > 0) {
|
---|
| 315 |
|
---|
[922] | 316 | QStringList prns;
|
---|
| 317 |
|
---|
[924] | 318 | /// for (int oldEph = 0; oldEph <= 1; oldEph++) {
|
---|
| 319 | for (int oldEph = 0; oldEph <= 0; oldEph++) {
|
---|
[905] | 320 |
|
---|
| 321 | struct ClockOrbit co;
|
---|
| 322 | memset(&co, 0, sizeof(co));
|
---|
[907] | 323 | co.GPSEpochTime = (int)GPSweeks;
|
---|
[909] | 324 | co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
|
---|
[905] | 325 | co.ClockDataSupplied = 1;
|
---|
| 326 | co.OrbitDataSupplied = 1;
|
---|
| 327 | co.SatRefPoint = POINT_CENTER;
|
---|
| 328 | co.SatRefDatum = DATUM_ITRF;
|
---|
| 329 |
|
---|
| 330 | for (int ii = 1; ii <= numSat; ii++) {
|
---|
| 331 |
|
---|
| 332 | QString prn;
|
---|
[927] | 333 | ColumnVector xx(5);
|
---|
[905] | 334 | t_eph* ep = 0;
|
---|
| 335 |
|
---|
| 336 | if (oldEph == 0) {
|
---|
| 337 | line = _clkSocket->readLine();
|
---|
| 338 | QTextStream in(line);
|
---|
| 339 | in >> prn;
|
---|
[922] | 340 | prns << prn;
|
---|
[905] | 341 | if ( _ephList.contains(prn) ) {
|
---|
[927] | 342 | in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5); xx(4) *= 1e-6;
|
---|
| 343 |
|
---|
| 344 | //// beg test (zero clock correction for Gerhard Wuebbena)
|
---|
[946] | 345 | //// xx(4) -= xx(5) / 299792458.0;
|
---|
[927] | 346 | //// end test
|
---|
| 347 |
|
---|
[905] | 348 | t_ephPair* pair = _ephList[prn];
|
---|
| 349 | pair->xx = xx;
|
---|
| 350 | ep = pair->eph;
|
---|
[872] | 351 | }
|
---|
[905] | 352 | }
|
---|
| 353 | else {
|
---|
[922] | 354 | prn = prns[ii-1];
|
---|
[905] | 355 | if ( _ephList.contains(prn) ) {
|
---|
| 356 | t_ephPair* pair = _ephList[prn];
|
---|
| 357 | prn = pair->eph->prn();
|
---|
| 358 | xx = pair->xx;
|
---|
| 359 | ep = pair->oldEph;
|
---|
[873] | 360 | }
|
---|
| 361 | }
|
---|
[905] | 362 |
|
---|
| 363 | if (ep != 0) {
|
---|
| 364 | struct ClockOrbit::SatData* sd = 0;
|
---|
| 365 | if (prn[0] == 'G') {
|
---|
| 366 | sd = co.Sat + co.NumberOfGPSSat;
|
---|
| 367 | ++co.NumberOfGPSSat;
|
---|
[872] | 368 | }
|
---|
[905] | 369 | else if (prn[0] == 'R') {
|
---|
| 370 | sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
|
---|
| 371 | ++co.NumberOfGLONASSSat;
|
---|
| 372 | }
|
---|
| 373 | if (sd) {
|
---|
[923] | 374 | processSatellite(oldEph, ep, GPSweek, GPSweeks, prn, xx, sd);
|
---|
[905] | 375 | }
|
---|
[872] | 376 | }
|
---|
[863] | 377 | }
|
---|
[905] | 378 |
|
---|
| 379 | if ( _outSocket &&
|
---|
| 380 | (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
|
---|
| 381 | char obuffer[CLOCKORBIT_BUFFERSIZE];
|
---|
| 382 | int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
|
---|
| 383 | if (len > 0) {
|
---|
| 384 | _outSocket->write(obuffer, len);
|
---|
| 385 | _outSocket->flush();
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
[863] | 388 | }
|
---|
[780] | 389 | }
|
---|
[778] | 390 | }
|
---|
[784] | 391 |
|
---|
| 392 | //
|
---|
| 393 | ////////////////////////////////////////////////////////////////////////////
|
---|
[923] | 394 | void t_bns::processSatellite(int oldEph, t_eph* ep, int GPSweek, double GPSweeks,
|
---|
[872] | 395 | const QString& prn, const ColumnVector& xx,
|
---|
[863] | 396 | struct ClockOrbit::SatData* sd) {
|
---|
[784] | 397 |
|
---|
[799] | 398 | ColumnVector xB(4);
|
---|
[802] | 399 | ColumnVector vv(3);
|
---|
[799] | 400 |
|
---|
[884] | 401 | ep->position(GPSweek, GPSweeks, xB, vv);
|
---|
[799] | 402 |
|
---|
[804] | 403 | ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
|
---|
[927] | 404 |
|
---|
[804] | 405 | double dClk = (xx(4) - xB(4)) * 299792458.0;
|
---|
| 406 | ColumnVector rsw(3);
|
---|
[800] | 407 |
|
---|
[806] | 408 | XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
|
---|
[804] | 409 |
|
---|
[863] | 410 | if (sd) {
|
---|
| 411 | sd->ID = prn.mid(1).toInt();
|
---|
[884] | 412 | sd->IOD = ep->IOD();
|
---|
[862] | 413 | sd->Clock.DeltaA0 = dClk;
|
---|
| 414 | sd->Orbit.DeltaRadial = rsw(1);
|
---|
| 415 | sd->Orbit.DeltaAlongTrack = rsw(2);
|
---|
| 416 | sd->Orbit.DeltaCrossTrack = rsw(3);
|
---|
[863] | 417 | }
|
---|
[862] | 418 |
|
---|
[863] | 419 | if (_outStream) {
|
---|
| 420 | QString line;
|
---|
[923] | 421 | char oldCh = (oldEph ? '!' : ' ');
|
---|
| 422 | line.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
|
---|
| 423 | oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
|
---|
[884] | 424 | ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
|
---|
| 425 | *_outStream << line;
|
---|
[863] | 426 | _outStream->flush();
|
---|
[811] | 427 | }
|
---|
[923] | 428 | if (!oldEph) {
|
---|
| 429 | if (_rnx) {
|
---|
| 430 | _rnx->write(GPSweek, GPSweeks, prn, xx);
|
---|
| 431 | }
|
---|
| 432 | if (_sp3) {
|
---|
| 433 | _sp3->write(GPSweek, GPSweeks, prn, xx);
|
---|
| 434 | }
|
---|
[847] | 435 | }
|
---|
[784] | 436 | }
|
---|
[836] | 437 |
|
---|
| 438 | //
|
---|
| 439 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 440 | void t_bns::slotMoveSocket(QThread* tt) {
|
---|
| 441 | _clkSocket->setParent(0);
|
---|
| 442 | _clkSocket->moveToThread(tt);
|
---|
| 443 | slotMessage("bns::slotMoveSocket");
|
---|
| 444 | }
|
---|