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