source: ntrip/trunk/BNS/bns.cpp@ 1054

Last change on this file since 1054 was 1017, checked in by weber, 16 years ago

* empty log message *

File size: 13.2 KB
RevLine 
[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
[858]17#include <math.h>
[756]18#include <iostream>
[800]19#include <newmatio.h>
[756]20
21#include "bns.h"
[799]22#include "bnsutils.h"
[847]23#include "bnsrinex.h"
[848]24#include "bnssp3.h"
[756]25
26using namespace std;
27
28// Constructor
29////////////////////////////////////////////////////////////////////////////
[757]30t_bns::t_bns(QObject* parent) : QThread(parent) {
[760]31
[764]32 this->setTerminationEnabled(true);
[828]33
[836]34 connect(this, SIGNAL(moveSocket(QThread*)),
35 this, SLOT(slotMoveSocket(QThread*)));
36
[979]37 QSettings settings;
38
39 // Set Proxy (application-wide)
40 // ----------------------------
41 QString proxyHost = settings.value("proxyHost").toString();
42 int proxyPort = settings.value("proxyPort").toInt();
[980]43
44 QNetworkProxy proxy;
45 if (proxyHost.isEmpty()) {
46 proxy.setType(QNetworkProxy::NoProxy);
47 }
48 else {
[979]49 proxy.setType(QNetworkProxy::Socks5Proxy);
50 proxy.setHostName(proxyHost);
51 proxy.setPort(proxyPort);
52 }
[980]53 QNetworkProxy::setApplicationProxy(proxy);
[979]54
[828]55 // Thread that handles broadcast ephemeris
56 // ---------------------------------------
57 _bnseph = new t_bnseph(parent);
[827]58
[884]59 connect(_bnseph, SIGNAL(newEph(t_eph*)), this, SLOT(slotNewEph(t_eph*)));
[828]60 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
61 this, SLOT(slotMessage(const QByteArray)));
62 connect(_bnseph, SIGNAL(error(QByteArray)),
63 this, SLOT(slotError(const QByteArray)));
[760]64
[827]65 // Server listening for rtnet results
66 // ----------------------------------
[828]67 _clkSocket = 0;
[827]68 _clkServer = new QTcpServer;
69 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
[828]70 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
[827]71
[828]72 // Socket and file for outputting the results
73 // -------------------------------------------
[858]74 _outSocket = 0;
75 _outSocketOpenTrial = 0;
[827]76
[983]77 QIODevice::OpenMode oMode;
78 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
79 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
80 }
81 else {
82 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
83 }
84
[816]85 QString outFileName = settings.value("outFile").toString();
86 if (outFileName.isEmpty()) {
[833]87 _outFile = 0;
88 _outStream = 0;
[811]89 }
[816]90 else {
91 _outFile = new QFile(outFileName);
[983]92 if (_outFile->open(oMode)) {
[816]93 _outStream = new QTextStream(_outFile);
94 }
95 }
[812]96
97 // Log File
98 // --------
[816]99 QString logFileName = settings.value("logFile").toString();
100 if (logFileName.isEmpty()) {
[947]101 _logFile = 0;
102 _logStream = 0;
[812]103 }
[816]104 else {
105 _logFile = new QFile(logFileName);
[983]106 if (_logFile->open(oMode)) {
[816]107 _logStream = new QTextStream(_logFile);
108 }
[948]109 else {
110 _logStream = 0;
111 }
[816]112 }
[847]113
114 // RINEX writer
115 // ------------
116 if ( settings.value("rnxPath").toString().isEmpty() ) {
117 _rnx = 0;
118 }
119 else {
[850]120 QString prep = "BNS";
[857]121 QString ext = ".clk";
[850]122 QString path = settings.value("rnxPath").toString();
123 QString intr = settings.value("rnxIntr").toString();
124 int sampl = settings.value("rnxSampl").toInt();
125 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
[847]126 }
[848]127
128 // SP3 writer
129 // ----------
130 if ( settings.value("sp3Path").toString().isEmpty() ) {
131 _sp3 = 0;
132 }
133 else {
[850]134 QString prep = "BNS";
[857]135 QString ext = ".sp3";
[850]136 QString path = settings.value("sp3Path").toString();
137 QString intr = settings.value("sp3Intr").toString();
138 int sampl = settings.value("sp3Sampl").toInt();
139 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
[848]140 }
[984]141
142 // Reference frame
143 // ---------------
144 _crdTrafo = false;
145 if (settings.value("refSys").toString() == "ETRS89") {
146 _crdTrafo = true;
147 }
[756]148}
149
150// Destructor
151////////////////////////////////////////////////////////////////////////////
[757]152t_bns::~t_bns() {
[763]153 deleteBnsEph();
[769]154 delete _clkServer;
[837]155 delete _clkSocket;
[770]156 delete _outSocket;
[816]157 delete _outStream;
158 delete _logStream;
[812]159 delete _outFile;
160 delete _logFile;
[779]161 QMapIterator<QString, t_ephPair*> it(_ephList);
162 while (it.hasNext()) {
163 it.next();
164 delete it.value();
165 }
[849]166 delete _rnx;
167 delete _sp3;
[756]168}
169
[763]170// Delete bns thread
171////////////////////////////////////////////////////////////////////////////
172void t_bns::deleteBnsEph() {
173 if (_bnseph) {
174 _bnseph->terminate();
[764]175 _bnseph->wait(100);
[763]176 delete _bnseph;
177 _bnseph = 0;
178 }
179}
180
[756]181// Write a Program Message
182////////////////////////////////////////////////////////////////////////////
[758]183void t_bns::slotMessage(const QByteArray msg) {
[816]184 if (_logStream) {
185 *_logStream << msg << endl;
[818]186 _logStream->flush();
[812]187 }
[757]188 emit(newMessage(msg));
[756]189}
190
[760]191// Write a Program Message
192////////////////////////////////////////////////////////////////////////////
193void t_bns::slotError(const QByteArray msg) {
[816]194 if (_logStream) {
195 *_logStream << msg << endl;
[818]196 _logStream->flush();
[812]197 }
[763]198 deleteBnsEph();
[760]199 emit(error(msg));
200}
201
[769]202// New Connection
203////////////////////////////////////////////////////////////////////////////
204void t_bns::slotNewConnection() {
[786]205 slotMessage("t_bns::slotNewConnection");
[787]206 delete _clkSocket;
[769]207 _clkSocket = _clkServer->nextPendingConnection();
208}
209
[770]210// Start the Communication with NTRIP Caster
211////////////////////////////////////////////////////////////////////////////
212void t_bns::openCaster() {
[858]213
214 delete _outSocket; _outSocket = 0;
215
216 double minDt = exp2(_outSocketOpenTrial);
217 if (++_outSocketOpenTrial > 8) {
218 _outSocketOpenTrial = 8;
219 }
220 if (_outSocketOpenTime.isValid() &&
221 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
222 return;
223 }
224 else {
225 _outSocketOpenTime = QDateTime::currentDateTime();
226 }
227
[770]228 QSettings settings;
229 _outSocket = new QTcpSocket();
[811]230 _outSocket->connectToHost(settings.value("outHost").toString(),
231 settings.value("outPort").toInt());
[770]232
[819]233 const int timeOut = 100; // 0.1 seconds
234 if (!_outSocket->waitForConnected(timeOut)) {
235 delete _outSocket;
236 _outSocket = 0;
237 emit(error("bns::openCaster Connect Timeout"));
[840]238 return;
[819]239 }
240
[770]241 QString mountpoint = settings.value("mountpoint").toString();
242 QString password = settings.value("password").toString();
243
244 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
245 mountpoint.toAscii() + "\r\n" +
246 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
247
248 _outSocket->write(msg);
[820]249 _outSocket->waitForBytesWritten();
[770]250
[820]251 _outSocket->waitForReadyRead();
[770]252 QByteArray ans = _outSocket->readLine();
253
254 if (ans.indexOf("OK") == -1) {
255 delete _outSocket;
256 _outSocket = 0;
[831]257 slotMessage("bns::openCaster socket deleted");
[770]258 }
[831]259 else {
260 slotMessage("bns::openCaster socket OK");
[858]261 _outSocketOpenTrial = 0;
[831]262 }
[770]263}
264
[784]265//
266////////////////////////////////////////////////////////////////////////////
[884]267void t_bns::slotNewEph(t_eph* ep) {
[784]268
269 QMutexLocker locker(&_mutex);
270
271 t_ephPair* pair;
[884]272 if ( !_ephList.contains(ep->prn()) ) {
[784]273 pair = new t_ephPair();
[884]274 _ephList.insert(ep->prn(), pair);
[784]275 }
276 else {
[884]277 pair = _ephList[ep->prn()];
[784]278 }
279
280 if (pair->eph == 0) {
281 pair->eph = ep;
282 }
283 else {
[884]284 if (ep->isNewerThan(pair->eph)) {
[784]285 delete pair->oldEph;
286 pair->oldEph = pair->eph;
287 pair->eph = ep;
288 }
289 else {
290 delete ep;
291 }
292 }
293}
294
[756]295// Start
296////////////////////////////////////////////////////////////////////////////
[757]297void t_bns::run() {
[769]298
[758]299 slotMessage("============ Start BNS ============");
[770]300
[828]301 // Start Thread that retrieves broadcast Ephemeris
302 // -----------------------------------------------
[758]303 _bnseph->start();
[769]304
[770]305 // Endless loop
306 // ------------
[769]307 while (true) {
[836]308
309 if (_clkSocket && _clkSocket->thread() != currentThread()) {
310 emit(moveSocket(currentThread()));
311 }
312
[796]313 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
314 if ( _clkSocket->canReadLine()) {
[832]315 if (_outSocket == 0 ||
316 _outSocket->state() != QAbstractSocket::ConnectedState) {
[830]317 openCaster();
318 }
[796]319 readEpoch();
320 }
[809]321 else {
322 _clkSocket->waitForReadyRead(10);
323 }
[769]324 }
325 else {
[794]326 msleep(10);
[769]327 }
328 }
[756]329}
330
[778]331//
332////////////////////////////////////////////////////////////////////////////
[784]333void t_bns::readEpoch() {
[778]334
[784]335 QByteArray line = _clkSocket->readLine();
[786]336
[784]337 if (line.indexOf('*') == -1) {
338 return;
[778]339 }
340
[784]341 QTextStream in(line);
342
343 QString hlp;
[798]344 int GPSweek, numSat;
345 double GPSweeks;
[784]346
[798]347 in >> hlp >> GPSweek >> GPSweeks >> numSat;
[784]348
[874]349 if (numSat > 0) {
350
[922]351 QStringList prns;
352
[924]353 /// for (int oldEph = 0; oldEph <= 1; oldEph++) {
354 for (int oldEph = 0; oldEph <= 0; oldEph++) {
[905]355
356 struct ClockOrbit co;
357 memset(&co, 0, sizeof(co));
[907]358 co.GPSEpochTime = (int)GPSweeks;
[909]359 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
[905]360 co.ClockDataSupplied = 1;
361 co.OrbitDataSupplied = 1;
362 co.SatRefPoint = POINT_CENTER;
363 co.SatRefDatum = DATUM_ITRF;
364
365 for (int ii = 1; ii <= numSat; ii++) {
366
367 QString prn;
[927]368 ColumnVector xx(5);
[905]369 t_eph* ep = 0;
370
371 if (oldEph == 0) {
372 line = _clkSocket->readLine();
373 QTextStream in(line);
374 in >> prn;
[922]375 prns << prn;
[905]376 if ( _ephList.contains(prn) ) {
[927]377 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5); xx(4) *= 1e-6;
378
379 //// beg test (zero clock correction for Gerhard Wuebbena)
[946]380 //// xx(4) -= xx(5) / 299792458.0;
[927]381 //// end test
382
[905]383 t_ephPair* pair = _ephList[prn];
384 pair->xx = xx;
385 ep = pair->eph;
[872]386 }
[905]387 }
388 else {
[922]389 prn = prns[ii-1];
[905]390 if ( _ephList.contains(prn) ) {
391 t_ephPair* pair = _ephList[prn];
392 prn = pair->eph->prn();
393 xx = pair->xx;
394 ep = pair->oldEph;
[873]395 }
396 }
[905]397
398 if (ep != 0) {
399 struct ClockOrbit::SatData* sd = 0;
400 if (prn[0] == 'G') {
401 sd = co.Sat + co.NumberOfGPSSat;
402 ++co.NumberOfGPSSat;
[872]403 }
[905]404 else if (prn[0] == 'R') {
405 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
406 ++co.NumberOfGLONASSSat;
407 }
408 if (sd) {
[923]409 processSatellite(oldEph, ep, GPSweek, GPSweeks, prn, xx, sd);
[905]410 }
[872]411 }
[863]412 }
[905]413
[981]414 if ( (_outSocket || _outFile) &&
[905]415 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
416 char obuffer[CLOCKORBIT_BUFFERSIZE];
417 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
418 if (len > 0) {
[981]419 if (_outSocket) {
420 _outSocket->write(obuffer, len);
421 _outSocket->flush();
422 }
423 if (_outFile) {
424 _outFile->write(obuffer, len);
425 _outFile->flush();
426 }
[905]427 }
428 }
[863]429 }
[780]430 }
[778]431}
[784]432
433//
434////////////////////////////////////////////////////////////////////////////
[923]435void t_bns::processSatellite(int oldEph, t_eph* ep, int GPSweek, double GPSweeks,
[872]436 const QString& prn, const ColumnVector& xx,
[863]437 struct ClockOrbit::SatData* sd) {
[784]438
[799]439 ColumnVector xB(4);
[802]440 ColumnVector vv(3);
[799]441
[884]442 ep->position(GPSweek, GPSweeks, xB, vv);
[799]443
[984]444 ColumnVector xyz = xx.Rows(1,3);
445 if (_crdTrafo) {
[985]446 crdTrafo(GPSweek, xyz);
[984]447 }
[927]448
[984]449 ColumnVector dx = xyz - xB.Rows(1,3);
450
[804]451 double dClk = (xx(4) - xB(4)) * 299792458.0;
452 ColumnVector rsw(3);
[800]453
[806]454 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
[804]455
[863]456 if (sd) {
457 sd->ID = prn.mid(1).toInt();
[884]458 sd->IOD = ep->IOD();
[862]459 sd->Clock.DeltaA0 = dClk;
460 sd->Orbit.DeltaRadial = rsw(1);
461 sd->Orbit.DeltaAlongTrack = rsw(2);
462 sd->Orbit.DeltaCrossTrack = rsw(3);
[863]463 }
[862]464
[863]465 if (_outStream) {
466 QString line;
[923]467 char oldCh = (oldEph ? '!' : ' ');
468 line.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
469 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
[884]470 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
471 *_outStream << line;
[863]472 _outStream->flush();
[811]473 }
[923]474 if (!oldEph) {
475 if (_rnx) {
476 _rnx->write(GPSweek, GPSweeks, prn, xx);
477 }
478 if (_sp3) {
479 _sp3->write(GPSweek, GPSweeks, prn, xx);
480 }
[847]481 }
[784]482}
[836]483
484//
485////////////////////////////////////////////////////////////////////////////
486void t_bns::slotMoveSocket(QThread* tt) {
487 _clkSocket->setParent(0);
488 _clkSocket->moveToThread(tt);
489 slotMessage("bns::slotMoveSocket");
490}
[984]491
492// Transform Coordinates IGS -> ETRF
493////////////////////////////////////////////////////////////////////////////
[985]494void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
[984]495
[985]496 ColumnVector dx(3);
[1017]497 dx(1) = 0.054;
498 dx(2) = 0.051;
499 dx(3) = -0.048;
[985]500 const static double arcSec = 180.0 * 3600.0 / M_PI;
[1017]501 const static double ox = 0.000081 / arcSec;
502 const static double oy = 0.000490 / arcSec;
503 const static double oz = -0.000792 / arcSec;
[984]504
[985]505 Matrix rMat(3,3); rMat = 0.0;
506 rMat(1,2) = -oz;
507 rMat(1,3) = oy;
508 rMat(2,1) = oz;
509 rMat(2,3) = -ox;
510 rMat(3,1) = -oy;
511 rMat(3,2) = ox;
512
513 // Current epoch minus 1989.0 in years
514 // ------------------------------------
515 double dt = (GPSWeek - 469.0) / 365.2422 * 7.0;
516
517 xyz += dx + dt * rMat * xyz;
[984]518}
Note: See TracBrowser for help on using the repository browser.