]> git.mxchange.org Git - flightgear.git/commitdiff
Fix max-metar-age, bug #1076.
authorJames Turner <zakalawe@mac.com>
Sun, 17 Mar 2013 13:14:08 +0000 (13:14 +0000)
committerJames Turner <zakalawe@mac.com>
Sun, 17 Mar 2013 13:14:08 +0000 (13:14 +0000)
Will merge to 2.10 branch once verified.

src/Environment/metarproperties.cxx
src/Environment/metarproperties.hxx
src/Environment/realwx_ctrl.cxx

index 944307b0169825e7e3afdbeb89a193d0aae35b8e..816ca5e3cb9a277f9f6f4a91a093799f07e1d15d 100644 (file)
@@ -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<FGMetar> 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<FGMetar> m )
+{
+    _metar = m;
     _decoded.clear();
+    if (!m) {
+        return;
+    }
+    
     const vector<string> weather = m->getWeather();
     for( vector<string>::const_iterator it = weather.begin(); it != weather.end(); ++it ) {
         if( false == _decoded.empty() ) _decoded.append(", ");
index eac1af780003ba343af1db0bf4bc8d20a501e5b0..336ba406e56be6541f2e6e846806d61a91760494 100644 (file)
@@ -27,6 +27,8 @@
 #include <simgear/props/props.hxx>
 #include <simgear/props/tiedpropertylist.hxx>
 
+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<FGMetar> 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<FGMetar> _metar;
     SGPropertyNode_ptr _rootNode;
     SGPropertyNode_ptr _metarValidNode;
-    std::string _metar;
+    
     std::string _station_id;
     double _station_elevation;
     double _station_latitude;
index f7d09fba72bd2bd60f5aea1af702cdeb9070a2da..056a0bb6a8d38d6169ef2cf12679861b192e7ce2 100644 (file)
@@ -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> 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<FGMetar> 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);
 }