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 |
|
---|
26 | using namespace std;
|
---|
27 |
|
---|
28 | // Constructor
|
---|
29 | ////////////////////////////////////////////////////////////////////////////
|
---|
30 | t_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*)), this, SLOT(slotNewEph(t_eph*)));
|
---|
60 | connect(_bnseph, SIGNAL(newMessage(QByteArray)),
|
---|
61 | this, SLOT(slotMessage(const QByteArray)));
|
---|
62 | connect(_bnseph, SIGNAL(error(QByteArray)),
|
---|
63 | this, SLOT(slotError(const QByteArray)));
|
---|
64 |
|
---|
65 | // Server listening for rtnet results
|
---|
66 | // ----------------------------------
|
---|
67 | _clkSocket = 0;
|
---|
68 | _clkServer = new QTcpServer;
|
---|
69 | _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
|
---|
70 | connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
|
---|
71 |
|
---|
72 | // Socket and file for outputting the results
|
---|
73 | // -------------------------------------------
|
---|
74 | _outSocket = 0;
|
---|
75 | _outSocketOpenTrial = 0;
|
---|
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 | ////////////////////////////////////////////////////////////////////////////
|
---|
152 | t_bns::~t_bns() {
|
---|
153 | deleteBnsEph();
|
---|
154 | delete _clkServer;
|
---|
155 | delete _clkSocket;
|
---|
156 | delete _outSocket;
|
---|
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 | ////////////////////////////////////////////////////////////////////////////
|
---|
172 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
183 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
193 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
204 | void t_bns::slotNewConnection() {
|
---|
205 | slotMessage("t_bns::slotNewConnection");
|
---|
206 | delete _clkSocket;
|
---|
207 | _clkSocket = _clkServer->nextPendingConnection();
|
---|
208 | }
|
---|
209 |
|
---|
210 | // Start the Communication with NTRIP Caster
|
---|
211 | ////////////////////////////////////////////////////////////////////////////
|
---|
212 | void t_bns::openCaster() {
|
---|
213 |
|
---|
214 | delete _outSocket; _outSocket = 0;
|
---|
215 |
|
---|
216 | double minDt = exp2(_outSocketOpenTrial);
|
---|
217 | if (++_outSocketOpenTrial > 8) {
|
---|
218 | _outSocketOpenTrial = 8;
|
---|
219 | }
|
---|
220 | if (_outSocketOpenTime.isValid() &&
|
---|
221 | _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
|
---|
222 | return;
|
---|
223 | }
|
---|
224 | else {
|
---|
225 | _outSocketOpenTime = QDateTime::currentDateTime();
|
---|
226 | }
|
---|
227 |
|
---|
228 | QSettings settings;
|
---|
229 | _outSocket = new QTcpSocket();
|
---|
230 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
---|
231 | settings.value("outPort").toInt());
|
---|
232 |
|
---|
233 | const int timeOut = 100; // 0.1 seconds
|
---|
234 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
235 | delete _outSocket;
|
---|
236 | _outSocket = 0;
|
---|
237 | emit(error("bns::openCaster Connect Timeout"));
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | QString mountpoint = settings.value("mountpoint").toString();
|
---|
242 | QString password = settings.value("password").toString();
|
---|
243 |
|
---|
244 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
245 | mountpoint.toAscii() + "\r\n" +
|
---|
246 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
247 |
|
---|
248 | _outSocket->write(msg);
|
---|
249 | _outSocket->waitForBytesWritten();
|
---|
250 |
|
---|
251 | _outSocket->waitForReadyRead();
|
---|
252 | QByteArray ans = _outSocket->readLine();
|
---|
253 |
|
---|
254 | if (ans.indexOf("OK") == -1) {
|
---|
255 | delete _outSocket;
|
---|
256 | _outSocket = 0;
|
---|
257 | slotMessage("bns::openCaster socket deleted");
|
---|
258 | }
|
---|
259 | else {
|
---|
260 | slotMessage("bns::openCaster socket OK");
|
---|
261 | _outSocketOpenTrial = 0;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | //
|
---|
266 | ////////////////////////////////////////////////////////////////////////////
|
---|
267 | void t_bns::slotNewEph(t_eph* ep) {
|
---|
268 |
|
---|
269 | QMutexLocker locker(&_mutex);
|
---|
270 |
|
---|
271 | t_ephPair* pair;
|
---|
272 | if ( !_ephList.contains(ep->prn()) ) {
|
---|
273 | pair = new t_ephPair();
|
---|
274 | _ephList.insert(ep->prn(), pair);
|
---|
275 | }
|
---|
276 | else {
|
---|
277 | pair = _ephList[ep->prn()];
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (pair->eph == 0) {
|
---|
281 | pair->eph = ep;
|
---|
282 | }
|
---|
283 | else {
|
---|
284 | if (ep->isNewerThan(pair->eph)) {
|
---|
285 | delete pair->oldEph;
|
---|
286 | pair->oldEph = pair->eph;
|
---|
287 | pair->eph = ep;
|
---|
288 | }
|
---|
289 | else {
|
---|
290 | delete ep;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | // Start
|
---|
296 | ////////////////////////////////////////////////////////////////////////////
|
---|
297 | void t_bns::run() {
|
---|
298 |
|
---|
299 | slotMessage("============ Start BNS ============");
|
---|
300 |
|
---|
301 | // Start Thread that retrieves broadcast Ephemeris
|
---|
302 | // -----------------------------------------------
|
---|
303 | _bnseph->start();
|
---|
304 |
|
---|
305 | // Endless loop
|
---|
306 | // ------------
|
---|
307 | while (true) {
|
---|
308 |
|
---|
309 | if (_clkSocket && _clkSocket->thread() != currentThread()) {
|
---|
310 | emit(moveSocket(currentThread()));
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
314 | if ( _clkSocket->canReadLine()) {
|
---|
315 | if (_outSocket == 0 ||
|
---|
316 | _outSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
317 | openCaster();
|
---|
318 | }
|
---|
319 | readEpoch();
|
---|
320 | }
|
---|
321 | else {
|
---|
322 | _clkSocket->waitForReadyRead(10);
|
---|
323 | }
|
---|
324 | }
|
---|
325 | else {
|
---|
326 | msleep(10);
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | //
|
---|
332 | ////////////////////////////////////////////////////////////////////////////
|
---|
333 | void t_bns::readEpoch() {
|
---|
334 |
|
---|
335 | QByteArray line = _clkSocket->readLine();
|
---|
336 |
|
---|
337 | if (line.indexOf('*') == -1) {
|
---|
338 | return;
|
---|
339 | }
|
---|
340 |
|
---|
341 | QTextStream in(line);
|
---|
342 |
|
---|
343 | QString hlp;
|
---|
344 | int GPSweek, numSat;
|
---|
345 | double GPSweeks;
|
---|
346 |
|
---|
347 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
---|
348 |
|
---|
349 | if (numSat > 0) {
|
---|
350 |
|
---|
351 | QStringList prns;
|
---|
352 |
|
---|
353 | /// for (int oldEph = 0; oldEph <= 1; oldEph++) {
|
---|
354 | for (int oldEph = 0; oldEph <= 0; oldEph++) {
|
---|
355 |
|
---|
356 | struct ClockOrbit co;
|
---|
357 | memset(&co, 0, sizeof(co));
|
---|
358 | co.GPSEpochTime = (int)GPSweeks;
|
---|
359 | co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
|
---|
360 | co.ClockDataSupplied = 1;
|
---|
361 | co.OrbitDataSupplied = 1;
|
---|
362 | co.SatRefPoint = POINT_CENTER;
|
---|
363 | co.SatRefDatum = DATUM_ITRF;
|
---|
364 |
|
---|
365 | for (int ii = 1; ii <= numSat; ii++) {
|
---|
366 |
|
---|
367 | QString prn;
|
---|
368 | ColumnVector xx(5);
|
---|
369 | t_eph* ep = 0;
|
---|
370 |
|
---|
371 | if (oldEph == 0) {
|
---|
372 | line = _clkSocket->readLine();
|
---|
373 | QTextStream in(line);
|
---|
374 | in >> prn;
|
---|
375 | prns << prn;
|
---|
376 | if ( _ephList.contains(prn) ) {
|
---|
377 | in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5); xx(4) *= 1e-6;
|
---|
378 |
|
---|
379 | //// beg test (zero clock correction for Gerhard Wuebbena)
|
---|
380 | //// xx(4) -= xx(5) / 299792458.0;
|
---|
381 | //// end test
|
---|
382 |
|
---|
383 | t_ephPair* pair = _ephList[prn];
|
---|
384 | pair->xx = xx;
|
---|
385 | ep = pair->eph;
|
---|
386 | }
|
---|
387 | }
|
---|
388 | else {
|
---|
389 | prn = prns[ii-1];
|
---|
390 | if ( _ephList.contains(prn) ) {
|
---|
391 | t_ephPair* pair = _ephList[prn];
|
---|
392 | prn = pair->eph->prn();
|
---|
393 | xx = pair->xx;
|
---|
394 | ep = pair->oldEph;
|
---|
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 | processSatellite(oldEph, ep, GPSweek, GPSweeks, prn, xx, sd);
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | if ( (_outSocket || _outFile) &&
|
---|
415 | (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
|
---|
416 | char obuffer[CLOCKORBIT_BUFFERSIZE];
|
---|
417 | int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
|
---|
418 | if (len > 0) {
|
---|
419 | if (_outSocket) {
|
---|
420 | _outSocket->write(obuffer, len);
|
---|
421 | _outSocket->flush();
|
---|
422 | }
|
---|
423 | if (_outFile) {
|
---|
424 | _outFile->write(obuffer, len);
|
---|
425 | _outFile->flush();
|
---|
426 | }
|
---|
427 | }
|
---|
428 | }
|
---|
429 | }
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | //
|
---|
434 | ////////////////////////////////////////////////////////////////////////////
|
---|
435 | void t_bns::processSatellite(int oldEph, t_eph* ep, int GPSweek, double GPSweeks,
|
---|
436 | const QString& prn, const ColumnVector& xx,
|
---|
437 | struct ClockOrbit::SatData* sd) {
|
---|
438 |
|
---|
439 | ColumnVector xB(4);
|
---|
440 | ColumnVector vv(3);
|
---|
441 |
|
---|
442 | ep->position(GPSweek, GPSweeks, xB, vv);
|
---|
443 |
|
---|
444 | ColumnVector xyz = xx.Rows(1,3);
|
---|
445 | if (_crdTrafo) {
|
---|
446 | crdTrafo(GPSweek, xyz);
|
---|
447 | }
|
---|
448 |
|
---|
449 | ColumnVector dx = xyz - xB.Rows(1,3);
|
---|
450 |
|
---|
451 | double dClk = (xx(4) - xB(4)) * 299792458.0;
|
---|
452 | ColumnVector rsw(3);
|
---|
453 |
|
---|
454 | XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
|
---|
455 |
|
---|
456 | if (sd) {
|
---|
457 | sd->ID = prn.mid(1).toInt();
|
---|
458 | sd->IOD = ep->IOD();
|
---|
459 | sd->Clock.DeltaA0 = dClk;
|
---|
460 | sd->Orbit.DeltaRadial = rsw(1);
|
---|
461 | sd->Orbit.DeltaAlongTrack = rsw(2);
|
---|
462 | sd->Orbit.DeltaCrossTrack = rsw(3);
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (_outStream) {
|
---|
466 | QString line;
|
---|
467 | char oldCh = (oldEph ? '!' : ' ');
|
---|
468 | line.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
|
---|
469 | oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
|
---|
470 | ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
|
---|
471 | *_outStream << line;
|
---|
472 | _outStream->flush();
|
---|
473 | }
|
---|
474 | if (!oldEph) {
|
---|
475 | if (_rnx) {
|
---|
476 | _rnx->write(GPSweek, GPSweeks, prn, xx);
|
---|
477 | }
|
---|
478 | if (_sp3) {
|
---|
479 | _sp3->write(GPSweek, GPSweeks, prn, xx);
|
---|
480 | }
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | //
|
---|
485 | ////////////////////////////////////////////////////////////////////////////
|
---|
486 | void t_bns::slotMoveSocket(QThread* tt) {
|
---|
487 | _clkSocket->setParent(0);
|
---|
488 | _clkSocket->moveToThread(tt);
|
---|
489 | slotMessage("bns::slotMoveSocket");
|
---|
490 | }
|
---|
491 |
|
---|
492 | // Transform Coordinates IGS -> ETRF
|
---|
493 | ////////////////////////////////////////////////////////////////////////////
|
---|
494 | void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
|
---|
495 |
|
---|
496 | ColumnVector dx(3);
|
---|
497 | dx(1) = 0.054;
|
---|
498 | dx(2) = 0.051;
|
---|
499 | dx(3) = -0.048;
|
---|
500 | const static double arcSec = 180.0 * 3600.0 / M_PI;
|
---|
501 | const static double ox = 0.000081 / arcSec;
|
---|
502 | const static double oy = 0.000490 / arcSec;
|
---|
503 | const static double oz = -0.000792 / arcSec;
|
---|
504 |
|
---|
505 | Matrix rMat(3,3); rMat = 0.0;
|
---|
506 | rMat(1,2) = -oz;
|
---|
507 | rMat(1,3) = oy;
|
---|
508 | rMat(2,1) = oz;
|
---|
509 | rMat(2,3) = -ox;
|
---|
510 | rMat(3,1) = -oy;
|
---|
511 | rMat(3,2) = ox;
|
---|
512 |
|
---|
513 | // Current epoch minus 1989.0 in years
|
---|
514 | // ------------------------------------
|
---|
515 | double dt = (GPSWeek - 469.0) / 365.2422 * 7.0;
|
---|
516 |
|
---|
517 | xyz += dx + dt * rMat * xyz;
|
---|
518 | }
|
---|