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