]> git.mxchange.org Git - flightgear.git/blobdiff - src/Environment/environment_ctrl.cxx
Harald JOHNSEN:
[flightgear.git] / src / Environment / environment_ctrl.cxx
index f90e28f0d6f97167b0874eab98403c591405041c..9872c6d0c5f16d157c894961e26c27264eb7cddc 100644 (file)
@@ -324,12 +324,13 @@ FGMetarEnvironmentCtrl::FGMetarEnvironmentCtrl ()
       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 )
+      metar_max_age( fgGetNode("/environment/params/metar-max-age-min", true) ),
+      _error_count( 0 ),
+      _error_dt( 0.0 )
 {
 #if defined(ENABLE_THREADS) && ENABLE_THREADS
     thread = new MetarThread(this);
-    thread->start();
+    thread->start( 1 );
 #endif // ENABLE_THREADS
 }
 
@@ -399,11 +400,11 @@ FGMetarEnvironmentCtrl::init ()
             ->search( longitude->getDoubleValue(),
                       latitude->getDoubleValue(),
                       true );
-        FGMetarResult result = fetch_data( a.id );
+        FGMetarResult result = fetch_data( a.getId() );
         if ( result.m != NULL ) {
-            SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.id);
+            SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.getId());
             last_apt = a;
-            _icao = a.id;
+            _icao = a.getId();
             search_elapsed = 0.0;
             fetch_elapsed = 0.0;
             update_metar_properties( result.m );
@@ -413,8 +414,8 @@ FGMetarEnvironmentCtrl::init ()
         } 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.id );
-            globals->get_airports()->no_metar( a.id );
+            SG_LOG( SG_GENERAL, SG_INFO, "no metar at metar = " << a.getId() );
+            globals->get_airports()->no_metar( a.getId() );
         }
     }
 }
@@ -456,13 +457,13 @@ FGMetarEnvironmentCtrl::update(double delta_time_sec)
             ->search( longitude->getDoubleValue(),
                       latitude->getDoubleValue(),
                       true );
-        if ( last_apt.id != a.id
+        if ( last_apt.getId() != a.getId()
              || fetch_elapsed > same_station_interval_sec )
         {
-            SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.id);
-            request_queue.push( a.id );
+            SG_LOG( SG_GENERAL, SG_INFO, "closest station w/ metar = " << a.getId());
+            request_queue.push( a.getId() );
             last_apt = a;
-            _icao = a.id;
+            _icao = a.getId();
             search_elapsed = 0.0;
             fetch_elapsed = 0.0;
         } else {
@@ -493,6 +494,7 @@ FGMetarEnvironmentCtrl::update(double delta_time_sec)
         result_queue.pop();
         if ( result.m != NULL ) {
             update_metar_properties( result.m );
+            fgSetString("/environment/metar/last-metar", result.m->getData());
             delete result.m;
             update_env_config();
             env->reinit();
@@ -534,23 +536,32 @@ FGMetarEnvironmentCtrl::fetch_data( const string &icao )
 
     // fetch station elevation if exists
     FGAirport a = globals->get_airports()->search( icao );
-    station_elevation_ft = a.elevation;
+    station_elevation_ft = a.getElevation();
 
     // fetch current metar data
     try {
         string host = proxy_host->getStringValue();
         string auth = proxy_auth->getStringValue();
         string port = proxy_port->getStringValue();
-        result.m = new SGMetar( icao, host, port, auth);
+        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");
+            delete result.m;
+            result.m = NULL;
+        }
 
     } 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 (_error_count++ >= 3) {
            SG_LOG( SG_GENERAL, SG_WARN, "Stop fetching data permanently.");
            thread->cancel();
            thread->join();
         }
+#endif
 
         result.m = NULL;
     }
@@ -562,28 +573,24 @@ FGMetarEnvironmentCtrl::fetch_data( const string &icao )
 
 
 void
-FGMetarEnvironmentCtrl::update_metar_properties( SGMetar *m )
+FGMetarEnvironmentCtrl::update_metar_properties( FGMetar *m )
 {
     int i;
-    double d, dt;
+    double d;
     char s[128];
 
-    d = m->getMinVisibility().getVisibility_m();
-    d = (d != SGMetarNaN) ? d : 10000;
-    fgSetDouble("/environment/metar/min-visibility-m", d);
-
-    dt =  m->getMaxVisibility().getVisibility_m();
-    d = (dt != SGMetarNaN) ? dt : d;
-    fgSetDouble("/environment/metar/max-visibility-m", d);
+    fgSetString("/environment/metar/station-id", m->getId());
+    fgSetDouble("/environment/metar/min-visibility-m",
+                m->getMinVisibility().getVisibility_m() );
+    fgSetDouble("/environment/metar/max-visibility-m",
+                m->getMaxVisibility().getVisibility_m() );
 
     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";
-        char s[128];
 
         d = dirvis->getVisibility_m();
-        d = (d != SGMetarNaN) ? d : 10000;
 
         snprintf(s, 128, min, i);
         fgSetDouble(s, d);
@@ -591,37 +598,22 @@ FGMetarEnvironmentCtrl::update_metar_properties( SGMetar *m )
         fgSetDouble(s, d);
     }
 
-    i = m->getWindDir();
-    if ( i == -1 ) {
-        fgSetInt("/environment/metar/base-wind-range-from",
-                 m->getWindRangeFrom() );
-        fgSetInt("/environment/metar/base-wind-range-to",
-                 m->getWindRangeTo() );
-    } else {
-        fgSetInt("/environment/metar/base-wind-range-from", i);
-        fgSetInt("/environment/metar/base-wind-range-to", i);
-    }
+    fgSetInt("/environment/metar/base-wind-range-from",
+             m->getWindRangeFrom() );
+    fgSetInt("/environment/metar/base-wind-range-to",
+             m->getWindRangeTo() );
     fgSetDouble("/environment/metar/base-wind-speed-kt",
                 m->getWindSpeed_kt() );
-
-    d = m->getGustSpeed_kt();
-    d = (d != SGMetarNaN) ? d : 0.0;
-    fgSetDouble("/environment/metar/gust-wind-speed-kt", d);
-
-    d = m->getTemperature_C();
-    if (d != SGMetarNaN) {
-        dt = m->getDewpoint_C();
-        dt = (dt != SGMetarNaN) ? dt : 0.0;
-        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);
-
-    d = m->getPressure_inHg();
-    d = (d != SGMetarNaN) ? d : 30.0;
-    fgSetDouble("/environment/metar/pressure-inhg", d);
+    fgSetDouble("/environment/metar/gust-wind-speed-kt",
+                m->getGustSpeed_kt() );
+    fgSetDouble("/environment/metar/temperature-degc",
+                m->getTemperature_C() );
+    fgSetDouble("/environment/metar/dewpoint-degc",
+                m->getDewpoint_C() );
+    fgSetDouble("/environment/metar/rel-humidity-norm",
+                m->getRelHumidity() );
+    fgSetDouble("/environment/metar/pressure-inhg",
+                m->getPressure_inHg() );
 
     vector<SGMetarCloud> cv = m->getClouds();
     vector<SGMetarCloud>::iterator cloud;
@@ -636,14 +628,11 @@ FGMetarEnvironmentCtrl::update_metar_properties( SGMetar *m )
         snprintf(s, 128, cl, i);
         strncat(s, "/coverage", 128);
         q = cloud->getCoverage();
-        q = (q != -1 ) ? q : 0;
         fgSetString(s, coverage_string[q] );
 
         snprintf(s, 128, cl, i);
         strncat(s, "/elevation-ft", 128);
-        d = cloud->getAltitude_ft();
-        d = (d != SGMetarNaN) ? d : -9999;
-        fgSetDouble(s, d + station_elevation_ft);
+        fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
 
         snprintf(s, 128, cl, i);
         strncat(s, "/thickness-ft", 128);
@@ -671,6 +660,11 @@ FGMetarEnvironmentCtrl::update_metar_properties( SGMetar *m )
         strncat(s, "/span-m", 128);
         fgSetDouble(s, 40000.0);
     }
+
+    fgSetDouble("/environment/metar/rain-norm", m->getRain());
+    fgSetDouble("/environment/metar/hail-norm", m->getHail());
+    fgSetDouble("/environment/metar/snow-norm", m->getSnow());
+    fgSetBool("/environment/metar/snow-cover", m->getSnowCover());
 }