[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()) {
|
---|
| 76 | _logFile = 0;
|
---|
[812] | 77 | }
|
---|
[816] | 78 | else {
|
---|
| 79 | _logFile = new QFile(logFileName);
|
---|
[817] | 80 | if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
---|
[816] | 81 | _logStream = new QTextStream(_logFile);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[847] | 84 |
|
---|
| 85 | // RINEX writer
|
---|
| 86 | // ------------
|
---|
| 87 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
---|
| 88 | _rnx = 0;
|
---|
| 89 | }
|
---|
| 90 | else {
|
---|
[850] | 91 | QString prep = "BNS";
|
---|
[857] | 92 | QString ext = ".clk";
|
---|
[850] | 93 | QString path = settings.value("rnxPath").toString();
|
---|
| 94 | QString intr = settings.value("rnxIntr").toString();
|
---|
| 95 | int sampl = settings.value("rnxSampl").toInt();
|
---|
| 96 | _rnx = new bnsRinex(prep, ext, path, intr, sampl);
|
---|
[847] | 97 | }
|
---|
[848] | 98 |
|
---|
| 99 | // SP3 writer
|
---|
| 100 | // ----------
|
---|
| 101 | if ( settings.value("sp3Path").toString().isEmpty() ) {
|
---|
| 102 | _sp3 = 0;
|
---|
| 103 | }
|
---|
| 104 | else {
|
---|
[850] | 105 | QString prep = "BNS";
|
---|
[857] | 106 | QString ext = ".sp3";
|
---|
[850] | 107 | QString path = settings.value("sp3Path").toString();
|
---|
| 108 | QString intr = settings.value("sp3Intr").toString();
|
---|
| 109 | int sampl = settings.value("sp3Sampl").toInt();
|
---|
| 110 | _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
|
---|
[848] | 111 | }
|
---|
[756] | 112 | }
|
---|
| 113 |
|
---|
| 114 | // Destructor
|
---|
| 115 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 116 | t_bns::~t_bns() {
|
---|
[763] | 117 | deleteBnsEph();
|
---|
[769] | 118 | delete _clkServer;
|
---|
[837] | 119 | delete _clkSocket;
|
---|
[770] | 120 | delete _outSocket;
|
---|
[816] | 121 | delete _outStream;
|
---|
| 122 | delete _logStream;
|
---|
[812] | 123 | delete _outFile;
|
---|
| 124 | delete _logFile;
|
---|
[779] | 125 | QMapIterator<QString, t_ephPair*> it(_ephList);
|
---|
| 126 | while (it.hasNext()) {
|
---|
| 127 | it.next();
|
---|
| 128 | delete it.value();
|
---|
| 129 | }
|
---|
[849] | 130 | delete _rnx;
|
---|
| 131 | delete _sp3;
|
---|
[756] | 132 | }
|
---|
| 133 |
|
---|
[763] | 134 | // Delete bns thread
|
---|
| 135 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 136 | void t_bns::deleteBnsEph() {
|
---|
| 137 | if (_bnseph) {
|
---|
| 138 | _bnseph->terminate();
|
---|
[764] | 139 | _bnseph->wait(100);
|
---|
[763] | 140 | delete _bnseph;
|
---|
| 141 | _bnseph = 0;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[756] | 145 | // Write a Program Message
|
---|
| 146 | ////////////////////////////////////////////////////////////////////////////
|
---|
[758] | 147 | void t_bns::slotMessage(const QByteArray msg) {
|
---|
[816] | 148 | if (_logStream) {
|
---|
| 149 | *_logStream << msg << endl;
|
---|
[818] | 150 | _logStream->flush();
|
---|
[812] | 151 | }
|
---|
[757] | 152 | emit(newMessage(msg));
|
---|
[756] | 153 | }
|
---|
| 154 |
|
---|
[760] | 155 | // Write a Program Message
|
---|
| 156 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 157 | void t_bns::slotError(const QByteArray msg) {
|
---|
[816] | 158 | if (_logStream) {
|
---|
| 159 | *_logStream << msg << endl;
|
---|
[818] | 160 | _logStream->flush();
|
---|
[812] | 161 | }
|
---|
[763] | 162 | deleteBnsEph();
|
---|
[760] | 163 | emit(error(msg));
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[769] | 166 | // New Connection
|
---|
| 167 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 168 | void t_bns::slotNewConnection() {
|
---|
[786] | 169 | slotMessage("t_bns::slotNewConnection");
|
---|
[787] | 170 | delete _clkSocket;
|
---|
[769] | 171 | _clkSocket = _clkServer->nextPendingConnection();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[770] | 174 | // Start the Communication with NTRIP Caster
|
---|
| 175 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 176 | void t_bns::openCaster() {
|
---|
[858] | 177 |
|
---|
| 178 | delete _outSocket; _outSocket = 0;
|
---|
| 179 |
|
---|
| 180 | double minDt = exp2(_outSocketOpenTrial);
|
---|
| 181 | if (++_outSocketOpenTrial > 8) {
|
---|
| 182 | _outSocketOpenTrial = 8;
|
---|
| 183 | }
|
---|
| 184 | if (_outSocketOpenTime.isValid() &&
|
---|
| 185 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 | else {
|
---|
| 189 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[770] | 192 | QSettings settings;
|
---|
| 193 | _outSocket = new QTcpSocket();
|
---|
[811] | 194 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
---|
| 195 | settings.value("outPort").toInt());
|
---|
[770] | 196 |
|
---|
[819] | 197 | const int timeOut = 100; // 0.1 seconds
|
---|
| 198 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 199 | delete _outSocket;
|
---|
| 200 | _outSocket = 0;
|
---|
| 201 | emit(error("bns::openCaster Connect Timeout"));
|
---|
[840] | 202 | return;
|
---|
[819] | 203 | }
|
---|
| 204 |
|
---|
[770] | 205 | QString mountpoint = settings.value("mountpoint").toString();
|
---|
| 206 | QString password = settings.value("password").toString();
|
---|
| 207 |
|
---|
| 208 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
| 209 | mountpoint.toAscii() + "\r\n" +
|
---|
| 210 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
| 211 |
|
---|
| 212 | _outSocket->write(msg);
|
---|
[820] | 213 | _outSocket->waitForBytesWritten();
|
---|
[770] | 214 |
|
---|
[820] | 215 | _outSocket->waitForReadyRead();
|
---|
[770] | 216 | QByteArray ans = _outSocket->readLine();
|
---|
| 217 |
|
---|
| 218 | if (ans.indexOf("OK") == -1) {
|
---|
| 219 | delete _outSocket;
|
---|
| 220 | _outSocket = 0;
|
---|
[831] | 221 | slotMessage("bns::openCaster socket deleted");
|
---|
[770] | 222 | }
|
---|
[831] | 223 | else {
|
---|
| 224 | slotMessage("bns::openCaster socket OK");
|
---|
[858] | 225 | _outSocketOpenTrial = 0;
|
---|
[831] | 226 | }
|
---|
[770] | 227 | }
|
---|
| 228 |
|
---|
[784] | 229 | //
|
---|
| 230 | ////////////////////////////////////////////////////////////////////////////
|
---|
[884] | 231 | void t_bns::slotNewEph(t_eph* ep) {
|
---|
[784] | 232 |
|
---|
| 233 | QMutexLocker locker(&_mutex);
|
---|
| 234 |
|
---|
| 235 | t_ephPair* pair;
|
---|
[884] | 236 | if ( !_ephList.contains(ep->prn()) ) {
|
---|
[784] | 237 | pair = new t_ephPair();
|
---|
[884] | 238 | _ephList.insert(ep->prn(), pair);
|
---|
[784] | 239 | }
|
---|
| 240 | else {
|
---|
[884] | 241 | pair = _ephList[ep->prn()];
|
---|
[784] | 242 | }
|
---|
| 243 |
|
---|
| 244 | if (pair->eph == 0) {
|
---|
| 245 | pair->eph = ep;
|
---|
| 246 | }
|
---|
| 247 | else {
|
---|
[884] | 248 | if (ep->isNewerThan(pair->eph)) {
|
---|
[784] | 249 | delete pair->oldEph;
|
---|
| 250 | pair->oldEph = pair->eph;
|
---|
| 251 | pair->eph = ep;
|
---|
| 252 | }
|
---|
| 253 | else {
|
---|
| 254 | delete ep;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[756] | 259 | // Start
|
---|
| 260 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 261 | void t_bns::run() {
|
---|
[769] | 262 |
|
---|
[758] | 263 | slotMessage("============ Start BNS ============");
|
---|
[770] | 264 |
|
---|
[828] | 265 | // Start Thread that retrieves broadcast Ephemeris
|
---|
| 266 | // -----------------------------------------------
|
---|
[758] | 267 | _bnseph->start();
|
---|
[769] | 268 |
|
---|
[770] | 269 | // Endless loop
|
---|
| 270 | // ------------
|
---|
[769] | 271 | while (true) {
|
---|
[836] | 272 |
|
---|
| 273 | if (_clkSocket && _clkSocket->thread() != currentThread()) {
|
---|
| 274 | emit(moveSocket(currentThread()));
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[796] | 277 | if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 278 | if ( _clkSocket->canReadLine()) {
|
---|
[832] | 279 | if (_outSocket == 0 ||
|
---|
| 280 | _outSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
[830] | 281 | openCaster();
|
---|
| 282 | }
|
---|
[796] | 283 | readEpoch();
|
---|
| 284 | }
|
---|
[809] | 285 | else {
|
---|
| 286 | _clkSocket->waitForReadyRead(10);
|
---|
| 287 | }
|
---|
[769] | 288 | }
|
---|
| 289 | else {
|
---|
[794] | 290 | msleep(10);
|
---|
[769] | 291 | }
|
---|
| 292 | }
|
---|
[756] | 293 | }
|
---|
| 294 |
|
---|
[778] | 295 | //
|
---|
| 296 | ////////////////////////////////////////////////////////////////////////////
|
---|
[784] | 297 | void t_bns::readEpoch() {
|
---|
[778] | 298 |
|
---|
[784] | 299 | QByteArray line = _clkSocket->readLine();
|
---|
[786] | 300 |
|
---|
[784] | 301 | if (line.indexOf('*') == -1) {
|
---|
| 302 | return;
|
---|
[778] | 303 | }
|
---|
| 304 |
|
---|
[784] | 305 | QTextStream in(line);
|
---|
| 306 |
|
---|
| 307 | QString hlp;
|
---|
[798] | 308 | int GPSweek, numSat;
|
---|
| 309 | double GPSweeks;
|
---|
[784] | 310 |
|
---|
[798] | 311 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
---|
[784] | 312 |
|
---|
[874] | 313 | if (numSat > 0) {
|
---|
| 314 |
|
---|
| 315 | for (int oldEph = 0; oldEph <= 1; oldEph++) {
|
---|
[863] | 316 |
|
---|
[872] | 317 | struct ClockOrbit co;
|
---|
| 318 | memset(&co, 0, sizeof(co));
|
---|
| 319 | co.GPSEpochTime = (int)GPSweeks;
|
---|
| 320 | co.ClockDataSupplied = 1;
|
---|
| 321 | co.OrbitDataSupplied = 1;
|
---|
| 322 | co.SatRefPoint = POINT_CENTER;
|
---|
| 323 | co.SatRefDatum = DATUM_ITRF;
|
---|
[863] | 324 |
|
---|
[872] | 325 | for (int ii = 1; ii <= numSat; ii++) {
|
---|
[873] | 326 |
|
---|
[872] | 327 | QString prn;
|
---|
| 328 | ColumnVector xx(4);
|
---|
[884] | 329 | t_eph* ep = 0;
|
---|
[784] | 330 |
|
---|
[873] | 331 | if (oldEph == 0) {
|
---|
| 332 | line = _clkSocket->readLine();
|
---|
| 333 | QTextStream in(line);
|
---|
| 334 | in >> prn;
|
---|
| 335 | if ( _ephList.contains(prn) ) {
|
---|
| 336 | in >> xx(1) >> xx(2) >> xx(3) >> xx(4); xx(4) *= 1e-6;
|
---|
| 337 | t_ephPair* pair = _ephList[prn];
|
---|
| 338 | pair->xx = xx;
|
---|
[872] | 339 | ep = pair->eph;
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
[873] | 342 | else {
|
---|
| 343 | if ( _ephList.contains(prn) ) {
|
---|
| 344 | t_ephPair* pair = _ephList[prn];
|
---|
[884] | 345 | prn = pair->eph->prn();
|
---|
[873] | 346 | xx = pair->xx;
|
---|
| 347 | ep = pair->oldEph;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
[784] | 350 |
|
---|
[872] | 351 | if (ep != 0) {
|
---|
| 352 | struct ClockOrbit::SatData* sd = 0;
|
---|
| 353 | if (prn[0] == 'G') {
|
---|
| 354 | sd = co.Sat + co.NumberOfGPSSat;
|
---|
| 355 | ++co.NumberOfGPSSat;
|
---|
| 356 | }
|
---|
| 357 | else if (prn[0] == 'R') {
|
---|
| 358 | sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
|
---|
| 359 | ++co.NumberOfGLONASSSat;
|
---|
| 360 | }
|
---|
| 361 | processSatellite(ep, GPSweek, GPSweeks, prn, xx, sd);
|
---|
| 362 | }
|
---|
[863] | 363 | }
|
---|
[872] | 364 |
|
---|
| 365 | if ( _outSocket &&
|
---|
| 366 | (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
|
---|
| 367 | char obuffer[CLOCKORBIT_BUFFERSIZE];
|
---|
| 368 | int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
|
---|
| 369 | if (len > 0) {
|
---|
| 370 | _outSocket->write(obuffer, len);
|
---|
| 371 | _outSocket->flush();
|
---|
| 372 | }
|
---|
| 373 | }
|
---|
[863] | 374 | }
|
---|
[780] | 375 | }
|
---|
[778] | 376 | }
|
---|
[784] | 377 |
|
---|
| 378 | //
|
---|
| 379 | ////////////////////////////////////////////////////////////////////////////
|
---|
[884] | 380 | void t_bns::processSatellite(t_eph* ep, int GPSweek, double GPSweeks,
|
---|
[872] | 381 | const QString& prn, const ColumnVector& xx,
|
---|
[863] | 382 | struct ClockOrbit::SatData* sd) {
|
---|
[784] | 383 |
|
---|
[799] | 384 | ColumnVector xB(4);
|
---|
[802] | 385 | ColumnVector vv(3);
|
---|
[799] | 386 |
|
---|
[884] | 387 | ep->position(GPSweek, GPSweeks, xB, vv);
|
---|
[799] | 388 |
|
---|
[804] | 389 | ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
|
---|
| 390 | double dClk = (xx(4) - xB(4)) * 299792458.0;
|
---|
| 391 | ColumnVector rsw(3);
|
---|
[800] | 392 |
|
---|
[806] | 393 | XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
|
---|
[804] | 394 |
|
---|
[863] | 395 | if (sd) {
|
---|
| 396 | sd->ID = prn.mid(1).toInt();
|
---|
[884] | 397 | sd->IOD = ep->IOD();
|
---|
[862] | 398 | sd->Clock.DeltaA0 = dClk;
|
---|
| 399 | sd->Orbit.DeltaRadial = rsw(1);
|
---|
| 400 | sd->Orbit.DeltaAlongTrack = rsw(2);
|
---|
| 401 | sd->Orbit.DeltaCrossTrack = rsw(3);
|
---|
[863] | 402 | }
|
---|
[862] | 403 |
|
---|
[863] | 404 | if (_outStream) {
|
---|
| 405 | QString line;
|
---|
[884] | 406 | line.sprintf("%d %.1f %s %3d %8.3f %8.3f %8.3f %8.3f\n",
|
---|
| 407 | GPSweek, GPSweeks, ep->prn().toAscii().data(),
|
---|
| 408 | ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
|
---|
| 409 | *_outStream << line;
|
---|
[863] | 410 | _outStream->flush();
|
---|
[811] | 411 | }
|
---|
[847] | 412 | if (_rnx) {
|
---|
| 413 | _rnx->write(GPSweek, GPSweeks, prn, xx);
|
---|
| 414 | }
|
---|
[848] | 415 | if (_sp3) {
|
---|
| 416 | _sp3->write(GPSweek, GPSweeks, prn, xx);
|
---|
| 417 | }
|
---|
[784] | 418 | }
|
---|
[836] | 419 |
|
---|
| 420 | //
|
---|
| 421 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 422 | void t_bns::slotMoveSocket(QThread* tt) {
|
---|
| 423 | _clkSocket->setParent(0);
|
---|
| 424 | _clkSocket->moveToThread(tt);
|
---|
| 425 | slotMessage("bns::slotMoveSocket");
|
---|
| 426 | }
|
---|