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

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

* empty log message *

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