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