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

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

* empty log message *

File size: 11.4 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 // Thread that handles broadcast ephemeris
38 // ---------------------------------------
39 _bnseph = new t_bnseph(parent);
40
41 connect(_bnseph, SIGNAL(newEph(t_eph*)), this, SLOT(slotNewEph(t_eph*)));
42 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
43 this, SLOT(slotMessage(const QByteArray)));
44 connect(_bnseph, SIGNAL(error(QByteArray)),
45 this, SLOT(slotError(const QByteArray)));
46
47 // Server listening for rtnet results
48 // ----------------------------------
49 QSettings settings;
50 _clkSocket = 0;
51 _clkServer = new QTcpServer;
52 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
53 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
54
55 // Socket and file for outputting the results
56 // -------------------------------------------
57 _outSocket = 0;
58 _outSocketOpenTrial = 0;
59
60 QString outFileName = settings.value("outFile").toString();
61 if (outFileName.isEmpty()) {
62 _outFile = 0;
63 _outStream = 0;
64 }
65 else {
66 _outFile = new QFile(outFileName);
67 if (_outFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
68 _outStream = new QTextStream(_outFile);
69 }
70 }
71
72 // Log File
73 // --------
74 QString logFileName = settings.value("logFile").toString();
75 if (logFileName.isEmpty()) {
76 _logFile = 0;
77 _logStream = 0;
78 }
79 else {
80 _logFile = new QFile(logFileName);
81 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
82 _logStream = new QTextStream(_logFile);
83 }
84 }
85
86 // RINEX writer
87 // ------------
88 if ( settings.value("rnxPath").toString().isEmpty() ) {
89 _rnx = 0;
90 }
91 else {
92 QString prep = "BNS";
93 QString ext = ".clk";
94 QString path = settings.value("rnxPath").toString();
95 QString intr = settings.value("rnxIntr").toString();
96 int sampl = settings.value("rnxSampl").toInt();
97 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
98 }
99
100 // SP3 writer
101 // ----------
102 if ( settings.value("sp3Path").toString().isEmpty() ) {
103 _sp3 = 0;
104 }
105 else {
106 QString prep = "BNS";
107 QString ext = ".sp3";
108 QString path = settings.value("sp3Path").toString();
109 QString intr = settings.value("sp3Intr").toString();
110 int sampl = settings.value("sp3Sampl").toInt();
111 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
112 }
113}
114
115// Destructor
116////////////////////////////////////////////////////////////////////////////
117t_bns::~t_bns() {
118 deleteBnsEph();
119 delete _clkServer;
120 delete _clkSocket;
121 delete _outSocket;
122 delete _outStream;
123 delete _logStream;
124 delete _outFile;
125 delete _logFile;
126 QMapIterator<QString, t_ephPair*> it(_ephList);
127 while (it.hasNext()) {
128 it.next();
129 delete it.value();
130 }
131 delete _rnx;
132 delete _sp3;
133}
134
135// Delete bns thread
136////////////////////////////////////////////////////////////////////////////
137void t_bns::deleteBnsEph() {
138 if (_bnseph) {
139 _bnseph->terminate();
140 _bnseph->wait(100);
141 delete _bnseph;
142 _bnseph = 0;
143 }
144}
145
146// Write a Program Message
147////////////////////////////////////////////////////////////////////////////
148void t_bns::slotMessage(const QByteArray msg) {
149 if (_logStream) {
150 *_logStream << msg << endl;
151 _logStream->flush();
152 }
153 emit(newMessage(msg));
154}
155
156// Write a Program Message
157////////////////////////////////////////////////////////////////////////////
158void t_bns::slotError(const QByteArray msg) {
159 if (_logStream) {
160 *_logStream << msg << endl;
161 _logStream->flush();
162 }
163 deleteBnsEph();
164 emit(error(msg));
165}
166
167// New Connection
168////////////////////////////////////////////////////////////////////////////
169void t_bns::slotNewConnection() {
170 slotMessage("t_bns::slotNewConnection");
171 delete _clkSocket;
172 _clkSocket = _clkServer->nextPendingConnection();
173}
174
175// Start the Communication with NTRIP Caster
176////////////////////////////////////////////////////////////////////////////
177void t_bns::openCaster() {
178
179 delete _outSocket; _outSocket = 0;
180
181 double minDt = exp2(_outSocketOpenTrial);
182 if (++_outSocketOpenTrial > 8) {
183 _outSocketOpenTrial = 8;
184 }
185 if (_outSocketOpenTime.isValid() &&
186 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
187 return;
188 }
189 else {
190 _outSocketOpenTime = QDateTime::currentDateTime();
191 }
192
193 QSettings settings;
194 _outSocket = new QTcpSocket();
195 _outSocket->connectToHost(settings.value("outHost").toString(),
196 settings.value("outPort").toInt());
197
198 const int timeOut = 100; // 0.1 seconds
199 if (!_outSocket->waitForConnected(timeOut)) {
200 delete _outSocket;
201 _outSocket = 0;
202 emit(error("bns::openCaster Connect Timeout"));
203 return;
204 }
205
206 QString mountpoint = settings.value("mountpoint").toString();
207 QString password = settings.value("password").toString();
208
209 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
210 mountpoint.toAscii() + "\r\n" +
211 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
212
213 _outSocket->write(msg);
214 _outSocket->waitForBytesWritten();
215
216 _outSocket->waitForReadyRead();
217 QByteArray ans = _outSocket->readLine();
218
219 if (ans.indexOf("OK") == -1) {
220 delete _outSocket;
221 _outSocket = 0;
222 slotMessage("bns::openCaster socket deleted");
223 }
224 else {
225 slotMessage("bns::openCaster socket OK");
226 _outSocketOpenTrial = 0;
227 }
228}
229
230//
231////////////////////////////////////////////////////////////////////////////
232void t_bns::slotNewEph(t_eph* ep) {
233
234 QMutexLocker locker(&_mutex);
235
236 t_ephPair* pair;
237 if ( !_ephList.contains(ep->prn()) ) {
238 pair = new t_ephPair();
239 _ephList.insert(ep->prn(), pair);
240 }
241 else {
242 pair = _ephList[ep->prn()];
243 }
244
245 if (pair->eph == 0) {
246 pair->eph = ep;
247 }
248 else {
249 if (ep->isNewerThan(pair->eph)) {
250 delete pair->oldEph;
251 pair->oldEph = pair->eph;
252 pair->eph = ep;
253 }
254 else {
255 delete ep;
256 }
257 }
258}
259
260// Start
261////////////////////////////////////////////////////////////////////////////
262void t_bns::run() {
263
264 slotMessage("============ Start BNS ============");
265
266 // Start Thread that retrieves broadcast Ephemeris
267 // -----------------------------------------------
268 _bnseph->start();
269
270 // Endless loop
271 // ------------
272 while (true) {
273
274 if (_clkSocket && _clkSocket->thread() != currentThread()) {
275 emit(moveSocket(currentThread()));
276 }
277
278 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
279 if ( _clkSocket->canReadLine()) {
280 if (_outSocket == 0 ||
281 _outSocket->state() != QAbstractSocket::ConnectedState) {
282 openCaster();
283 }
284 readEpoch();
285 }
286 else {
287 _clkSocket->waitForReadyRead(10);
288 }
289 }
290 else {
291 msleep(10);
292 }
293 }
294}
295
296//
297////////////////////////////////////////////////////////////////////////////
298void t_bns::readEpoch() {
299
300 QByteArray line = _clkSocket->readLine();
301
302 if (line.indexOf('*') == -1) {
303 return;
304 }
305
306 QTextStream in(line);
307
308 QString hlp;
309 int GPSweek, numSat;
310 double GPSweeks;
311
312 in >> hlp >> GPSweek >> GPSweeks >> numSat;
313
314 if (numSat > 0) {
315
316 QStringList prns;
317
318 /// for (int oldEph = 0; oldEph <= 1; oldEph++) {
319 for (int oldEph = 0; oldEph <= 0; oldEph++) {
320
321 struct ClockOrbit co;
322 memset(&co, 0, sizeof(co));
323 co.GPSEpochTime = (int)GPSweeks;
324 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
325 co.ClockDataSupplied = 1;
326 co.OrbitDataSupplied = 1;
327 co.SatRefPoint = POINT_CENTER;
328 co.SatRefDatum = DATUM_ITRF;
329
330 for (int ii = 1; ii <= numSat; ii++) {
331
332 QString prn;
333 ColumnVector xx(5);
334 t_eph* ep = 0;
335
336 if (oldEph == 0) {
337 line = _clkSocket->readLine();
338 QTextStream in(line);
339 in >> prn;
340 prns << prn;
341 if ( _ephList.contains(prn) ) {
342 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5); xx(4) *= 1e-6;
343
344 //// beg test (zero clock correction for Gerhard Wuebbena)
345 //// xx(4) -= xx(5) / 299792458.0;
346 //// end test
347
348 t_ephPair* pair = _ephList[prn];
349 pair->xx = xx;
350 ep = pair->eph;
351 }
352 }
353 else {
354 prn = prns[ii-1];
355 if ( _ephList.contains(prn) ) {
356 t_ephPair* pair = _ephList[prn];
357 prn = pair->eph->prn();
358 xx = pair->xx;
359 ep = pair->oldEph;
360 }
361 }
362
363 if (ep != 0) {
364 struct ClockOrbit::SatData* sd = 0;
365 if (prn[0] == 'G') {
366 sd = co.Sat + co.NumberOfGPSSat;
367 ++co.NumberOfGPSSat;
368 }
369 else if (prn[0] == 'R') {
370 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
371 ++co.NumberOfGLONASSSat;
372 }
373 if (sd) {
374 processSatellite(oldEph, ep, GPSweek, GPSweeks, prn, xx, sd);
375 }
376 }
377 }
378
379 if ( _outSocket &&
380 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
381 char obuffer[CLOCKORBIT_BUFFERSIZE];
382 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
383 if (len > 0) {
384 _outSocket->write(obuffer, len);
385 _outSocket->flush();
386 }
387 }
388 }
389 }
390}
391
392//
393////////////////////////////////////////////////////////////////////////////
394void t_bns::processSatellite(int oldEph, t_eph* ep, int GPSweek, double GPSweeks,
395 const QString& prn, const ColumnVector& xx,
396 struct ClockOrbit::SatData* sd) {
397
398 ColumnVector xB(4);
399 ColumnVector vv(3);
400
401 ep->position(GPSweek, GPSweeks, xB, vv);
402
403 ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
404
405 double dClk = (xx(4) - xB(4)) * 299792458.0;
406 ColumnVector rsw(3);
407
408 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
409
410 if (sd) {
411 sd->ID = prn.mid(1).toInt();
412 sd->IOD = ep->IOD();
413 sd->Clock.DeltaA0 = dClk;
414 sd->Orbit.DeltaRadial = rsw(1);
415 sd->Orbit.DeltaAlongTrack = rsw(2);
416 sd->Orbit.DeltaCrossTrack = rsw(3);
417 }
418
419 if (_outStream) {
420 QString line;
421 char oldCh = (oldEph ? '!' : ' ');
422 line.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
423 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
424 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
425 *_outStream << line;
426 _outStream->flush();
427 }
428 if (!oldEph) {
429 if (_rnx) {
430 _rnx->write(GPSweek, GPSweeks, prn, xx);
431 }
432 if (_sp3) {
433 _sp3->write(GPSweek, GPSweeks, prn, xx);
434 }
435 }
436}
437
438//
439////////////////////////////////////////////////////////////////////////////
440void t_bns::slotMoveSocket(QThread* tt) {
441 _clkSocket->setParent(0);
442 _clkSocket->moveToThread(tt);
443 slotMessage("bns::slotMoveSocket");
444}
Note: See TracBrowser for help on using the repository browser.