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

Last change on this file since 1058 was 1058, checked in by mervart, 16 years ago

* empty log message *

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