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

Last change on this file since 1802 was 1802, checked in by mervart, 15 years ago

* empty log message *

File size: 16.1 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 }
254
255 t_ephPair* pair;
256 if ( !_ephList.contains(ep->prn()) ) {
257 pair = new t_ephPair();
258 _ephList.insert(ep->prn(), pair);
259 }
260 else {
261 pair = _ephList[ep->prn()];
262 }
263
264 if (pair->eph == 0) {
265 pair->eph = ep;
266 }
267 else {
268 if (ep->isNewerThan(pair->eph)) {
269 delete pair->oldEph;
270 pair->oldEph = pair->eph;
271 pair->eph = ep;
272 }
273 else {
274 delete ep;
275 }
276 }
277}
278
279// Start
280////////////////////////////////////////////////////////////////////////////
281void t_bns::run() {
282
283 slotMessage("============ Start BNS ============");
284
285 // Start Thread that retrieves broadcast Ephemeris
286 // -----------------------------------------------
287 _bnseph->start();
288
289 // Endless loop
290 // ------------
291 while (true) {
292
293 if (_clkSocket && _clkSocket->thread() != currentThread()) {
294 emit(moveSocket(currentThread()));
295 }
296
297 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
298 if ( _clkSocket->canReadLine()) {
299 readEpoch();
300 }
301 else {
302 _clkSocket->waitForReadyRead(10);
303 }
304 }
305 else {
306 msleep(10);
307 }
308 }
309}
310
311//
312////////////////////////////////////////////////////////////////////////////
313void t_bns::readEpoch() {
314
315 bnsSettings settings;
316
317 // Read the first line (if not already read)
318 // -----------------------------------------
319 if (_clkLine.indexOf('*') == -1) {
320 _clkLine = _clkSocket->readLine();
321 if (_echoStream) {
322 *_echoStream << _clkLine;
323 _echoStream->flush();
324 }
325 emit(newClkBytes(_clkLine.length()));
326 }
327
328 if (_clkLine.indexOf('*') == -1) {
329 return;
330 }
331
332 QTextStream in(_clkLine);
333
334 QString hlp;
335 int year, month, day, hour, min;
336 double sec;
337 in >> hlp >> year >> month >> day >> hour >> min >> sec;
338
339 int GPSweek;
340 double GPSweeks;
341
342 GPSweekFromYMDhms(year, month, day, hour, min, sec, GPSweek, GPSweeks);
343
344 QStringList prns;
345
346 // Loop over all satellites
347 // ------------------------
348 QStringList lines;
349 for (;;) {
350 if (!_clkSocket->canReadLine()) {
351 break;
352 }
353 _clkLine = _clkSocket->readLine();
354 if (_echoStream) {
355 *_echoStream << _clkLine;
356 _echoStream->flush();
357 }
358 if (_clkLine[0] == '*') {
359 return;
360 }
361 if (_clkLine[0] == 'P') {
362 _clkLine.remove(0,1);
363 lines.push_back(_clkLine);
364 }
365 }
366
367 if (lines.size() > 0) {
368
369 QStringList prns;
370
371 for (int ic = 0; ic < _caster.size(); ic++) {
372 _caster.at(ic)->open();
373
374 for (int oldEph = 0; oldEph <= 0; oldEph++) { // TODO: handle old ephemeris
375
376 struct ClockOrbit co;
377 memset(&co, 0, sizeof(co));
378 co.GPSEpochTime = (int)GPSweeks;
379 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
380 co.ClockDataSupplied = 1;
381 co.OrbitDataSupplied = 1;
382 co.SatRefPoint = POINT_CENTER;
383 co.SatRefDatum = DATUM_ITRF;
384
385 for (int ii = 0; ii < lines.size(); ii++) {
386
387 QString prn;
388 ColumnVector xx(5); xx = 0.0;
389 t_eph* ep = 0;
390
391 if (oldEph == 0 && ic == 0) {
392 QTextStream in(lines[ii].toAscii());
393 in >> prn;
394 prns << prn;
395 if ( _ephList.contains(prn) ) {
396 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5);
397 xx(1) *= 1e3;
398 xx(2) *= 1e3;
399 xx(3) *= 1e3;
400 xx(4) *= 1e-6;
401 xx(5) *= 1e-6;
402
403 t_ephPair* pair = _ephList[prn];
404 pair->xx = xx;
405 ep = pair->eph;
406 }
407 }
408 else {
409 prn = prns[ii];
410 if ( _ephList.contains(prn) ) {
411 t_ephPair* pair = _ephList[prn];
412 prn = pair->eph->prn();
413 xx = pair->xx;
414 if (oldEph) {
415 ep = pair->oldEph;
416 }
417 else {
418 ep = pair->eph;
419 }
420 }
421 }
422
423 if (ep != 0) {
424 struct ClockOrbit::SatData* sd = 0;
425 if (prn[0] == 'G') {
426 sd = co.Sat + co.NumberOfGPSSat;
427 ++co.NumberOfGPSSat;
428 }
429 else if (prn[0] == 'R') {
430 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
431 ++co.NumberOfGLONASSSat;
432 }
433 if (sd) {
434 QString outLine;
435 processSatellite(oldEph, ic, _caster.at(ic)->crdTrafo(),
436 _caster.at(ic)->beClocks(), ep,
437 GPSweek, GPSweeks, prn, xx, sd, outLine);
438 _caster.at(ic)->printAscii(outLine);
439 }
440 }
441 }
442
443 if ( _caster.at(ic)->usedSocket() &&
444 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
445 char obuffer[CLOCKORBIT_BUFFERSIZE];
446 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
447 if (len > 0) {
448 if (_caster.at(ic)->ic() == 1) { emit(newOutBytes1(len));}
449 if (_caster.at(ic)->ic() == 2) { emit(newOutBytes2(len));}
450 if (_caster.at(ic)->ic() == 3) { emit(newOutBytes3(len));}
451 _caster.at(ic)->write(obuffer, len);
452 }
453 }
454 }
455 }
456 }
457}
458
459//
460////////////////////////////////////////////////////////////////////////////
461void t_bns::processSatellite(int oldEph, int iCaster, const QString trafo,
462 bool beClocks, t_eph* ep, int GPSweek,
463 double GPSweeks, const QString& prn,
464 const ColumnVector& xx,
465 struct ClockOrbit::SatData* sd,
466 QString& outLine) {
467
468 ColumnVector xB(4);
469 ColumnVector vv(3);
470
471 ep->position(GPSweek, GPSweeks, xB, vv);
472
473 ColumnVector xyz = xx.Rows(1,3);
474 if (trafo != "IGS05") {
475 crdTrafo(GPSweek, xyz, trafo);
476 }
477
478 ColumnVector dx = xyz - xB.Rows(1,3);
479
480 ColumnVector rsw(3);
481 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
482
483 double dClk;
484 if (beClocks) {
485 dClk = (xx(4) - xB(4) - xx(5)) * 299792458.0;
486 }
487 else {
488 dClk = (xx(4) - xB(4)) * 299792458.0;
489 }
490
491 if (sd) {
492 sd->ID = prn.mid(1).toInt();
493 sd->IOD = ep->IOD();
494 sd->Clock.DeltaA0 = dClk;
495 sd->Orbit.DeltaRadial = rsw(1);
496 sd->Orbit.DeltaAlongTrack = rsw(2);
497 sd->Orbit.DeltaCrossTrack = rsw(3);
498 }
499
500 char oldCh = (oldEph ? '!' : ' ');
501 outLine.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
502 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
503 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
504
505 if (!oldEph && iCaster == 0) {
506 if (_rnx) {
507 _rnx->write(GPSweek, GPSweeks, prn, xx);
508 }
509 if (_sp3) {
510 _sp3->write(GPSweek, GPSweeks, prn, xx);
511 }
512 }
513}
514
515//
516////////////////////////////////////////////////////////////////////////////
517void t_bns::slotMoveSocket(QThread* tt) {
518 _clkSocket->setParent(0);
519 _clkSocket->moveToThread(tt);
520//slotMessage("bns::slotMoveSocket");
521 slotMessage("Clocks & orbits port: Socket moved to thread"); // weber
522}
523
524// Transform Coordinates
525////////////////////////////////////////////////////////////////////////////
526void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz, const QString trafo) {
527
528 bnsSettings settings;
529
530 if (trafo == "ETRF2000") {
531 _dx = 0.0541;
532 _dy = 0.0502;
533 _dz = -0.0538;
534 _dxr = -0.0002;
535 _dyr = 0.0001;
536 _dzr = -0.0018;
537 _ox = 0.000891;
538 _oy = 0.005390;
539 _oz = -0.008712;
540 _oxr = 0.000081;
541 _oyr = 0.000490;
542 _ozr = -0.000792;
543 _sc = 0.40;
544 _scr = 0.08;
545 _t0 = 2000.0;
546 }
547 else if (trafo == "Custom") {
548 _dx = settings.value("trafo_dx").toDouble();
549 _dy = settings.value("trafo_dy").toDouble();
550 _dz = settings.value("trafo_dz").toDouble();
551 _dxr = settings.value("trafo_dxr").toDouble();
552 _dyr = settings.value("trafo_dyr").toDouble();
553 _dzr = settings.value("trafo_dzr").toDouble();
554 _ox = settings.value("trafo_ox").toDouble();
555 _oy = settings.value("trafo_oy").toDouble();
556 _oz = settings.value("trafo_oz").toDouble();
557 _oxr = settings.value("trafo_oxr").toDouble();
558 _oyr = settings.value("trafo_oyr").toDouble();
559 _ozr = settings.value("trafo_ozr").toDouble();
560 _sc = settings.value("trafo_sc").toDouble();
561 _scr = settings.value("trafo_scr").toDouble();
562 _t0 = settings.value("trafo_t0").toDouble();
563 }
564
565 // Current epoch minus 2000.0 in years
566 // ------------------------------------
567 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
568
569 ColumnVector dx(3);
570
571 dx(1) = _dx + dt * _dxr;
572 dx(2) = _dy + dt * _dyr;
573 dx(3) = _dz + dt * _dzr;
574
575 static const double arcSec = 180.0 * 3600.0 / M_PI;
576
577 double ox = (_ox + dt * _oxr) / arcSec;
578 double oy = (_oy + dt * _oyr) / arcSec;
579 double oz = (_oz + dt * _ozr) / arcSec;
580
581 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
582
583 Matrix rMat(3,3);
584 rMat(1,1) = 1.0;
585 rMat(1,2) = -oz;
586 rMat(1,3) = oy;
587 rMat(2,1) = oz;
588 rMat(2,2) = 1.0;
589 rMat(2,3) = -ox;
590 rMat(3,1) = -oy;
591 rMat(3,2) = ox;
592 rMat(3,3) = 1.0;
593
594 xyz = sc * rMat * xyz + dx;
595}
Note: See TracBrowser for help on using the repository browser.