SG_USING_STD(sort);
-// FIXME, from options.cxx
-extern void fgSetupWind (double min_hdg, double max_hdg, double speed, double gust);
-
\f
////////////////////////////////////////////////////////////////////////
FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
: env( new FGInterpolateEnvironmentCtrl ),
- _icao( fgGetString("/sim/presets/airport-id") ),
- update_interval_sec( 60.0 ),
- elapsed( 60.0 )
+ _icao( "" ),
+ search_interval_sec( 60.0 ), // 1 minute
+ same_station_interval_sec( 900.0 ), // 15 minutes
+ search_elapsed( 9999.0 ),
+ fetch_elapsed( 9999.0 ),
+ proxy_host( fgGetNode("/sim/presets/proxy/host", true) ),
+ proxy_port( fgGetNode("/sim/presets/proxy/port", true) ),
+ proxy_auth( fgGetNode("/sim/presets/proxy/authentication", true) ),
+ _error_dt( 0.0 ),
+ _error_count( 0 )
{
+#if defined(ENABLE_THREADS) && ENABLE_THREADS
+ thread = new MetarThread(this);
+ thread->start();
+#endif // ENABLE_THREADS
}
FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
{
+#if defined(ENABLE_THREADS) && ENABLE_THREADS
+ thread->cancel();
+ thread->join();
+#endif // ENABLE_THREADS
+
delete env;
env = NULL;
}
= fgGetNode( "/position/latitude-deg", true );
bool found_metar = false;
- while ( !found_metar ) {
+
+ while ( !found_metar && (_error_count < 3) ) {
FGAirport a = globals->get_airports()
->search( longitude->getDoubleValue(),
latitude->getDoubleValue(),
true );
- if ( fetch_data( a.id ) ) {
- cout << "closest station w/ metar = " << a.id << endl;
+ FGMetarResult result = fetch_data( a.id );
+ if ( result.m != NULL ) {
+ SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.id);
+ last_apt = a;
_icao = a.id;
- elapsed = 0.0;
+ search_elapsed = 0.0;
+ fetch_elapsed = 0.0;
+ update_metar_properties( result.m );
update_env_config();
env->init();
found_metar = true;
} else {
// mark as no metar so it doesn't show up in subsequent
// searches.
- cout << "no metar at metar = " << a.id << endl;
+ SG_LOG( SG_GENERAL, SG_INFO, "no metar at metar = " << a.id );
globals->get_airports()->no_metar( a.id );
}
}
void
FGMetarEnvironmentCtrl::reinit ()
{
+ _error_count = 0;
+ _error_dt = 0.0;
+
#if 0
update_env_config();
#endif
void
FGMetarEnvironmentCtrl::update(double delta_time_sec)
{
- const SGPropertyNode *longitude
+
+ _dt += delta_time_sec;
+ if (_error_count >= 3)
+ return;
+
+ FGMetarResult result;
+
+ static const SGPropertyNode *longitude
= fgGetNode( "/position/longitude-deg", true );
- const SGPropertyNode *latitude
+ static const SGPropertyNode *latitude
= fgGetNode( "/position/latitude-deg", true );
- elapsed += delta_time_sec;
- if ( elapsed > update_interval_sec ) {
+ search_elapsed += delta_time_sec;
+ fetch_elapsed += delta_time_sec;
+
+ // if time for a new search request, push it onto the request
+ // queue
+ if ( search_elapsed > search_interval_sec ) {
FGAirport a = globals->get_airports()
->search( longitude->getDoubleValue(),
latitude->getDoubleValue(),
true );
- if ( fetch_data( a.id ) ) {
- cout << "closest station w/ metar = " << a.id << endl;
- _icao = a.id;
- elapsed = 0.0;
+ if ( last_apt.id != a.id
+ || fetch_elapsed > same_station_interval_sec )
+ {
+ SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.id);
+ request_queue.push( a.id );
+ last_apt = a;
+ _icao = a.id;
+ search_elapsed = 0.0;
+ fetch_elapsed = 0.0;
+ } else {
+ search_elapsed = 0.0;
+ SG_LOG( SG_GENERAL, SG_INFO, "same station, waiting = "
+ << same_station_interval_sec - fetch_elapsed );
+ }
+ }
+
+#if defined(ENABLE_THREADS) && ENABLE_THREADS
+ // No loader thread running so manually fetch the data
+ string id = "";
+ while ( !request_queue.empty() ) {
+ id = request_queue.front();
+ request_queue.pop();
+ }
+
+ if ( !id.empty() ) {
+ SG_LOG( SG_GENERAL, SG_INFO, "inline fetching = " << id );
+ result = fetch_data( id );
+ result_queue.push( result );
+ }
+#endif // ENABLE_THREADS
+
+ // process any results from the loader.
+ while ( !result_queue.empty() ) {
+ result = result_queue.front();
+ result_queue.pop();
+ if ( result.m != NULL ) {
+ update_metar_properties( result.m );
+ delete result.m;
update_env_config();
- env->init();
+ env->reinit();
} else {
// mark as no metar so it doesn't show up in subsequent
- // searches.
- cout << "no metar at metar = " << a.id << endl;
- globals->get_airports()->no_metar( a.id );
+ // searches, and signal an immediate re-search.
+ SG_LOG( SG_GENERAL, SG_WARN,
+ "no metar at station = " << result.icao );
+ globals->get_airports()->no_metar( result.icao );
+ search_elapsed = 9999.0;
}
}
+
env->update(delta_time_sec);
}
+
void
FGMetarEnvironmentCtrl::setEnvironment (FGEnvironment * environment)
{
env->setEnvironment(environment);
}
-bool
-FGMetarEnvironmentCtrl::fetch_data (const string &icao)
+FGMetarResult
+FGMetarEnvironmentCtrl::fetch_data( const string &icao )
{
- char s[128];
- double d, dt;
- int i;
+ FGMetarResult result;
+ result.icao = icao;
- if ((icao == "") && (_icao == "")) {
- _icao = fgGetString("/sim/presets/airport-id");
+ // if the last error was more than three seconds ago,
+ // then pretent nothing happened.
+ if (_error_dt < 3) {
+ _error_dt += _dt;
- } else if (icao != "") {
- _icao = icao;
+ } else {
+ _error_dt = 0.0;
+ _error_count = 0;
}
// fetch station elevation if exists
- FGAirport a = globals->get_airports()->search( _icao );
+ FGAirport a = globals->get_airports()->search( icao );
station_elevation_ft = a.elevation;
// fetch current metar data
- SGMetar *m;
try {
- m = new SGMetar( _icao.c_str() );
+ string host = proxy_host->getStringValue();
+ string auth = proxy_auth->getStringValue();
+ string port = proxy_port->getStringValue();
+ result.m = new SGMetar( icao, host, port, auth);
+
} catch (const sg_io_exception& e) {
SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
- << e.getFormattedMessage().c_str() );
- return false;
+ << e.getFormattedMessage().c_str() );
+#if defined(ENABLE_THREADS) && ENABLE_THREADS
+ if (_error_count++ >= 3) {
+ SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently.");
+ thread->cancel();
+ thread->join();
+ }
+#endif
+
+ result.m = NULL;
}
+ _dt = 0;
+
+ return result;
+}
+
+
+void
+FGMetarEnvironmentCtrl::update_metar_properties( SGMetar *m )
+{
+ int i;
+ double d, dt;
+ char s[128];
+
d = m->getMinVisibility().getVisibility_m();
d = (d != SGMetarNaN) ? d : 10000;
fgSetDouble("/environment/metar/min-visibility-m", d);
i = m->getWindDir();
if ( i == -1 ) {
fgSetInt("/environment/metar/base-wind-range-from",
- m->getWindRangeFrom() );
+ m->getWindRangeFrom() );
fgSetInt("/environment/metar/base-wind-range-to",
- m->getWindRangeTo() );
+ m->getWindRangeTo() );
} else {
fgSetInt("/environment/metar/base-wind-range-from", i);
fgSetInt("/environment/metar/base-wind-range-to", i);
fgSetDouble("/environment/metar/dewpoint-degc", dt);
fgSetDouble("/environment/metar/rel-humidity-norm",
m->getRelHumidity() );
- }
+ }
d = (d != SGMetarNaN) ? d : 15.0;
fgSetDouble("/environment/metar/temperature-degc", d);
strncat(s, "/span-m", 128);
fgSetDouble(s, 40000.0);
}
+
for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
snprintf(s, 128, cl, i);
strncat(s, "/coverage", 128);
strncat(s, "/span-m", 128);
fgSetDouble(s, 40000.0);
}
+}
+
+
+#if defined(ENABLE_THREADS) && ENABLE_THREADS
+/**
+ *
+ */
+void
+FGMetarEnvironmentCtrl::MetarThread::run()
+{
+ pthread_cleanup_push( metar_cleanup_handler, fetcher );
+ while ( true )
+ {
+ set_cancel( SGThread::CANCEL_DISABLE );
- delete m;
+ string icao = fetcher->request_queue.pop();
+ SG_LOG( SG_GENERAL, SG_INFO, "Thread: fetch metar data = " << icao );
+ FGMetarResult result = fetcher->fetch_data( icao );
- return true;
+ set_cancel( SGThread::CANCEL_DEFERRED );
+
+ fetcher->result_queue.push( result );
+ }
+ pthread_cleanup_pop(1);
+}
+
+/**
+ * Ensure mutex is unlocked.
+ */
+void
+metar_cleanup_handler( void* arg )
+{
+ FGMetarEnvironmentCtrl* fetcher = (FGMetarEnvironmentCtrl*) arg;
+ fetcher->mutex.unlock();
}
+#endif // ENABLE_THREADS
// end of environment_ctrl.cxx