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

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

* empty log message *

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