EISController.nas 4.4 KB

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