[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 |
|
---|
| 17 | #include <iostream>
|
---|
[800] | 18 | #include <newmatio.h>
|
---|
[756] | 19 |
|
---|
| 20 | #include "bns.h"
|
---|
[799] | 21 | #include "bnsutils.h"
|
---|
[847] | 22 | #include "bnsrinex.h"
|
---|
[848] | 23 | #include "bnssp3.h"
|
---|
[756] | 24 |
|
---|
| 25 | using namespace std;
|
---|
| 26 |
|
---|
| 27 | // Constructor
|
---|
| 28 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 29 | t_bns::t_bns(QObject* parent) : QThread(parent) {
|
---|
[760] | 30 |
|
---|
[764] | 31 | this->setTerminationEnabled(true);
|
---|
[828] | 32 |
|
---|
[836] | 33 | connect(this, SIGNAL(moveSocket(QThread*)),
|
---|
| 34 | this, SLOT(slotMoveSocket(QThread*)));
|
---|
| 35 |
|
---|
[828] | 36 | // Thread that handles broadcast ephemeris
|
---|
| 37 | // ---------------------------------------
|
---|
| 38 | _bnseph = new t_bnseph(parent);
|
---|
[827] | 39 |
|
---|
[828] | 40 | connect(_bnseph, SIGNAL(newEph(gpsEph*)), this, SLOT(slotNewEph(gpsEph*)));
|
---|
| 41 | connect(_bnseph, SIGNAL(newMessage(QByteArray)),
|
---|
| 42 | this, SLOT(slotMessage(const QByteArray)));
|
---|
| 43 | connect(_bnseph, SIGNAL(error(QByteArray)),
|
---|
| 44 | this, SLOT(slotError(const QByteArray)));
|
---|
[760] | 45 |
|
---|
[827] | 46 | // Server listening for rtnet results
|
---|
| 47 | // ----------------------------------
|
---|
[786] | 48 | QSettings settings;
|
---|
[828] | 49 | _clkSocket = 0;
|
---|
[827] | 50 | _clkServer = new QTcpServer;
|
---|
| 51 | _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
|
---|
[828] | 52 | connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
|
---|
[827] | 53 |
|
---|
[828] | 54 | // Socket and file for outputting the results
|
---|
| 55 | // -------------------------------------------
|
---|
| 56 | _outSocket = 0;
|
---|
[827] | 57 |
|
---|
[816] | 58 | QString outFileName = settings.value("outFile").toString();
|
---|
| 59 | if (outFileName.isEmpty()) {
|
---|
[833] | 60 | _outFile = 0;
|
---|
| 61 | _outStream = 0;
|
---|
[811] | 62 | }
|
---|
[816] | 63 | else {
|
---|
| 64 | _outFile = new QFile(outFileName);
|
---|
[817] | 65 | if (_outFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
---|
[816] | 66 | _outStream = new QTextStream(_outFile);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
[812] | 69 |
|
---|
| 70 | // Log File
|
---|
| 71 | // --------
|
---|
[816] | 72 | QString logFileName = settings.value("logFile").toString();
|
---|
| 73 | if (logFileName.isEmpty()) {
|
---|
| 74 | _logFile = 0;
|
---|
[812] | 75 | }
|
---|
[816] | 76 | else {
|
---|
| 77 | _logFile = new QFile(logFileName);
|
---|
[817] | 78 | if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
|
---|
[816] | 79 | _logStream = new QTextStream(_logFile);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[847] | 82 |
|
---|
| 83 | // RINEX writer
|
---|
| 84 | // ------------
|
---|
| 85 | if ( settings.value("rnxPath").toString().isEmpty() ) {
|
---|
| 86 | _rnx = 0;
|
---|
| 87 | }
|
---|
| 88 | else {
|
---|
| 89 | _rnx = new bnsRinex();
|
---|
| 90 | }
|
---|
[848] | 91 |
|
---|
| 92 | // SP3 writer
|
---|
| 93 | // ----------
|
---|
| 94 | if ( settings.value("sp3Path").toString().isEmpty() ) {
|
---|
| 95 | _sp3 = 0;
|
---|
| 96 | }
|
---|
| 97 | else {
|
---|
| 98 | _sp3 = new bnsSP3();
|
---|
| 99 | }
|
---|
[756] | 100 | }
|
---|
| 101 |
|
---|
| 102 | // Destructor
|
---|
| 103 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 104 | t_bns::~t_bns() {
|
---|
[763] | 105 | deleteBnsEph();
|
---|
[769] | 106 | delete _clkServer;
|
---|
[837] | 107 | delete _clkSocket;
|
---|
[770] | 108 | delete _outSocket;
|
---|
[816] | 109 | delete _outStream;
|
---|
| 110 | delete _logStream;
|
---|
[812] | 111 | delete _outFile;
|
---|
| 112 | delete _logFile;
|
---|
[779] | 113 | QMapIterator<QString, t_ephPair*> it(_ephList);
|
---|
| 114 | while (it.hasNext()) {
|
---|
| 115 | it.next();
|
---|
| 116 | delete it.value();
|
---|
| 117 | }
|
---|
[849] | 118 | delete _rnx;
|
---|
| 119 | delete _sp3;
|
---|
[756] | 120 | }
|
---|
| 121 |
|
---|
[763] | 122 | // Delete bns thread
|
---|
| 123 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 124 | void t_bns::deleteBnsEph() {
|
---|
| 125 | if (_bnseph) {
|
---|
| 126 | _bnseph->terminate();
|
---|
[764] | 127 | _bnseph->wait(100);
|
---|
[763] | 128 | delete _bnseph;
|
---|
| 129 | _bnseph = 0;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[756] | 133 | // Write a Program Message
|
---|
| 134 | ////////////////////////////////////////////////////////////////////////////
|
---|
[758] | 135 | void t_bns::slotMessage(const QByteArray msg) {
|
---|
[816] | 136 | if (_logStream) {
|
---|
| 137 | *_logStream << msg << endl;
|
---|
[818] | 138 | _logStream->flush();
|
---|
[812] | 139 | }
|
---|
[757] | 140 | emit(newMessage(msg));
|
---|
[756] | 141 | }
|
---|
| 142 |
|
---|
[760] | 143 | // Write a Program Message
|
---|
| 144 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 145 | void t_bns::slotError(const QByteArray msg) {
|
---|
[816] | 146 | if (_logStream) {
|
---|
| 147 | *_logStream << msg << endl;
|
---|
[818] | 148 | _logStream->flush();
|
---|
[812] | 149 | }
|
---|
[763] | 150 | deleteBnsEph();
|
---|
[760] | 151 | emit(error(msg));
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[769] | 154 | // New Connection
|
---|
| 155 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 156 | void t_bns::slotNewConnection() {
|
---|
[786] | 157 | slotMessage("t_bns::slotNewConnection");
|
---|
[787] | 158 | delete _clkSocket;
|
---|
[769] | 159 | _clkSocket = _clkServer->nextPendingConnection();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[770] | 162 | // Start the Communication with NTRIP Caster
|
---|
| 163 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 164 | void t_bns::openCaster() {
|
---|
| 165 |
|
---|
| 166 | QSettings settings;
|
---|
| 167 |
|
---|
[835] | 168 | delete _outSocket;
|
---|
[770] | 169 | _outSocket = new QTcpSocket();
|
---|
[811] | 170 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
---|
| 171 | settings.value("outPort").toInt());
|
---|
[770] | 172 |
|
---|
[819] | 173 | const int timeOut = 100; // 0.1 seconds
|
---|
| 174 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
| 175 | delete _outSocket;
|
---|
| 176 | _outSocket = 0;
|
---|
| 177 | emit(error("bns::openCaster Connect Timeout"));
|
---|
[840] | 178 | return;
|
---|
[819] | 179 | }
|
---|
| 180 |
|
---|
[770] | 181 | QString mountpoint = settings.value("mountpoint").toString();
|
---|
| 182 | QString password = settings.value("password").toString();
|
---|
| 183 |
|
---|
| 184 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
| 185 | mountpoint.toAscii() + "\r\n" +
|
---|
| 186 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
| 187 |
|
---|
| 188 | _outSocket->write(msg);
|
---|
[820] | 189 | _outSocket->waitForBytesWritten();
|
---|
[770] | 190 |
|
---|
[820] | 191 | _outSocket->waitForReadyRead();
|
---|
[770] | 192 | QByteArray ans = _outSocket->readLine();
|
---|
| 193 |
|
---|
| 194 | if (ans.indexOf("OK") == -1) {
|
---|
| 195 | delete _outSocket;
|
---|
| 196 | _outSocket = 0;
|
---|
[831] | 197 | slotMessage("bns::openCaster socket deleted");
|
---|
[770] | 198 | }
|
---|
[831] | 199 | else {
|
---|
| 200 | slotMessage("bns::openCaster socket OK");
|
---|
| 201 | }
|
---|
[770] | 202 | }
|
---|
| 203 |
|
---|
[784] | 204 | //
|
---|
| 205 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 206 | void t_bns::slotNewEph(gpsEph* ep) {
|
---|
| 207 |
|
---|
| 208 | QMutexLocker locker(&_mutex);
|
---|
| 209 |
|
---|
| 210 | t_ephPair* pair;
|
---|
| 211 | if ( !_ephList.contains(ep->prn) ) {
|
---|
| 212 | pair = new t_ephPair();
|
---|
| 213 | _ephList.insert(ep->prn, pair);
|
---|
| 214 | }
|
---|
| 215 | else {
|
---|
| 216 | pair = _ephList[ep->prn];
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | if (pair->eph == 0) {
|
---|
| 220 | pair->eph = ep;
|
---|
| 221 | }
|
---|
| 222 | else {
|
---|
| 223 | if (ep->GPSweek > pair->eph->GPSweek ||
|
---|
| 224 | (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
|
---|
| 225 | delete pair->oldEph;
|
---|
| 226 | pair->oldEph = pair->eph;
|
---|
| 227 | pair->eph = ep;
|
---|
| 228 | }
|
---|
| 229 | else {
|
---|
| 230 | delete ep;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[756] | 235 | // Start
|
---|
| 236 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 237 | void t_bns::run() {
|
---|
[769] | 238 |
|
---|
[758] | 239 | slotMessage("============ Start BNS ============");
|
---|
[770] | 240 |
|
---|
[828] | 241 | // Start Thread that retrieves broadcast Ephemeris
|
---|
| 242 | // -----------------------------------------------
|
---|
[758] | 243 | _bnseph->start();
|
---|
[769] | 244 |
|
---|
[770] | 245 | // Endless loop
|
---|
| 246 | // ------------
|
---|
[769] | 247 | while (true) {
|
---|
[836] | 248 |
|
---|
| 249 | if (_clkSocket && _clkSocket->thread() != currentThread()) {
|
---|
| 250 | emit(moveSocket(currentThread()));
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[796] | 253 | if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 254 | if ( _clkSocket->canReadLine()) {
|
---|
[832] | 255 | if (_outSocket == 0 ||
|
---|
| 256 | _outSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
[830] | 257 | openCaster();
|
---|
| 258 | }
|
---|
[796] | 259 | readEpoch();
|
---|
| 260 | }
|
---|
[809] | 261 | else {
|
---|
| 262 | _clkSocket->waitForReadyRead(10);
|
---|
| 263 | }
|
---|
[769] | 264 | }
|
---|
| 265 | else {
|
---|
[794] | 266 | msleep(10);
|
---|
[769] | 267 | }
|
---|
| 268 | }
|
---|
[756] | 269 | }
|
---|
| 270 |
|
---|
[778] | 271 | //
|
---|
| 272 | ////////////////////////////////////////////////////////////////////////////
|
---|
[784] | 273 | void t_bns::readEpoch() {
|
---|
[778] | 274 |
|
---|
[784] | 275 | QByteArray line = _clkSocket->readLine();
|
---|
[786] | 276 |
|
---|
[784] | 277 | if (line.indexOf('*') == -1) {
|
---|
| 278 | return;
|
---|
[778] | 279 | }
|
---|
| 280 |
|
---|
[784] | 281 | QTextStream in(line);
|
---|
| 282 |
|
---|
| 283 | QString hlp;
|
---|
[798] | 284 | int GPSweek, numSat;
|
---|
| 285 | double GPSweeks;
|
---|
[784] | 286 |
|
---|
[798] | 287 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
---|
[784] | 288 |
|
---|
| 289 | for (int ii = 1; ii <= numSat; ii++) {
|
---|
[792] | 290 | line = _clkSocket->readLine();
|
---|
[791] | 291 |
|
---|
[784] | 292 | QTextStream in(line);
|
---|
| 293 |
|
---|
| 294 | QString prn;
|
---|
| 295 | ColumnVector xx(4);
|
---|
| 296 |
|
---|
[795] | 297 | in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
|
---|
[797] | 298 | xx(4) *= 1e-6;
|
---|
[784] | 299 |
|
---|
[798] | 300 | processSatellite(GPSweek, GPSweeks, prn, xx);
|
---|
[780] | 301 | }
|
---|
[778] | 302 | }
|
---|
[784] | 303 |
|
---|
| 304 | //
|
---|
| 305 | ////////////////////////////////////////////////////////////////////////////
|
---|
[798] | 306 | void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
|
---|
[784] | 307 | const ColumnVector& xx) {
|
---|
| 308 |
|
---|
[795] | 309 | // No broadcast ephemeris available
|
---|
| 310 | // --------------------------------
|
---|
| 311 | if ( !_ephList.contains(prn) ) {
|
---|
| 312 | return;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | t_ephPair* pair = _ephList[prn];
|
---|
| 316 | gpsEph* ep = pair->eph;
|
---|
| 317 |
|
---|
[799] | 318 | ColumnVector xB(4);
|
---|
[802] | 319 | ColumnVector vv(3);
|
---|
[799] | 320 |
|
---|
[803] | 321 | satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
|
---|
[802] | 322 | vv(1), vv(2), vv(3));
|
---|
[799] | 323 |
|
---|
[804] | 324 | ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
|
---|
| 325 | double dClk = (xx(4) - xB(4)) * 299792458.0;
|
---|
| 326 | ColumnVector rsw(3);
|
---|
[800] | 327 |
|
---|
[806] | 328 | XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
|
---|
[804] | 329 |
|
---|
[811] | 330 | QString line;
|
---|
[817] | 331 | line.sprintf("%d %.1f %s %3d %3d %8.3f %8.3f %8.3f %8.3f\n",
|
---|
[811] | 332 | GPSweek, GPSweeks, ep->prn.toAscii().data(),
|
---|
| 333 | int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
|
---|
| 334 |
|
---|
[816] | 335 | if (_outStream) {
|
---|
| 336 | *_outStream << line;
|
---|
[818] | 337 | _outStream->flush();
|
---|
[811] | 338 | }
|
---|
| 339 | if (_outSocket) {
|
---|
| 340 | _outSocket->write(line.toAscii());
|
---|
[831] | 341 | _outSocket->flush();
|
---|
[811] | 342 | }
|
---|
[847] | 343 | if (_rnx) {
|
---|
| 344 | _rnx->write(GPSweek, GPSweeks, prn, xx);
|
---|
| 345 | }
|
---|
[848] | 346 | if (_sp3) {
|
---|
| 347 | _sp3->write(GPSweek, GPSweeks, prn, xx);
|
---|
| 348 | }
|
---|
[784] | 349 | }
|
---|
[836] | 350 |
|
---|
| 351 | //
|
---|
| 352 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 353 | void t_bns::slotMoveSocket(QThread* tt) {
|
---|
| 354 | _clkSocket->setParent(0);
|
---|
| 355 | _clkSocket->moveToThread(tt);
|
---|
| 356 | slotMessage("bns::slotMoveSocket");
|
---|
| 357 | }
|
---|