1 | // Part of BNC, a utility for retrieving decoding and
|
---|
2 | // converting GNSS data streams from NTRIP broadcasters.
|
---|
3 | //
|
---|
4 | // Copyright (C) 2007
|
---|
5 | // German Federal Agency for Cartography and Geodesy (BKG)
|
---|
6 | // http://www.bkg.bund.de
|
---|
7 | // Czech Technical University Prague, Department of Geodesy
|
---|
8 | // http://www.fsv.cvut.cz
|
---|
9 | //
|
---|
10 | // Email: euref-ip@bkg.bund.de
|
---|
11 | //
|
---|
12 | // This program is free software; you can redistribute it and/or
|
---|
13 | // modify it under the terms of the GNU General Public License
|
---|
14 | // as published by the Free Software Foundation, version 2.
|
---|
15 | //
|
---|
16 | // This program is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU General Public License
|
---|
22 | // along with this program; if not, write to the Free Software
|
---|
23 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
24 |
|
---|
25 | /* -------------------------------------------------------------------------
|
---|
26 | * BKG NTRIP Client
|
---|
27 | * -------------------------------------------------------------------------
|
---|
28 | *
|
---|
29 | * Class: bncCaster
|
---|
30 | *
|
---|
31 | * Purpose: buffers and disseminates the data
|
---|
32 | *
|
---|
33 | * Author: L. Mervart
|
---|
34 | *
|
---|
35 | * Created: 24-Dec-2005
|
---|
36 | *
|
---|
37 | * Changes:
|
---|
38 | *
|
---|
39 | * -----------------------------------------------------------------------*/
|
---|
40 |
|
---|
41 | #include <math.h>
|
---|
42 |
|
---|
43 | #include "bnccaster.h"
|
---|
44 | #include "bncgetthread.h"
|
---|
45 | #include "bncutils.h"
|
---|
46 | #include "RTCM/GPSDecoder.h"
|
---|
47 |
|
---|
48 | // Constructor
|
---|
49 | ////////////////////////////////////////////////////////////////////////////
|
---|
50 | bncCaster::bncCaster(const QString& outFileName, int port) {
|
---|
51 |
|
---|
52 | QSettings settings;
|
---|
53 |
|
---|
54 | if ( !outFileName.isEmpty() ) {
|
---|
55 | QString lName = outFileName;
|
---|
56 | expandEnvVar(lName);
|
---|
57 | _outFile = new QFile(lName);
|
---|
58 | if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
|
---|
59 | _outFile->open(QIODevice::WriteOnly | QIODevice::Append);
|
---|
60 | }
|
---|
61 | else {
|
---|
62 | _outFile->open(QIODevice::WriteOnly);
|
---|
63 | }
|
---|
64 | _out = new QTextStream(_outFile);
|
---|
65 | _out->setRealNumberNotation(QTextStream::FixedNotation);
|
---|
66 | }
|
---|
67 | else {
|
---|
68 | _outFile = 0;
|
---|
69 | _out = 0;
|
---|
70 | }
|
---|
71 |
|
---|
72 | _port = port;
|
---|
73 |
|
---|
74 | if (_port != 0) {
|
---|
75 | _server = new QTcpServer;
|
---|
76 | _server->listen(QHostAddress::Any, _port);
|
---|
77 | connect(_server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));
|
---|
78 | _sockets = new QList<QTcpSocket*>;
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | _server = 0;
|
---|
82 | _sockets = 0;
|
---|
83 | }
|
---|
84 |
|
---|
85 | _epochs = new QMultiMap<long, p_obs>;
|
---|
86 |
|
---|
87 | _lastDumpSec = 0;
|
---|
88 |
|
---|
89 | _samplingRate = settings.value("rnxSampl").toInt();
|
---|
90 | _waitTime = settings.value("waitTime").toInt();
|
---|
91 | if (_waitTime < 1) {
|
---|
92 | _waitTime = 1;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Destructor
|
---|
97 | ////////////////////////////////////////////////////////////////////////////
|
---|
98 | bncCaster::~bncCaster() {
|
---|
99 | QListIterator<bncGetThread*> it(_threads);
|
---|
100 | while(it.hasNext()){
|
---|
101 | bncGetThread* thread = it.next();
|
---|
102 | thread->terminate();
|
---|
103 | thread->wait();
|
---|
104 | delete thread;
|
---|
105 | }
|
---|
106 | delete _out;
|
---|
107 | delete _outFile;
|
---|
108 | delete _server;
|
---|
109 | delete _sockets;
|
---|
110 | if (_epochs) {
|
---|
111 | QListIterator<p_obs> it(_epochs->values());
|
---|
112 | while (it.hasNext()) {
|
---|
113 | delete it.next();
|
---|
114 | }
|
---|
115 | delete _epochs;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | // New Observations
|
---|
120 | ////////////////////////////////////////////////////////////////////////////
|
---|
121 | void bncCaster::newObs(const QByteArray staID, bool firstObs, p_obs obs) {
|
---|
122 |
|
---|
123 | QMutexLocker locker(&_mutex);
|
---|
124 |
|
---|
125 | obs->_status = t_obs::received;
|
---|
126 |
|
---|
127 | long iSec = long(floor(obs->_o.GPSWeeks+0.5));
|
---|
128 | long newTime = obs->_o.GPSWeek * 7*24*3600 + iSec;
|
---|
129 |
|
---|
130 | // Rename the Station
|
---|
131 | // ------------------
|
---|
132 | strncpy(obs->_o.StatID, staID.constData(),sizeof(obs->_o.StatID));
|
---|
133 | obs->_o.StatID[sizeof(obs->_o.StatID)-1] = '\0';
|
---|
134 |
|
---|
135 | // First time, set the _lastDumpSec immediately
|
---|
136 | // --------------------------------------------
|
---|
137 | if (_lastDumpSec == 0) {
|
---|
138 | _lastDumpSec = newTime - 1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | // An old observation - throw it away
|
---|
142 | // ----------------------------------
|
---|
143 | if (newTime <= _lastDumpSec) {
|
---|
144 | if (firstObs) {
|
---|
145 | QSettings settings;
|
---|
146 | if ( !settings.value("outFile").toString().isEmpty() ||
|
---|
147 | !settings.value("outPort").toString().isEmpty() ) {
|
---|
148 | emit( newMessage(QString("Station %1: old epoch %2 thrown away")
|
---|
149 | .arg(staID.data()).arg(iSec).toAscii()) );
|
---|
150 | }
|
---|
151 | }
|
---|
152 | delete obs;
|
---|
153 | return;
|
---|
154 | }
|
---|
155 |
|
---|
156 | // Save the observation
|
---|
157 | // --------------------
|
---|
158 | _epochs->insert(newTime, obs);
|
---|
159 |
|
---|
160 | // Dump Epochs
|
---|
161 | // -----------
|
---|
162 | if (newTime - _waitTime > _lastDumpSec) {
|
---|
163 | dumpEpochs(_lastDumpSec + 1, newTime - _waitTime);
|
---|
164 | _lastDumpSec = newTime - _waitTime;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | // New Connection
|
---|
169 | ////////////////////////////////////////////////////////////////////////////
|
---|
170 | void bncCaster::slotNewConnection() {
|
---|
171 | _sockets->push_back( _server->nextPendingConnection() );
|
---|
172 | }
|
---|
173 |
|
---|
174 | // Add New Thread
|
---|
175 | ////////////////////////////////////////////////////////////////////////////
|
---|
176 | void bncCaster::addGetThread(bncGetThread* getThread) {
|
---|
177 |
|
---|
178 | qRegisterMetaType<p_obs>("p_obs");
|
---|
179 |
|
---|
180 | connect(getThread, SIGNAL(newObs(QByteArray, bool, p_obs)),
|
---|
181 | this, SLOT(newObs(QByteArray, bool, p_obs)));
|
---|
182 |
|
---|
183 | connect(getThread, SIGNAL(error(QByteArray)),
|
---|
184 | this, SLOT(slotGetThreadError(QByteArray)));
|
---|
185 |
|
---|
186 | _staIDs.push_back(getThread->staID());
|
---|
187 | _threads.push_back(getThread);
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Error in get thread
|
---|
191 | ////////////////////////////////////////////////////////////////////////////
|
---|
192 | void bncCaster::slotGetThreadError(QByteArray staID) {
|
---|
193 | QMutexLocker locker(&_mutex);
|
---|
194 | _staIDs.removeAll(staID);
|
---|
195 | emit( newMessage(
|
---|
196 | QString("Mountpoint size %1").arg(_staIDs.size()).toAscii()) );
|
---|
197 | if (_staIDs.size() == 0) {
|
---|
198 | emit(newMessage("bncCaster:: last get thread terminated"));
|
---|
199 | emit getThreadErrors();
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | // Dump Complete Epochs
|
---|
204 | ////////////////////////////////////////////////////////////////////////////
|
---|
205 | void bncCaster::dumpEpochs(long minTime, long maxTime) {
|
---|
206 |
|
---|
207 | const char begEpoch = 'A';
|
---|
208 | const char begObs = 'B';
|
---|
209 | const char endEpoch = 'C';
|
---|
210 |
|
---|
211 | for (long sec = minTime; sec <= maxTime; sec++) {
|
---|
212 |
|
---|
213 | bool first = true;
|
---|
214 | QList<p_obs> allObs = _epochs->values(sec);
|
---|
215 | QListIterator<p_obs> it(allObs);
|
---|
216 | while (it.hasNext()) {
|
---|
217 | p_obs obs = it.next();
|
---|
218 |
|
---|
219 | if (_samplingRate == 0 || sec % _samplingRate == 0) {
|
---|
220 |
|
---|
221 | // Output into the file
|
---|
222 | // --------------------
|
---|
223 | if (_out) {
|
---|
224 | if (first) {
|
---|
225 | _out->setFieldWidth(1); *_out << begEpoch << endl;;
|
---|
226 | }
|
---|
227 | _out->setFieldWidth(0); *_out << obs->_o.StatID;
|
---|
228 | _out->setFieldWidth(1); *_out << " " << obs->_o.satSys;
|
---|
229 | _out->setPadChar('0');
|
---|
230 | _out->setFieldWidth(2); *_out << obs->_o.satNum;
|
---|
231 | _out->setPadChar(' ');
|
---|
232 | _out->setFieldWidth(1); *_out << " ";
|
---|
233 | _out->setFieldWidth(4); *_out << obs->_o.GPSWeek;
|
---|
234 | _out->setFieldWidth(1); *_out << " ";
|
---|
235 | _out->setFieldWidth(14); _out->setRealNumberPrecision(7); *_out << obs->_o.GPSWeeks;
|
---|
236 | _out->setFieldWidth(1); *_out << " ";
|
---|
237 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.C1;
|
---|
238 | _out->setFieldWidth(1); *_out << " ";
|
---|
239 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.C2;
|
---|
240 | _out->setFieldWidth(1); *_out << " ";
|
---|
241 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.P1;
|
---|
242 | _out->setFieldWidth(1); *_out << " ";
|
---|
243 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.P2;
|
---|
244 | _out->setFieldWidth(1); *_out << " ";
|
---|
245 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.L1;
|
---|
246 | _out->setFieldWidth(1); *_out << " ";
|
---|
247 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.L2;
|
---|
248 | _out->setFieldWidth(1); *_out << " ";
|
---|
249 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.S1;
|
---|
250 | _out->setFieldWidth(1); *_out << " ";
|
---|
251 | _out->setFieldWidth(14); _out->setRealNumberPrecision(3); *_out << obs->_o.S2;
|
---|
252 | _out->setFieldWidth(1);
|
---|
253 | *_out << " " << obs->_o.SNR1 << " " << obs->_o.SNR2 << endl;
|
---|
254 | if (!it.hasNext()) {
|
---|
255 | _out->setFieldWidth(1); *_out << endEpoch << endl;
|
---|
256 | }
|
---|
257 | _out->flush();
|
---|
258 | }
|
---|
259 |
|
---|
260 | // Output into the socket
|
---|
261 | // ----------------------
|
---|
262 | if (_sockets) {
|
---|
263 | int numBytes = sizeof(obs->_o);
|
---|
264 | QListIterator<QTcpSocket*> is(*_sockets);
|
---|
265 | while (is.hasNext()) {
|
---|
266 | QTcpSocket* sock = is.next();
|
---|
267 | if (sock->state() == QAbstractSocket::ConnectedState) {
|
---|
268 | if (first) {
|
---|
269 | sock->write(&begEpoch, 1);
|
---|
270 | }
|
---|
271 | sock->write(&begObs, 1);
|
---|
272 | sock->write((char*) &obs->_o, numBytes);
|
---|
273 | if (!it.hasNext()) {
|
---|
274 | sock->write(&endEpoch, 1);
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | delete obs;
|
---|
282 | _epochs->remove(sec);
|
---|
283 | first = false;
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|