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

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

* empty log message *

File size: 11.6 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*, int)),
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 _caster = new t_bnscaster(settings.value("mountpoint").toString());
76
77 QIODevice::OpenMode oMode;
78 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
79 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
80 }
81 else {
82 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
83 }
84
85 QString outFileName = settings.value("outFile").toString();
86 if (outFileName.isEmpty()) {
87 _outFile = 0;
88 _outStream = 0;
89 }
90 else {
91 _outFile = new QFile(outFileName);
92 if (_outFile->open(oMode)) {
93 _outStream = new QTextStream(_outFile);
94 }
95 }
96
97 // Log File
98 // --------
99 QString logFileName = settings.value("logFile").toString();
100 if (logFileName.isEmpty()) {
101 _logFile = 0;
102 _logStream = 0;
103 }
104 else {
105 _logFile = new QFile(logFileName);
106 if (_logFile->open(oMode)) {
107 _logStream = new QTextStream(_logFile);
108 }
109 else {
110 _logStream = 0;
111 }
112 }
113
114 // RINEX writer
115 // ------------
116 if ( settings.value("rnxPath").toString().isEmpty() ) {
117 _rnx = 0;
118 }
119 else {
120 QString prep = "BNS";
121 QString ext = ".clk";
122 QString path = settings.value("rnxPath").toString();
123 QString intr = settings.value("rnxIntr").toString();
124 int sampl = settings.value("rnxSampl").toInt();
125 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
126 }
127
128 // SP3 writer
129 // ----------
130 if ( settings.value("sp3Path").toString().isEmpty() ) {
131 _sp3 = 0;
132 }
133 else {
134 QString prep = "BNS";
135 QString ext = ".sp3";
136 QString path = settings.value("sp3Path").toString();
137 QString intr = settings.value("sp3Intr").toString();
138 int sampl = settings.value("sp3Sampl").toInt();
139 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
140 }
141
142 // Reference frame
143 // ---------------
144 _crdTrafo = false;
145 if (settings.value("refSys").toString() == "ETRS89") {
146 _crdTrafo = true;
147 }
148}
149
150// Destructor
151////////////////////////////////////////////////////////////////////////////
152t_bns::~t_bns() {
153 deleteBnsEph();
154 delete _clkServer;
155 delete _clkSocket;
156 delete _caster;
157 delete _outStream;
158 delete _logStream;
159 delete _outFile;
160 delete _logFile;
161 QMapIterator<QString, t_ephPair*> it(_ephList);
162 while (it.hasNext()) {
163 it.next();
164 delete it.value();
165 }
166 delete _rnx;
167 delete _sp3;
168}
169
170// Delete bns thread
171////////////////////////////////////////////////////////////////////////////
172void t_bns::deleteBnsEph() {
173 if (_bnseph) {
174 _bnseph->terminate();
175 _bnseph->wait(100);
176 delete _bnseph;
177 _bnseph = 0;
178 }
179}
180
181// Write a Program Message
182////////////////////////////////////////////////////////////////////////////
183void t_bns::slotMessage(const QByteArray msg) {
184 if (_logStream) {
185 *_logStream << msg << endl;
186 _logStream->flush();
187 }
188 emit(newMessage(msg));
189}
190
191// Write a Program Message
192////////////////////////////////////////////////////////////////////////////
193void t_bns::slotError(const QByteArray msg) {
194 if (_logStream) {
195 *_logStream << msg << endl;
196 _logStream->flush();
197 }
198 deleteBnsEph();
199 emit(error(msg));
200}
201
202// New Connection
203////////////////////////////////////////////////////////////////////////////
204void t_bns::slotNewConnection() {
205 slotMessage("t_bns::slotNewConnection");
206 delete _clkSocket;
207 _clkSocket = _clkServer->nextPendingConnection();
208}
209
210//
211////////////////////////////////////////////////////////////////////////////
212void t_bns::slotNewEph(t_eph* ep, int nBytes) {
213
214 QMutexLocker locker(&_mutex);
215
216 emit(newEphBytes(nBytes));
217
218 t_ephPair* pair;
219 if ( !_ephList.contains(ep->prn()) ) {
220 pair = new t_ephPair();
221 _ephList.insert(ep->prn(), pair);
222 }
223 else {
224 pair = _ephList[ep->prn()];
225 }
226
227 if (pair->eph == 0) {
228 pair->eph = ep;
229 }
230 else {
231 if (ep->isNewerThan(pair->eph)) {
232 delete pair->oldEph;
233 pair->oldEph = pair->eph;
234 pair->eph = ep;
235 }
236 else {
237 delete ep;
238 }
239 }
240}
241
242// Start
243////////////////////////////////////////////////////////////////////////////
244void t_bns::run() {
245
246 slotMessage("============ Start BNS ============");
247
248 // Start Thread that retrieves broadcast Ephemeris
249 // -----------------------------------------------
250 _bnseph->start();
251
252 // Endless loop
253 // ------------
254 while (true) {
255
256 if (_clkSocket && _clkSocket->thread() != currentThread()) {
257 emit(moveSocket(currentThread()));
258 }
259
260 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
261 if ( _clkSocket->canReadLine()) {
262 _caster->open();
263 readEpoch();
264 }
265 else {
266 _clkSocket->waitForReadyRead(10);
267 }
268 }
269 else {
270 msleep(10);
271 }
272 }
273}
274
275//
276////////////////////////////////////////////////////////////////////////////
277void t_bns::readEpoch() {
278
279 QByteArray line = _clkSocket->readLine();
280
281 emit(newClkBytes(line.length()));
282
283 if (line.indexOf('*') == -1) {
284 return;
285 }
286
287 QTextStream in(line);
288
289 QString hlp;
290 int GPSweek, numSat;
291 double GPSweeks;
292
293 in >> hlp >> GPSweek >> GPSweeks >> numSat;
294
295 if (numSat > 0) {
296
297 QStringList prns;
298
299 /// for (int oldEph = 0; oldEph <= 1; oldEph++) {
300 for (int oldEph = 0; oldEph <= 0; oldEph++) {
301
302 struct ClockOrbit co;
303 memset(&co, 0, sizeof(co));
304 co.GPSEpochTime = (int)GPSweeks;
305 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
306 co.ClockDataSupplied = 1;
307 co.OrbitDataSupplied = 1;
308 co.SatRefPoint = POINT_CENTER;
309 co.SatRefDatum = DATUM_ITRF;
310
311 for (int ii = 1; ii <= numSat; ii++) {
312
313 QString prn;
314 ColumnVector xx(5);
315 t_eph* ep = 0;
316
317 if (oldEph == 0) {
318 line = _clkSocket->readLine();
319 QTextStream in(line);
320 in >> prn;
321 prns << prn;
322 if ( _ephList.contains(prn) ) {
323 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5); xx(4) *= 1e-6;
324
325 //// beg test (zero clock correction for Gerhard Wuebbena)
326 //// xx(4) -= xx(5) / 299792458.0;
327 //// end test
328
329 t_ephPair* pair = _ephList[prn];
330 pair->xx = xx;
331 ep = pair->eph;
332 }
333 }
334 else {
335 prn = prns[ii-1];
336 if ( _ephList.contains(prn) ) {
337 t_ephPair* pair = _ephList[prn];
338 prn = pair->eph->prn();
339 xx = pair->xx;
340 ep = pair->oldEph;
341 }
342 }
343
344 if (ep != 0) {
345 struct ClockOrbit::SatData* sd = 0;
346 if (prn[0] == 'G') {
347 sd = co.Sat + co.NumberOfGPSSat;
348 ++co.NumberOfGPSSat;
349 }
350 else if (prn[0] == 'R') {
351 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
352 ++co.NumberOfGLONASSSat;
353 }
354 if (sd) {
355 processSatellite(oldEph, ep, GPSweek, GPSweeks, prn, xx, sd);
356 }
357 }
358 }
359
360 if ( (_caster->used() || _outFile) &&
361 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
362 char obuffer[CLOCKORBIT_BUFFERSIZE];
363 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
364 if (len > 0) {
365 emit(newOutBytes(len));
366 _caster->write(obuffer, len);
367 }
368 }
369 }
370 }
371}
372
373//
374////////////////////////////////////////////////////////////////////////////
375void t_bns::processSatellite(int oldEph, t_eph* ep, int GPSweek, double GPSweeks,
376 const QString& prn, const ColumnVector& xx,
377 struct ClockOrbit::SatData* sd) {
378
379 ColumnVector xB(4);
380 ColumnVector vv(3);
381
382 ep->position(GPSweek, GPSweeks, xB, vv);
383
384 ColumnVector xyz = xx.Rows(1,3);
385 if (_crdTrafo) {
386 crdTrafo(GPSweek, xyz);
387 }
388
389 ColumnVector dx = xyz - xB.Rows(1,3);
390
391 double dClk = (xx(4) - xB(4)) * 299792458.0;
392 ColumnVector rsw(3);
393
394 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
395
396 if (sd) {
397 sd->ID = prn.mid(1).toInt();
398 sd->IOD = ep->IOD();
399 sd->Clock.DeltaA0 = dClk;
400 sd->Orbit.DeltaRadial = rsw(1);
401 sd->Orbit.DeltaAlongTrack = rsw(2);
402 sd->Orbit.DeltaCrossTrack = rsw(3);
403 }
404
405 if (_outStream) {
406 QString line;
407 char oldCh = (oldEph ? '!' : ' ');
408 line.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
409 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
410 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
411 *_outStream << line;
412 _outStream->flush();
413 }
414 if (!oldEph) {
415 if (_rnx) {
416 _rnx->write(GPSweek, GPSweeks, prn, xx);
417 }
418 if (_sp3) {
419 _sp3->write(GPSweek, GPSweeks, prn, xx);
420 }
421 }
422}
423
424//
425////////////////////////////////////////////////////////////////////////////
426void t_bns::slotMoveSocket(QThread* tt) {
427 _clkSocket->setParent(0);
428 _clkSocket->moveToThread(tt);
429 slotMessage("bns::slotMoveSocket");
430}
431
432// Transform Coordinates IGS -> ETRF
433////////////////////////////////////////////////////////////////////////////
434void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
435
436 ColumnVector dx(3);
437 dx(1) = 0.054;
438 dx(2) = 0.051;
439 dx(3) = -0.048;
440 const static double arcSec = 180.0 * 3600.0 / M_PI;
441 const static double ox = 0.000081 / arcSec;
442 const static double oy = 0.000490 / arcSec;
443 const static double oz = -0.000792 / arcSec;
444
445 Matrix rMat(3,3); rMat = 0.0;
446 rMat(1,2) = -oz;
447 rMat(1,3) = oy;
448 rMat(2,1) = oz;
449 rMat(2,3) = -ox;
450 rMat(3,1) = -oy;
451 rMat(3,2) = ox;
452
453 // Current epoch minus 1989.0 in years
454 // ------------------------------------
455 double dt = (GPSWeek - 469.0) / 365.2422 * 7.0;
456
457 xyz += dx + dt * rMat * xyz;
458}
Note: See TracBrowser for help on using the repository browser.