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

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

* empty log message *

File size: 6.8 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 <iostream>
18#include <newmatio.h>
19
20#include "bns.h"
21#include "bnsutils.h"
22
23using namespace std;
24
25// Constructor
26////////////////////////////////////////////////////////////////////////////
27t_bns::t_bns(QObject* parent) : QThread(parent) {
28
29 this->setTerminationEnabled(true);
30
31 // Thread that handles broadcast ephemeris
32 // ---------------------------------------
33 _bnseph = new t_bnseph(parent);
34
35 connect(_bnseph, SIGNAL(newEph(gpsEph*)), this, SLOT(slotNewEph(gpsEph*)));
36 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
37 this, SLOT(slotMessage(const QByteArray)));
38 connect(_bnseph, SIGNAL(error(QByteArray)),
39 this, SLOT(slotError(const QByteArray)));
40
41 // Server listening for rtnet results
42 // ----------------------------------
43 QSettings settings;
44 _clkSocket = 0;
45 _clkServer = new QTcpServer;
46 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
47 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
48
49 // Socket for outputting the results
50 // ---------------------------------
51 _outSocket = 0;
52 _outFile = 0;
53 QFile outFile(settings.value("outFile").toString());
54 if (outFile.open(QFile::WriteOnly | QFile::Truncate)) {
55 _outFile = new QTextStream(&outFile);
56 }
57
58 // Log File
59 // --------
60 _logFile = 0;
61 QFile logFile(settings.value("logFile").toString());
62 if (logFile.open(QFile::WriteOnly | QFile::Truncate)) {
63 _logFile = new QTextStream(&logFile);
64 }
65}
66
67// Destructor
68////////////////////////////////////////////////////////////////////////////
69t_bns::~t_bns() {
70 deleteBnsEph();
71 delete _clkServer;
72 /// delete _clkSocket;
73 delete _outSocket;
74 delete _outFile;
75 delete _logFile;
76 QMapIterator<QString, t_ephPair*> it(_ephList);
77 while (it.hasNext()) {
78 it.next();
79 delete it.value();
80 }
81}
82
83// Delete bns thread
84////////////////////////////////////////////////////////////////////////////
85void t_bns::deleteBnsEph() {
86 if (_bnseph) {
87 _bnseph->terminate();
88 _bnseph->wait(100);
89 delete _bnseph;
90 _bnseph = 0;
91 }
92}
93
94// Write a Program Message
95////////////////////////////////////////////////////////////////////////////
96void t_bns::slotMessage(const QByteArray msg) {
97 if (_logFile) {
98 *_logFile << msg << endl;
99 }
100 emit(newMessage(msg));
101}
102
103// Write a Program Message
104////////////////////////////////////////////////////////////////////////////
105void t_bns::slotError(const QByteArray msg) {
106 if (_logFile) {
107 *_logFile << msg << endl;
108 }
109 deleteBnsEph();
110 emit(error(msg));
111}
112
113// New Connection
114////////////////////////////////////////////////////////////////////////////
115void t_bns::slotNewConnection() {
116 slotMessage("t_bns::slotNewConnection");
117 delete _clkSocket;
118 _clkSocket = _clkServer->nextPendingConnection();
119}
120
121// Start the Communication with NTRIP Caster
122////////////////////////////////////////////////////////////////////////////
123void t_bns::openCaster() {
124
125 QSettings settings;
126
127 _outSocket = new QTcpSocket();
128 _outSocket->connectToHost(settings.value("outHost").toString(),
129 settings.value("outPort").toInt());
130
131 QString mountpoint = settings.value("mountpoint").toString();
132 QString password = settings.value("password").toString();
133
134 QByteArray msg = "SOURCE " + password.toAscii() + " /" +
135 mountpoint.toAscii() + "\r\n" +
136 "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
137
138 _outSocket->write(msg);
139
140 QByteArray ans = _outSocket->readLine();
141
142 if (ans.indexOf("OK") == -1) {
143 delete _outSocket;
144 _outSocket = 0;
145 }
146}
147
148//
149////////////////////////////////////////////////////////////////////////////
150void t_bns::slotNewEph(gpsEph* ep) {
151
152 QMutexLocker locker(&_mutex);
153
154 t_ephPair* pair;
155 if ( !_ephList.contains(ep->prn) ) {
156 pair = new t_ephPair();
157 _ephList.insert(ep->prn, pair);
158 }
159 else {
160 pair = _ephList[ep->prn];
161 }
162
163 if (pair->eph == 0) {
164 pair->eph = ep;
165 }
166 else {
167 if (ep->GPSweek > pair->eph->GPSweek ||
168 (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
169 delete pair->oldEph;
170 pair->oldEph = pair->eph;
171 pair->eph = ep;
172 }
173 else {
174 delete ep;
175 }
176 }
177}
178
179// Start
180////////////////////////////////////////////////////////////////////////////
181void t_bns::run() {
182
183 slotMessage("============ Start BNS ============");
184
185 // Start Thread that retrieves broadcast Ephemeris
186 // -----------------------------------------------
187 _bnseph->start();
188
189 // Endless loop
190 // ------------
191 while (true) {
192 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
193 if ( _clkSocket->canReadLine()) {
194 if (_outSocket == 0) {
195 openCaster();
196 }
197 readEpoch();
198 }
199 else {
200 _clkSocket->waitForReadyRead(10);
201 }
202 }
203 else {
204 msleep(10);
205 }
206 }
207}
208
209//
210////////////////////////////////////////////////////////////////////////////
211void t_bns::readEpoch() {
212
213 QByteArray line = _clkSocket->readLine();
214
215 if (line.indexOf('*') == -1) {
216 return;
217 }
218
219 QTextStream in(line);
220
221 QString hlp;
222 int GPSweek, numSat;
223 double GPSweeks;
224
225 in >> hlp >> GPSweek >> GPSweeks >> numSat;
226
227 for (int ii = 1; ii <= numSat; ii++) {
228 line = _clkSocket->readLine();
229
230 QTextStream in(line);
231
232 QString prn;
233 ColumnVector xx(4);
234
235 in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
236 xx(4) *= 1e-6;
237
238 processSatellite(GPSweek, GPSweeks, prn, xx);
239 }
240}
241
242//
243////////////////////////////////////////////////////////////////////////////
244void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
245 const ColumnVector& xx) {
246
247 // No broadcast ephemeris available
248 // --------------------------------
249 if ( !_ephList.contains(prn) ) {
250 return;
251 }
252
253 t_ephPair* pair = _ephList[prn];
254 gpsEph* ep = pair->eph;
255
256 ColumnVector xB(4);
257 ColumnVector vv(3);
258
259 satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
260 vv(1), vv(2), vv(3));
261
262 ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
263 double dClk = (xx(4) - xB(4)) * 299792458.0;
264 ColumnVector rsw(3);
265
266 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
267
268 QString line;
269 line.sprintf("%d %.1f %s %d %d %8.3f %8.3f %8.3f %8.3f\n",
270 GPSweek, GPSweeks, ep->prn.toAscii().data(),
271 int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
272
273 if (_outFile) {
274 *_outFile << line;
275 }
276 if (_outSocket) {
277 _outSocket->write(line.toAscii());
278 }
279}
Note: See TracBrowser for help on using the repository browser.