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