| 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 |
# EIS Controller |
| 18 |
var EISController = |
| 19 |
{ |
| 20 |
new : func (page, svg) |
| 21 |
{ |
| 22 |
var obj = { |
| 23 |
parents : [ EISController ], |
| 24 |
_crsrToggle : 0, |
| 25 |
_recipient : nil, |
| 26 |
_page : page, |
| 27 |
}; |
| 28 |
|
| 29 |
return obj; |
| 30 |
}, |
| 31 |
|
| 32 |
|
| 33 |
# Function to handle the data provided from the EngineData Emesary Notification. |
| 34 |
# This implementation assumes a vector containing a single engine. |
| 35 |
handleEngineData : func (engineData) { |
| 36 |
assert(size(engineData) > 0, "handleEngineData expects vector of hash"); |
| 37 |
var data = engineData[0]; |
| 38 |
|
| 39 |
# Sanitize data |
| 40 |
var elements = [ |
| 41 |
"RPM", |
| 42 |
"Man", |
| 43 |
"MBusVolts", |
| 44 |
"MBattAmps", |
| 45 |
"EngineHours", |
| 46 |
"FuelFlowGPH", |
| 47 |
"OilPressurePSI", |
| 48 |
"OilTemperatureF", |
| 49 |
"EGTNorm", |
| 50 |
"CHTDegF", |
| 51 |
"VacuumSuctionInHG"]; |
| 52 |
|
| 53 |
foreach (var val; elements) { |
| 54 |
if (data[val] == nil) data[val] = 0; |
| 55 |
} |
| 56 |
|
| 57 |
# Display it |
| 58 |
me._page.updateEngineData(data); |
| 59 |
return emesary.Transmitter.ReceiptStatus_OK; |
| 60 |
}, |
| 61 |
|
| 62 |
# Function to handle the data provided from the FuelData Emesary Notification. |
| 63 |
# This implementation assumes a vector containing hashes of "FuelUSGal" entries |
| 64 |
handleFuelData : func (fuelData) { |
| 65 |
assert(size(fuelData) > 1, "handleEngineData expects vector of size > 1"); |
| 66 |
var data = {}; |
| 67 |
data["LeftFuelUSGal"] = (fuelData[0]["FuelUSGal"] or 0); |
| 68 |
data["RightFuelUSGal"] = (fuelData[1]["FuelUSGal"] or 0); |
| 69 |
|
| 70 |
# Display it |
| 71 |
me._page.updateFuelData(data); |
| 72 |
return emesary.Transmitter.ReceiptStatus_OK; |
| 73 |
}, |
| 74 |
|
| 75 |
RegisterWithEmesary : func(transmitter = nil){ |
| 76 |
if (transmitter == nil) |
| 77 |
transmitter = emesary.GlobalTransmitter; |
| 78 |
|
| 79 |
if (me._recipient == nil){ |
| 80 |
me._recipient = emesary.Recipient.new("EISController_" ~ me._page.device.designation); |
| 81 |
var pfd_obj = me._page.device; |
| 82 |
var controller = me; |
| 83 |
me._recipient.Receive = func(notification) |
| 84 |
{ |
| 85 |
if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and |
| 86 |
notification.Event_Id == notifications.PFDEventNotification.EngineData and |
| 87 |
notification.EventParameter.Id == "EngineData") |
| 88 |
{ |
| 89 |
return controller.handleEngineData(notification.EventParameter.Value); |
| 90 |
} |
| 91 |
|
| 92 |
if (notification.NotificationType == notifications.PFDEventNotification.DefaultType and |
| 93 |
notification.Event_Id == notifications.PFDEventNotification.FuelData and |
| 94 |
notification.EventParameter.Id == "FuelData") |
| 95 |
{ |
| 96 |
return controller.handleFuelData(notification.EventParameter.Value); |
| 97 |
} |
| 98 |
|
| 99 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 100 |
}; |
| 101 |
} |
| 102 |
transmitter.Register(me._recipient); |
| 103 |
me.transmitter = transmitter; |
| 104 |
}, |
| 105 |
DeRegisterWithEmesary : func(transmitter = nil){ |
| 106 |
# remove registration from transmitter; but keep the recipient once it is created. |
| 107 |
if (me.transmitter != nil) |
| 108 |
me.transmitter.DeRegister(me._recipient); |
| 109 |
me.transmitter = nil; |
| 110 |
}, |
| 111 |
|
| 112 |
# Reset controller if required when the page is displayed or hidden |
| 113 |
ondisplay : func() { |
| 114 |
me.RegisterWithEmesary(); |
| 115 |
}, |
| 116 |
offdisplay : func() { |
| 117 |
me.DeRegisterWithEmesary(); |
| 118 |
}, |
| 119 |
|
| 120 |
setFuelQuantity : func(val) { |
| 121 |
me.sendFuelUpdateNotification("SetFuelQuantity", val); |
| 122 |
}, |
| 123 |
|
| 124 |
updateFuelQuantity : func(val) { |
| 125 |
me.sendFuelUpdateNotification("UpdateFuelQuantity", val); |
| 126 |
}, |
| 127 |
|
| 128 |
# Send an update to fuel quantities |
| 129 |
sendFuelUpdateNotification : func(type, val) |
| 130 |
{ |
| 131 |
# Use Emesary to set the default DTO waypoint |
| 132 |
var notification = notifications.PFDEventNotification.new( |
| 133 |
"MFD", |
| 134 |
me._page.mfd.getDeviceID(), |
| 135 |
notifications.PFDEventNotification.FuelData, |
| 136 |
{Id: type, Value: val}); |
| 137 |
|
| 138 |
var response = me.transmitter.NotifyAll(notification); |
| 139 |
if (me.transmitter.IsFailed(response)) print("Failed to set Fuel Data notification " ~ type ~ " " ~ val); |
| 140 |
}, |
| 141 |
|
| 142 |
}; |