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

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

* empty log message *

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