]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_ctrl.cxx
Fix line endings
[flightgear.git] / src / Environment / environment_ctrl.cxx
index d41f50b9bbd70483830031129e7edad6a2ab8f56..0b901640b69c7eb7e3e978787ebd96f4b40d5d1e 100644 (file)
 //
 // $Id$
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <simgear/debug/logstream.hxx>
 
 #include <stdlib.h>
@@ -326,10 +330,12 @@ FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
       proxy_auth( fgGetNode("/sim/presets/proxy/authentication", true) ),
       metar_max_age( fgGetNode("/environment/params/metar-max-age-min", true) ),
       _error_count( 0 ),
+      _stale_count( 0 ),
       _dt( 0.0 ),
-      _error_dt( 0.0 )
+      _error_dt( 0.0 ),
+      last_apt(0)
 {
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
     thread = new MetarThread(this);
     thread->start( 1 );
 #endif // ENABLE_THREADS
@@ -337,7 +343,7 @@ FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
 
 FGMetarEnvironmentCtrl::~FGMetarEnvironmentCtrl ()
 {
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
    thread->cancel();
    thread->join();
 #endif // ENABLE_THREADS
@@ -399,29 +405,33 @@ FGMetarEnvironmentCtrl::init ()
     // Don't check max age during init so that we don't loop over a lot
     // of airports metar if there is a problem.
     // The update() calls will find a correct metar if things went wrong here
-    metar_max_age->setLongValue(60 * 24 * 7);
+    metar_max_age->setLongValue(0);
 
     while ( !found_metar && (_error_count < 3) ) {
-        FGAirport a = globals->get_airports()
-            ->search( longitude->getDoubleValue(),
-                      latitude->getDoubleValue(),
-                      true );
-        FGMetarResult result = fetch_data( a.getId() );
-        if ( result.m != NULL ) {
-            SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.getId());
-            last_apt = a;
-            _icao = a.getId();
-            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.
-            SG_LOG( SG_GENERAL, SG_INFO, "no metar at metar = " << a.getId() );
-            globals->get_airports()->no_metar( a.getId() );
+        const FGAirport* a = globals->get_airports()
+                   ->search( longitude->getDoubleValue(),
+                             latitude->getDoubleValue(),
+                             true );
+        if ( a ) {  
+            FGMetarResult result = fetch_data( a->getId() );
+            if ( result.m != NULL ) {
+                SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
+                        << a->getId());
+                last_apt = a;
+                _icao = a->getId();
+                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.
+                SG_LOG( SG_GENERAL, SG_INFO, "no metar at metar = "
+                        << a->getId() );
+                globals->get_airports()->no_metar( a->getId() );
+            }
         }
     }
     metar_max_age->setLongValue(max_age);
@@ -460,27 +470,33 @@ FGMetarEnvironmentCtrl::update(double 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 ( last_apt.getId() != a.getId()
-             || fetch_elapsed > same_station_interval_sec )
-        {
-            SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.getId());
-            request_queue.push( a.getId() );
-            last_apt = a;
-            _icao = a.getId();
-            search_elapsed = 0.0;
-            fetch_elapsed = 0.0;
+        const FGAirport* a = globals->get_airports()
+                   ->search( longitude->getDoubleValue(),
+                             latitude->getDoubleValue(),
+                             true );
+        if ( a ) {
+            if ( !last_apt || last_apt->getId() != a->getId()
+                 || fetch_elapsed > same_station_interval_sec )
+            {
+                SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = "
+                        << a->getId());
+                request_queue.push( a->getId() );
+                last_apt = a;
+                _icao = a->getId();
+                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 );
+            }
         } else {
-            search_elapsed = 0.0;
-            SG_LOG( SG_GENERAL, SG_INFO, "same station, waiting = "
-                 << same_station_interval_sec - fetch_elapsed );
+            SG_LOG( SG_GENERAL, SG_WARN,
+                    "Unable to find any airports with metar" );
         }
     }
 
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if !defined(ENABLE_THREADS)
     // No loader thread running so manually fetch the data
     string id = "";
     while ( !request_queue.empty() ) {
@@ -541,8 +557,10 @@ FGMetarEnvironmentCtrl::fetch_data( const string &icao )
     }
 
     // fetch station elevation if exists
-    FGAirport a = globals->get_airports()->search( icao );
-    station_elevation_ft = a.getElevation();
+    const FGAirport* a = globals->get_airports()->search( icao );
+    if ( a ) {
+        station_elevation_ft = a->getElevation();
+    }
 
     // fetch current metar data
     try {
@@ -552,16 +570,24 @@ FGMetarEnvironmentCtrl::fetch_data( const string &icao )
         result.m = new FGMetar( icao, host, port, auth);
 
         long max_age = metar_max_age->getLongValue();
-        if (max_age && result.m->getAge_min() > max_age) {
-            SG_LOG( SG_GENERAL, SG_WARN, "METAR data too old");
+        long age = result.m->getAge_min();
+        if (max_age &&  age > max_age) {
+            SG_LOG( SG_GENERAL, SG_WARN, "METAR data too old (" << age << " min).");
             delete result.m;
             result.m = NULL;
-        }
+
+            if (++_stale_count > 10) {
+                _error_count = 1000;
+                throw sg_io_exception("More than 10 stale METAR messages in a row."
+                        " Check your system time!");
+            }
+        } else
+            _stale_count = 0;
 
     } catch (const sg_io_exception& e) {
         SG_LOG( SG_GENERAL, SG_WARN, "Error fetching live weather data: "
                 << e.getFormattedMessage().c_str() );
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
         if (_error_count++ >= 3) {
            SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently.");
            thread->cancel();
@@ -579,7 +605,7 @@ FGMetarEnvironmentCtrl::fetch_data( const string &icao )
 
 
 void
-FGMetarEnvironmentCtrl::update_metar_properties( FGMetar *m )
+FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m )
 {
     int i;
     double d;
@@ -597,7 +623,7 @@ FGMetarEnvironmentCtrl::update_metar_properties( FGMetar *m )
     fgSetDouble("/environment/metar/max-visibility-m",
                 m->getMaxVisibility().getVisibility_m() );
 
-    SGMetarVisibility *dirvis = m->getDirVisibility();
+    const SGMetarVisibility *dirvis = m->getDirVisibility();
     for (i = 0; i < 8; i++, dirvis++) {
         const char *min = "/environment/metar/visibility[%d]/min-m";
         const char *max = "/environment/metar/visibility[%d]/max-m";
@@ -628,7 +654,7 @@ FGMetarEnvironmentCtrl::update_metar_properties( FGMetar *m )
                 m->getPressure_inHg() );
 
     vector<SGMetarCloud> cv = m->getClouds();
-    vector<SGMetarCloud>::iterator cloud;
+    vector<SGMetarCloud>::const_iterator cloud;
 
     const char *cl = "/environment/clouds/layer[%i]";
     for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
@@ -680,7 +706,17 @@ FGMetarEnvironmentCtrl::update_metar_properties( FGMetar *m )
 }
 
 
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
+/**
+ * Ensure mutex is unlocked.
+ */
+void
+metar_cleanup_handler( void* arg )
+{
+    FGMetarEnvironmentCtrl* fetcher = (FGMetarEnvironmentCtrl*) arg;
+    fetcher->mutex.unlock();
+}
+
 /**
  *
  */
@@ -702,16 +738,6 @@ FGMetarEnvironmentCtrl::MetarThread::run()
     }
     pthread_cleanup_pop(1);
 }
-
-/**
- * Ensure mutex is unlocked.
- */
-void
-metar_cleanup_handler( void* arg )
-{
-    FGMetarEnvironmentCtrl* fetcher = (FGMetarEnvironmentCtrl*) arg;
-    fetcher->mutex.unlock();
-}
 #endif // ENABLE_THREADS