[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);
|
---|
| 30 |
|
---|
[787] | 31 | // Thread that handles broadcast ephemeris
|
---|
| 32 | // ---------------------------------------
|
---|
[758] | 33 | _bnseph = new t_bnseph(parent);
|
---|
[760] | 34 |
|
---|
[778] | 35 | connect(_bnseph, SIGNAL(newEph(gpsEph*)), this, SLOT(slotNewEph(gpsEph*)));
|
---|
[758] | 36 | connect(_bnseph, SIGNAL(newMessage(QByteArray)),
|
---|
| 37 | this, SLOT(slotMessage(const QByteArray)));
|
---|
[760] | 38 | connect(_bnseph, SIGNAL(error(QByteArray)),
|
---|
| 39 | this, SLOT(slotError(const QByteArray)));
|
---|
[769] | 40 |
|
---|
[787] | 41 | // Server listening for rtnet results
|
---|
| 42 | // ----------------------------------
|
---|
[786] | 43 | QSettings settings;
|
---|
[769] | 44 | _clkSocket = 0;
|
---|
[786] | 45 | _clkServer = new QTcpServer;
|
---|
[787] | 46 | _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
|
---|
[786] | 47 | connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
|
---|
[770] | 48 |
|
---|
[787] | 49 | // Socket for outputting the results
|
---|
| 50 | // ---------------------------------
|
---|
[770] | 51 | _outSocket = 0;
|
---|
[756] | 52 | }
|
---|
| 53 |
|
---|
| 54 | // Destructor
|
---|
| 55 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 56 | t_bns::~t_bns() {
|
---|
[763] | 57 | deleteBnsEph();
|
---|
[769] | 58 | delete _clkServer;
|
---|
[789] | 59 | /// delete _clkSocket;
|
---|
[770] | 60 | delete _outSocket;
|
---|
[779] | 61 | QMapIterator<QString, t_ephPair*> it(_ephList);
|
---|
| 62 | while (it.hasNext()) {
|
---|
| 63 | it.next();
|
---|
| 64 | delete it.value();
|
---|
| 65 | }
|
---|
[756] | 66 | }
|
---|
| 67 |
|
---|
[763] | 68 | // Delete bns thread
|
---|
| 69 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 70 | void t_bns::deleteBnsEph() {
|
---|
| 71 | if (_bnseph) {
|
---|
| 72 | _bnseph->terminate();
|
---|
[764] | 73 | _bnseph->wait(100);
|
---|
[763] | 74 | delete _bnseph;
|
---|
| 75 | _bnseph = 0;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[756] | 79 | // Write a Program Message
|
---|
| 80 | ////////////////////////////////////////////////////////////////////////////
|
---|
[758] | 81 | void t_bns::slotMessage(const QByteArray msg) {
|
---|
[756] | 82 | cout << msg.data() << endl;
|
---|
[757] | 83 | emit(newMessage(msg));
|
---|
[756] | 84 | }
|
---|
| 85 |
|
---|
[760] | 86 | // Write a Program Message
|
---|
| 87 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 88 | void t_bns::slotError(const QByteArray msg) {
|
---|
| 89 | cerr << msg.data() << endl;
|
---|
[763] | 90 | deleteBnsEph();
|
---|
[760] | 91 | emit(error(msg));
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[769] | 94 | // New Connection
|
---|
| 95 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 96 | void t_bns::slotNewConnection() {
|
---|
[786] | 97 | slotMessage("t_bns::slotNewConnection");
|
---|
[787] | 98 | delete _clkSocket;
|
---|
[769] | 99 | _clkSocket = _clkServer->nextPendingConnection();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[770] | 102 | // Start the Communication with NTRIP Caster
|
---|
| 103 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 104 | void t_bns::openCaster() {
|
---|
| 105 |
|
---|
| 106 | QSettings settings;
|
---|
| 107 | QString host = settings.value("outHost").toString();
|
---|
| 108 | int port = settings.value("outPort").toInt();
|
---|
| 109 |
|
---|
| 110 | _outSocket = new QTcpSocket();
|
---|
| 111 | _outSocket->connectToHost(host, port);
|
---|
| 112 |
|
---|
| 113 | QString mountpoint = settings.value("mountpoint").toString();
|
---|
| 114 | QString password = settings.value("password").toString();
|
---|
| 115 |
|
---|
| 116 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
| 117 | mountpoint.toAscii() + "\r\n" +
|
---|
| 118 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
| 119 |
|
---|
| 120 | _outSocket->write(msg);
|
---|
| 121 |
|
---|
| 122 | QByteArray ans = _outSocket->readLine();
|
---|
| 123 |
|
---|
| 124 | if (ans.indexOf("OK") == -1) {
|
---|
| 125 | delete _outSocket;
|
---|
| 126 | _outSocket = 0;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[784] | 130 | //
|
---|
| 131 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 132 | void t_bns::slotNewEph(gpsEph* ep) {
|
---|
| 133 |
|
---|
| 134 | QMutexLocker locker(&_mutex);
|
---|
| 135 |
|
---|
| 136 | t_ephPair* pair;
|
---|
| 137 | if ( !_ephList.contains(ep->prn) ) {
|
---|
| 138 | pair = new t_ephPair();
|
---|
| 139 | _ephList.insert(ep->prn, pair);
|
---|
| 140 | }
|
---|
| 141 | else {
|
---|
| 142 | pair = _ephList[ep->prn];
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (pair->eph == 0) {
|
---|
| 146 | pair->eph = ep;
|
---|
| 147 | }
|
---|
| 148 | else {
|
---|
| 149 | if (ep->GPSweek > pair->eph->GPSweek ||
|
---|
| 150 | (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
|
---|
| 151 | delete pair->oldEph;
|
---|
| 152 | pair->oldEph = pair->eph;
|
---|
| 153 | pair->eph = ep;
|
---|
| 154 | }
|
---|
| 155 | else {
|
---|
| 156 | delete ep;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[756] | 161 | // Start
|
---|
| 162 | ////////////////////////////////////////////////////////////////////////////
|
---|
[757] | 163 | void t_bns::run() {
|
---|
[769] | 164 |
|
---|
[758] | 165 | slotMessage("============ Start BNS ============");
|
---|
[770] | 166 |
|
---|
| 167 | // Start Thread that retrieves broadcast Ephemeris
|
---|
| 168 | // -----------------------------------------------
|
---|
[758] | 169 | _bnseph->start();
|
---|
[769] | 170 |
|
---|
[770] | 171 | // Open the connection to NTRIP Caster
|
---|
| 172 | // -----------------------------------
|
---|
| 173 | openCaster();
|
---|
| 174 |
|
---|
| 175 | // Endless loop
|
---|
| 176 | // ------------
|
---|
[769] | 177 | while (true) {
|
---|
[796] | 178 | if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
| 179 | if ( _clkSocket->canReadLine()) {
|
---|
| 180 | readEpoch();
|
---|
| 181 | }
|
---|
[769] | 182 | }
|
---|
| 183 | else {
|
---|
[794] | 184 | msleep(10);
|
---|
[769] | 185 | }
|
---|
| 186 | }
|
---|
[756] | 187 | }
|
---|
| 188 |
|
---|
[778] | 189 | //
|
---|
| 190 | ////////////////////////////////////////////////////////////////////////////
|
---|
[784] | 191 | void t_bns::readEpoch() {
|
---|
[778] | 192 |
|
---|
[784] | 193 | QByteArray line = _clkSocket->readLine();
|
---|
[786] | 194 |
|
---|
[784] | 195 | if (line.indexOf('*') == -1) {
|
---|
| 196 | return;
|
---|
[778] | 197 | }
|
---|
| 198 |
|
---|
[784] | 199 | QTextStream in(line);
|
---|
| 200 |
|
---|
| 201 | QString hlp;
|
---|
[798] | 202 | int GPSweek, numSat;
|
---|
| 203 | double GPSweeks;
|
---|
[784] | 204 |
|
---|
[798] | 205 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
---|
[784] | 206 |
|
---|
| 207 | for (int ii = 1; ii <= numSat; ii++) {
|
---|
[792] | 208 | line = _clkSocket->readLine();
|
---|
[791] | 209 |
|
---|
[784] | 210 | QTextStream in(line);
|
---|
| 211 |
|
---|
| 212 | QString prn;
|
---|
| 213 | ColumnVector xx(4);
|
---|
| 214 |
|
---|
[795] | 215 | in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
|
---|
[797] | 216 | xx(4) *= 1e-6;
|
---|
[784] | 217 |
|
---|
[798] | 218 | processSatellite(GPSweek, GPSweeks, prn, xx);
|
---|
[780] | 219 | }
|
---|
[778] | 220 | }
|
---|
[784] | 221 |
|
---|
| 222 | //
|
---|
| 223 | ////////////////////////////////////////////////////////////////////////////
|
---|
[798] | 224 | void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
|
---|
[784] | 225 | const ColumnVector& xx) {
|
---|
| 226 |
|
---|
[795] | 227 | // No broadcast ephemeris available
|
---|
| 228 | // --------------------------------
|
---|
| 229 | if ( !_ephList.contains(prn) ) {
|
---|
| 230 | return;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | t_ephPair* pair = _ephList[prn];
|
---|
| 234 | gpsEph* ep = pair->eph;
|
---|
| 235 |
|
---|
[799] | 236 | ColumnVector xB(4);
|
---|
| 237 |
|
---|
| 238 | satellitePosition(GPSweeks, ep, xB(1), xB(2), xB(3), xB(4));
|
---|
| 239 |
|
---|
[800] | 240 | ColumnVector dx = xx - xB;
|
---|
| 241 | dx(4) *= 299792458.0;
|
---|
| 242 |
|
---|
| 243 | cout << GPSweek << " " << GPSweeks << " " << ep->prn.toAscii().data() << " "
|
---|
| 244 | << setprecision(3) << dx.t();
|
---|
[784] | 245 | }
|
---|