Debian Codegraph @

Aircraft/Instruments-3d/FG1000/Nasal/Interfaces/NavDataInterface.nas

raw
1 # Copyright 2018 Stuart Buchanan
2 # This file is part of FlightGear.
3 #
4 # FlightGear is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # FlightGear is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with FlightGear. If not, see <http://www.gnu.org/licenses/>.
16 #
17 #
18 # Emesary interface to access nav data such as airport information, fixes etc.
19 #
20
21 var NavDataInterface = {
22
23 # Valid FMS Modes
24
25 FMS_MODES : {
26 direct : 1,
27 leg : 1,
28 obs : 1,
29 },
30
31 new : func ()
32 {
33 var obj = { parents : [ NavDataInterface ] };
34
35 # Emesary
36 obj._recipient = nil;
37 obj._transmitter = emesary.GlobalTransmitter;
38 obj._registered = 0;
39 obj._defaultDTO = "";
40
41 # List of recently use waypoints
42 obj._recentWaypoints = std.Vector.new();
43
44 # Some methods are really slow the first time they are called, typically
45 # because they are populating some cached database. Call them now to reduce
46 # freezes in flight.
47 var apts = obj.getNearestAirports();
48 var navdata = obj.getNavDataWithinRange({type : "VOR"});
49 return obj;
50 },
51
52 # Find the airports within 200nm and return them.
53 getNearestAirports : func()
54 {
55 # To make this more efficient for areas with a high density of airports, we'll try
56 # a small radius first and expand until we have reached 200nm or have 25 airports.
57 var radius = 0;
58 var apts = [];
59
60 while ((radius <= 200) and (size(apts) < 25)) {
61 radius = radius + 50;
62 apts = findAirportsWithinRange(radius);
63 }
64
65 if (size(apts) > 25) {
66 apts = subvec(apts, 0, 25);
67 }
68
69 return apts;
70 },
71
72 # Find the nearest nav aids of a given type within 200nm, to a maximum of 25.
73 getNavDataWithinRange: func(params)
74 {
75 # To make this more efficient for areas with a high density of fixes, we'll try
76 # a small radius first and expand until we have reached 200nm or have 25 nav aids.
77 var radius = 0;
78 var navdata = [];
79
80 while ((radius <= 200) and (size(navdata) < 25)) {
81 radius = radius + 50;
82 if ((params["lat"] == nil) and (params["lon"] == nil)) {
83 navdata = findNavaidsWithinRange(radius, params.type);
84 } else {
85 navdata = findNavaidsWithinRange(params.lat, params.lon, radius, params.type);
86 }
87 }
88
89 if (size(navdata) > 25) {
90 navdata = subvec(navdata, 0, 25);
91 }
92
93 return navdata;
94 },
95
96 # Find a specific airport by ID. Return an array of airport objects
97 getAirportById : func(id)
98 {
99 var apt = findAirportsByICAO(id, "airport");
100
101 if ((apt != nil) and (! me._recentWaypoints.contains(id))) {
102 me._recentWaypoints.insert(0, id);
103 }
104
105 return apt;
106 },
107
108 # Find an arbritrary piece of nav data by ID. This searches based on the
109 # current location and returns an array of objects that match the id.
110 getNavDataById : func (id)
111 {
112 # Check for airport first
113 var navdata = findAirportsByICAO(id, "airport");
114
115 # Check for Navaids.
116 if (size(navdata) == 0) navdata = findNavaidsByID(id);
117
118 # Check for fix.
119 if (size(navdata) == 0) navdata = findFixesByID(id);
120
121 # Check for a pseudo-fix in the flightplan
122 if (size(navdata) == 0) {
123 var fp = flightplan();
124 if (fp != nil) {
125 for (var i = 0; i < fp.getPlanSize(); i = i +1) {
126 var wp = fp.getWP(i);
127 if (wp.wp_name == id) {
128 append(navdata, wp);
129 }
130 }
131 }
132 }
133
134 if ((size(navdata) > 0) and (! me._recentWaypoints.contains(id))) {
135 me._recentWaypoints.insert(0, id);
136 }
137
138 return navdata;
139 },
140
141 # Find a Nav Aid by ID. This searches based on the
142 # current location and returns an array of objects that match the id.
143 getNavAidById : func (params)
144 {
145 var id = params.id;
146 var type = "all";
147 if (params.type != nil) type = params.type;
148
149 var navdata = findNavaidsByID(id, type);
150 if ((size(navdata) > 0) and (! me._recentWaypoints.contains(id))) {
151 me._recentWaypoints.insert(0, id);
152 }
153 return navdata;
154 },
155
156 # Retrieve the current flightplan and return it
157 getFlightplan : func ()
158 {
159 return flightplan();
160 },
161
162 # Retrieve the checklists for this aircraft.
163 getChecklists : func()
164 {
165 var checklists = {};
166 var checklistprops = props.globals.getNode("/sim/checklists");
167
168 if (checklistprops == nil) return nil;
169
170 var groups = checklistprops.getChildren("group");
171
172 if (size(groups) > 0) {
173 foreach (var group; groups) {
174 var grp = group.getNode("name", 1).getValue();
175 checklists[grp] = {};
176 foreach (var chklist; group.getChildren("checklist")) {
177 var items = [];
178 var title = chklist.getNode("title", 1).getValue();
179
180 # Checklists can optionally be broken down into individual pages.
181 foreach (var pg; chklist.getChildren("page")) {
182 foreach (var item; pg.getChildren("item")) {
183 var name = item.getNode("name", 1).getValue();
184 var value = item.getNode("value", 1).getValue();
185 append(items, { Name : name, Value: value, Checked: 0 });
186 }
187 }
188
189 foreach (var item; chklist.getChildren("item")) {
190 var name = item.getNode("name", 1).getValue();
191 var value = item.getNode("value", 1).getValue();
192 append(items, { Name : name, Value: value, Checked: 0 });
193 }
194
195 # items now contains a list of all the checklists for
196 checklists[grp][title] = items;
197 }
198 }
199 } else {
200 # Checklist doesn't contain any groups, so try to split into Standard
201 # and Emergency groups by looking at the checklist titles.
202
203 foreach (var chklist; checklistprops.getChildren("checklist")) {
204 var title = chklist.getNode("title", 1).getValue();
205 var grp = "Standard";
206 var items = [];
207 if (find("emergency", string.lc(title)) != -1) {
208 grp = "EMERGENCY";
209 }
210
211 # Checklists can optionally be broken down into individual pages.
212 foreach (var pg; chklist.getChildren("page")) {
213 foreach (var item; pg.getChildren("item")) {
214 var name = item.getNode("name", 1).getValue();
215 var value = item.getNode("value", 1).getValue();
216 append(items, { Name : name, Value: value, Checked: 0 });
217 }
218 }
219
220 foreach (var item; chklist.getChildren("item")) {
221 var name = item.getNode("name", 1).getValue();
222 var value = item.getNode("value", 1).getValue();
223 append(items, { Name : name, Value: value, Checked: 0 });
224 }
225
226 # items now contains a list of all the checklists for
227 checklists[grp][title] = items;
228 }
229 }
230
231 return checklists;
232 },
233
234 insertWaypoint : func (data)
235 {
236 assert(data["index"] != nil, "InsertWaypoint message with no index parameter");
237 assert(data["wp"] != nil, "InsertWaypoint message with no wp parameter");
238
239 var wp = data["wp"];
240 var idx = int(data["index"]);
241
242 # Simple data verification that we have the parameters we need
243 assert(idx != nil, "InsertWaypoint index parameter does not contain an integer index");
244 assert(wp.id != nil, "InsertWaypoint wp parameter does not contain an id");
245 assert(wp.lat != nil, "InsertWaypoint wp parameter does not contain a lat");
246 assert(wp.lon != nil, "InsertWaypoint wp parameter does not contain an lon");
247
248 var newwp = createWP(wp.lat, wp.lon, wp.id);
249
250 var fp = flightplan();
251 fp.insertWP(newwp, idx);
252
253 # Set a suitable name.
254 if ((fp.id == nil) or (fp.id == "default-flightplan")) {
255 var from = "????";
256 var dest = "????";
257
258 if ((fp.getWP(0) != nil) and (fp.getWP(0).wp_name != nil)) {
259 from = fp.getWP(0).wp_name;
260 }
261
262 if ((fp.getWP(fp.getPlanSize() -1) != nil) and (fp.getWP(fp.getPlanSize() -1).wp_name != nil)) {
263 dest = fp.getWP(fp.getPlanSize() -1).wp_name;
264 }
265
266 if (fp.departure != nil) from = fp.departure.id;
267 if (fp.destination != nil) dest = fp.destination.id;
268 fp.id == from ~ " / " ~ dest;
269 }
270
271 # Activate flightplan
272 if (fp.getPlanSize() == 2) fgcommand("activate-flightplan", props.Node.new({"activate": 1}));
273 },
274
275 # Retrieve the Airway waypoints on the current leg.
276 getAirwayWaypoints : func() {
277 var fp = flightplan();
278 if (fp != nil) {
279 var current_wp = fp.currentWP();
280 if ((current_wp != nil) and (fp.indexOfWP(current_wp) > 0)) {
281 var last_wp = fp.getWP(fp.indexOfWP(current_wp) -1);
282 return airwaysRoute(last_wp, current_wp);
283 }
284 }
285 return nil;
286 },
287
288 # Return the recently seen waypoints, collected from previous calls to
289 # other nav data functions
290 getRecentWaypoints : func()
291 {
292 return me._recentWaypoints.vector;
293 },
294
295 # Add an ID to the list of recent waypoints
296 addRecentWaypoint : func(id)
297 {
298 if ((id != nil) and (! me._recentWaypoints.contains(id))) {
299 me._recentWaypoints.insert(0, id);
300 }
301 },
302
303 # Return the array of user waypoints. TODO
304 getUserWaypoints : func()
305 {
306 return [];
307 },
308
309 # Set up a DirectTo a given ID, with optional VNAV altitude offset.
310 setDirectTo : func(param)
311 {
312 var id = param.id;
313 var alt_ft = param.alt_ft;
314 var offset_nm = param.offset_nm;
315
316 var fp = flightplan();
317 var wp_idx = -1;
318
319 if (fp != nil) {
320 # We've already got a flightplan, so see if this WP already exists.
321
322 for (var i = 0; i < fp.getPlanSize(); i = i + 1) {
323 var wp = fp.getWP(i);
324 if ((wp.wp_name != nil) and (wp.wp_name == id)) {
325 # OK, we're assuming that if the names actually match, then
326 # they refer to the same ID. So direct to that index.
327 wp_idx = i;
328 break;
329 }
330 }
331 }
332
333 if (wp_idx != -1) {
334 # Found the waypoint in the plan, so use that as the DTO.
335 var wp = fp.getWP(wp_idx);
336 setprop("/instrumentation/gps/scratch/ident", wp.wp_name);
337 setprop("/instrumentation/gps/scratch/altitude-ft", 0);
338 setprop("/instrumentation/gps/scratch/latitude-deg", wp.lat);
339 setprop("/instrumentation/gps/scratch/longitude-deg", wp.lon);
340 } else {
341 # No flightplan, or waypoint not found, so use the GPS DTO function.
342 # Hokey property-based interface.
343 setprop("/instrumentation/gps/scratch/ident", id);
344 setprop("/instrumentation/gps/scratch/altitude-ft", alt_ft);
345 setprop("/instrumentation/gps/scratch/latitude-deg", getprop("/position/latitude-deg"));
346 setprop("/instrumentation/gps/scratch/longitude-deg", getprop("/position/longitude-deg"));
347 }
348
349 # Switch the GPS to DTO mode.
350 setprop("/instrumentation/gps/command", "direct");
351 },
352
353 setFMSMode : func(mode) {
354 if (NavDataInterface.FMS_MODES[mode] != nil) {
355 # mode is valid, so simply set it as the GPS command
356 setprop("/instrumentation/gps/command", mode);
357 } else {
358 die("Invalid FMS Mode " ~ mode);
359 }
360 },
361
362 # Return the current DTO location to use
363 getCurrentDTO : func()
364 {
365 return me._defaultDTO;
366 },
367
368 # Set the current DTO location to use
369 setDefaultDTO : func(id)
370 {
371 me._defaultDTO = id;
372 },
373
374
375 # Find the nearest Air Route Traffic Control Center or equivalent.
376 # As we don't have that data right now, we simply return some placeholder
377 getNearestATRCC : func()
378 {
379 var atrcc = {};
380 atrcc.name = "NONE AVAILABLE";
381 atrcc.lat = 0;
382 atrcc.lon = 0;
383 atrcc.brg = nil;
384 atrcc.dis = nil;
385 atrcc.freqs = []; # an array of frequencies for the ATRCC
386 return atrcc;
387 },
388
389 # Find the nearest Flight Service Station
390 # As we don't have that data right now, we simply return some placeholder
391 getNearestFSS : func()
392 {
393 var fss = {};
394 fss.name = "NONE AVAILABLE";
395 fss.lat = nil;
396 fss.lon = nil;
397 fss.brg = nil;
398 fss.dis = nil;
399 fss.freqs = []; # an array of frequencies for the ATRCC
400 return fss;
401 },
402
403 # Find the nearest weather information. We do this simply by picking up
404 # appropriate frequencies from the airports frequencies within 200nm.
405 getNearestWX : func()
406 {
407 # To make this more efficient for areas with a high density of airports, we'll try
408 # a small radius first and expand until we have reached 200nm or have 25 frequencies.
409 var radius = 0;
410 var freqs = [];
411
412 while ((radius <= 200) and (size(freqs) < 25)) {
413 freqs = [];
414 radius = radius + 50;
415 apts = findAirportsWithinRange(radius);
416 foreach (var apt; apts) {
417 var apt_comms = apt.comms();
418 if (size(apt_comms) > 0) {
419 # Airport has one or more frequencies assigned to it.
420 foreach (var c; apt_comms) {
421 if ((c.ident == "ATIS") or (c.ident == "ASOS")) {
422 var freq = {
423 id: apt.id,
424 type : c.ident,
425 freq: c.frequency,
426 lat : apt.lat,
427 lon : apt.lon,
428 };
429
430 append(freqs, freq);
431 }
432 }
433 }
434 }
435 }
436
437 return freqs;
438 },
439
440 RegisterWithEmesary : func()
441 {
442 if (me._recipient == nil){
443 me._recipient = emesary.Recipient.new("DataInterface");
444 var controller = me;
445
446 # Note that unlike the various keys, this data isn't specific to a particular
447 # Device - it's shared by all. Hence we don't check for the notificaiton
448 # Device_Id.
449 me._recipient.Receive = func(notification)
450 {
451 if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and
452 notification.Event_Id == notifications.PFDEventNotification.NavData and
453 notification.EventParameter != nil)
454 {
455 var id = notification.EventParameter.Id;
456
457 if (id == "NearestAirports") {
458 notification.EventParameter.Value = controller.getNearestAirports();
459 return emesary.Transmitter.ReceiptStatus_Finished;
460 }
461 if (id == "AirportByID") {
462 notification.EventParameter.Value = controller.getAirportById(notification.EventParameter.Value);
463 return emesary.Transmitter.ReceiptStatus_Finished;
464 }
465 if (id == "NavDataByID") {
466 notification.EventParameter.Value = controller.getNavDataById(notification.EventParameter.Value);
467 return emesary.Transmitter.ReceiptStatus_Finished;
468 }
469 if (id == "NavAidByID") {
470 notification.EventParameter.Value = controller.getNavAidById(notification.EventParameter.Value);
471 return emesary.Transmitter.ReceiptStatus_Finished;
472 }
473 if (id == "NavDataWithinRange") {
474 notification.EventParameter.Value = controller.getNavDataWithinRange(notification.EventParameter.Value);
475 return emesary.Transmitter.ReceiptStatus_Finished;
476 }
477 if (id == "Flightplan") {
478 notification.EventParameter.Value = controller.getFlightplan();
479 return emesary.Transmitter.ReceiptStatus_Finished;
480 }
481 if (id == "RecentWaypoints") {
482 notification.EventParameter.Value = controller.getRecentWaypoints();
483 return emesary.Transmitter.ReceiptStatus_Finished;
484 }
485 if (id == "AddRecentWaypoint") {
486 controller.addRecentWaypoint(notification.EventParameter.Value);
487 return emesary.Transmitter.ReceiptStatus_Finished;
488 }
489 if (id == "AirwayWaypoints") {
490 notification.EventParameter.Value = controller.getAirwayWaypoints();
491 return emesary.Transmitter.ReceiptStatus_Finished;
492 }
493 if (id == "UserWaypoints") {
494 notification.EventParameter.Value = controller.getUserWaypoints();
495 return emesary.Transmitter.ReceiptStatus_Finished;
496 }
497 if (id == "CurrentDTO") {
498 notification.EventParameter.Value = controller.getCurrentDTO();
499 return emesary.Transmitter.ReceiptStatus_Finished;
500 }
501 if (id == "SetDirectTo") {
502 controller.setDirectTo(notification.EventParameter.Value);
503 return emesary.Transmitter.ReceiptStatus_Finished;
504 }
505 if (id == "SetDefaultDTO") {
506 controller.setDefaultDTO(notification.EventParameter.Value);
507 return emesary.Transmitter.ReceiptStatus_Finished;
508 }
509 if (id == "SetFMSMode") {
510 controller.setFMSMode(notification.EventParameter.Value);
511 return emesary.Transmitter.ReceiptStatus_Finished;
512 }
513 if (id == "InsertWaypoint") {
514 controller.insertWaypoint(notification.EventParameter.Value);
515 return emesary.Transmitter.ReceiptStatus_Finished;
516 }
517 if (id == "GetChecklists") {
518 notification.EventParameter.Value = controller.getChecklists();
519 return emesary.Transmitter.ReceiptStatus_Finished;
520 }
521 if (id == "GetNearestATRCC") {
522 notification.EventParameter.Value = controller.getNearestATRCC();
523 return emesary.Transmitter.ReceiptStatus_Finished;
524 }
525 if (id == "GetNearestFSS") {
526 notification.EventParameter.Value = controller.getNearestFSS();
527 return emesary.Transmitter.ReceiptStatus_Finished;
528 }
529 if (id == "GetNearestWX") {
530 notification.EventParameter.Value = controller.getNearestWX();
531 return emesary.Transmitter.ReceiptStatus_Finished;
532 }
533 }
534 return emesary.Transmitter.ReceiptStatus_NotProcessed;
535 };
536 }
537
538 me._transmitter.Register(me._recipient);
539 me._registered = 1;
540 },
541
542 DeRegisterWithEmesary : func()
543 {
544 # remove registration from transmitter; but keep the recipient once it is created.
545 if (me._registered == 1) me._transmitter.DeRegister(me._recipient);
546 me._registered = 0;
547 },
548
549
550 start : func() {
551 me.RegisterWithEmesary();
552 },
553 stop : func() {
554 me.DeRegisterWithEmesary();
555 },
556
557 };