]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/realwx_ctrl.cxx
Fix max-metar-age, bug #1076.
[flightgear.git] / src / Environment / realwx_ctrl.cxx
index a481268ea069075a9e3253e1f33115a87461f1ae..056a0bb6a8d38d6169ef2cf12679861b192e7ce2 100644 (file)
 #endif
 
 #include "realwx_ctrl.hxx"
-#include "metarproperties.hxx"
-#include "metarairportfilter.hxx"
-#include "fgmetar.hxx"
-
-#include <Main/fg_props.hxx>
 
+#include <algorithm>
 #include <boost/foreach.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
 
 #include <simgear/structure/exception.hxx>
 #include <simgear/misc/strutils.hxx>
 #include <simgear/props/tiedpropertylist.hxx>
-#include <simgear/io/HTTPClient.hxx>
 #include <simgear/io/HTTPRequest.hxx>
 #include <simgear/timing/sg_time.hxx>
+#include <simgear/structure/event_mgr.hxx>
+#include <simgear/structure/commands.hxx>
 
-#include <algorithm>
+#include "metarproperties.hxx"
+#include "metarairportfilter.hxx"
+#include "fgmetar.hxx"
+#include <Network/HTTPClient.hxx>
+#include <Main/fg_props.hxx>
 
 namespace Environment {
-/* -------------------------------------------------------------------------------- */
 
-class FGHTTPClient : public simgear::HTTP::Client {
-public:
-    FGHTTPClient();
-};
-
-FGHTTPClient::FGHTTPClient()
-{
-    string proxyHost(fgGetString("/sim/presets/proxy/host"));
-    int proxyPort(fgGetInt("/sim/presets/proxy/port"));
-    string proxyAuth(fgGetString("/sim/presets/proxy/auth"));
-    
-    if (!proxyHost.empty()) {
-        setProxy(proxyHost, proxyPort, proxyAuth);
-    }
-}
 
 /* -------------------------------------------------------------------------------- */
 
@@ -77,12 +63,13 @@ 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 );
 
     virtual double getTimeToLive() const { return _timeToLive; }
-    virtual void setTimeToLive( double value ) { _timeToLive = value; }
+    virtual void resetTimeToLive()
+    { _timeToLive = 0.00; _pollingTimer = 0.0; }
 
     // implementation of MetarDataHandler
     virtual void handleMetarData( const std::string & data );
@@ -94,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 );
 }
@@ -116,7 +105,7 @@ void LiveMetarProperties::update( double dt )
 {
     _timeToLive -= dt;
     _pollingTimer -= dt;
-    if( _timeToLive < 0.0 ) {
+    if( _timeToLive <= 0.0 ) {
         _timeToLive = 0.0;
         std::string stationId = getStationId();
         if( stationId.empty() ) return;
@@ -128,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 );
 }
 
 /* -------------------------------------------------------------------------------- */
@@ -143,13 +148,23 @@ public:
 
     virtual void init ();
     virtual void reinit ();
-
+    virtual void shutdown ();
+    
+    /**
+     * Create a metar-property binding at the specified property path,
+     * and initiate a request for the specified station-ID (which may be
+     * empty). If the property path is already mapped, the station ID
+     * will be updated.
+     */
+    void addMetarAtPath(const string& propPath, const string& icao);
+  
+    void removeMetarAtPath(const string& propPath);
 protected:
     void bind();
     void unbind();
     void update( double dt );
 
-    virtual void update (bool first, double delta_time_sec);
+    void checkNearbyMetar();
 
     long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); }
 
@@ -158,14 +173,50 @@ protected:
     SGPropertyNode_ptr _max_age_n;
 
     bool _enabled;
-    bool __enabled;
+    bool _wasEnabled;
     simgear::TiedPropertyList _tiedProperties;
     typedef std::vector<LiveMetarProperties_ptr> MetarPropertiesList;
     MetarPropertiesList _metarProperties;
-private:
-    double _positionTimeToLive;
+    MetarRequester* _requester;
+
 };
 
+static bool commandRequestMetar(const SGPropertyNode* arg)
+{
+  SGSubsystemGroup* envMgr = (SGSubsystemGroup*) globals->get_subsystem("environment");
+  if (!envMgr) {
+    return false;
+  }
+  
+  BasicRealWxController* self = (BasicRealWxController*) envMgr->get_subsystem("realwx");
+  if (!self) {
+    return false;
+  }
+  
+  string icao(arg->getStringValue("station"));
+  boost::to_upper(icao);
+  string path = arg->getStringValue("path");
+  self->addMetarAtPath(path, icao);
+  return true;
+}
+  
+static bool commandClearMetar(const SGPropertyNode* arg)
+{
+  SGSubsystemGroup* envMgr = (SGSubsystemGroup*) globals->get_subsystem("environment");
+  if (!envMgr) {
+    return false;
+  }
+  
+  BasicRealWxController* self = (BasicRealWxController*) envMgr->get_subsystem("realwx");
+  if (!self) {
+    return false;
+  }
+  
+  string path = arg->getStringValue("path");
+  self->removeMetarAtPath(path);
+  return true;
+}
+  
 /* -------------------------------------------------------------------------------- */
 /*
 Properties
@@ -178,33 +229,50 @@ BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode, Metar
   _ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )),
   _max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ),
   _enabled(true),
-  __enabled(false),
-  _positionTimeToLive(0.0)
+  _wasEnabled(false),
+  _requester(metarRequester)
 {
     // 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") ) {
-       SG_LOG( SG_ENVIRONMENT, SG_INFO, "Adding metar properties at " << n->getStringValue() );
-        _metarProperties.push_back( new LiveMetarProperties( 
-            fgGetNode( n->getStringValue(), true ), metarRequester ));
+        SGPropertyNode_ptr metarNode = fgGetNode( n->getStringValue(), true );
+        addMetarAtPath(metarNode->getPath(), "");
     }
+  
+    SGCommandMgr::instance()->addCommand("request-metar", commandRequestMetar);
+    SGCommandMgr::instance()->addCommand("clear-metar", commandClearMetar);
 }
 
 BasicRealWxController::~BasicRealWxController()
 {
+  //SGCommandMgr::instance()->removeCommand("request-metar");
 }
 
 void BasicRealWxController::init()
 {
-    __enabled = false;
+    _wasEnabled = false;
+    
+    checkNearbyMetar();
     update(0); // fetch data ASAP
+    
+    globals->get_event_mgr()->addTask("checkNearbyMetar", this,
+                                      &BasicRealWxController::checkNearbyMetar, 60 );
 }
 
 void BasicRealWxController::reinit()
 {
-    __enabled = false;
+    _wasEnabled = false;
+    checkNearbyMetar();
+    update(0); // fetch data ASAP
+}
+    
+void BasicRealWxController::shutdown()
+{
+    globals->get_event_mgr()->removeTask("checkNearbyMetar");
 }
 
 void BasicRealWxController::bind()
@@ -219,61 +287,90 @@ void BasicRealWxController::unbind()
 }
 
 void BasicRealWxController::update( double dt )
-{
+{  
   if( _enabled ) {
-    bool firstIteration = !__enabled; // first iteration after being enabled?
-
+    bool firstIteration = !_wasEnabled;
     // clock tick for every METAR in stock
     BOOST_FOREACH(LiveMetarProperties* p, _metarProperties) {
       // first round? All received METARs are outdated
-      if( firstIteration ) p->setTimeToLive( 0.0 );
+      if( firstIteration ) p->resetTimeToLive();
       p->update(dt);
     }
 
-    update( firstIteration, dt );
-    __enabled = true;
+    _wasEnabled = true;
   } else {
-    __enabled = false;
+    _wasEnabled = false;
   }
 }
 
-void BasicRealWxController::update( bool first, double dt )
+void BasicRealWxController::addMetarAtPath(const string& propPath, const string& icao)
 {
-    _positionTimeToLive -= dt;
-
-    if( _positionTimeToLive <= 0.0 ) {
-
-
-        try {
-            const SGGeod & pos = globals->get_aircraft_position();
-
-            // check nearest airport
-            SG_LOG(SG_ENVIRONMENT, SG_INFO, "NoaaMetarRealWxController::update(): (re) checking nearby airport with METAR" );
-            _positionTimeToLive = 60.0;
-
-            FGAirport * nearestAirport = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
-            if( nearestAirport == NULL ) {
-                SG_LOG(SG_ENVIRONMENT,SG_WARN,"RealWxController::update can't find airport with METAR within 10000NM"  );
-                return;
-            }
+  // check for duplicate entries
+  BOOST_FOREACH( LiveMetarProperties_ptr p, _metarProperties ) {
+    if( p->get_root_node()->getPath() == propPath ) {
+      // already exists
+      if (p->getStationId() != icao) {
+        p->setStationId(icao);
+        p->resetTimeToLive();
+      }
+      
+      return;
+    }
+  } // of exitsing metar properties iteration
 
-            SG_LOG(SG_ENVIRONMENT, SG_INFO, 
-                "NoaaMetarRealWxController::update(): nearest airport with METAR is: " << nearestAirport->ident() );
+  SGPropertyNode_ptr metarNode = fgGetNode(propPath, true);
+  SG_LOG( SG_ENVIRONMENT, SG_INFO, "Adding metar properties at " << propPath );
+  LiveMetarProperties_ptr p(new LiveMetarProperties( metarNode, _requester, getMetarMaxAgeMin() ));
+  _metarProperties.push_back(p);
+  p->setStationId(icao);
+}
 
-            // if it has changed, invalidate the associated METAR
-            if( _metarProperties[0]->getStationId() != nearestAirport->ident() ) {
-                SG_LOG(SG_ENVIRONMENT, SG_INFO, 
-                    "NoaaMetarRealWxController::update(): nearest airport with METAR has changed. Old: '" << 
-                    _metarProperties[0]->getStationId() <<
-                    "', new: '" << nearestAirport->ident() << "'" );
-                _metarProperties[0]->setStationId( nearestAirport->ident() );
-                _metarProperties[0]->setTimeToLive( 0.0 );
-            }
-        }
-        catch( sg_exception & ) {
-            return;
-        }
+void BasicRealWxController::removeMetarAtPath(const string &propPath)
+{
+  MetarPropertiesList::iterator it = _metarProperties.begin();
+  for (; it != _metarProperties.end(); ++it) {
+    LiveMetarProperties_ptr p(*it);
+    if( p->get_root_node()->getPath() == propPath ) {
+      _metarProperties.erase(it);
+      // final ref will drop, and delete the MetarProperties, when we return
+      return;
     }
+  }
+  
+  SG_LOG(SG_ENVIRONMENT, SG_WARN, "no metar properties at " << propPath);
+}
+  
+void BasicRealWxController::checkNearbyMetar()
+{
+    try {
+      const SGGeod & pos = globals->get_aircraft_position();
+
+      // check nearest airport
+      SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "NoaaMetarRealWxController::update(): (re) checking nearby airport with METAR" );
+
+      FGAirport * nearestAirport = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
+      if( nearestAirport == NULL ) {
+          SG_LOG(SG_ENVIRONMENT,SG_WARN,"RealWxController::update can't find airport with METAR within 10000NM"  );
+          return;
+      }
+
+      SG_LOG(SG_ENVIRONMENT, SG_DEBUG, 
+          "NoaaMetarRealWxController::update(): nearest airport with METAR is: " << nearestAirport->ident() );
+
+      // if it has changed, invalidate the associated METAR
+      if( _metarProperties[0]->getStationId() != nearestAirport->ident() ) {
+          SG_LOG(SG_ENVIRONMENT, SG_INFO, 
+              "NoaaMetarRealWxController::update(): nearest airport with METAR has changed. Old: '" << 
+              _metarProperties[0]->getStationId() <<
+              "', new: '" << nearestAirport->ident() << "'" );
+          _metarProperties[0]->setStationId( nearestAirport->ident() );
+          _metarProperties[0]->resetTimeToLive();
+      }
+    }
+    catch( sg_exception & ) {
+      return;
+    }
+    
 }
 
 /* -------------------------------------------------------------------------------- */
@@ -281,14 +378,12 @@ void BasicRealWxController::update( bool first, double dt )
 class NoaaMetarRealWxController : public BasicRealWxController, MetarRequester {
 public:
     NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
-    virtual ~NoaaMetarRealWxController();
-    virtual void update( double dt );
 
     // implementation of MetarRequester
     virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id );
 
 private:
-    FGHTTPClient _http;
+    
 };
 
 NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) :
@@ -296,16 +391,6 @@ NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNod
 {
 }
 
-NoaaMetarRealWxController::~NoaaMetarRealWxController()
-{
-}
-
-void NoaaMetarRealWxController::update( double dt )
-{
-    _http.update();
-    BasicRealWxController::update( dt );
-}
-
 void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandler, const std::string & id )
 {
     class NoaaMetarGetRequest : public simgear::HTTP::Request
@@ -330,7 +415,7 @@ void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandle
               string reply;
 
               if( name == "X-TIME" ) {
-                  ostringstream buf;
+                  std::ostringstream buf;
                   buf <<  globals->get_time_params()->get_cur_time();
                   reply = buf.str();
               }
@@ -353,36 +438,40 @@ void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandle
           virtual void responseComplete()
           {
               if (responseCode() == 200) {
-                  _metarDataHandler->handleMetarData( _metar);
+                  _metarDataHandler->handleMetarData( simgear::strutils::simplify(_metar) );
               } else {
                   SG_LOG(SG_ENVIRONMENT, SG_WARN, "metar download failed:" << url() << ": reason:" << responseReason());
               }
           }
+        
+        virtual void failed()
+        {
+            SG_LOG(SG_ENVIRONMENT, SG_INFO, "metar download failure");
+        }
 
-          bool fromMetarProxy() const
-          { return _fromProxy; }
+//          bool fromMetarProxy() const
+//          { return _fromProxy; }
     private:  
         string _metar;
         bool _fromProxy;
         MetarDataHandler * _metarDataHandler;
     };
 
+    string upperId = boost::to_upper_copy(id);
 
-    SG_LOG(SG_ENVIRONMENT, SG_INFO, 
-        "NoaaMetarRealWxController::update(): spawning load request for station-id '" << id << "'" );
-    _http.makeRequest(new NoaaMetarGetRequest(metarDataHandler, id));
+    SG_LOG(SG_ENVIRONMENT, SG_INFO,
+        "NoaaMetarRealWxController::update(): spawning load request for station-id '" << upperId << "'" );
+    FGHTTPClient* http = static_cast<FGHTTPClient*>(globals->get_subsystem("http"));
+    if (http) {
+        http->makeRequest(new NoaaMetarGetRequest(metarDataHandler, upperId));
+    }
 }
 
 /* -------------------------------------------------------------------------------- */
 
 RealWxController * RealWxController::createInstance( SGPropertyNode_ptr rootNode )
 {
-//    string dataSource = rootNode->getStringValue("data-source", "noaa" );
-//    if( dataSource == "nwx" ) {
-//      return new NwxMetarRealWxController( rootNode );
-//    } else {
-      return new NoaaMetarRealWxController( rootNode );
-//    }
+  return new NoaaMetarRealWxController( rootNode );
 }
 
 /* -------------------------------------------------------------------------------- */