Debian Codegraph @

Aircraft/Instruments-3d/FG1000/Nasal/Interfaces/GenericFuelInterface.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 # Emesary interface to set fuel information, mainly updating fuel levels.
18 #
19
20 var GenericFuelInterface = {
21
22 new : func ()
23 {
24 var obj = { parents : [ GenericFuelInterface ] };
25
26 # Emesary
27 obj._recipient = nil;
28 obj._transmitter = emesary.GlobalTransmitter;
29 obj._registered = 0;
30
31 return obj;
32 },
33
34 # Simply update the fuel quantities with the increment. Note that we assume
35 # the increment is added to both left and right tanks, rather than being a
36 # total quanity split between tanks.
37 updateFuelQuantity : func(incr)
38 {
39 var fuel = getprop("/consumables/fuel/tank[0]/fg1000-indicated-level-gal_us") + incr;
40 setprop("/consumables/fuel/tank[0]/fg1000-indicated-level-gal_us", fuel);
41
42 fuel = getprop("/consumables/fuel/tank[1]/fg1000-indicated-level-gal_us") + incr;
43 setprop("/consumables/fuel/tank[1]/fg1000-indicated-level-gal_us", fuel);
44 },
45
46 # Set the fuel quantities. Note that we assume we are setting the value
47 # to both tanks, rather than splitting between them.
48 setFuelQuantity : func(val)
49 {
50 setprop("/consumables/fuel/tank[0]/fg1000-indicated-level-gal_us", val);
51 setprop("/consumables/fuel/tank[1]/fg1000-indicated-level-gal_us", val);
52 },
53
54 RegisterWithEmesary : func()
55 {
56 if (me._recipient == nil){
57 me._recipient = emesary.Recipient.new("FuelInterface");
58 var controller = me;
59
60 # Note that unlike the various keys, this data isn't specific to a particular
61 # Device - it's shared by all. Hence we don't check for the notificaiton
62 # Device_Id.
63 me._recipient.Receive = func(notification)
64 {
65 if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and
66 notification.Event_Id == notifications.PFDEventNotification.FuelData and
67 notification.EventParameter != nil)
68 {
69 var id = notification.EventParameter.Id;
70
71 if (id == "UpdateFuelQuantity") {
72 notification.EventParameter.Value = controller.updateFuelQuantity(notification.EventParameter.Value);
73 return emesary.Transmitter.ReceiptStatus_Finished;
74 }
75 if (id == "SetFuelQuantity") {
76 notification.EventParameter.Value = controller.setFuelQuantity(notification.EventParameter.Value);
77 return emesary.Transmitter.ReceiptStatus_Finished;
78 }
79 }
80 return emesary.Transmitter.ReceiptStatus_NotProcessed;
81 };
82 }
83
84 me._transmitter.Register(me._recipient);
85 me._registered = 1;
86 },
87
88 DeRegisterWithEmesary : func()
89 {
90 # remove registration from transmitter; but keep the recipient once it is created.
91 if (me._registered == 1) me._transmitter.DeRegister(me._recipient);
92 me._registered = 0;
93 },
94
95
96 start : func() {
97 me.RegisterWithEmesary();
98 },
99 stop : func() {
100 me.DeRegisterWithEmesary();
101 },
102
103 };