1 // realwx_ctrl.cxx -- Process real weather data
3 // Written by David Megginson, started February 2002.
4 // Rewritten by Torsten Dreyer, August 2010
6 // Copyright (C) 2002 David Megginson - david@megginson.com
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "realwx_ctrl.hxx"
28 #include "tiedpropertylist.hxx"
29 #include "metarproperties.hxx"
30 #include "metarairportfilter.hxx"
31 #include "fgmetar.hxx"
33 #include <Main/fg_props.hxx>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/misc/strutils.hxx>
38 #if defined(ENABLE_THREADS)
39 #include <OpenThreads/Thread>
40 #include <simgear/threads/SGQueue.hxx>
44 namespace Environment {
46 class BasicRealWxController : public RealWxController
49 BasicRealWxController( SGPropertyNode_ptr rootNode );
50 virtual ~BasicRealWxController ();
53 virtual void reinit ();
59 long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); }
61 SGPropertyNode_ptr _rootNode;
62 SGPropertyNode_ptr _longitude_n;
63 SGPropertyNode_ptr _latitude_n;
64 SGPropertyNode_ptr _ground_elevation_n;
65 SGPropertyNode_ptr _max_age_n;
68 TiedPropertyList _tiedProperties;
69 MetarProperties _metarProperties;
72 /* -------------------------------------------------------------------------------- */
75 ~/enabled: bool Enables/Disables the realwx controller
76 ~/metar[1..n]: string Target property path for metar data
79 BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode ) :
81 _longitude_n( fgGetNode( "/position/longitude-deg", true )),
82 _latitude_n( fgGetNode( "/position/latitude-deg", true )),
83 _ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )),
84 _max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ),
86 _metarProperties( fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) )
90 BasicRealWxController::~BasicRealWxController()
94 void BasicRealWxController::init()
96 update(0); // fetch data ASAP
99 void BasicRealWxController::reinit()
103 void BasicRealWxController::bind()
105 _tiedProperties.setRoot( _rootNode );
106 _tiedProperties.Tie( "enabled", &_enabled );
109 void BasicRealWxController::unbind()
111 _tiedProperties.Untie();
114 /* -------------------------------------------------------------------------------- */
116 class NoaaMetarRealWxController : public BasicRealWxController {
118 NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
119 virtual ~NoaaMetarRealWxController();
120 virtual void update (double delta_time_sec);
122 class MetarLoadRequest {
124 MetarLoadRequest( const string & stationId ) {
125 _stationId = stationId;
126 _proxyHost = fgGetNode("/sim/presets/proxy/host", true)->getStringValue();
127 _proxyPort = fgGetNode("/sim/presets/proxy/port", true)->getStringValue();
128 _proxyAuth = fgGetNode("/sim/presets/proxy/authentication", true)->getStringValue();
137 double _metarTimeToLive;
138 double _positionTimeToLive;
139 double _minimumRequestInterval;
141 SGPropertyNode_ptr _metarDataNode;
142 SGPropertyNode_ptr _metarValidNode;
143 SGPropertyNode_ptr _metarStationIdNode;
146 #if defined(ENABLE_THREADS)
147 class MetarLoadThread : public OpenThreads::Thread {
149 MetarLoadThread( long maxAge );
150 void requestMetar( const MetarLoadRequest & metarRequest );
151 bool hasMetar() { return _responseQueue.size() > 0; }
152 string getMetar() { return _responseQueue.pop(); }
156 SGBlockingQueue <MetarLoadRequest> _requestQueue;
157 SGBlockingQueue <string> _responseQueue;
160 MetarLoadThread * _metarLoadThread;
164 NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) :
165 BasicRealWxController(rootNode),
166 _metarTimeToLive(0.0),
167 _positionTimeToLive(0.0),
168 _minimumRequestInterval(0.0),
169 _metarDataNode(_metarProperties.get_root_node()->getNode("data",true)),
170 _metarValidNode(_metarProperties.get_root_node()->getNode("valid",true)),
171 _metarStationIdNode(_metarProperties.get_root_node()->getNode("station-id",true))
173 #if defined(ENABLE_THREADS)
174 _metarLoadThread = new MetarLoadThread(getMetarMaxAgeMin());
175 _metarLoadThread->start();
179 NoaaMetarRealWxController::~NoaaMetarRealWxController()
181 #if defined(ENABLE_THREADS)
182 if( _metarLoadThread ) {
183 MetarLoadRequest request("");
184 _metarLoadThread->requestMetar(request);
185 _metarLoadThread->join();
186 delete _metarLoadThread;
188 #endif // ENABLE_THREADS
191 void NoaaMetarRealWxController::update( double dt )
196 if( _metarLoadThread->hasMetar() ) {
197 string metar = _metarLoadThread->getMetar();
198 SG_LOG( SG_ALL, SG_ALERT, "NoaaMetarRwalWxController::update() received METAR " << metar );
199 _metarDataNode->setStringValue( metar );
202 _metarTimeToLive -= dt;
203 _positionTimeToLive -= dt;
204 _minimumRequestInterval -= dt;
206 bool valid = _metarValidNode->getBoolValue();
207 string stationId = valid ? _metarStationIdNode->getStringValue() : "";
210 if( _metarTimeToLive <= 0.0 ) {
212 _metarTimeToLive = 900;
213 _positionTimeToLive = 0;
216 if( _positionTimeToLive <= 0.0 || valid == false ) {
217 _positionTimeToLive = 60.0;
219 SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
221 FGAirport * nearestAirport = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
222 if( nearestAirport == NULL ) {
223 SG_LOG(SG_ALL,SG_WARN,"RealWxController::update can't find airport with METAR within 10000NM" );
227 if( stationId != nearestAirport->ident() ) {
229 stationId = nearestAirport->ident();
235 if( _minimumRequestInterval <= 0 && stationId.length() > 0 ) {
236 MetarLoadRequest request( stationId );
237 _metarLoadThread->requestMetar( request );
238 _minimumRequestInterval = 10;
244 /* -------------------------------------------------------------------------------- */
246 #if defined(ENABLE_THREADS)
247 NoaaMetarRealWxController::MetarLoadThread::MetarLoadThread( long maxAge ) :
252 void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest )
254 if( _requestQueue.size() > 10 ) {
255 SG_LOG(SG_ALL,SG_ALERT,
256 "NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping "
257 << metarRequest._stationId );
261 _requestQueue.push( metarRequest );
264 void NoaaMetarRealWxController::MetarLoadThread::run()
267 const MetarLoadRequest request = _requestQueue.pop();
269 if( request._stationId.size() == 0 )
272 SGSharedPtr<FGMetar> result = NULL;
275 result = new FGMetar( request._stationId, request._proxyHost, request._proxyPort, request._proxyAuth );
276 } catch (const sg_io_exception& e) {
277 SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): can't get METAR for "
278 << request._stationId << ":" << e.getFormattedMessage().c_str() );
282 string reply = result->getData();
283 std::replace(reply.begin(), reply.end(), '\n', ' ');
284 string metar = simgear::strutils::strip( reply );
286 if( metar.empty() ) {
287 SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for "
288 << request._stationId );
291 if( _maxAge && result->getAge_min() > _maxAge ) {
292 SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR "
297 _responseQueue.push( metar );
302 /* -------------------------------------------------------------------------------- */
304 RealWxController * RealWxController::createInstance( SGPropertyNode_ptr rootNode )
306 // string dataSource = rootNode->getStringValue("data-source", "noaa" );
307 // if( dataSource == "nwx" ) {
308 // return new NwxMetarRealWxController( rootNode );
310 return new NoaaMetarRealWxController( rootNode );
314 /* -------------------------------------------------------------------------------- */
316 } // namespace Environment