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 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 | // Constructor
|
---|
26 | ////////////////////////////////////////////////////////////////////////////
|
---|
27 | t_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 | QIODevice::Unbuffered)) {
|
---|
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 | QIODevice::Unbuffered)) {
|
---|
73 | _logStream = new QTextStream(_logFile);
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | // Destructor
|
---|
79 | ////////////////////////////////////////////////////////////////////////////
|
---|
80 | t_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 | ////////////////////////////////////////////////////////////////////////////
|
---|
98 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
109 | void t_bns::slotMessage(const QByteArray msg) {
|
---|
110 | if (_logStream) {
|
---|
111 | *_logStream << msg << endl;
|
---|
112 | _logStream->flush();
|
---|
113 | }
|
---|
114 | emit(newMessage(msg));
|
---|
115 | }
|
---|
116 |
|
---|
117 | // Write a Program Message
|
---|
118 | ////////////////////////////////////////////////////////////////////////////
|
---|
119 | void t_bns::slotError(const QByteArray msg) {
|
---|
120 | if (_logStream) {
|
---|
121 | *_logStream << msg << endl;
|
---|
122 | _logStream->flush();
|
---|
123 | }
|
---|
124 | deleteBnsEph();
|
---|
125 | emit(error(msg));
|
---|
126 | }
|
---|
127 |
|
---|
128 | // New Connection
|
---|
129 | ////////////////////////////////////////////////////////////////////////////
|
---|
130 | void t_bns::slotNewConnection() {
|
---|
131 | slotMessage("t_bns::slotNewConnection");
|
---|
132 | delete _clkSocket;
|
---|
133 | _clkSocket = _clkServer->nextPendingConnection();
|
---|
134 | }
|
---|
135 |
|
---|
136 | // Start the Communication with NTRIP Caster
|
---|
137 | ////////////////////////////////////////////////////////////////////////////
|
---|
138 | void t_bns::openCaster() {
|
---|
139 |
|
---|
140 | QSettings settings;
|
---|
141 |
|
---|
142 | _outSocket = new QTcpSocket();
|
---|
143 | _outSocket->connectToHost(settings.value("outHost").toString(),
|
---|
144 | settings.value("outPort").toInt());
|
---|
145 |
|
---|
146 | const int timeOut = 100; // 0.1 seconds
|
---|
147 | if (!_outSocket->waitForConnected(timeOut)) {
|
---|
148 | delete _outSocket;
|
---|
149 | _outSocket = 0;
|
---|
150 | emit(error("bns::openCaster Connect Timeout"));
|
---|
151 | }
|
---|
152 |
|
---|
153 | QString mountpoint = settings.value("mountpoint").toString();
|
---|
154 | QString password = settings.value("password").toString();
|
---|
155 |
|
---|
156 | QByteArray msg = "SOURCE " + password.toAscii() + " /" +
|
---|
157 | mountpoint.toAscii() + "\r\n" +
|
---|
158 | "Source-Agent: NTRIP BNS/1.0\r\n\r\n";
|
---|
159 |
|
---|
160 | _outSocket->write(msg);
|
---|
161 | _outSocket->waitForBytesWritten();
|
---|
162 |
|
---|
163 | _outSocket->waitForReadyRead();
|
---|
164 | QByteArray ans = _outSocket->readLine();
|
---|
165 |
|
---|
166 | if (ans.indexOf("OK") == -1) {
|
---|
167 | delete _outSocket;
|
---|
168 | _outSocket = 0;
|
---|
169 | slotMessage("bns::openCaster socket deleted");
|
---|
170 | }
|
---|
171 | else {
|
---|
172 | slotMessage("bns::openCaster socket OK");
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | //
|
---|
177 | ////////////////////////////////////////////////////////////////////////////
|
---|
178 | void t_bns::slotNewEph(gpsEph* ep) {
|
---|
179 |
|
---|
180 | QMutexLocker locker(&_mutex);
|
---|
181 |
|
---|
182 | t_ephPair* pair;
|
---|
183 | if ( !_ephList.contains(ep->prn) ) {
|
---|
184 | pair = new t_ephPair();
|
---|
185 | _ephList.insert(ep->prn, pair);
|
---|
186 | }
|
---|
187 | else {
|
---|
188 | pair = _ephList[ep->prn];
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (pair->eph == 0) {
|
---|
192 | pair->eph = ep;
|
---|
193 | }
|
---|
194 | else {
|
---|
195 | if (ep->GPSweek > pair->eph->GPSweek ||
|
---|
196 | (ep->GPSweek == pair->eph->GPSweek && ep->TOC > pair->eph->TOC)) {
|
---|
197 | delete pair->oldEph;
|
---|
198 | pair->oldEph = pair->eph;
|
---|
199 | pair->eph = ep;
|
---|
200 | }
|
---|
201 | else {
|
---|
202 | delete ep;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | // Start
|
---|
208 | ////////////////////////////////////////////////////////////////////////////
|
---|
209 | void t_bns::run() {
|
---|
210 |
|
---|
211 | slotMessage("============ Start BNS ============");
|
---|
212 |
|
---|
213 | // Start Thread that retrieves broadcast Ephemeris
|
---|
214 | // -----------------------------------------------
|
---|
215 | _bnseph->start();
|
---|
216 |
|
---|
217 | // Endless loop
|
---|
218 | // ------------
|
---|
219 | while (true) {
|
---|
220 | if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
|
---|
221 | if ( _clkSocket->canReadLine()) {
|
---|
222 | if (_outSocket == 0) {
|
---|
223 | openCaster();
|
---|
224 | }
|
---|
225 | readEpoch();
|
---|
226 | }
|
---|
227 | else {
|
---|
228 | _clkSocket->waitForReadyRead(10);
|
---|
229 | }
|
---|
230 | }
|
---|
231 | else {
|
---|
232 | msleep(10);
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | //
|
---|
238 | ////////////////////////////////////////////////////////////////////////////
|
---|
239 | void t_bns::readEpoch() {
|
---|
240 |
|
---|
241 | QByteArray line = _clkSocket->readLine();
|
---|
242 |
|
---|
243 | if (line.indexOf('*') == -1) {
|
---|
244 | return;
|
---|
245 | }
|
---|
246 |
|
---|
247 | QTextStream in(line);
|
---|
248 |
|
---|
249 | QString hlp;
|
---|
250 | int GPSweek, numSat;
|
---|
251 | double GPSweeks;
|
---|
252 |
|
---|
253 | in >> hlp >> GPSweek >> GPSweeks >> numSat;
|
---|
254 |
|
---|
255 | for (int ii = 1; ii <= numSat; ii++) {
|
---|
256 | line = _clkSocket->readLine();
|
---|
257 |
|
---|
258 | QTextStream in(line);
|
---|
259 |
|
---|
260 | QString prn;
|
---|
261 | ColumnVector xx(4);
|
---|
262 |
|
---|
263 | in >> prn >> xx(1) >> xx(2) >> xx(3) >> xx(4);
|
---|
264 | xx(4) *= 1e-6;
|
---|
265 |
|
---|
266 | processSatellite(GPSweek, GPSweeks, prn, xx);
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | //
|
---|
271 | ////////////////////////////////////////////////////////////////////////////
|
---|
272 | void t_bns::processSatellite(int GPSweek, double GPSweeks, const QString& prn,
|
---|
273 | const ColumnVector& xx) {
|
---|
274 |
|
---|
275 | // No broadcast ephemeris available
|
---|
276 | // --------------------------------
|
---|
277 | if ( !_ephList.contains(prn) ) {
|
---|
278 | return;
|
---|
279 | }
|
---|
280 |
|
---|
281 | t_ephPair* pair = _ephList[prn];
|
---|
282 | gpsEph* ep = pair->eph;
|
---|
283 |
|
---|
284 | ColumnVector xB(4);
|
---|
285 | ColumnVector vv(3);
|
---|
286 |
|
---|
287 | satellitePosition(GPSweek, GPSweeks, ep, xB(1), xB(2), xB(3), xB(4),
|
---|
288 | vv(1), vv(2), vv(3));
|
---|
289 |
|
---|
290 | ColumnVector dx = xx.Rows(1,3) - xB.Rows(1,3);
|
---|
291 | double dClk = (xx(4) - xB(4)) * 299792458.0;
|
---|
292 | ColumnVector rsw(3);
|
---|
293 |
|
---|
294 | XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
|
---|
295 |
|
---|
296 | QString line;
|
---|
297 | line.sprintf("%d %.1f %s %3d %3d %8.3f %8.3f %8.3f %8.3f\n",
|
---|
298 | GPSweek, GPSweeks, ep->prn.toAscii().data(),
|
---|
299 | int(ep->IODC), int(ep->IODE), dClk, rsw(1), rsw(2), rsw(3));
|
---|
300 |
|
---|
301 | if (_outStream) {
|
---|
302 | *_outStream << line;
|
---|
303 | _outStream->flush();
|
---|
304 | }
|
---|
305 | if (_outSocket) {
|
---|
306 | _outSocket->write(line.toAscii());
|
---|
307 | _outSocket->flush();
|
---|
308 | }
|
---|
309 | }
|
---|