]> 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 1c1131b047efe0fc267435ce16fde3c8d23631b3..056a0bb6a8d38d6169ef2cf12679861b192e7ce2 100644 (file)
@@ -1,7 +1,7 @@
 // realwx_ctrl.cxx -- Process real weather data
 //
 // Written by David Megginson, started February 2002.
-// Rewritten by Torsten Dreyer, August 2010
+// Rewritten by Torsten Dreyer, August 2010, August 2011
 //
 // Copyright (C) 2002  David Megginson - david@megginson.com
 //
 #endif
 
 #include "realwx_ctrl.hxx"
-#include "tiedpropertylist.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 <algorithm>
-#if defined(ENABLE_THREADS)
-#include <OpenThreads/Thread>
-#include <simgear/threads/SGQueue.hxx>
-#endif
+#include <simgear/props/tiedpropertylist.hxx>
+#include <simgear/io/HTTPRequest.hxx>
+#include <simgear/timing/sg_time.hxx>
+#include <simgear/structure/event_mgr.hxx>
+#include <simgear/structure/commands.hxx>
 
+#include "metarproperties.hxx"
+#include "metarairportfilter.hxx"
+#include "fgmetar.hxx"
+#include <Network/HTTPClient.hxx>
+#include <Main/fg_props.hxx>
 
 namespace Environment {
 
+
+/* -------------------------------------------------------------------------------- */
+
+class MetarDataHandler {
+public:
+    virtual void handleMetarData( const std::string & data ) = 0;
+};
+
+class MetarRequester {
+public:
+    virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id ) = 0;
+};
+
+/* -------------------------------------------------------------------------------- */
+
+class LiveMetarProperties : public MetarProperties, MetarDataHandler {
+public:
+    LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester, int maxAge );
+    virtual ~LiveMetarProperties();
+    virtual void update( double dt );
+
+    virtual double getTimeToLive() const { return _timeToLive; }
+    virtual void resetTimeToLive()
+    { _timeToLive = 0.00; _pollingTimer = 0.0; }
+
+    // implementation of MetarDataHandler
+    virtual void handleMetarData( const std::string & data );
+
+    static const unsigned MAX_POLLING_INTERVAL_SECONDS = 10;
+    static const unsigned DEFAULT_TIME_TO_LIVE_SECONDS = 900;
+
+private:
+    double _timeToLive;
+    double _pollingTimer;
+    MetarRequester * _metarRequester;
+    int _maxAge;
+};
+
+typedef SGSharedPtr<LiveMetarProperties> LiveMetarProperties_ptr;
+
+LiveMetarProperties::LiveMetarProperties( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester, int maxAge ) :
+    MetarProperties( rootNode ),
+    _timeToLive(0.0),
+    _pollingTimer(0.0),
+    _metarRequester(metarRequester),
+    _maxAge(maxAge)
+{
+    _tiedProperties.Tie("time-to-live", &_timeToLive );
+}
+
+LiveMetarProperties::~LiveMetarProperties()
+{
+    _tiedProperties.Untie();
+}
+
+void LiveMetarProperties::update( double dt )
+{
+    _timeToLive -= dt;
+    _pollingTimer -= dt;
+    if( _timeToLive <= 0.0 ) {
+        _timeToLive = 0.0;
+        std::string stationId = getStationId();
+        if( stationId.empty() ) return;
+        if( _pollingTimer > 0.0 ) return;
+        _metarRequester->requestMetar( this, stationId );
+        _pollingTimer = MAX_POLLING_INTERVAL_SECONDS;
+    }
+}
+
+void LiveMetarProperties::handleMetarData( const std::string & data )
+{
+    SG_LOG( SG_ENVIRONMENT, SG_DEBUG, "LiveMetarProperties::handleMetarData() received METAR for " << getStationId() << ": " << data );
+    _timeToLive = DEFAULT_TIME_TO_LIVE_SECONDS;
+    
+    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 );
+}
+
+/* -------------------------------------------------------------------------------- */
+
 class BasicRealWxController : public RealWxController
 {
 public:
-    BasicRealWxController( SGPropertyNode_ptr rootNode );
+    BasicRealWxController( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester );
     virtual ~BasicRealWxController ();
 
     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 );
+
+    void checkNearbyMetar();
+
+    long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); }
 
     SGPropertyNode_ptr _rootNode;
-    SGPropertyNode_ptr _longitude_n;
-    SGPropertyNode_ptr _latitude_n;
     SGPropertyNode_ptr _ground_elevation_n;
+    SGPropertyNode_ptr _max_age_n;
 
     bool _enabled;
-    TiedPropertyList _tiedProperties;
-    MetarProperties  _metarProperties;
+    bool _wasEnabled;
+    simgear::TiedPropertyList _tiedProperties;
+    typedef std::vector<LiveMetarProperties_ptr> MetarPropertiesList;
+    MetarPropertiesList _metarProperties;
+    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
@@ -73,27 +224,55 @@ Properties
  ~/metar[1..n]: string        Target property path for metar data
  */
 
-BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode ) :
+BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode, MetarRequester * metarRequester ) :
   _rootNode(rootNode),
-  _longitude_n( fgGetNode( "/position/longitude-deg", true )),
-  _latitude_n( fgGetNode( "/position/latitude-deg", true )),
   _ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )),
+  _max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ),
   _enabled(true),
-  _metarProperties( fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) )
+  _wasEnabled(false),
+  _requester(metarRequester)
 {
+    // at least instantiate MetarProperties for /environment/metar
+    _metarProperties.push_back( new LiveMetarProperties( 
+            fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ),
+            metarRequester,
+            getMetarMaxAgeMin()));
+
+    BOOST_FOREACH( SGPropertyNode_ptr n, rootNode->getChildren("metar") ) {
+        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()
 {
+    _wasEnabled = false;
+    
+    checkNearbyMetar();
     update(0); // fetch data ASAP
+    
+    globals->get_event_mgr()->addTask("checkNearbyMetar", this,
+                                      &BasicRealWxController::checkNearbyMetar, 60 );
 }
 
 void BasicRealWxController::reinit()
 {
+    _wasEnabled = false;
+    checkNearbyMetar();
+    update(0); // fetch data ASAP
+}
+    
+void BasicRealWxController::shutdown()
+{
+    globals->get_event_mgr()->removeTask("checkNearbyMetar");
 }
 
 void BasicRealWxController::bind()
@@ -107,193 +286,192 @@ void BasicRealWxController::unbind()
     _tiedProperties.Untie();
 }
 
-/* -------------------------------------------------------------------------------- */
-
-class NoaaMetarRealWxController : public BasicRealWxController {
-public:
-    NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
-    virtual ~NoaaMetarRealWxController();
-    virtual void update (double delta_time_sec);
-
-    class MetarLoadRequest {
-    public:
-        MetarLoadRequest( const string & stationId ) {
-            _stationId = stationId;
-            _proxyHost = fgGetNode("/sim/presets/proxy/host", true)->getStringValue();
-            _proxyPort = fgGetNode("/sim/presets/proxy/port", true)->getStringValue();
-            _proxyAuth = fgGetNode("/sim/presets/proxy/authentication", true)->getStringValue();
-        }
-        string _stationId;
-        string _proxyHost;
-        string _proxyPort;
-        string _proxyAuth;
-    private:
-    };
-private:
-    double _metarTimeToLive;
-    double _positionTimeToLive;
-    double _minimumRequestInterval;
-    
-    SGPropertyNode_ptr _metarDataNode;
-    SGPropertyNode_ptr _metarValidNode;
-    SGPropertyNode_ptr _metarStationIdNode;
-
-
-#if defined(ENABLE_THREADS)
-     class MetarLoadThread : public OpenThreads::Thread {
-     public:
-         MetarLoadThread( NoaaMetarRealWxController & controller );
-         void requestMetar( const MetarLoadRequest & metarRequest );
-         bool hasMetar() { return _responseQueue.size() > 0; }
-         string getMetar() { return _responseQueue.pop(); }
-         virtual void run();
-     private:
-        NoaaMetarRealWxController & _controller;
-        SGBlockingQueue <MetarLoadRequest> _requestQueue;
-        SGBlockingQueue <string> _responseQueue;
-     };
-
-     MetarLoadThread * _metarLoadThread;
-#endif
-};
+void BasicRealWxController::update( double dt )
+{  
+  if( _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->resetTimeToLive();
+      p->update(dt);
+    }
 
-NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) :
-  BasicRealWxController(rootNode),
-  _metarTimeToLive(0.0),
-  _positionTimeToLive(0.0),
-  _minimumRequestInterval(0.0),
-  _metarDataNode(_metarProperties.get_root_node()->getNode("data",true)),
-  _metarValidNode(_metarProperties.get_root_node()->getNode("valid",true)),
-  _metarStationIdNode(_metarProperties.get_root_node()->getNode("station-id",true))
-{
-#if defined(ENABLE_THREADS)
-    _metarLoadThread = new MetarLoadThread(*this);
-    _metarLoadThread->start();
-#endif
+    _wasEnabled = true;
+  } else {
+    _wasEnabled = false;
+  }
 }
 
-NoaaMetarRealWxController::~NoaaMetarRealWxController()
+void BasicRealWxController::addMetarAtPath(const string& propPath, const string& icao)
 {
-#if defined(ENABLE_THREADS)
-    if( _metarLoadThread ) {
-        MetarLoadRequest request("");
-        _metarLoadThread->requestMetar(request);
-        _metarLoadThread->join();
-        delete _metarLoadThread;
+  // 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;
     }
-#endif // ENABLE_THREADS
+  } // of exitsing metar properties iteration
+
+  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);
 }
 
-void NoaaMetarRealWxController::update( double dt )
+void BasicRealWxController::removeMetarAtPath(const string &propPath)
 {
-    if( !_enabled )
-        return;
-
-    if( _metarLoadThread->hasMetar() )
-        _metarDataNode->setStringValue( _metarLoadThread->getMetar() );
-
-    _metarTimeToLive -= dt;
-    _positionTimeToLive -= dt;
-    _minimumRequestInterval -= dt;
-
-    bool valid = _metarValidNode->getBoolValue();
-    string stationId = valid ? _metarStationIdNode->getStringValue() : "";
-
-
-    if( _metarTimeToLive <= 0.0 ) {
-        valid = false;
-        _metarTimeToLive = 900;
-        _positionTimeToLive = 0;
+  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;
     }
-
-    if( _positionTimeToLive <= 0.0 || valid == false ) {
-        _positionTimeToLive = 60.0;
-
-        SGGeod pos = SGGeod::fromDeg(_longitude_n->getDoubleValue(), _latitude_n->getDoubleValue());
-
-        FGAirport * nearestAirport = FGAirport::findClosest(pos, 10000.0, MetarAirportFilter::instance() );
-        if( nearestAirport == NULL ) {
-            SG_LOG(SG_ALL,SG_WARN,"RealWxController::update can't find airport with METAR within 10000NM"  );
-            return;
-        }
-
-        if( stationId != nearestAirport->ident() ) {
-            valid = false;
-            stationId = nearestAirport->ident();
-        }
-
+  }
+  
+  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();
+      }
     }
-
-    if( !valid ) {
-        if( _minimumRequestInterval <= 0 && stationId.length() > 0 ) {
-            MetarLoadRequest request( stationId );
-            _metarLoadThread->requestMetar( request );
-            _minimumRequestInterval = 10;
-        }
+    catch( sg_exception & ) {
+      return;
     }
-
+    
 }
 
 /* -------------------------------------------------------------------------------- */
 
-#if defined(ENABLE_THREADS)
-NoaaMetarRealWxController::MetarLoadThread::MetarLoadThread( NoaaMetarRealWxController & controller ) :
-  _controller(controller)
-{
-}
+class NoaaMetarRealWxController : public BasicRealWxController, MetarRequester {
+public:
+    NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
 
-void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest )
-{
-    if( _requestQueue.size() > 10 ) {
-        SG_LOG(SG_ALL,SG_ALERT,
-            "NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping " 
-            << metarRequest._stationId );
-        return;
-    }
+    // implementation of MetarRequester
+    virtual void requestMetar( MetarDataHandler * metarDataHandler, const std::string & id );
 
-    _requestQueue.push( metarRequest );
-}
+private:
+    
+};
 
-void NoaaMetarRealWxController::MetarLoadThread::run()
+NoaaMetarRealWxController::NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ) :
+  BasicRealWxController(rootNode, this )
 {
-    for( ;; ) {
-        const MetarLoadRequest request = _requestQueue.pop();
-
-        if( request._stationId.size() == 0 )
-            break;
-
-       SGSharedPtr<FGMetar> result = NULL;
+}
 
-        try {
-            result = new FGMetar( request._stationId, request._proxyHost, request._proxyPort, request._proxyAuth );
-        } catch (const sg_io_exception& e) {
-            SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): can't get METAR for " 
-                                        << request._stationId << ":" << e.getFormattedMessage().c_str() );
-            continue;
+void NoaaMetarRealWxController::requestMetar( MetarDataHandler * metarDataHandler, const std::string & id )
+{
+    class NoaaMetarGetRequest : public simgear::HTTP::Request
+    {
+    public:
+        NoaaMetarGetRequest(MetarDataHandler* metarDataHandler, const string& stationId ) :
+              Request("http://weather.noaa.gov/pub/data/observations/metar/stations/" + stationId + ".TXT"),
+              _fromProxy(false),
+              _metarDataHandler(metarDataHandler)
+          {
+          }
+
+          virtual string_list requestHeaders() const
+          {
+              string_list reply;
+              reply.push_back("X-TIME");
+              return reply;
+          }
+
+          virtual std::string header(const std::string& name) const
+          {
+              string reply;
+
+              if( name == "X-TIME" ) {
+                  std::ostringstream buf;
+                  buf <<  globals->get_time_params()->get_cur_time();
+                  reply = buf.str();
+              }
+
+              return reply;
+          }
+
+          virtual void responseHeader(const string& key, const string& value)
+          {
+              if (key == "x-metarproxy") {
+                  _fromProxy = true;
+              }
+          }
+
+          virtual void gotBodyData(const char* s, int n)
+          {
+              _metar += string(s, n);
+          }
+
+          virtual void responseComplete()
+          {
+              if (responseCode() == 200) {
+                  _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");
         }
 
-        if( result == NULL )
-            continue;
+//          bool fromMetarProxy() const
+//          { return _fromProxy; }
+    private:  
+        string _metar;
+        bool _fromProxy;
+        MetarDataHandler * _metarDataHandler;
+    };
+
+    string upperId = boost::to_upper_copy(id);
 
-        string reply = result->getData();
-        std::replace(reply.begin(), reply.end(), '\n', ' ');
-        string metar = simgear::strutils::strip( reply );
-        if( metar.length() > 0 )
-            _responseQueue.push( metar );
+    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));
     }
 }
-#endif
 
 /* -------------------------------------------------------------------------------- */
 
 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 );
 }
 
 /* -------------------------------------------------------------------------------- */