protected:
void bind();
void unbind();
+ void update( double dt );
+
+ virtual void update( bool first, double dt ) = 0;
long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); }
SGPropertyNode_ptr _max_age_n;
bool _enabled;
+ bool __enabled;
TiedPropertyList _tiedProperties;
MetarProperties _metarProperties;
};
_ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )),
_max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ),
_enabled(true),
+ __enabled(false),
_metarProperties( fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) )
{
}
void BasicRealWxController::init()
{
+ __enabled = false;
update(0); // fetch data ASAP
}
void BasicRealWxController::reinit()
{
+ __enabled = false;
}
void BasicRealWxController::bind()
_tiedProperties.Untie();
}
+void BasicRealWxController::update( double dt )
+{
+ if( _enabled ) {
+ update( !__enabled, dt );
+ __enabled = true;
+ } else {
+ __enabled = false;
+ }
+}
+
/* -------------------------------------------------------------------------------- */
class NoaaMetarRealWxController : public BasicRealWxController {
public:
NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
virtual ~NoaaMetarRealWxController();
- virtual void update (double delta_time_sec);
+ virtual void update (bool first, double delta_time_sec);
class MetarLoadRequest {
public:
#if defined(ENABLE_THREADS)
class MetarLoadThread : public OpenThreads::Thread {
public:
- MetarLoadThread( long maxAge );
- void requestMetar( const MetarLoadRequest & metarRequest );
- bool hasMetar() { return _responseQueue.size() > 0; }
- string getMetar() { return _responseQueue.pop(); }
- virtual void run();
+ MetarLoadThread( long maxAge );
+ void requestMetar( const MetarLoadRequest & metarRequest, bool background = true );
+ bool hasMetar() { return _responseQueue.size() > 0; }
+ string getMetar() { return _responseQueue.pop(); }
+ virtual void run();
private:
+ void fetch( const MetarLoadRequest & );
long _maxAge;
SGBlockingQueue <MetarLoadRequest> _requestQueue;
SGBlockingQueue <string> _responseQueue;
#endif // ENABLE_THREADS
}
-void NoaaMetarRealWxController::update( double dt )
+void NoaaMetarRealWxController::update( bool first, double dt )
{
- if( !_enabled )
- return;
-
- if( _metarLoadThread->hasMetar() ) {
- string metar = _metarLoadThread->getMetar();
- SG_LOG( SG_ALL, SG_ALERT, "NoaaMetarRwalWxController::update() received METAR " << metar );
- _metarDataNode->setStringValue( metar );
- }
-
_metarTimeToLive -= dt;
_positionTimeToLive -= dt;
_minimumRequestInterval -= dt;
if( !valid ) {
if( _minimumRequestInterval <= 0 && stationId.length() > 0 ) {
MetarLoadRequest request( stationId );
- _metarLoadThread->requestMetar( request );
+ // load the first metar in the foreground to make sure a metar is received
+ // before the automatic runway selection code runs. All subsequent calls
+ // run in the background
+ _metarLoadThread->requestMetar( request, !first );
_minimumRequestInterval = 10;
}
}
+ if( _metarLoadThread->hasMetar() ) {
+ string metar = _metarLoadThread->getMetar();
+ SG_LOG( SG_ALL, SG_ALERT, "NoaaMetarRwalWxController::update() received METAR " << metar );
+ _metarDataNode->setStringValue( metar );
+ }
+
+
}
/* -------------------------------------------------------------------------------- */
{
}
-void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest )
+void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest, bool background )
{
- if( _requestQueue.size() > 10 ) {
- SG_LOG(SG_ALL,SG_ALERT,
- "NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping "
- << metarRequest._stationId );
- return;
- }
+ if( background ) {
+ if( _requestQueue.size() > 10 ) {
+ SG_LOG(SG_ALL,SG_ALERT,
+ "NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping "
+ << metarRequest._stationId );
+ return;
+ }
- _requestQueue.push( metarRequest );
+ _requestQueue.push( metarRequest );
+ } else {
+ fetch( metarRequest );
+ }
}
void NoaaMetarRealWxController::MetarLoadThread::run()
if( request._stationId.size() == 0 )
break;
- SGSharedPtr<FGMetar> result = NULL;
+ fetch( request );
+ }
+}
- 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::MetarLoadThread::fetch( const MetarLoadRequest & request )
+{
+ SGSharedPtr<FGMetar> result = NULL;
- string reply = result->getData();
- std::replace(reply.begin(), reply.end(), '\n', ' ');
- string metar = simgear::strutils::strip( reply );
+ 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() );
+ return;
+ }
- if( metar.empty() ) {
- SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for "
- << request._stationId );
- }
+ string reply = result->getData();
+ std::replace(reply.begin(), reply.end(), '\n', ' ');
+ string metar = simgear::strutils::strip( reply );
- if( _maxAge && result->getAge_min() > _maxAge ) {
- SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR "
- << metar );
- return;
- }
+ if( metar.empty() ) {
+ SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for "
+ << request._stationId );
+ return;
+ }
- _responseQueue.push( metar );
+ if( _maxAge && result->getAge_min() > _maxAge ) {
+ SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR "
+ << metar );
+ return;
}
+
+ _responseQueue.push( metar );
}
#endif