1 // realwx_ctrl.cxx -- Process real weather data
3 // Written by David Megginson, started February 2002.
4 // Rewritten by Torsten Dreyer, August 2010, August 2011
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 "metarproperties.hxx"
29 #include "metarairportfilter.hxx"
30 #include "fgmetar.hxx"
32 #include <Main/fg_props.hxx>
34 #include <boost/foreach.hpp>
36 #include <simgear/structure/exception.hxx>
37 #include <simgear/misc/strutils.hxx>
38 #include <simgear/props/tiedpropertylist.hxx>
39 #include <simgear/io/HTTPClient.hxx>
40 #include <simgear/io/HTTPRequest.hxx>
41 #include <simgear/timing/sg_time.hxx>
45 namespace Environment {
46 /* -------------------------------------------------------------------------------- */
48 class FGHTTPClient : public simgear::HTTP::Client {
53 FGHTTPClient::FGHTTPClient()
55 string proxyHost(fgGetString("/sim/presets/proxy/host"));
56 int proxyPort(fgGetInt("/sim/presets/proxy/port"));
57 string proxyAuth(fgGetString("/sim/presets/proxy/auth"));
59 if (!proxyHost.empty()) {
60 setProxy(proxyHost, proxyPort, proxyAuth);
64 /* -------------------------------------------------------------------------------- */
66 class MetarDataHandler {
68 virtual void handleMetarData( const std::string & data ) = 0;
71 class MetarRequester {
73 virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id ) = 0;
76 /* -------------------------------------------------------------------------------- */
78 class LiveMetarProperties : public MetarProperties, MetarDataHandler {
80 LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester );
81 virtual ~LiveMetarProperties();
82 virtual void update( double dt );
84 virtual double getTimeToLive() const { return _timeToLive; }
85 virtual void setTimeToLive( double value ) { _timeToLive = value; }
87 // implementation of MetarDataHandler
88 virtual void handleMetarData( const std::string & data );
90 static const unsigned MAX_POLLING_INTERVAL_SECONDS = 10;
91 static const unsigned DEFAULT_TIME_TO_LIVE_SECONDS = 900;
96 MetarRequester * _metarRequester;
99 typedef SGSharedPtr<LiveMetarProperties> LiveMetarProperties_ptr;
101 LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ) :
102 MetarProperties( rootNode ),
105 _metarRequester(metarRequester)
107 _tiedProperties.Tie("time-to-live", &_timeToLive );
110 LiveMetarProperties::~LiveMetarProperties()
112 _tiedProperties.Untie();
115 void LiveMetarProperties::update( double dt )
119 if( _timeToLive < 0.0 ) {
121 std::string stationId = getStationId();
122 if( stationId.empty() ) return;
123 if( _pollingTimer > 0.0 ) return;
124 _metarRequester->requestMetar( this, stationId );
125 _pollingTimer = MAX_POLLING_INTERVAL_SECONDS;
129 void LiveMetarProperties::handleMetarData( const std::string & data )
131 SG_LOG( SG_ENVIRONMENT, SG_INFO, "LiveMetarProperties::handleMetarData() received METAR for " << getStationId() << ": " << data );
132 _timeToLive = DEFAULT_TIME_TO_LIVE_SECONDS;
136 /* -------------------------------------------------------------------------------- */
138 class BasicRealWxController : public RealWxController
141 BasicRealWxController( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester );
142 virtual ~BasicRealWxController ();
144 virtual void init ();
145 virtual void reinit ();
150 void update( double dt );
152 virtual void update (bool first, double delta_time_sec);
154 long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); }
156 SGPropertyNode_ptr _rootNode;
157 SGPropertyNode_ptr _ground_elevation_n;
158 SGPropertyNode_ptr _max_age_n;
162 simgear::TiedPropertyList _tiedProperties;
163 typedef std::vector<LiveMetarProperties_ptr> MetarPropertiesList;
164 MetarPropertiesList _metarProperties;
166 double _positionTimeToLive;
169 /* -------------------------------------------------------------------------------- */
172 ~/enabled: bool Enables/Disables the realwx controller
173 ~/metar[1..n]: string Target property path for metar data
176 BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ) :
178 _ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )),
179 _max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ),
182 _positionTimeToLive(0.0)
184 // at least instantiate MetarProperties for /environment/metar
185 _metarProperties.push_back( new LiveMetarProperties(
186 fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ), metarRequester ));
188 BOOST_FOREACH( SGPropertyNode_ptr n, rootNode->getChildren("metar") ) {
189 SG_LOG( SG_ENVIRONMENT, SG_INFO, "Adding metar properties at " << n->getStringValue() );
190 _metarProperties.push_back( new LiveMetarProperties(
191 fgGetNode( n->getStringValue(), true ), metarRequester ));
195 BasicRealWxController::~BasicRealWxController()
199 void BasicRealWxController::init()
202 update(0); // fetch data ASAP
205 void BasicRealWxController::reinit()
210 void BasicRealWxController::bind()
212 _tiedProperties.setRoot( _rootNode );
213 _tiedProperties.Tie( "enabled", &_enabled );
216 void BasicRealWxController::unbind()
218 _tiedProperties.Untie();
221 void BasicRealWxController::update( double dt )
224 bool firstIteration = !__enabled; // first iteration after being enabled?
226 // clock tick for every METAR in stock
227 BOOST_FOREACH(LiveMetarProperties* p, _metarProperties) {
228 // first round? All received METARs are outdated
229 if( firstIteration ) p->setTimeToLive( 0.0 );
233 update( firstIteration, dt );
240 void BasicRealWxController::update( bool first, double dt )
242 _positionTimeToLive -= dt;
244 if( _positionTimeToLive <= 0.0 ) {
248 const SGGeod & pos = globals->get_aircraft_position();
250 // check nearest airport
251 SG_LOG(SG_ENVIRONMENT, SG_INFO, "NoaaMetarRealWxController::update(): (re) checking nearby airport with METAR" );
252 _positionTimeToLive = 60.0;
254 FGAirport * nearestAirport = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
255 if( nearestAirport == NULL ) {
256 SG_LOG(SG_ENVIRONMENT,SG_WARN,"RealWxController::update can't find airport with METAR within 10000NM" );
260 SG_LOG(SG_ENVIRONMENT, SG_INFO,
261 "NoaaMetarRealWxController::update(): nearest airport with METAR is: " << nearestAirport->ident() );
263 // if it has changed, invalidate the associated METAR
264 if( _metarProperties[0]->getStationId() != nearestAirport->ident() ) {
265 SG_LOG(SG_ENVIRONMENT, SG_INFO,
266 "NoaaMetarRealWxController::update(): nearest airport with METAR has changed. Old: '" <<
267 _metarProperties[0]->getStationId() <<
268 "', new: '" << nearestAirport->ident() << "'" );
269 _metarProperties[0]->setStationId( nearestAirport->ident() );
270 _metarProperties[0]->setTimeToLive( 0.0 );
273 catch( sg_exception & ) {
279 /* -------------------------------------------------------------------------------- */
281 class NoaaMetarRealWxController : public BasicRealWxController, MetarRequester {
283 NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
284 virtual ~NoaaMetarRealWxController();
285 virtual void update( double dt );
287 // implementation of MetarRequester
288 virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id );
294 NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) :
295 BasicRealWxController(rootNode, this )
299 NoaaMetarRealWxController::~NoaaMetarRealWxController()
303 void NoaaMetarRealWxController::update( double dt )
306 BasicRealWxController::update( dt );
309 void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandler, const std::string & id )
311 class NoaaMetarGetRequest : public simgear::HTTP::Request
314 NoaaMetarGetRequest(MetarDataHandler* metarDataHandler, const string& stationId ) :
315 Request("http://weather.noaa.gov/pub/data/observations/metar/stations/" + stationId + ".TXT"),
317 _metarDataHandler(metarDataHandler)
321 virtual string_list requestHeaders() const
324 reply.push_back("X-TIME");
328 virtual std::string header(const std::string& name) const
332 if( name == "X-TIME" ) {
333 std::ostringstream buf;
334 buf << globals->get_time_params()->get_cur_time();
341 virtual void responseHeader(const string& key, const string& value)
343 if (key == "x-metarproxy") {
348 virtual void gotBodyData(const char* s, int n)
350 _metar += string(s, n);
353 virtual void responseComplete()
355 if (responseCode() == 200) {
356 _metarDataHandler->handleMetarData( _metar);
358 SG_LOG(SG_ENVIRONMENT, SG_WARN, "metar download failed:" << url() << ": reason:" << responseReason());
362 bool fromMetarProxy() const
363 { return _fromProxy; }
367 MetarDataHandler * _metarDataHandler;
371 SG_LOG(SG_ENVIRONMENT, SG_INFO,
372 "NoaaMetarRealWxController::update(): spawning load request for station-id '" << id << "'" );
373 _http.makeRequest(new NoaaMetarGetRequest(metarDataHandler, id));
376 /* -------------------------------------------------------------------------------- */
378 RealWxController * RealWxController::createInstance( SGPropertyNode_ptr rootNode )
380 // string dataSource = rootNode->getStringValue("data-source", "noaa" );
381 // if( dataSource == "nwx" ) {
382 // return new NwxMetarRealWxController( rootNode );
384 return new NoaaMetarRealWxController( rootNode );
388 /* -------------------------------------------------------------------------------- */
390 } // namespace Environment