1 // environment-ctrl.hxx -- controller for environment information.
3 // Written by David Megginson, started May 2002.
5 // Copyright (C) 2002 David Megginson - david@megginson.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #ifndef _ENVIRONMENT_CTRL_HXX
24 #define _ENVIRONMENT_CTRL_HXX
26 #include <simgear/compiler.h>
27 #include <simgear/structure/subsystem_mgr.hxx>
28 #include <simgear/environment/metar.hxx>
30 #if defined(ENABLE_THREADS) && ENABLE_THREADS
31 # include <simgear/threads/SGThread.hxx>
32 # include <simgear/threads/SGQueue.hxx>
35 #ifdef SG_HAVE_STD_INCLUDES
49 #include "environment.hxx"
50 #include "fgmetar.hxx"
55 * Interface to control environment information for a specific location.
57 class FGEnvironmentCtrl : public SGSubsystem
63 virtual ~FGEnvironmentCtrl ();
65 virtual void setEnvironment (FGEnvironment * environment);
67 virtual FGEnvironment * getEnvironment () const { return _environment; }
69 virtual void setLongitudeDeg (double lon_deg);
70 virtual void setLatitudeDeg (double lat_deg);
71 virtual void setElevationFt (double elev_ft);
72 virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
74 virtual double getLongitudeDeg () const { return _lon_deg; }
75 virtual double getLatitudeDeg () const { return _lat_deg; }
76 virtual double getElevationFt () const { return _elev_ft; }
80 FGEnvironment * _environment;
90 * Environment controller using user-supplied parameters.
92 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
95 FGUserDefEnvironmentCtrl ();
96 virtual ~FGUserDefEnvironmentCtrl ();
99 virtual void update (double dt);
103 SGPropertyNode * _base_wind_speed_node;
104 SGPropertyNode * _gust_wind_speed_node;
106 double _current_wind_speed_kt;
107 double _delta_wind_speed_kt;
114 * Interplation controller using user-supplied parameters.
116 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
119 FGInterpolateEnvironmentCtrl ();
120 virtual ~FGInterpolateEnvironmentCtrl ();
122 virtual void init ();
123 virtual void reinit ();
124 virtual void update (double delta_time_sec);
130 FGEnvironment environment;
131 bool operator< (const bucket &b) const;
134 void read_table (const SGPropertyNode * node, vector<bucket *> &table);
135 void do_interpolate (vector<bucket *> &table, double altitude_ft,
136 FGEnvironment * environment);
138 FGEnvironment env1, env2; // temporaries
140 vector<bucket *> _boundary_table;
141 vector<bucket *> _aloft_table;
145 // A convenience wrapper around FGMetar
146 struct FGMetarResult {
154 * Interplation controller using the FGMetar class
156 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
159 FGMetarEnvironmentCtrl ();
160 virtual ~FGMetarEnvironmentCtrl ();
162 virtual void init ();
163 virtual void reinit ();
164 virtual void update (double delta_time_sec);
165 virtual void setEnvironment (FGEnvironment * environment);
168 FGInterpolateEnvironmentCtrl *env;
171 float station_elevation_ft;
172 float search_interval_sec;
173 float same_station_interval_sec;
174 float search_elapsed;
177 SGPropertyNode *proxy_host;
178 SGPropertyNode *proxy_port;
179 SGPropertyNode *proxy_auth;
180 SGPropertyNode *metar_max_age;
182 FGMetarResult fetch_data( const string &icao );
183 virtual void update_metar_properties( FGMetar *m );
184 void update_env_config();
188 #if defined(ENABLE_THREADS) && ENABLE_THREADS
190 * FIFO queue which holds a pointer to the fetched metar data.
192 SGBlockingQueue < string > request_queue;
195 * FIFO queue which holds a pointer to the fetched metar data.
197 SGBlockingQueue < FGMetarResult > result_queue;
200 * FIFO queue which holds a pointer to the fetched metar data.
202 queue < string > request_queue;
205 * FIFO queue which holds a pointer to the fetched metar data.
207 queue < FGMetarResult > result_queue;
210 #if defined(ENABLE_THREADS) && ENABLE_THREADS
212 * This class represents the thread of execution responsible for
213 * fetching the metar data.
215 class MetarThread : public SGThread
218 MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
222 * Fetche the metar data from the NOAA.
227 FGMetarEnvironmentCtrl *fetcher;
232 MetarThread( const MetarThread& );
233 MetarThread& operator=( const MetarThread& );
236 friend class MetarThread;
239 * Metar data fetching thread.
244 * Lock and synchronize access to metar queue.
247 SGPthreadCond metar_cond;
250 * Thread cleanup handler.
252 friend void metar_cleanup_handler( void* );
254 #endif // ENABLE_THREADS
261 #endif // _ENVIRONMENT_CTRL_HXX