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