From: James Turner Date: Sun, 17 Mar 2013 13:14:08 +0000 (+0000) Subject: Fix max-metar-age, bug #1076. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=c6221b2c84f47041ef92aff2a2cfffee25b84a73;p=flightgear.git Fix max-metar-age, bug #1076. Will merge to 2.10 branch once verified. --- diff --git a/src/Environment/metarproperties.cxx b/src/Environment/metarproperties.cxx index 944307b01..816ca5e3c 100644 --- a/src/Environment/metarproperties.cxx +++ b/src/Environment/metarproperties.cxx @@ -183,21 +183,40 @@ MetarProperties::~MetarProperties() static const double thickness_value[] = { 0, 65, 600, 750, 1000 }; -void MetarProperties::set_metar( const char * metar ) +const char* MetarProperties::get_metar() const +{ + if (!_metar) return ""; + return _metar->getData(); +} + +void MetarProperties::set_metar( const char * metarString ) { - _metar = metar; - SGSharedPtr m; + if ((metarString == NULL) || (strlen(metarString) == 0)) { + setMetar(m); + return; + } + try { - m = new FGMetar( _metar ); + m = new FGMetar( metarString ); } catch( sg_io_exception ) { - SG_LOG( SG_ENVIRONMENT, SG_WARN, "Can't parse metar: " << _metar ); + SG_LOG( SG_ENVIRONMENT, SG_WARN, "Can't parse metar: " << metarString ); _metarValidNode->setBoolValue(false); return; } - + + setMetar(m); +} + +void MetarProperties::setMetar( SGSharedPtr m ) +{ + _metar = m; _decoded.clear(); + if (!m) { + return; + } + const vector weather = m->getWeather(); for( vector::const_iterator it = weather.begin(); it != weather.end(); ++it ) { if( false == _decoded.empty() ) _decoded.append(", "); diff --git a/src/Environment/metarproperties.hxx b/src/Environment/metarproperties.hxx index eac1af780..336ba406e 100644 --- a/src/Environment/metarproperties.hxx +++ b/src/Environment/metarproperties.hxx @@ -27,6 +27,8 @@ #include #include +class FGMetar; + namespace Environment { class MagneticVariation; @@ -41,11 +43,12 @@ public: virtual bool isValid() const { return _metarValidNode->getBoolValue(); } virtual const std::string & getStationId() const { return _station_id; } virtual void setStationId( const std::string & value ); - virtual void setMetar( const std::string & value ) { set_metar( value.c_str() ); } + virtual void setMetar(SGSharedPtr m); private: - const char * get_metar() const { return _metar.c_str(); } + const char * get_metar() const; void set_metar( const char * metar ); + const char * get_station_id() const { return _station_id.c_str(); } void set_station_id( const char * value ); const char * get_decoded() const { return _decoded.c_str(); } @@ -60,9 +63,10 @@ private: void set_base_wind_dir( double value ); void set_wind_speed( double value ); + SGSharedPtr _metar; SGPropertyNode_ptr _rootNode; SGPropertyNode_ptr _metarValidNode; - std::string _metar; + std::string _station_id; double _station_elevation; double _station_latitude; diff --git a/src/Environment/realwx_ctrl.cxx b/src/Environment/realwx_ctrl.cxx index f7d09fba7..056a0bb6a 100644 --- a/src/Environment/realwx_ctrl.cxx +++ b/src/Environment/realwx_ctrl.cxx @@ -63,7 +63,7 @@ public: class LiveMetarProperties : public MetarProperties, MetarDataHandler { public: - LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ); + LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester, int maxAge ); virtual ~LiveMetarProperties(); virtual void update( double dt ); @@ -81,15 +81,17 @@ private: double _timeToLive; double _pollingTimer; MetarRequester * _metarRequester; + int _maxAge; }; typedef SGSharedPtr LiveMetarProperties_ptr; -LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ) : +LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester, int maxAge ) : MetarProperties( rootNode ), _timeToLive(0.0), _pollingTimer(0.0), - _metarRequester(metarRequester) + _metarRequester(metarRequester), + _maxAge(maxAge) { _tiedProperties.Tie("time-to-live", &_timeToLive ); } @@ -115,9 +117,25 @@ void LiveMetarProperties::update( double dt ) void LiveMetarProperties::handleMetarData( const std::string & data ) { - SG_LOG( SG_ENVIRONMENT, SG_INFO, "LiveMetarProperties::handleMetarData() received METAR for " << getStationId() << ": " << data ); + SG_LOG( SG_ENVIRONMENT, SG_DEBUG, "LiveMetarProperties::handleMetarData() received METAR for " << getStationId() << ": " << data ); _timeToLive = DEFAULT_TIME_TO_LIVE_SECONDS; - setMetar( data ); + + SGSharedPtr m; + try { + m = new FGMetar(data.c_str()); + } + catch( sg_io_exception ) { + SG_LOG( SG_ENVIRONMENT, SG_WARN, "Can't parse metar: " << data ); + return; + } + + if (_maxAge && (m->getAge_min() > _maxAge)) { + // METAR is older than max-age, ignore + SG_LOG( SG_ENVIRONMENT, SG_DEBUG, "Ignoring outdated METAR for " << getStationId()); + return; + } + + setMetar( m ); } /* -------------------------------------------------------------------------------- */ @@ -216,7 +234,9 @@ BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode, Metar { // at least instantiate MetarProperties for /environment/metar _metarProperties.push_back( new LiveMetarProperties( - fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ), metarRequester )); + fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ), + metarRequester, + getMetarMaxAgeMin())); BOOST_FOREACH( SGPropertyNode_ptr n, rootNode->getChildren("metar") ) { SGPropertyNode_ptr metarNode = fgGetNode( n->getStringValue(), true ); @@ -300,7 +320,7 @@ void BasicRealWxController::addMetarAtPath(const string& propPath, const string& SGPropertyNode_ptr metarNode = fgGetNode(propPath, true); SG_LOG( SG_ENVIRONMENT, SG_INFO, "Adding metar properties at " << propPath ); - LiveMetarProperties_ptr p(new LiveMetarProperties( metarNode, _requester )); + LiveMetarProperties_ptr p(new LiveMetarProperties( metarNode, _requester, getMetarMaxAgeMin() )); _metarProperties.push_back(p); p->setStationId(icao); }