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