1 // route_mgr.hxx - manage a route (i.e. a collection of waypoints)
2 //
3 // Written by Curtis Olson, started January 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
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 
24 #ifndef _ROUTE_MGR_HXX
25 #define _ROUTE_MGR_HXX 1
26 
27 #include <simgear/props/props.hxx>
28 #include <simgear/structure/subsystem_mgr.hxx>
29 
30 #include <Navaids/FlightPlan.hxx>
31 
32 // forward decls
33 class SGPath;
34 class PropertyWatcher;
35 
36 /**
37  * Top level route manager class
38  *
39  */
40 
41 class FGRouteMgr : public SGSubsystem,
42                    public flightgear::FlightPlan::Delegate
43 {
44 public:
45     FGRouteMgr();
46     ~FGRouteMgr();
47 
48     // Subsystem API.
49     void bind() override;
50     void init() override;
51     void postinit() override;
52     void unbind() override;
53     void update(double dt) override;
54 
55     // Subsystem identification.
staticSubsystemClassId()56     static const char* staticSubsystemClassId() { return "route-manager"; }
57 
58     bool isRouteActive() const;
59 
60     int currentIndex() const;
61 
62     void setFlightPlan(const flightgear::FlightPlanRef& plan);
63     flightgear::FlightPlanRef flightPlan() const;
64 
65     void clearRoute();
66 
67     flightgear::Waypt* currentWaypt() const;
68 
69     int numLegs() const;
70 
71     // deprecated
numWaypts() const72     int numWaypts() const
73     { return numLegs(); }
74 
75     // deprecated
76     flightgear::Waypt* wayptAtIndex(int index) const;
77 
78     SGPropertyNode_ptr wayptNodeAtIndex(int index) const;
79 
80     void removeLegAtIndex(int aIndex);
81 
82     /**
83      * Activate a built route. This checks for various mandatory pieces of
84      * data, such as departure and destination airports, and creates waypoints
85      * for them on the route structure.
86      *
87      * returns true if the route was activated successfully, or false if the
88      * route could not be activated for some reason
89      */
90     bool activate();
91 
92     /**
93      * deactivate the route if active
94      */
95     void deactivate();
96 
97     /**
98      * Set the current waypoint to the specified index.
99      */
100     void jumpToIndex(int index);
101 
102     bool saveRoute(const SGPath& p);
103     bool loadRoute(const SGPath& p);
104 
105     flightgear::WayptRef waypointFromString(const std::string& target);
106 
107 private:
108     bool commandDefineUserWaypoint(const SGPropertyNode * arg, SGPropertyNode * root);
109     bool commandDeleteUserWaypoint(const SGPropertyNode * arg, SGPropertyNode * root);
110 
111     flightgear::FlightPlanRef _plan;
112 
113     time_t _takeoffTime;
114     time_t _touchdownTime;
115 
116     // automatic inputs
117     SGPropertyNode_ptr magvar;
118 
119     // automatic outputs
120     SGPropertyNode_ptr departure; ///< departure airport information
121     SGPropertyNode_ptr destination; ///< destination airport information
122     SGPropertyNode_ptr alternate; ///< alternate airport information
123     SGPropertyNode_ptr cruise; ///< cruise information
124 
125     SGPropertyNode_ptr totalDistance;
126     SGPropertyNode_ptr distanceToGo;
127     SGPropertyNode_ptr ete;
128     SGPropertyNode_ptr elapsedFlightTime;
129 
130     SGPropertyNode_ptr active;
131     SGPropertyNode_ptr airborne;
132 
133     SGPropertyNode_ptr wp0;
134     SGPropertyNode_ptr wp1;
135     SGPropertyNode_ptr wpn;
136 
137 
138     SGPropertyNode_ptr _pathNode;
139     SGPropertyNode_ptr _currentWpt;
140 
141 
142     /**
143      * Signal property to notify people that the route was edited
144      */
145     SGPropertyNode_ptr _edited;
146 
147     /**
148      * Signal property to notify when the last waypoint is reached
149      */
150     SGPropertyNode_ptr _finished;
151 
152     SGPropertyNode_ptr _flightplanChanged;
153 
154     void setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance);
155 
156     /**
157      * retrieve the cached path distance along a leg
158      */
159     double cachedLegPathDistanceM(int index) const;
160     double cachedWaypointPathTotalDistance(int index) const;
161 
162     class InputListener : public SGPropertyChangeListener {
163     public:
InputListener(FGRouteMgr * m)164         InputListener(FGRouteMgr *m) : mgr(m) {}
165         virtual void valueChanged (SGPropertyNode * prop);
166     private:
167         FGRouteMgr *mgr;
168     };
169 
170     SGPropertyNode_ptr input;
171     SGPropertyNode_ptr weightOnWheels;
172     SGPropertyNode_ptr groundSpeed;
173 
174     InputListener *listener;
175     SGPropertyNode_ptr mirror;
176 
177     /**
178      * Helper to keep various pieces of state in sync when the route is
179      * modified (waypoints added, inserted, removed). Notably, this fires the
180      * 'edited' signal.
181      */
182     void waypointsChanged() override;
183 
184     void update_mirror();
185 
186     void currentWaypointChanged() override;
187 
188     // tied getters and setters
189     std::string getDepartureICAO() const;
190     std::string getDepartureName() const;
191     void setDepartureICAO(const std::string& aIdent);
192 
193     std::string getDepartureRunway() const;
194     void setDepartureRunway(const std::string& aIdent);
195 
196     std::string getSID() const;
197     void setSID(const std::string& aIdent);
198 
199     std::string getDestinationICAO() const;
200     std::string getDestinationName() const;
201     void setDestinationICAO(const std::string& aIdent);
202 
203     std::string getDestinationRunway() const;
204     void setDestinationRunway(const std::string& aIdent);
205 
206     std::string getApproach() const;
207     void setApproach(const std::string& aIdent);
208 
209     std::string getSTAR() const;
210     void setSTAR(const std::string& aIdent);
211 
212     double getDepartureFieldElevation() const;
213     double getDestinationFieldElevation() const;
214 
215     int getCruiseAltitudeFt() const;
216     void setCruiseAltitudeFt(int ft);
217 
218     int getCruiseFlightLevel() const;
219     void setCruiseFlightLevel(int fl);
220 
221     int getCruiseSpeedKnots() const;
222     void setCruiseSpeedKnots(int kts);
223 
224     double getCruiseSpeedMach() const;
225     void setCruiseSpeedMach(double m);
226 
227     std::string getAlternate() const;
228     std::string getAlternateName() const;
229     void setAlternate(const std::string &icao);
230 };
231 
232 #endif // _ROUTE_MGR_HXX
233