]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
29107ed58d01a202d2a52374b7a36221624bccd7
[flightgear.git] / src / Environment / environment_ctrl.hxx
1 // environment-ctrl.hxx -- controller for environment information.
2 //
3 // Written by David Megginson, started May 2002.
4 //
5 // Copyright (C) 2002  David Megginson - david@megginson.com
6 //
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.
11 //
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.
16 //
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifndef _ENVIRONMENT_CTRL_HXX
24 #define _ENVIRONMENT_CTRL_HXX
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <simgear/compiler.h>
31 #include <simgear/structure/subsystem_mgr.hxx>
32 #include <simgear/environment/metar.hxx>
33
34 #if defined(ENABLE_THREADS)
35 # include <simgear/threads/SGThread.hxx>
36 # include <simgear/threads/SGQueue.hxx>
37 #endif
38
39 #ifdef SG_HAVE_STD_INCLUDES
40 #  include <cmath>
41 #else
42 #  include <math.h>
43 #endif
44
45 #include <queue>
46 #include <vector>
47
48 SG_USING_STD(queue);
49 SG_USING_STD(vector);
50
51 class SGPropertyNode;
52 class FGAirport;
53
54 #include "environment.hxx"
55 #include "fgmetar.hxx"
56
57
58 \f
59 /**
60  * Interface to control environment information for a specific location.
61  */
62 class FGEnvironmentCtrl : public SGSubsystem
63 {
64
65 public:
66
67   FGEnvironmentCtrl ();
68   virtual ~FGEnvironmentCtrl ();
69
70   virtual void setEnvironment (FGEnvironment * environment);
71
72   virtual const FGEnvironment * getEnvironment () const { return _environment; }
73
74   virtual void setLongitudeDeg (double lon_deg);
75   virtual void setLatitudeDeg (double lat_deg);
76   virtual void setElevationFt (double elev_ft);
77   virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
78
79   virtual double getLongitudeDeg () const { return _lon_deg; }
80   virtual double getLatitudeDeg () const { return _lat_deg; }
81   virtual double getElevationFt () const { return _elev_ft; }
82
83 protected:
84
85   FGEnvironment * _environment;
86   double _lon_deg;
87   double _lat_deg;
88   double _elev_ft;
89
90 };
91
92
93 \f
94 /**
95  * Environment controller using user-supplied parameters.
96  */
97 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
98 {
99 public:
100   FGUserDefEnvironmentCtrl ();
101   virtual ~FGUserDefEnvironmentCtrl ();
102
103   virtual void init ();
104   virtual void update (double dt);
105
106 private:
107
108   SGPropertyNode * _base_wind_speed_node;
109   SGPropertyNode * _gust_wind_speed_node;
110
111   double _current_wind_speed_kt;
112   double _delta_wind_speed_kt;
113
114 };
115
116
117 \f
118 /**
119  * Interplation controller using user-supplied parameters.
120  */
121 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
122 {
123 public:
124     FGInterpolateEnvironmentCtrl ();
125     virtual ~FGInterpolateEnvironmentCtrl ();
126     
127     virtual void init ();
128     virtual void reinit ();
129     virtual void update (double delta_time_sec);
130
131 private:
132     
133     struct bucket {
134         double altitude_ft;
135         FGEnvironment environment;
136         bool operator< (const bucket &b) const;
137     };
138
139     void read_table (const SGPropertyNode * node, vector<bucket *> &table);
140     void do_interpolate (vector<bucket *> &table, double altitude_ft,
141                          FGEnvironment * environment);
142
143     FGEnvironment env1, env2;   // temporaries
144
145     vector<bucket *> _boundary_table;
146     vector<bucket *> _aloft_table;
147 };
148
149
150 // A convenience wrapper around FGMetar
151 struct FGMetarResult {
152     string icao;
153     FGMetar *m;
154 };
155
156
157 \f
158 /**
159  * Interplation controller using the FGMetar class
160  */
161 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
162 {
163 public:
164     FGMetarEnvironmentCtrl ();
165     virtual ~FGMetarEnvironmentCtrl ();
166
167     virtual void init ();
168     virtual void reinit ();
169     virtual void update (double delta_time_sec);
170     virtual void setEnvironment (FGEnvironment * environment);
171
172 private:
173     FGInterpolateEnvironmentCtrl *env;
174
175     string _icao;
176     float station_elevation_ft;
177     float search_interval_sec;
178     float same_station_interval_sec;
179     float search_elapsed;
180     float fetch_elapsed;
181     const FGAirport *last_apt;
182     SGPropertyNode *proxy_host;
183     SGPropertyNode *proxy_port;
184     SGPropertyNode *proxy_auth;
185     SGPropertyNode *metar_max_age;
186
187     FGMetarResult fetch_data( const string &icao );
188     virtual void update_metar_properties( const FGMetar *m );
189     void update_env_config();
190
191 private:
192
193 #if defined(ENABLE_THREADS)
194     /**
195      * FIFO queue which holds a pointer to the fetched metar data.
196      */
197     SGBlockingQueue < string > request_queue;
198
199     /**
200      * FIFO queue which holds a pointer to the fetched metar data.
201      */
202     SGLockedQueue < FGMetarResult > result_queue;
203 #else
204     /**
205      * FIFO queue which holds a pointer to the fetched metar data.
206      */
207     queue < string > request_queue;
208
209     /**
210      * FIFO queue which holds a pointer to the fetched metar data.
211      */
212     queue < FGMetarResult > result_queue;
213 #endif
214
215 #if defined(ENABLE_THREADS)
216     /**
217      * This class represents the thread of execution responsible for
218      * fetching the metar data.
219      */
220     class MetarThread : public SGThread
221     {
222     public:
223         MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
224         ~MetarThread() {}
225
226         /**
227          * Fetche the metar data from the NOAA.
228          */
229         void run();
230
231     private:
232         FGMetarEnvironmentCtrl *fetcher;
233
234     private:
235         // not implemented.
236         MetarThread();
237         MetarThread( const MetarThread& );
238         MetarThread& operator=( const MetarThread& );
239     };
240
241     friend class MetarThread;
242
243     /**
244      * Metar data fetching thread.
245      */
246     MetarThread* thread;
247
248     void thread_stop();
249 #endif // ENABLE_THREADS
250
251     int _error_count;
252     int _stale_count;
253     double _dt;
254     double _error_dt;
255 };
256
257 #endif // _ENVIRONMENT_CTRL_HXX