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

Last change on this file since 1803 was 1803, checked in by weber, 15 years ago

* empty log message *

File size: 16.2 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#include "bnssettings.h"
26
27using namespace std;
28
29// Constructor
30////////////////////////////////////////////////////////////////////////////
31t_bns::t_bns(QObject* parent) : QThread(parent) {
32
33 this->setTerminationEnabled(true);
34
35 connect(this, SIGNAL(moveSocket(QThread*)),
36 this, SLOT(slotMoveSocket(QThread*)));
37
38 bnsSettings settings;
39
40 // Set Proxy (application-wide)
41 // ----------------------------
42 QString proxyHost = settings.value("proxyHost").toString();
43 int proxyPort = settings.value("proxyPort").toInt();
44
45 QNetworkProxy proxy;
46 if (proxyHost.isEmpty()) {
47 proxy.setType(QNetworkProxy::NoProxy);
48 }
49 else {
50 proxy.setType(QNetworkProxy::Socks5Proxy);
51 proxy.setHostName(proxyHost);
52 proxy.setPort(proxyPort);
53 }
54 QNetworkProxy::setApplicationProxy(proxy);
55
56 // Thread that handles broadcast ephemeris
57 // ---------------------------------------
58 _bnseph = new t_bnseph(parent);
59
60 connect(_bnseph, SIGNAL(newEph(t_eph*, int)),
61 this, SLOT(slotNewEph(t_eph*, int)));
62 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
63 this, SLOT(slotMessage(const QByteArray)));
64 connect(_bnseph, SIGNAL(error(QByteArray)),
65 this, SLOT(slotError(const QByteArray)));
66
67 // Server listening for rtnet results
68 // ----------------------------------
69 _clkSocket = 0;
70 _clkServer = new QTcpServer;
71 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
72 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
73
74 // Socket and file for outputting the results
75 // -------------------------------------------
76 for (int ic = 1; ic <= 3; ic++) {
77 QString mountpoint = settings.value(QString("mountpoint_%1").arg(ic)).toString();
78 QString outFileName = settings.value(QString("outFile_%1").arg(ic)).toString();
79 if (!mountpoint.isEmpty() || !outFileName.isEmpty()) {
80 _caster.push_back(new t_bnscaster(mountpoint, outFileName, ic));
81 connect(_caster.back(), SIGNAL(error(const QByteArray)),
82 this, SLOT(slotError(const QByteArray)));
83 connect(_caster.back(), SIGNAL(newMessage(const QByteArray)),
84 this, SLOT(slotMessage(const QByteArray)));
85 }
86 }
87
88 // Socket for outputting the Ephemerides
89 // -------------------------------------
90 QString mountpoint = settings.value("mountpoint_Eph").toString();
91 if (mountpoint.isEmpty()) {
92 _casterEph = 0;
93 }
94 else {
95 _casterEph = new t_bnscaster(mountpoint);
96 connect(_casterEph, SIGNAL(error(const QByteArray)),
97 this, SLOT(slotError(const QByteArray)));
98 connect(_casterEph, SIGNAL(newMessage(const QByteArray)),
99 this, SLOT(slotMessage(const QByteArray)));
100 }
101
102 // Log File
103 // --------
104 QIODevice::OpenMode oMode;
105 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
106 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
107 }
108 else {
109 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
110 }
111
112 QString logFileName = settings.value("logFile").toString();
113 if (logFileName.isEmpty()) {
114 _logFile = 0;
115 _logStream = 0;
116 }
117 else {
118 _logFile = new QFile(logFileName);
119 if (_logFile->open(oMode)) {
120 _logStream = new QTextStream(_logFile);
121 }
122 else {
123 _logStream = 0;
124 }
125 }
126
127 // Echo input from RTNet into a file
128 // ---------------------------------
129 QString echoFileName = settings.value("inpEcho").toString();
130 if (echoFileName.isEmpty()) {
131 _echoFile = 0;
132 _echoStream = 0;
133 }
134 else {
135 _echoFile = new QFile(echoFileName);
136 if (_echoFile->open(oMode)) {
137 _echoStream = new QTextStream(_echoFile);
138 }
139 else {
140 _echoStream = 0;
141 }
142 }
143
144 // RINEX writer
145 // ------------
146 if ( settings.value("rnxPath").toString().isEmpty() ) {
147 _rnx = 0;
148 }
149 else {
150 QString prep = "BNS";
151 QString ext = ".clk";
152 QString path = settings.value("rnxPath").toString();
153 QString intr = settings.value("rnxIntr").toString();
154 int sampl = settings.value("rnxSampl").toInt();
155 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
156 }
157
158 // SP3 writer
159 // ----------
160 if ( settings.value("sp3Path").toString().isEmpty() ) {
161 _sp3 = 0;
162 }
163 else {
164 QString prep = "BNS";
165 QString ext = ".sp3";
166 QString path = settings.value("sp3Path").toString();
167 QString intr = settings.value("sp3Intr").toString();
168 int sampl = settings.value("sp3Sampl").toInt();
169 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
170 }
171}
172
173// Destructor
174////////////////////////////////////////////////////////////////////////////
175t_bns::~t_bns() {
176 deleteBnsEph();
177 delete _clkServer;
178 delete _clkSocket;
179 for (int ic = 0; ic < _caster.size(); ic++) {
180 delete _caster.at(ic);
181 }
182 delete _casterEph;
183 delete _logStream;
184 delete _logFile;
185 delete _echoStream;
186 delete _echoFile;
187 QMapIterator<QString, t_ephPair*> it(_ephList);
188 while (it.hasNext()) {
189 it.next();
190 delete it.value();
191 }
192 delete _rnx;
193 delete _sp3;
194}
195
196// Delete bns thread
197////////////////////////////////////////////////////////////////////////////
198void t_bns::deleteBnsEph() {
199 if (_bnseph) {
200 _bnseph->terminate();
201 _bnseph->wait(100);
202 delete _bnseph;
203 _bnseph = 0;
204 }
205}
206
207// Write a Program Message
208////////////////////////////////////////////////////////////////////////////
209void t_bns::slotMessage(const QByteArray msg) {
210 if (_logStream) {
211 QString txt = QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ");
212 *_logStream << txt << msg << endl;
213 _logStream->flush();
214 }
215 emit(newMessage(msg));
216}
217
218// Write a Program Message
219////////////////////////////////////////////////////////////////////////////
220void t_bns::slotError(const QByteArray msg) {
221 if (_logStream) {
222 *_logStream << msg << endl;
223 _logStream->flush();
224 }
225 deleteBnsEph();
226 emit(error(msg));
227}
228
229// New Connection
230////////////////////////////////////////////////////////////////////////////
231void t_bns::slotNewConnection() {
232//slotMessage("t_bns::slotNewConnection");
233 slotMessage("Clocks & orbits port: Waiting for client to connect"); // weber
234 delete _clkSocket;
235 _clkSocket = _clkServer->nextPendingConnection();
236}
237
238//
239////////////////////////////////////////////////////////////////////////////
240void t_bns::slotNewEph(t_eph* ep, int nBytes) {
241
242 QMutexLocker locker(&_mutex);
243
244 emit(newEphBytes(nBytes));
245
246 // Output Ephemerides as they are
247 // ------------------------------
248 if (_casterEph) {
249 _casterEph->open();
250 // TODO encode ephemerides into RTCM v3 format
251 QByteArray buffer = "New Ephemeris " + ep->prn().toAscii() + "\n";
252 _casterEph->write(buffer.data(), buffer.length());
253 int len = buffer.length();
254 if (len > 0) {
255 emit(newOutEphBytes(len));
256 }
257 }
258
259 t_ephPair* pair;
260 if ( !_ephList.contains(ep->prn()) ) {
261 pair = new t_ephPair();
262 _ephList.insert(ep->prn(), pair);
263 }
264 else {
265 pair = _ephList[ep->prn()];
266 }
267
268 if (pair->eph == 0) {
269 pair->eph = ep;
270 }
271 else {
272 if (ep->isNewerThan(pair->eph)) {
273 delete pair->oldEph;
274 pair->oldEph = pair->eph;
275 pair->eph = ep;
276 }
277 else {
278 delete ep;
279 }
280 }
281}
282
283// Start
284////////////////////////////////////////////////////////////////////////////
285void t_bns::run() {
286
287 slotMessage("============ Start BNS ============");
288
289 // Start Thread that retrieves broadcast Ephemeris
290 // -----------------------------------------------
291 _bnseph->start();
292
293 // Endless loop
294 // ------------
295 while (true) {
296
297 if (_clkSocket && _clkSocket->thread() != currentThread()) {
298 emit(moveSocket(currentThread()));
299 }
300
301 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
302 if ( _clkSocket->canReadLine()) {
303 readEpoch();
304 }
305 else {
306 _clkSocket->waitForReadyRead(10);
307 }
308 }
309 else {
310 msleep(10);
311 }
312 }
313}
314
315//
316////////////////////////////////////////////////////////////////////////////
317void t_bns::readEpoch() {
318
319 bnsSettings settings;
320
321 // Read the first line (if not already read)
322 // -----------------------------------------
323 if (_clkLine.indexOf('*') == -1) {
324 _clkLine = _clkSocket->readLine();
325 if (_echoStream) {
326 *_echoStream << _clkLine;
327 _echoStream->flush();
328 }
329 emit(newClkBytes(_clkLine.length()));
330 }
331
332 if (_clkLine.indexOf('*') == -1) {
333 return;
334 }
335
336 QTextStream in(_clkLine);
337
338 QString hlp;
339 int year, month, day, hour, min;
340 double sec;
341 in >> hlp >> year >> month >> day >> hour >> min >> sec;
342
343 int GPSweek;
344 double GPSweeks;
345
346 GPSweekFromYMDhms(year, month, day, hour, min, sec, GPSweek, GPSweeks);
347
348 QStringList prns;
349
350 // Loop over all satellites
351 // ------------------------
352 QStringList lines;
353 for (;;) {
354 if (!_clkSocket->canReadLine()) {
355 break;
356 }
357 _clkLine = _clkSocket->readLine();
358 if (_echoStream) {
359 *_echoStream << _clkLine;
360 _echoStream->flush();
361 }
362 if (_clkLine[0] == '*') {
363 return;
364 }
365 if (_clkLine[0] == 'P') {
366 _clkLine.remove(0,1);
367 lines.push_back(_clkLine);
368 }
369 }
370
371 if (lines.size() > 0) {
372
373 QStringList prns;
374
375 for (int ic = 0; ic < _caster.size(); ic++) {
376 _caster.at(ic)->open();
377
378 for (int oldEph = 0; oldEph <= 0; oldEph++) { // TODO: handle old ephemeris
379
380 struct ClockOrbit co;
381 memset(&co, 0, sizeof(co));
382 co.GPSEpochTime = (int)GPSweeks;
383 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
384 co.ClockDataSupplied = 1;
385 co.OrbitDataSupplied = 1;
386 co.SatRefPoint = POINT_CENTER;
387 co.SatRefDatum = DATUM_ITRF;
388
389 for (int ii = 0; ii < lines.size(); ii++) {
390
391 QString prn;
392 ColumnVector xx(5); xx = 0.0;
393 t_eph* ep = 0;
394
395 if (oldEph == 0 && ic == 0) {
396 QTextStream in(lines[ii].toAscii());
397 in >> prn;
398 prns << prn;
399 if ( _ephList.contains(prn) ) {
400 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5);
401 xx(1) *= 1e3;
402 xx(2) *= 1e3;
403 xx(3) *= 1e3;
404 xx(4) *= 1e-6;
405 xx(5) *= 1e-6;
406
407 t_ephPair* pair = _ephList[prn];
408 pair->xx = xx;
409 ep = pair->eph;
410 }
411 }
412 else {
413 prn = prns[ii];
414 if ( _ephList.contains(prn) ) {
415 t_ephPair* pair = _ephList[prn];
416 prn = pair->eph->prn();
417 xx = pair->xx;
418 if (oldEph) {
419 ep = pair->oldEph;
420 }
421 else {
422 ep = pair->eph;
423 }
424 }
425 }
426
427 if (ep != 0) {
428 struct ClockOrbit::SatData* sd = 0;
429 if (prn[0] == 'G') {
430 sd = co.Sat + co.NumberOfGPSSat;
431 ++co.NumberOfGPSSat;
432 }
433 else if (prn[0] == 'R') {
434 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
435 ++co.NumberOfGLONASSSat;
436 }
437 if (sd) {
438 QString outLine;
439 processSatellite(oldEph, ic, _caster.at(ic)->crdTrafo(),
440 _caster.at(ic)->beClocks(), ep,
441 GPSweek, GPSweeks, prn, xx, sd, outLine);
442 _caster.at(ic)->printAscii(outLine);
443 }
444 }
445 }
446
447 if ( _caster.at(ic)->usedSocket() &&
448 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
449 char obuffer[CLOCKORBIT_BUFFERSIZE];
450 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
451 if (len > 0) {
452 if (_caster.at(ic)->ic() == 1) { emit(newOutBytes1(len));}
453 if (_caster.at(ic)->ic() == 2) { emit(newOutBytes2(len));}
454 if (_caster.at(ic)->ic() == 3) { emit(newOutBytes3(len));}
455 _caster.at(ic)->write(obuffer, len);
456 }
457 }
458 }
459 }
460 }
461}
462
463//
464////////////////////////////////////////////////////////////////////////////
465void t_bns::processSatellite(int oldEph, int iCaster, const QString trafo,
466 bool beClocks, t_eph* ep, int GPSweek,
467 double GPSweeks, const QString& prn,
468 const ColumnVector& xx,
469 struct ClockOrbit::SatData* sd,
470 QString& outLine) {
471
472 ColumnVector xB(4);
473 ColumnVector vv(3);
474
475 ep->position(GPSweek, GPSweeks, xB, vv);
476
477 ColumnVector xyz = xx.Rows(1,3);
478 if (trafo != "IGS05") {
479 crdTrafo(GPSweek, xyz, trafo);
480 }
481
482 ColumnVector dx = xyz - xB.Rows(1,3);
483
484 ColumnVector rsw(3);
485 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
486
487 double dClk;
488 if (beClocks) {
489 dClk = (xx(4) - xB(4) - xx(5)) * 299792458.0;
490 }
491 else {
492 dClk = (xx(4) - xB(4)) * 299792458.0;
493 }
494
495 if (sd) {
496 sd->ID = prn.mid(1).toInt();
497 sd->IOD = ep->IOD();
498 sd->Clock.DeltaA0 = dClk;
499 sd->Orbit.DeltaRadial = rsw(1);
500 sd->Orbit.DeltaAlongTrack = rsw(2);
501 sd->Orbit.DeltaCrossTrack = rsw(3);
502 }
503
504 char oldCh = (oldEph ? '!' : ' ');
505 outLine.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
506 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
507 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
508
509 if (!oldEph && iCaster == 0) {
510 if (_rnx) {
511 _rnx->write(GPSweek, GPSweeks, prn, xx);
512 }
513 if (_sp3) {
514 _sp3->write(GPSweek, GPSweeks, prn, xx);
515 }
516 }
517}
518
519//
520////////////////////////////////////////////////////////////////////////////
521void t_bns::slotMoveSocket(QThread* tt) {
522 _clkSocket->setParent(0);
523 _clkSocket->moveToThread(tt);
524//slotMessage("bns::slotMoveSocket");
525 slotMessage("Clocks & orbits port: Socket moved to thread"); // weber
526}
527
528// Transform Coordinates
529////////////////////////////////////////////////////////////////////////////
530void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz, const QString trafo) {
531
532 bnsSettings settings;
533
534 if (trafo == "ETRF2000") {
535 _dx = 0.0541;
536 _dy = 0.0502;
537 _dz = -0.0538;
538 _dxr = -0.0002;
539 _dyr = 0.0001;
540 _dzr = -0.0018;
541 _ox = 0.000891;
542 _oy = 0.005390;
543 _oz = -0.008712;
544 _oxr = 0.000081;
545 _oyr = 0.000490;
546 _ozr = -0.000792;
547 _sc = 0.40;
548 _scr = 0.08;
549 _t0 = 2000.0;
550 }
551 else if (trafo == "Custom") {
552 _dx = settings.value("trafo_dx").toDouble();
553 _dy = settings.value("trafo_dy").toDouble();
554 _dz = settings.value("trafo_dz").toDouble();
555 _dxr = settings.value("trafo_dxr").toDouble();
556 _dyr = settings.value("trafo_dyr").toDouble();
557 _dzr = settings.value("trafo_dzr").toDouble();
558 _ox = settings.value("trafo_ox").toDouble();
559 _oy = settings.value("trafo_oy").toDouble();
560 _oz = settings.value("trafo_oz").toDouble();
561 _oxr = settings.value("trafo_oxr").toDouble();
562 _oyr = settings.value("trafo_oyr").toDouble();
563 _ozr = settings.value("trafo_ozr").toDouble();
564 _sc = settings.value("trafo_sc").toDouble();
565 _scr = settings.value("trafo_scr").toDouble();
566 _t0 = settings.value("trafo_t0").toDouble();
567 }
568
569 // Current epoch minus 2000.0 in years
570 // ------------------------------------
571 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
572
573 ColumnVector dx(3);
574
575 dx(1) = _dx + dt * _dxr;
576 dx(2) = _dy + dt * _dyr;
577 dx(3) = _dz + dt * _dzr;
578
579 static const double arcSec = 180.0 * 3600.0 / M_PI;
580
581 double ox = (_ox + dt * _oxr) / arcSec;
582 double oy = (_oy + dt * _oyr) / arcSec;
583 double oz = (_oz + dt * _ozr) / arcSec;
584
585 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
586
587 Matrix rMat(3,3);
588 rMat(1,1) = 1.0;
589 rMat(1,2) = -oz;
590 rMat(1,3) = oy;
591 rMat(2,1) = oz;
592 rMat(2,2) = 1.0;
593 rMat(2,3) = -ox;
594 rMat(3,1) = -oy;
595 rMat(3,2) = ox;
596 rMat(3,3) = 1.0;
597
598 xyz = sc * rMat * xyz + dx;
599}
Note: See TracBrowser for help on using the repository browser.