X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FEnvironment%2Frealwx_ctrl.cxx;h=fcc73550a5e090da6af3372c3b5f5f6d0a190cce;hb=9d067bc89023c5127b263603316dcbda71182aeb;hp=e555cb6b4d858c3137cf0a86f5f33cb0a80769da;hpb=9ab41ea15809c02ba46bf9e6911fbb1aa808b8b1;p=flightgear.git diff --git a/src/Environment/realwx_ctrl.cxx b/src/Environment/realwx_ctrl.cxx index e555cb6b4..fcc73550a 100644 --- a/src/Environment/realwx_ctrl.cxx +++ b/src/Environment/realwx_ctrl.cxx @@ -25,42 +25,27 @@ #endif #include "realwx_ctrl.hxx" -#include "metarproperties.hxx" -#include "metarairportfilter.hxx" -#include "fgmetar.hxx" - -#include
+#include #include +#include #include #include #include -#include #include #include #include +#include -#include +#include "metarproperties.hxx" +#include "metarairportfilter.hxx" +#include "fgmetar.hxx" +#include +#include
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); - } -} /* -------------------------------------------------------------------------------- */ @@ -146,6 +131,15 @@ public: 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(); @@ -160,13 +154,50 @@ protected: SGPropertyNode_ptr _max_age_n; bool _enabled; - bool __enabled; + bool _wasEnabled; simgear::TiedPropertyList _tiedProperties; typedef std::vector 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 @@ -179,26 +210,30 @@ 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) + _wasEnabled(false), + _requester(metarRequester) { // 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 ); + 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; update(0); // fetch data ASAP globals->get_event_mgr()->addTask("checkNearbyMetar", this, @@ -207,7 +242,7 @@ void BasicRealWxController::init() void BasicRealWxController::reinit() { - __enabled = false; + _wasEnabled = false; } void BasicRealWxController::shutdown() @@ -229,7 +264,7 @@ 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) { @@ -238,12 +273,49 @@ void BasicRealWxController::update( double dt ) p->update(dt); } - __enabled = true; + _wasEnabled = true; } else { - __enabled = false; + _wasEnabled = false; } } +void BasicRealWxController::addMetarAtPath(const string& propPath, const string& icao) +{ + // 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->setTimeToLive(0.0); + } + + return; + } + } // 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 )); + _metarProperties.push_back(p); + p->setStationId(icao); +} + +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 { @@ -282,14 +354,12 @@ void BasicRealWxController::checkNearbyMetar() 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 ) : @@ -297,16 +367,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 @@ -354,36 +414,32 @@ 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; 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::instance()->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 ); } /* -------------------------------------------------------------------------------- */