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