1
0
Fork 0

Fix timing issue on startup for auto runway selection

The automatic runway selection code in startup.nas depends
on a valid metar before /sim/signals/nasal-dir-initialized
is fired. If the METAR arrives after that signal, no automatic
runway selection is performed. This patch waits for a METAR
on the first update() loop of the subsystem.
This commit is contained in:
Torsten Dreyer 2010-10-11 21:21:53 +02:00
parent b9f2f432b3
commit 06fb956f2c

View file

@ -55,6 +55,9 @@ public:
protected: protected:
void bind(); void bind();
void unbind(); 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(); } long getMetarMaxAgeMin() const { return _max_age_n == NULL ? 0 : _max_age_n->getLongValue(); }
@ -65,6 +68,7 @@ protected:
SGPropertyNode_ptr _max_age_n; SGPropertyNode_ptr _max_age_n;
bool _enabled; bool _enabled;
bool __enabled;
TiedPropertyList _tiedProperties; TiedPropertyList _tiedProperties;
MetarProperties _metarProperties; MetarProperties _metarProperties;
}; };
@ -83,6 +87,7 @@ BasicRealWxController::BasicRealWxController( SGPropertyNode_ptr rootNode ) :
_ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )), _ground_elevation_n( fgGetNode( "/position/ground-elev-m", true )),
_max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ), _max_age_n( fgGetNode( "/environment/params/metar-max-age-min", false ) ),
_enabled(true), _enabled(true),
__enabled(false),
_metarProperties( fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) ) _metarProperties( fgGetNode( rootNode->getStringValue("metar", "/environment/metar"), true ) )
{ {
} }
@ -93,11 +98,13 @@ BasicRealWxController::~BasicRealWxController()
void BasicRealWxController::init() void BasicRealWxController::init()
{ {
__enabled = false;
update(0); // fetch data ASAP update(0); // fetch data ASAP
} }
void BasicRealWxController::reinit() void BasicRealWxController::reinit()
{ {
__enabled = false;
} }
void BasicRealWxController::bind() void BasicRealWxController::bind()
@ -111,13 +118,23 @@ void BasicRealWxController::unbind()
_tiedProperties.Untie(); _tiedProperties.Untie();
} }
void BasicRealWxController::update( double dt )
{
if( _enabled ) {
update( !__enabled, dt );
__enabled = true;
} else {
__enabled = false;
}
}
/* -------------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------------- */
class NoaaMetarRealWxController : public BasicRealWxController { class NoaaMetarRealWxController : public BasicRealWxController {
public: public:
NoaaMetarRealWxController( SGPropertyNode_ptr rootNode ); NoaaMetarRealWxController( SGPropertyNode_ptr rootNode );
virtual ~NoaaMetarRealWxController(); virtual ~NoaaMetarRealWxController();
virtual void update (double delta_time_sec); virtual void update (bool first, double delta_time_sec);
class MetarLoadRequest { class MetarLoadRequest {
public: public:
@ -146,12 +163,13 @@ private:
#if defined(ENABLE_THREADS) #if defined(ENABLE_THREADS)
class MetarLoadThread : public OpenThreads::Thread { class MetarLoadThread : public OpenThreads::Thread {
public: public:
MetarLoadThread( long maxAge ); MetarLoadThread( long maxAge );
void requestMetar( const MetarLoadRequest & metarRequest ); void requestMetar( const MetarLoadRequest & metarRequest, bool background = true );
bool hasMetar() { return _responseQueue.size() > 0; } bool hasMetar() { return _responseQueue.size() > 0; }
string getMetar() { return _responseQueue.pop(); } string getMetar() { return _responseQueue.pop(); }
virtual void run(); virtual void run();
private: private:
void fetch( const MetarLoadRequest & );
long _maxAge; long _maxAge;
SGBlockingQueue <MetarLoadRequest> _requestQueue; SGBlockingQueue <MetarLoadRequest> _requestQueue;
SGBlockingQueue <string> _responseQueue; SGBlockingQueue <string> _responseQueue;
@ -188,17 +206,8 @@ NoaaMetarRealWxController::~NoaaMetarRealWxController()
#endif // ENABLE_THREADS #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; _metarTimeToLive -= dt;
_positionTimeToLive -= dt; _positionTimeToLive -= dt;
_minimumRequestInterval -= dt; _minimumRequestInterval -= dt;
@ -234,11 +243,21 @@ void NoaaMetarRealWxController::update( double dt )
if( !valid ) { if( !valid ) {
if( _minimumRequestInterval <= 0 && stationId.length() > 0 ) { if( _minimumRequestInterval <= 0 && stationId.length() > 0 ) {
MetarLoadRequest request( stationId ); 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; _minimumRequestInterval = 10;
} }
} }
if( _metarLoadThread->hasMetar() ) {
string metar = _metarLoadThread->getMetar();
SG_LOG( SG_ALL, SG_ALERT, "NoaaMetarRwalWxController::update() received METAR " << metar );
_metarDataNode->setStringValue( metar );
}
} }
/* -------------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------------- */
@ -249,16 +268,20 @@ NoaaMetarRealWxController::MetarLoadThread::MetarLoadThread( long maxAge ) :
{ {
} }
void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest ) void NoaaMetarRealWxController::MetarLoadThread::requestMetar( const MetarLoadRequest & metarRequest, bool background )
{ {
if( _requestQueue.size() > 10 ) { if( background ) {
SG_LOG(SG_ALL,SG_ALERT, if( _requestQueue.size() > 10 ) {
"NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping " SG_LOG(SG_ALL,SG_ALERT,
<< metarRequest._stationId ); "NoaaMetarRealWxController::MetarLoadThread::requestMetar() more than 10 outstanding METAR requests, dropping "
return; << metarRequest._stationId );
} return;
}
_requestQueue.push( metarRequest ); _requestQueue.push( metarRequest );
} else {
fetch( metarRequest );
}
} }
void NoaaMetarRealWxController::MetarLoadThread::run() void NoaaMetarRealWxController::MetarLoadThread::run()
@ -269,34 +292,40 @@ void NoaaMetarRealWxController::MetarLoadThread::run()
if( request._stationId.size() == 0 ) if( request._stationId.size() == 0 )
break; 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;
}
string reply = result->getData();
std::replace(reply.begin(), reply.end(), '\n', ' ');
string metar = simgear::strutils::strip( reply );
if( metar.empty() ) {
SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for "
<< request._stationId );
}
if( _maxAge && result->getAge_min() > _maxAge ) {
SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR "
<< metar );
return;
}
_responseQueue.push( metar );
} }
} }
void NoaaMetarRealWxController::MetarLoadThread::fetch( const MetarLoadRequest & request )
{
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() );
return;
}
string reply = result->getData();
std::replace(reply.begin(), reply.end(), '\n', ' ');
string metar = simgear::strutils::strip( reply );
if( metar.empty() ) {
SG_LOG( SG_GENERAL, SG_WARN, "NoaaMetarRealWxController::fetchMetar(): dropping empty METAR for "
<< request._stationId );
return;
}
if( _maxAge && result->getAge_min() > _maxAge ) {
SG_LOG( SG_GENERAL, SG_ALERT, "NoaaMetarRealWxController::fetchMetar(): dropping outdated METAR "
<< metar );
return;
}
_responseQueue.push( metar );
}
#endif #endif
/* -------------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------------- */