]> git.mxchange.org Git - flightgear.git/blob - src/Environment/environment_ctrl.hxx
5032a9f590214d02e9c4f324ff9fc128f521ecf3
[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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifndef _ENVIRONMENT_CTRL_HXX
24 #define _ENVIRONMENT_CTRL_HXX
25
26 #include <simgear/compiler.h>
27 #include <simgear/structure/subsystem_mgr.hxx>
28 #include <simgear/environment/metar.hxx>
29
30 #ifdef ENABLE_THREADS
31 # include <simgear/threads/SGThread.hxx>
32 # include <simgear/threads/SGQueue.hxx>
33 #endif
34
35 #ifdef SG_HAVE_STD_INCLUDES
36 #  include <cmath>
37 #else
38 #  include <math.h>
39 #endif
40
41 #include <vector>
42
43 SG_USING_STD(vector);
44
45 class SGPropertyNode;
46
47 #include "environment.hxx"
48
49
50 \f
51 /**
52  * Interface to control environment information for a specific location.
53  */
54 class FGEnvironmentCtrl : public SGSubsystem
55 {
56
57 public:
58
59   FGEnvironmentCtrl ();
60   virtual ~FGEnvironmentCtrl ();
61
62   virtual void setEnvironment (FGEnvironment * environment);
63
64   virtual FGEnvironment * getEnvironment () const { return _environment; }
65
66   virtual void setLongitudeDeg (double lon_deg);
67   virtual void setLatitudeDeg (double lat_deg);
68   virtual void setElevationFt (double elev_ft);
69   virtual void setPosition (double lon_deg, double lat_deg, double elev_ft);
70
71   virtual double getLongitudeDeg () const { return _lon_deg; }
72   virtual double getLatitudeDeg () const { return _lat_deg; }
73   virtual double getElevationFt () const { return _elev_ft; }
74
75 protected:
76
77   FGEnvironment * _environment;
78   double _lon_deg;
79   double _lat_deg;
80   double _elev_ft;
81
82 };
83
84
85 \f
86 /**
87  * Environment controller using user-supplied parameters.
88  */
89 class FGUserDefEnvironmentCtrl : public FGEnvironmentCtrl
90 {
91 public:
92   FGUserDefEnvironmentCtrl ();
93   virtual ~FGUserDefEnvironmentCtrl ();
94
95   virtual void init ();
96   virtual void update (double dt);
97
98 private:
99
100   SGPropertyNode * _base_wind_speed_node;
101   SGPropertyNode * _gust_wind_speed_node;
102
103   double _current_wind_speed_kt;
104   double _delta_wind_speed_kt;
105
106 };
107
108
109 \f
110 /**
111  * Interplation controller using user-supplied parameters.
112  */
113 class FGInterpolateEnvironmentCtrl : public FGEnvironmentCtrl
114 {
115 public:
116     FGInterpolateEnvironmentCtrl ();
117     virtual ~FGInterpolateEnvironmentCtrl ();
118     
119     virtual void init ();
120     virtual void reinit ();
121     virtual void update (double delta_time_sec);
122
123 private:
124     
125     struct bucket {
126         double altitude_ft;
127         FGEnvironment environment;
128         bool operator< (const bucket &b) const;
129     };
130
131     void read_table (const SGPropertyNode * node, vector<bucket *> &table);
132     void do_interpolate (vector<bucket *> &table, double altitude_ft,
133                          FGEnvironment * environment);
134
135     FGEnvironment env1, env2;   // temporaries
136
137     vector<bucket *> _boundary_table;
138     vector<bucket *> _aloft_table;
139 };
140
141
142 \f
143 /**
144  * Interplation controller using the SGMetar class
145  */
146 class FGMetarEnvironmentCtrl : public FGEnvironmentCtrl
147 {
148 public:
149     FGMetarEnvironmentCtrl ();
150     virtual ~FGMetarEnvironmentCtrl ();
151
152     virtual void init ();
153     virtual void reinit ();
154     virtual void update (double delta_time_sec);
155
156     virtual void setEnvironment (FGEnvironment * environment);
157
158 private:
159     FGInterpolateEnvironmentCtrl *env;
160
161     float station_elevation_ft;
162     float update_interval_sec;
163     float elapsed;
164     bool fetch_data (const string &icao);
165     void update_env_config();
166
167     string _icao;
168     SGPropertyNode *proxy_host;
169     SGPropertyNode *proxy_port;
170     SGPropertyNode *proxy_auth;
171
172 private:
173
174 #ifdef ENABLE_THREADS
175     /**
176      * FIFO queue which holds a pointer to the fetched metar data.
177      */
178     SGBlockingQueue< SGMetar * > metar_queue;
179
180     /**
181      * This class represents the thread of execution responsible for
182      * fetching the metar data.
183      */
184     class MetarThread : public SGThread
185     {
186     public:
187         MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
188         ~MetarThread() {}
189
190         /**
191          * Reads the tile from disk.
192          */
193         void run();
194
195     private:
196         FGMetarEnvironmentCtrl *fetcher;
197
198     private:
199         // not implemented.
200         MetarThread();
201         MetarThread( const MetarThread& );
202         MetarThread& operator=( const MetarThread& );
203     };
204
205     friend class MetarThread;
206
207     /**
208      * Metar data fetching thread.
209      */
210     MetarThread* thread;
211
212     /**
213      * Lock and synchronize access to metar queue.
214      */
215     SGMutex mutex;
216     SGPthreadCond metar_cond;
217
218     /**
219      * Thread cleanup handler.
220      */
221     friend void metar_cleanup_handler( void* );
222 #endif // ENABLE_THREADS
223 };
224
225 #endif // _ENVIRONMENT_CTRL_HXX