| 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 |
|
|---|
| 17 | #include <math.h>
|
|---|
| 18 | #include <iostream>
|
|---|
| 19 | #include <newmatio.h>
|
|---|
| 20 |
|
|---|
| 21 | #include "bns.h"
|
|---|
| 22 | #include "bnsutils.h"
|
|---|
| 23 | #include "bnsrinex.h"
|
|---|
| 24 | #include "bnssp3.h"
|
|---|
| 25 |
|
|---|
| 26 | using namespace std;
|
|---|
| 27 |
|
|---|
| 28 | // Constructor
|
|---|
| 29 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 30 | t_bns::t_bns(QObject* parent) : QThread(parent) {
|
|---|
| 31 |
|
|---|
| 32 | this->setTerminationEnabled(true);
|
|---|
| 33 |
|
|---|
| 34 | connect(this, SIGNAL(moveSocket(QThread*)),
|
|---|
| 35 | this, SLOT(slotMoveSocket(QThread*)));
|
|---|
| 36 |
|
|---|
| 37 | // Thread that handles broadcast ephemeris
|
|---|
| 38 | // ---------------------------------------
|
|---|
| 39 | _bnseph = new t_bnseph(parent);
|
|---|
| 40 |
|
|---|
| 41 | connect(_bnseph, SIGNAL(newEph(gpsEph*)), this, SLOT(slotNewEph(gpsEph*)));
|
|---|
| 42 | connect(_bnseph, SIGNAL(newMessage(QByteArray)),
|
|---|
| 43 | this, SLOT(slotMessage(const QByteArray)));
|
|---|
| 44 | connect(_bnseph, SIGNAL(error(QByteArray)),
|
|---|
| 45 | this, SLOT(slotError(const QByteArray)));
|
|---|
| 46 |
|
|---|
| 47 | // Server listening for rtnet results
|
|---|
| 48 | // ----------------------------------
|
|---|
| 49 | QSettings settings;
|
|---|
| 50 | _clkSocket = 0;
|
|---|
| 51 | _clkServer = new QTcpServer;
|
|---|
| 52 | _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
|
|---|
| 53 | connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
|
|---|
| 54 |
|
|---|
| 55 | // Socket and file for outputting the results
|
|---|
| 56 | // -------------------------------------------
|
|---|
| 57 | _outSocket = 0;
|
|---|
| 58 | _outSocketOpenTrial = 0;
|
|---|
| 59 |
|
|---|
| 60 | QString outFileName = settings.value("outFile").toString();
|
|---|
| 61 | if (outFileName.isEmpty()) {
|
|---|
| 62 | _outFile = 0;
|
|---|
| 63 | _outStream = 0;
|
|---|
| 64 | }
|
|---|
| 65 | else {
|
|---|
| 66 | _outFile = new QFile(outFileName);
|
|---|
| 67 | if (_outFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
|---|
| 68 | _outStream = new QTextStream(_outFile);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // Log File
|
|---|
| 73 | // --------
|
|---|
| 74 | QString logFileName = settings.value("logFile").toString();
|
|---|
| 75 | if (logFileName.isEmpty()) {
|
|---|
| 76 | _logFile = 0;
|
|---|
| 77 | }
|
|---|
| 78 | else {
|
|---|
| 79 | _logFile = new QFile(logFileName);
|
|---|
| 80 | if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
|---|
| 81 | _logStream = new QTextStream(_logFile);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // RINEX writer
|
|---|
| 86 | // ------------
|
|---|
| 87 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
|---|
| 88 | _rnx = 0;
|
|---|
| 89 | }
|
|---|
| 90 | else {
|
|---|
| 91 | QString prep = "BNS";
|
|---|
| 92 | QString ext = ".clk";
|
|---|
| 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);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | // SP3 writer
|
|---|
| 100 | // ----------
|
|---|
| 101 | if ( settings.value("sp3Path").toString().isEmpty() ) {
|
|---|
| 102 | _sp3 = 0;
|
|---|
| 103 | }
|
|---|
| 104 | else {
|
|---|
| 105 | QString prep = "BNS";
|
|---|
| 106 | QString ext = ".sp3";
|
|---|
| 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);
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | // Destructor
|
|---|
| 115 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 116 | t_bns::~t_bns() {
|
|---|
| 117 | deleteBnsEph();
|
|---|
| 118 | delete _clkServer;
|
|---|
| 119 | delete _clkSocket;
|
|---|
| 120 | delete _outSocket;
|
|---|
| 121 | delete _outStream;
|
|---|
| 122 | delete _logStream;
|
|---|
| 123 | delete _outFile;
|
|---|
| 124 | delete _logFile;
|
|---|
| 125 | QMapIterator<QString, t_ephPair*> it(_ephList);
|
|---|
| 126 | while (it.hasNext()) {
|
|---|
| 127 | it.next();
|
|---|
| 128 | delete it.value();
|
|---|
| 129 | }
|
|---|
| 130 | delete _rnx;
|
|---|
| 131 | delete _sp3;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | // Delete bns thread
|
|---|
| 135 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 136 | void t_bns::deleteBnsEph() {
|
|---|
| 137 | if (_bnseph) {
|
|---|
| 138 | _bnseph->terminate();
|
|---|
| 139 | _bnseph->wait(100);
|
|---|
| 140 | delete _bnseph;
|
|---|
| 141 | _bnseph = 0;
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | // Write a Program Message
|
|---|
| 146 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 147 | void t_bns::slotMessage(const QByteArray msg) {
|
|---|
| 148 | if (_logStream) {
|
|---|
| 149 | *_logStream << msg << endl;
|
|---|
| 150 | _logStream->flush();
|
|---|
| 151 | }
|
|---|
| 152 | emit(newMessage(msg));
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | // Write a Program Message
|
|---|
| 156 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 157 | void t_bns::slotError(const QByteArray msg) {
|
|---|
| 158 | if (_logStream) {
|
|---|
| 159 | *_logStream << msg << endl;
|
|---|
| 160 | _logStream->flush();
|
|---|
| 161 | }
|
|---|
| 162 | deleteBnsEph();
|
|---|
| 163 | emit(error(msg));
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | // New Connection
|
|---|
| 167 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 168 | void t_bns::slotNewConnection() {
|
|---|
| 169 | slotMessage("t_bns::slotNewConnection");
|
|---|
| 170 | delete _clkSocket;
|
|---|
| 171 | _clkSocket = _clkServer->nextPendingConnection();
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | // Start the Communication with NTRIP Caster
|
|---|
| 175 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 176 | void t_bns::openCaster() {
|
|---|
| 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 |
|
|---|
| 192 | QSettings settings;
|
|---|
| 193 | _outSocket = new QTcpSocket();
|
|---|
| 194 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
|---|
| 195 | settings.value("outPort").toInt());
|
|---|
| 196 |
|
|---|
| 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"));
|
|---|
| 202 | return;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 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);
|
|---|
| 213 | _outSocket->waitForBytesWritten();
|
|---|
| 214 |
|
|---|
| 215 | _outSocket->waitForReadyRead();
|
|---|
| 216 | QByteArray ans = _outSocket->readLine();
|
|---|
| 217 |
|
|---|
| 218 | if (ans.indexOf("OK") == -1) {
|
|---|
| 219 | delete _outSocket;
|
|---|
| 220 | _outSocket = 0;
|
|---|
| 221 | slotMessage("bns::openCaster socket deleted");
|
|---|
| 222 | }
|
|---|
| 223 | else {
|
|---|
| 224 | slotMessage("bns::openCaster socket OK");
|
|---|
| 225 | _outSocketOpenTrial = 0;
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | //
|
|---|
| 230 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 231 | void t_bns::slotNewEph(gpsEph* ep) {
|
|---|
| 232 |
|
|---|
| 233 | QMutexLocker locker(&_mutex);
|
|---|
| 234 |
|
|---|
| 235 | t_ephPair* pair;
|
|---|
| 236 | if ( !_ephList.contains(ep->prn) ) {
|
|---|
| 237 | pair = new t_ephPair();
|
|---|
| 238 | _ephList.insert(ep->prn, pair);
|
|---|
| 239 | }
|
|---|
| 240 | else {
|
|---|
| 241 | pair = _ephList[ep->prn];
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | if (pair->eph == 0) {
|
|---|
| 245 | pair->eph = ep;
|
|---|
| 246 | }
|
|---|
| 247 | else {
|
|---|
| 248 | if (ep->GPSweek > pair->eph->GPSweek ||
|
|---|
| 249 | (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
|
|---|
| 250 | delete pair->oldEph;
|
|---|
| 251 | pair->oldEph = pair->eph;
|
|---|
| 252 | pair->eph = ep;
|
|---|
| 253 | }
|
|---|
| 254 | else {
|
|---|
| 255 | delete ep;
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | // Start
|
|---|
| 261 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 262 | void t_bns::run() {
|
|---|
| 263 |
|
|---|
| 264 | slotMessage("============ Start BNS ============");
|
|---|
| 265 |
|
|---|
| 266 | // Start Thread that retrieves broadcast Ephemeris
|
|---|
| 267 | // -----------------------------------------------
|
|---|
| 268 | _bnseph->start();
|
|---|
| 269 |
|
|---|
| 270 | // Endless loop
|
|---|
| 271 | // ------------
|
|---|
| 272 | while (true) {
|
|---|
| 273 |
|
|---|
| 274 | if (_clkSocket && _clkSocket->thread() != currentThread()) {
|
|---|
| 275 | emit(moveSocket(currentThread()));
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
|
|---|
| 279 | if ( _clkSocket->canReadLine()) {
|
|---|
| 280 | if (_outSocket == 0 ||
|
|---|
| 281 | _outSocket->state() != QAbstractSocket::ConnectedState) {
|
|---|
| 282 | openCaster();
|
|---|
| 283 | }
|
|---|
| 284 | readEpoch();
|
|---|
| 285 | }
|
|---|
| 286 | else {
|
|---|
| 287 | _clkSocket->waitForReadyRead(10);
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 | else {
|
|---|
| 291 | msleep(10);
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | //
|
|---|
| 297 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 298 | void t_bns::readEpoch() {
|
|---|
| 299 |
|
|---|
| 300 | QByteArray line = _clkSocket->readLine();
|
|---|
| 301 |
|
|---|
| 302 | if (line.indexOf('*') == -1) {
|
|---|
| 303 | return;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | QTextStream in(line);
|
|---|
| 307 |
|
|---|
| 308 | QString hlp;
|
|---|
| 309 | int GPSweek, numSat;
|
|---|
| 310 | double GPSweeks;
|
|---|
| 311 |
|
|---|
| 312 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
|---|
| 313 |
|
|---|
| 314 | if (numSat > 0) {
|
|---|
| 315 |
|
|---|
| 316 | for (int oldEph = 0; oldEph <= 1; oldEph++) {
|
|---|
| 317 |
|
|---|
| 318 | struct ClockOrbit co;
|
|---|
| 319 | memset(&co, 0, sizeof(co));
|
|---|
| 320 | co.GPSEpochTime = (int)GPSweeks;
|
|---|
| 321 | co.ClockDataSupplied = 1;
|
|---|
| 322 | co.OrbitDataSupplied = 1;
|
|---|
| 323 | co.SatRefPoint = POINT_CENTER;
|
|---|
| 324 | co.SatRefDatum = DATUM_ITRF;
|
|---|
| 325 |
|
|---|
| 326 | for (int ii = 1; ii <= numSat; ii++) {
|
|---|
| 327 |
|
|---|
| 328 | QString prn;
|
|---|
| 329 | ColumnVector xx(4);
|
|---|
| 330 | gpsEph* ep = 0;
|
|---|
| 331 |
|
|---|
| 332 | if (oldEph == 0) {
|
|---|
| 333 | line = _clkSocket->readLine();
|
|---|
| 334 | QTextStream in(line);
|
|---|
| 335 | in >> prn;
|
|---|
| 336 | if ( _ephList.contains(prn) ) {
|
|---|
| 337 | in >> xx(1) >> xx(2) >> xx(3) >> xx(4); xx(4) *= 1e-6;
|
|---|
| 338 | t_ephPair* pair = _ephList[prn];
|
|---|
| 339 | pair->xx = xx;
|
|---|
| 340 | ep = pair->eph;
|
|---|
| 341 | }
|
|---|
| 342 | }
|
|---|
| 343 | else {
|
|---|
| 344 | if ( _ephList.contains(prn) ) {
|
|---|
| 345 | t_ephPair* pair = _ephList[prn];
|
|---|
| 346 | prn = pair->eph->prn;
|
|---|
| 347 | xx = pair->xx;
|
|---|
| 348 | ep = pair->oldEph;
|
|---|
| 349 | }
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | if (ep != 0) {
|
|---|
| 353 | struct ClockOrbit::SatData* sd = 0;
|
|---|
| 354 | if (prn[0] == 'G') {
|
|---|
| 355 | sd = co.Sat + co.NumberOfGPSSat;
|
|---|
| 356 | ++co.NumberOfGPSSat;
|
|---|
| 357 | }
|
|---|
| 358 | else if (prn[0] == 'R') {
|
|---|
| 359 | sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
|
|---|
| 360 | ++co.NumberOfGLONASSSat;
|
|---|
| 361 | }
|
|---|
| 362 | processSatellite(ep, GPSweek, GPSweeks, prn, xx, sd);
|
|---|
| 363 | }
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | if ( _outSocket &&
|
|---|
| 367 | (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
|
|---|
| 368 | char obuffer[CLOCKORBIT_BUFFERSIZE];
|
|---|
| 369 | int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
|
|---|
| 370 | if (len > 0) {
|
|---|
| 371 | _outSocket->write(obuffer, len);
|
|---|
| 372 | _outSocket->flush();
|
|---|
| 373 | }
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 | }
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | //
|
|---|
| 380 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 381 | void t_bns::processSatellite(gpsEph* ep, int GPSweek, double GPSweeks,
|
|---|
| 382 | const QString& prn, const ColumnVector& xx,
|
|---|
| 383 | struct ClockOrbit::SatData* sd) {
|
|---|
| 384 |
|
|---|
| 385 | ColumnVector xB(4);
|
|---|
| 386 | ColumnVector vv(3);
|
|---|
| 387 |
|
|---|
| 388 | satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
|
|---|
| 389 | vv(1), vv(2), vv(3));
|
|---|
| 390 |
|
|---|
| 391 | ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
|
|---|
| 392 | double dClk = (xx(4) - xB(4)) * 299792458.0;
|
|---|
| 393 | ColumnVector rsw(3);
|
|---|
| 394 |
|
|---|
| 395 | XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
|
|---|
| 396 |
|
|---|
| 397 | if (sd) {
|
|---|
| 398 | sd->ID = prn.mid(1).toInt();
|
|---|
| 399 | sd->IOD = int(ep->IODE);
|
|---|
| 400 | sd->Clock.DeltaA0 = dClk;
|
|---|
| 401 | sd->Orbit.DeltaRadial = rsw(1);
|
|---|
| 402 | sd->Orbit.DeltaAlongTrack = rsw(2);
|
|---|
| 403 | sd->Orbit.DeltaCrossTrack = rsw(3);
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | if (_outStream) {
|
|---|
| 407 | QString line;
|
|---|
| 408 | line.sprintf("%d %.1f %s %3d %3d %8.3f %8.3f %8.3f %8.3f\n",
|
|---|
| 409 | GPSweek, GPSweeks, ep->prn.toAscii().data(),
|
|---|
| 410 | int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
|
|---|
| 411 | *_outStream << line;
|
|---|
| 412 | _outStream->flush();
|
|---|
| 413 | }
|
|---|
| 414 | if (_rnx) {
|
|---|
| 415 | _rnx->write(GPSweek, GPSweeks, prn, xx);
|
|---|
| 416 | }
|
|---|
| 417 | if (_sp3) {
|
|---|
| 418 | _sp3->write(GPSweek, GPSweeks, prn, xx);
|
|---|
| 419 | }
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | //
|
|---|
| 423 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 424 | void t_bns::slotMoveSocket(QThread* tt) {
|
|---|
| 425 | _clkSocket->setParent(0);
|
|---|
| 426 | _clkSocket->moveToThread(tt);
|
|---|
| 427 | slotMessage("bns::slotMoveSocket");
|
|---|
| 428 | }
|
|---|