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

Last change on this file since 1747 was 1740, checked in by weber, 16 years ago

* empty log message *

File size: 14.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 // Log File
89 // --------
90 QIODevice::OpenMode oMode;
91 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
92 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
93 }
94 else {
95 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
96 }
97
98 QString logFileName = settings.value("logFile").toString();
99 if (logFileName.isEmpty()) {
100 _logFile = 0;
101 _logStream = 0;
102 }
103 else {
104 _logFile = new QFile(logFileName);
105 if (_logFile->open(oMode)) {
106 _logStream = new QTextStream(_logFile);
107 }
108 else {
109 _logStream = 0;
110 }
111 }
112
113 // Echo input from RTNet into a file
114 // ---------------------------------
115 QString echoFileName = settings.value("inpEcho").toString();
116 if (echoFileName.isEmpty()) {
117 _echoFile = 0;
118 _echoStream = 0;
119 }
120 else {
121 _echoFile = new QFile(echoFileName);
122 if (_echoFile->open(oMode)) {
123 _echoStream = new QTextStream(_echoFile);
124 }
125 else {
126 _echoStream = 0;
127 }
128 }
129
130 // RINEX writer
131 // ------------
132 if ( settings.value("rnxPath").toString().isEmpty() ) {
133 _rnx = 0;
134 }
135 else {
136 QString prep = "BNS";
137 QString ext = ".clk";
138 QString path = settings.value("rnxPath").toString();
139 QString intr = settings.value("rnxIntr").toString();
140 int sampl = settings.value("rnxSampl").toInt();
141 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
142 }
143
144 // SP3 writer
145 // ----------
146 if ( settings.value("sp3Path").toString().isEmpty() ) {
147 _sp3 = 0;
148 }
149 else {
150 QString prep = "BNS";
151 QString ext = ".sp3";
152 QString path = settings.value("sp3Path").toString();
153 QString intr = settings.value("sp3Intr").toString();
154 int sampl = settings.value("sp3Sampl").toInt();
155 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
156 }
157}
158
159// Destructor
160////////////////////////////////////////////////////////////////////////////
161t_bns::~t_bns() {
162 deleteBnsEph();
163 delete _clkServer;
164 delete _clkSocket;
165 for (int ic = 0; ic < _caster.size(); ic++) {
166 delete _caster.at(ic);
167 }
168 delete _logStream;
169 delete _logFile;
170 delete _echoStream;
171 delete _echoFile;
172 QMapIterator<QString, t_ephPair*> it(_ephList);
173 while (it.hasNext()) {
174 it.next();
175 delete it.value();
176 }
177 delete _rnx;
178 delete _sp3;
179}
180
181// Delete bns thread
182////////////////////////////////////////////////////////////////////////////
183void t_bns::deleteBnsEph() {
184 if (_bnseph) {
185 _bnseph->terminate();
186 _bnseph->wait(100);
187 delete _bnseph;
188 _bnseph = 0;
189 }
190}
191
192// Write a Program Message
193////////////////////////////////////////////////////////////////////////////
194void t_bns::slotMessage(const QByteArray msg) {
195 if (_logStream) {
196 QString txt = QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ");
197 *_logStream << txt << msg << endl;
198 _logStream->flush();
199 }
200 emit(newMessage(msg));
201}
202
203// Write a Program Message
204////////////////////////////////////////////////////////////////////////////
205void t_bns::slotError(const QByteArray msg) {
206 if (_logStream) {
207 *_logStream << msg << endl;
208 _logStream->flush();
209 }
210 deleteBnsEph();
211 emit(error(msg));
212}
213
214// New Connection
215////////////////////////////////////////////////////////////////////////////
216void t_bns::slotNewConnection() {
217//slotMessage("t_bns::slotNewConnection");
218 slotMessage("Clocks & orbits port: Waiting for client to connect"); // weber
219 delete _clkSocket;
220 _clkSocket = _clkServer->nextPendingConnection();
221}
222
223//
224////////////////////////////////////////////////////////////////////////////
225void t_bns::slotNewEph(t_eph* ep, int nBytes) {
226
227 QMutexLocker locker(&_mutex);
228
229 emit(newEphBytes(nBytes));
230
231 t_ephPair* pair;
232 if ( !_ephList.contains(ep->prn()) ) {
233 pair = new t_ephPair();
234 _ephList.insert(ep->prn(), pair);
235 }
236 else {
237 pair = _ephList[ep->prn()];
238 }
239
240 if (pair->eph == 0) {
241 pair->eph = ep;
242 }
243 else {
244 if (ep->isNewerThan(pair->eph)) {
245 delete pair->oldEph;
246 pair->oldEph = pair->eph;
247 pair->eph = ep;
248 }
249 else {
250 delete ep;
251 }
252 }
253}
254
255// Start
256////////////////////////////////////////////////////////////////////////////
257void t_bns::run() {
258
259 slotMessage("============ Start BNS ============");
260
261 // Start Thread that retrieves broadcast Ephemeris
262 // -----------------------------------------------
263 _bnseph->start();
264
265 // Endless loop
266 // ------------
267 while (true) {
268
269 if (_clkSocket && _clkSocket->thread() != currentThread()) {
270 emit(moveSocket(currentThread()));
271 }
272
273 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
274 if ( _clkSocket->canReadLine()) {
275 readEpoch();
276 }
277 else {
278 _clkSocket->waitForReadyRead(10);
279 }
280 }
281 else {
282 msleep(10);
283 }
284 }
285}
286
287//
288////////////////////////////////////////////////////////////////////////////
289void t_bns::readEpoch() {
290
291 bnsSettings settings;
292
293 // Read the first line (if not already read)
294 // -----------------------------------------
295 if (_clkLine.indexOf('*') == -1) {
296 _clkLine = _clkSocket->readLine();
297 if (_echoStream) {
298 *_echoStream << _clkLine;
299 _echoStream->flush();
300 }
301 emit(newClkBytes(_clkLine.length()));
302 }
303
304 if (_clkLine.indexOf('*') == -1) {
305 return;
306 }
307
308 QTextStream in(_clkLine);
309
310 QString hlp;
311 int year, month, day, hour, min;
312 double sec;
313 in >> hlp >> year >> month >> day >> hour >> min >> sec;
314
315 int GPSweek;
316 double GPSweeks;
317
318 GPSweekFromYMDhms(year, month, day, hour, min, sec, GPSweek, GPSweeks);
319
320 QStringList prns;
321
322 // Loop over all satellites
323 // ------------------------
324 QStringList lines;
325 for (;;) {
326 if (!_clkSocket->canReadLine()) {
327 break;
328 }
329 _clkLine = _clkSocket->readLine();
330 if (_echoStream) {
331 *_echoStream << _clkLine;
332 _echoStream->flush();
333 }
334 if (_clkLine[0] == '*') {
335 return;
336 }
337 if (_clkLine[0] == 'P') {
338 _clkLine.remove(0,1);
339 lines.push_back(_clkLine);
340 }
341 }
342
343 if (lines.size() > 0) {
344
345 QStringList prns;
346
347 for (int ic = 0; ic < _caster.size(); ic++) {
348 _caster.at(ic)->open();
349
350 for (int oldEph = 0; oldEph <= 0; oldEph++) { // TODO: handle old ephemeris
351
352 struct ClockOrbit co;
353 memset(&co, 0, sizeof(co));
354 co.GPSEpochTime = (int)GPSweeks;
355 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
356 co.ClockDataSupplied = 1;
357 co.OrbitDataSupplied = 1;
358 co.SatRefPoint = POINT_CENTER;
359 co.SatRefDatum = DATUM_ITRF;
360
361 for (int ii = 0; ii < lines.size(); ii++) {
362
363 QString prn;
364 ColumnVector xx(5); xx = 0.0;
365 t_eph* ep = 0;
366
367 if (oldEph == 0 && ic == 0) {
368 QTextStream in(lines[ii].toAscii());
369 in >> prn;
370 prns << prn;
371 if ( _ephList.contains(prn) ) {
372 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5);
373 xx(1) *= 1e3;
374 xx(2) *= 1e3;
375 xx(3) *= 1e3;
376 xx(4) *= 1e-6;
377
378 t_ephPair* pair = _ephList[prn];
379 pair->xx = xx;
380 ep = pair->eph;
381 }
382 }
383 else {
384 prn = prns[ii];
385 if ( _ephList.contains(prn) ) {
386 t_ephPair* pair = _ephList[prn];
387 prn = pair->eph->prn();
388 xx = pair->xx;
389 if (oldEph) {
390 ep = pair->oldEph;
391 }
392 else {
393 ep = pair->eph;
394 }
395 }
396 }
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;
403 }
404 else if (prn[0] == 'R') {
405 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
406 ++co.NumberOfGLONASSSat;
407 }
408 if (sd) {
409 QString outLine;
410 processSatellite(oldEph, ic, _caster.at(ic)->crdTrafo(),
411 _caster.at(ic)->beClocks(), ep,
412 GPSweek, GPSweeks, prn, xx, sd, outLine);
413 _caster.at(ic)->printAscii(outLine);
414 }
415 }
416 }
417
418 if ( _caster.at(ic)->usedSocket() &&
419 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
420 char obuffer[CLOCKORBIT_BUFFERSIZE];
421 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
422 if (len > 0) {
423 if (_caster.at(ic)->ic() == 1) { emit(newOutBytes1(len));}
424 if (_caster.at(ic)->ic() == 2) { emit(newOutBytes2(len));}
425 if (_caster.at(ic)->ic() == 3) { emit(newOutBytes3(len));}
426 _caster.at(ic)->write(obuffer, len);
427 }
428 }
429 }
430 }
431 }
432}
433
434//
435////////////////////////////////////////////////////////////////////////////
436void t_bns::processSatellite(int oldEph, int iCaster, bool trafo,
437 bool beClocks, t_eph* ep, int GPSweek,
438 double GPSweeks, const QString& prn,
439 const ColumnVector& xx,
440 struct ClockOrbit::SatData* sd,
441 QString& outLine) {
442
443 ColumnVector xB(4);
444 ColumnVector vv(3);
445
446 ep->position(GPSweek, GPSweeks, xB, vv);
447
448 ColumnVector xyz = xx.Rows(1,3);
449 if (trafo) {
450 crdTrafo(GPSweek, xyz);
451 }
452
453 ColumnVector dx = xyz - xB.Rows(1,3);
454
455 ColumnVector rsw(3);
456 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
457
458 double dClk;
459 if (beClocks) {
460 dClk = (xx(4) - xB(4)) * 299792458.0 - xx(5);
461 }
462 else {
463 dClk = (xx(4) - xB(4)) * 299792458.0;
464 }
465
466 if (sd) {
467 sd->ID = prn.mid(1).toInt();
468 sd->IOD = ep->IOD();
469 sd->Clock.DeltaA0 = dClk;
470 sd->Orbit.DeltaRadial = rsw(1);
471 sd->Orbit.DeltaAlongTrack = rsw(2);
472 sd->Orbit.DeltaCrossTrack = rsw(3);
473 }
474
475 char oldCh = (oldEph ? '!' : ' ');
476 outLine.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
477 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
478 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
479
480 if (!oldEph && iCaster == 0) {
481 if (_rnx) {
482 _rnx->write(GPSweek, GPSweeks, prn, xx);
483 }
484 if (_sp3) {
485 _sp3->write(GPSweek, GPSweeks, prn, xx);
486 }
487 }
488}
489
490//
491////////////////////////////////////////////////////////////////////////////
492void t_bns::slotMoveSocket(QThread* tt) {
493 _clkSocket->setParent(0);
494 _clkSocket->moveToThread(tt);
495//slotMessage("bns::slotMoveSocket");
496 slotMessage("Clocks & orbits port: Socket moved to thread"); // weber
497}
498
499// Transform Coordinates IGS -> ETRF
500////////////////////////////////////////////////////////////////////////////
501void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
502
503 // Current epoch minus 2000.0 in years
504 // ------------------------------------
505 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0;
506
507 ColumnVector dx(3);
508 dx(1) = 0.0541 - dt * 0.0002;
509 dx(2) = 0.0502 + dt * 0.0001;
510 dx(3) = -0.0538 - dt * 0.0018;
511
512 static const double arcSec = 180.0 * 3600.0 / M_PI;
513 double ox = ( 0.000891 + dt * 0.000081) / arcSec;
514 double oy = ( 0.005390 + dt * 0.000490) / arcSec;
515 double oz = (-0.008712 - dt * 0.000792) / arcSec;
516
517 double sc = 1.0 + 0.4e-9 + dt * 0.08e-9;
518
519 Matrix rMat(3,3);
520 rMat(1,1) = 1.0;
521 rMat(1,2) = -oz;
522 rMat(1,3) = oy;
523 rMat(2,1) = oz;
524 rMat(2,2) = 1.0;
525 rMat(2,3) = -ox;
526 rMat(3,1) = -oy;
527 rMat(3,2) = ox;
528 rMat(3,3) = 1.0;
529
530 xyz = sc * rMat * xyz + dx;
531}
Note: See TracBrowser for help on using the repository browser.