]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/realwx_ctrl.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / Environment / realwx_ctrl.cxx
index a481268ea069075a9e3253e1f33115a87461f1ae..0e38d9355d3d6fcf8c5b7cbf295bcc8b897bf49b 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 <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 <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);
-    }
-}
 
 /* -------------------------------------------------------------------------------- */
 
@@ -143,13 +127,14 @@ public:
 
     virtual void init ();
     virtual void reinit ();
-
+    virtual void shutdown ();
+    
 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(); }
 
@@ -162,8 +147,7 @@ protected:
     simgear::TiedPropertyList _tiedProperties;
     typedef std::vector<LiveMetarProperties_ptr> MetarPropertiesList;
     MetarPropertiesList _metarProperties;
-private:
-    double _positionTimeToLive;
+
 };
 
 /* -------------------------------------------------------------------------------- */
@@ -178,17 +162,29 @@ 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)
+  __enabled(false)
 {
     // at least instantiate MetarProperties for /environment/metar
     _metarProperties.push_back( new LiveMetarProperties( 
             fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ), metarRequester ));
 
     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 );
+
+       // check for duplicate entries
+       bool existingElement = false;
+       BOOST_FOREACH( LiveMetarProperties_ptr p, _metarProperties ) {
+         if( p->get_root_node()->getPath() == metarNode->getPath() ) {
+           existingElement = true;
+           break;
+         }
+       }
+
+       if( existingElement )
+         continue;
+
+       SG_LOG( SG_ENVIRONMENT, SG_INFO, "Adding metar properties at " << metarNode->getPath() );
+        _metarProperties.push_back( new LiveMetarProperties( metarNode, metarRequester ));
     }
 }
 
@@ -200,12 +196,20 @@ void BasicRealWxController::init()
 {
     __enabled = false;
     update(0); // fetch data ASAP
+    
+    globals->get_event_mgr()->addTask("checkNearbyMetar", this,
+                                      &BasicRealWxController::checkNearbyMetar, 60 );
 }
 
 void BasicRealWxController::reinit()
 {
     __enabled = false;
 }
+    
+void BasicRealWxController::shutdown()
+{
+    globals->get_event_mgr()->removeTask("checkNearbyMetar");
+}
 
 void BasicRealWxController::bind()
 {
@@ -230,50 +234,43 @@ void BasicRealWxController::update( double dt )
       p->update(dt);
     }
 
-    update( firstIteration, dt );
     __enabled = true;
   } else {
     __enabled = false;
   }
 }
 
-void BasicRealWxController::update( bool first, double dt )
+void BasicRealWxController::checkNearbyMetar()
 {
-    _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;
-            }
-
-            SG_LOG(SG_ENVIRONMENT, SG_INFO, 
-                "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]->setTimeToLive( 0.0 );
-            }
-        }
-        catch( sg_exception & ) {
-            return;
-        }
+    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]->setTimeToLive( 0.0 );
+      }
+    }
+    catch( sg_exception & ) {
+      return;
     }
+    
 }
 
 /* -------------------------------------------------------------------------------- */
@@ -281,14 +278,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 +291,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 +315,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,14 +338,14 @@ 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());
               }
           }
 
-          bool fromMetarProxy() const
-          { return _fromProxy; }
+//          bool fromMetarProxy() const
+//          { return _fromProxy; }
     private:  
         string _metar;
         bool _fromProxy;
@@ -370,7 +355,7 @@ void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandle
 
     SG_LOG(SG_ENVIRONMENT, SG_INFO, 
         "NoaaMetarRealWxController::update(): spawning load request for station-id '" << id << "'" );
-    _http.makeRequest(new NoaaMetarGetRequest(metarDataHandler, id));
+    FGHTTPClient::instance()->makeRequest(new NoaaMetarGetRequest(metarDataHandler, id));
 }
 
 /* -------------------------------------------------------------------------------- */