| [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 | }
|
|---|
| [809] | 182 | else {
|
|---|
| 183 | _clkSocket->waitForReadyRead(10);
|
|---|
| 184 | }
|
|---|
| [769] | 185 | }
|
|---|
| 186 | else {
|
|---|
| [794] | 187 | msleep(10);
|
|---|
| [769] | 188 | }
|
|---|
| 189 | }
|
|---|
| [756] | 190 | }
|
|---|
| 191 |
|
|---|
| [778] | 192 | //
|
|---|
| 193 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [784] | 194 | void t_bns::readEpoch() {
|
|---|
| [778] | 195 |
|
|---|
| [784] | 196 | QByteArray line = _clkSocket->readLine();
|
|---|
| [786] | 197 |
|
|---|
| [784] | 198 | if (line.indexOf('*') == -1) {
|
|---|
| 199 | return;
|
|---|
| [778] | 200 | }
|
|---|
| 201 |
|
|---|
| [784] | 202 | QTextStream in(line);
|
|---|
| 203 |
|
|---|
| 204 | QString hlp;
|
|---|
| [798] | 205 | int GPSweek, numSat;
|
|---|
| 206 | double GPSweeks;
|
|---|
| [784] | 207 |
|
|---|
| [798] | 208 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
|---|
| [784] | 209 |
|
|---|
| 210 | for (int ii = 1; ii <= numSat; ii++) {
|
|---|
| [792] | 211 | line = _clkSocket->readLine();
|
|---|
| [791] | 212 |
|
|---|
| [784] | 213 | QTextStream in(line);
|
|---|
| 214 |
|
|---|
| 215 | QString prn;
|
|---|
| 216 | ColumnVector xx(4);
|
|---|
| 217 |
|
|---|
| [795] | 218 | in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
|
|---|
| [797] | 219 | xx(4) *= 1e-6;
|
|---|
| [784] | 220 |
|
|---|
| [798] | 221 | processSatellite(GPSweek, GPSweeks, prn, xx);
|
|---|
| [780] | 222 | }
|
|---|
| [778] | 223 | }
|
|---|
| [784] | 224 |
|
|---|
| 225 | //
|
|---|
| 226 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| [798] | 227 | void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
|
|---|
| [784] | 228 | const ColumnVector& xx) {
|
|---|
| 229 |
|
|---|
| [795] | 230 | // No broadcast ephemeris available
|
|---|
| 231 | // --------------------------------
|
|---|
| 232 | if ( !_ephList.contains(prn) ) {
|
|---|
| 233 | return;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | t_ephPair* pair = _ephList[prn];
|
|---|
| 237 | gpsEph* ep = pair->eph;
|
|---|
| 238 |
|
|---|
| [799] | 239 | ColumnVector xB(4);
|
|---|
| [802] | 240 | ColumnVector vv(3);
|
|---|
| [799] | 241 |
|
|---|
| [803] | 242 | satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
|
|---|
| [802] | 243 | vv(1), vv(2), vv(3));
|
|---|
| [799] | 244 |
|
|---|
| [804] | 245 | ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
|
|---|
| 246 | double dClk = (xx(4) - xB(4)) * 299792458.0;
|
|---|
| 247 | ColumnVector rsw(3);
|
|---|
| [800] | 248 |
|
|---|
| [806] | 249 | XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
|
|---|
| [804] | 250 |
|
|---|
| [801] | 251 | cout.setf(ios::showpoint | ios::fixed);
|
|---|
| 252 | cout << GPSweek << " "
|
|---|
| [809] | 253 | << setprecision(1) << GPSweeks << " " << ep->prn.toAscii().data()
|
|---|
| 254 | << " " << int(ep->IODC) << " " << int(ep->IODE) << " "
|
|---|
| [807] | 255 | << setw(8) << setprecision(3) << dClk << " "
|
|---|
| [804] | 256 | << setw(8) << setprecision(3) << rsw.t();
|
|---|
| [784] | 257 | }
|
|---|