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

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