]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/autopilotgroup.cxx
FlightPlan activation, delegate hook.
[flightgear.git] / src / Autopilot / autopilotgroup.cxx
1 // autopilotgroup.cxx - an even more flexible, generic way to build autopilots
2 //
3 // Written by Torsten Dreyer
4 // Based heavily on work created by Curtis Olson, started January 2004.
5 //
6 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
7 // Copyright (C) 2010  Torsten Dreyer - Torsten (at) t3r (dot) de
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include "autopilot.hxx"
29 #include "autopilotgroup.hxx"
30
31 #include <string>
32 #include <vector>
33
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/structure/subsystem_mgr.hxx>
36 #include <simgear/structure/exception.hxx>
37 #include <Main/fg_props.hxx>
38 #include <boost/foreach.hpp>
39
40 using std::vector;
41 using simgear::PropertyList;
42 using FGXMLAutopilot::Autopilot;
43
44 class FGXMLAutopilotGroupImplementation:
45   public FGXMLAutopilotGroup
46 {
47   public:
48     FGXMLAutopilotGroupImplementation(const std::string& nodeName):
49       FGXMLAutopilotGroup(),
50       _nodeName(nodeName)
51     {}
52
53     virtual void addAutopilot( const std::string& name,
54                                SGPropertyNode_ptr apNode,
55                                SGPropertyNode_ptr config );
56     virtual void removeAutopilot( const std::string & name );
57     void init();
58     InitStatus incrementalInit();
59     void reinit();
60
61   private:
62     void initFrom( SGPropertyNode_ptr rootNode, const char * childName );
63     std::string _nodeName;
64
65 };
66
67 //------------------------------------------------------------------------------
68 void FGXMLAutopilotGroupImplementation::addAutopilot( const std::string& name,
69                                                       SGPropertyNode_ptr apNode,
70                                                       SGPropertyNode_ptr config )
71 {
72   if( has_subsystem(name) )
73   {
74     SG_LOG( SG_AUTOPILOT,
75             SG_ALERT,
76             "NOT adding duplicate " << _nodeName << " name '" << name << "'");
77     return;
78   }
79
80   Autopilot* ap = new Autopilot(apNode, config);
81   ap->set_name( name );
82
83   double updateInterval = config->getDoubleValue("update-interval-secs", 0.0);
84   set_subsystem( name, ap, updateInterval );
85 }
86
87 //------------------------------------------------------------------------------
88 void FGXMLAutopilotGroupImplementation::removeAutopilot(const std::string& name)
89 {
90   Autopilot* ap = static_cast<Autopilot*>(get_subsystem(name));
91   if( !ap )
92   {
93     SG_LOG( SG_AUTOPILOT,
94             SG_ALERT,
95             "CAN NOT remove unknown " << _nodeName << " '" << name << "'");
96     return;
97   }
98
99   remove_subsystem(name);
100 }
101
102 //------------------------------------------------------------------------------
103 void FGXMLAutopilotGroupImplementation::reinit()
104 {
105   SGSubsystemGroup::unbind();
106   clearSubsystems();
107
108   init();
109 }
110
111 //------------------------------------------------------------------------------
112 SGSubsystem::InitStatus FGXMLAutopilotGroupImplementation::incrementalInit()
113 {
114   init();
115   return INIT_DONE;
116 }
117
118 //------------------------------------------------------------------------------
119 void FGXMLAutopilotGroupImplementation::init()
120 {
121   initFrom(fgGetNode("/sim/systems"), _nodeName.c_str());
122
123   SGSubsystemGroup::bind();
124   SGSubsystemGroup::init();
125 }
126
127 //------------------------------------------------------------------------------
128 void FGXMLAutopilotGroupImplementation::initFrom( SGPropertyNode_ptr rootNode,
129                                                   const char* childName )
130 {
131   if( !rootNode )
132     return;
133
134   BOOST_FOREACH( SGPropertyNode_ptr autopilotNode,
135                  rootNode->getChildren(childName) )
136   {
137     SGPropertyNode_ptr pathNode = autopilotNode->getNode("path");
138     if( !pathNode )
139     {
140       SG_LOG
141       (
142         SG_AUTOPILOT,
143         SG_WARN,
144         "No configuration file specified for this " << childName << "!"
145       );
146       continue;
147     }
148
149     std::string apName;
150     SGPropertyNode_ptr nameNode = autopilotNode->getNode( "name" );
151     if( nameNode != NULL ) {
152         apName = nameNode->getStringValue();
153     } else {
154       std::ostringstream buf;
155       buf <<  "unnamed_autopilot_" << autopilotNode->getIndex();
156       apName = buf.str();
157     }
158
159     {
160       // check for duplicate names
161       std::string name = apName;
162       for( unsigned i = 0; get_subsystem( apName.c_str() ) != NULL; i++ ) {
163           std::ostringstream buf;
164           buf <<  name << "_" << i;
165           apName = buf.str();
166       }
167       if( apName != name )
168         SG_LOG
169         (
170           SG_AUTOPILOT,
171           SG_WARN,
172           "Duplicate " << childName << " configuration name " << name
173                                     << ", renamed to " << apName
174         );
175     }
176
177     addAutopilotFromFile(apName, autopilotNode, pathNode->getStringValue());
178   }
179 }
180
181 void FGXMLAutopilotGroup::addAutopilotFromFile( const std::string& name,
182                                                 SGPropertyNode_ptr apNode,
183                                                 const char* path )
184 {
185   SGPath config = globals->resolve_maybe_aircraft_path(path);
186   if( config.isNull() )
187   {
188     SG_LOG
189     (
190       SG_AUTOPILOT,
191       SG_ALERT,
192       "Cannot find property-rule configuration file '" << path << "'."
193     );
194     return;
195   }
196   SG_LOG
197   (
198     SG_AUTOPILOT,
199     SG_INFO,
200     "Reading property-rule configuration from " << config.str()
201   );
202
203   try
204   {
205     SGPropertyNode_ptr configNode = new SGPropertyNode();
206     readProperties( config.str(), configNode );
207
208     SG_LOG(SG_AUTOPILOT, SG_INFO, "adding  property-rule subsystem " << name);
209     addAutopilot(name, apNode, configNode);
210   }
211   catch (const sg_exception& e)
212   {
213     SG_LOG
214     (
215       SG_AUTOPILOT,
216       SG_ALERT,
217       "Failed to load property-rule configuration: " << config.str()
218                                              << ": " << e.getMessage()
219     );
220     return;
221   }
222 }
223
224 //------------------------------------------------------------------------------
225 FGXMLAutopilotGroup*
226 FGXMLAutopilotGroup::createInstance(const std::string& nodeName)
227 {
228   return new FGXMLAutopilotGroupImplementation(nodeName);
229 }