Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.24 KB | None | 0 0
  1. -- Simbrief helper
  2. -- Alexander Garzon Jan - 2020
  3.  
  4. -- Description: pulls OFP data from simbrief (after you generate your flight plan) using your username (API call)
  5. -- This data wil be used: flight level, block fuel, payload, zfw, destination altitude, etc..
  6.  
  7. -- TODO:
  8. -- * Get RWY magnetic heading
  9.  
  10. -- Modules
  11. --require("LuaXml") // No longer required
  12. local xml2lua = require("xml2lua")
  13. local handler = require("xmlhandler.tree")
  14. -- replace mbox.print() with print()
  15. -- require("mbox")
  16. -- print = mbox.print
  17.  
  18. -- Useful debug tool
  19. -- local inspect = require 'inspect'
  20.  
  21. -- Variables
  22. local socket = require "socket"
  23. local http = require "socket.http"
  24. local LIP = require("LIP");
  25.  
  26. local SettingsFile = "simbrief_helper.ini"
  27. local SimbriefXMLFile = "simbrief.xml"
  28. local sbUser = ""
  29. local fontBig = false
  30.  
  31. local DataOfp = {}
  32. local Settings = {}
  33.  
  34. local iTOC = 1
  35. local originICAO, origElevation, origRwy, origName, origMetar, destICAO, destElevation, destRwy, destName, destMetar, cpt, callsign, aircraftName, units, routeDist, ete, route, level, rampFuel, minTakeOff, planFuel, adjusted_blockfuel, reserveFuel, payload, pax, zfw, costIndex, avg_isa_dev, TOC_wind_spd, TOC_wind_dir, TOC_oat, TOC_oat_isa_dev
  36.  
  37. local clickFetch = false
  38.  
  39. if not SUPPORTS_FLOATING_WINDOWS then
  40. -- to make sure the script doesn't stop old FlyWithLua versions
  41. logMsg("imgui not supported by your FlyWithLua version")
  42. return
  43. end
  44.  
  45. function readSettings()
  46. Settings = LIP.load(SCRIPT_DIRECTORY..SettingsFile);
  47. if Settings.simbrief.username ~= nil then
  48. sbUser = Settings.simbrief.username
  49. end
  50. end
  51.  
  52. function saveSettings(newSettings)
  53. LIP.save(SCRIPT_DIRECTORY..SettingsFile, newSettings);
  54. end
  55.  
  56. function fetchData()
  57. if sbUser == nil then
  58. logMsg("No simbrief username has been configured")
  59. return false
  60. end
  61.  
  62. local webRespose, err = http.request("http://www.simbrief.com/api/xml.fetcher.php?username=" .. sbUser)
  63. -- would be nice to have a try-cath here
  64. assert(webRespose, err)
  65.  
  66. local f = io.open(SCRIPT_DIRECTORY..SimbriefXMLFile, "w")
  67. f:write(webRespose)
  68. f:close()
  69.  
  70. logMsg("Simbrief XML data downloaded")
  71. return true
  72. end
  73.  
  74. -- Older LuaXML method
  75.  
  76. -- function readXML()
  77. -- local xfile = xml.load(SCRIPT_DIRECTORY..SimbriefXMLFile)
  78. -- DataOfp["Status"] = xfile:find("status")[1]
  79. -- -- validate the file is there
  80.  
  81. -- if DataOfp["Status"] ~= "Success" then
  82. -- logMsg("XML status is not success")
  83. -- return false
  84. -- end
  85.  
  86. -- DataOfp["Origin"] = xfile:find("origin"):find("icao_code")[1]
  87. -- DataOfp["Origlevation"] = xfile:find("origin"):find("elevation")[1]
  88. -- DataOfp["OrigName"] = xfile:find("origin"):find("name")[1]
  89. -- DataOfp["OrigRwy"] = xfile:find("origin"):find("plan_rwy")[1]
  90. -- DataOfp["OrigMetar"] = xfile:find("orig_metar")[1]
  91.  
  92. -- DataOfp["Destination"] = xfile:find("destination"):find("icao_code")[1]
  93. -- DataOfp["DestElevation"] = xfile:find("destination"):find("elevation")[1]
  94. -- DataOfp["DestName"] = xfile:find("destination"):find("name")[1]
  95. -- DataOfp["DestRwy"] = xfile:find("destination"):find("plan_rwy")[1]
  96. -- DataOfp["DestMetar"] = xfile:find("dest_metar")[1]
  97.  
  98. -- DataOfp["Cpt"] = xfile:find("cpt")[1]
  99. -- DataOfp["Callsign"] = xfile:find("callsign")[1]
  100. -- DataOfp["Aircraft"] = xfile:find("aircraft"):find("name")[1]
  101. -- DataOfp["Units"] = xfile:find("units")[1]
  102. -- DataOfp["Distance"] = xfile:find("route_distance")[1]
  103. -- DataOfp["Ete"] = xfile:find("est_time_enroute")[1]
  104. -- DataOfp["Route"] = xfile:find("route")[1]
  105. -- DataOfp["Level"] = xfile:find("initial_altitude")[1]
  106. -- DataOfp["RampFuel"] = xfile:find("plan_ramp")[1]
  107. -- DataOfp["MinTakeoff"] = xfile:find("min_takeoff")[1]
  108. -- DataOfp["PlanFuel"] = xfile:find("enroute_burn")[1] + xfile:find("contingency")[1]
  109. -- DataOfp["ReserveFuel"] = xfile:find("reserve")[1] + xfile:find("alternate_burn")[1]
  110. -- DataOfp["Payload"] = xfile:find("payload")[1]
  111. -- DataOfp["Pax"] = xfile:find("passengers")[1]
  112. -- DataOfp["Zfw"] = xfile:find("est_zfw")[1]
  113. -- DataOfp["CostIndex"] = xfile:find("costindex")[1]
  114. -- local TOC = xfile:find("navlog"):find(nil,nil,nil,"TOC");
  115. -- DataOfp["CrzWindDir"] = TOC:find("wind_dir")[1]
  116. -- DataOfp["CrzWindSpd"] = TOC:find("wind_spd")[1]
  117. -- DataOfp["CrzTemp"] = TOC:find("oat")[1]
  118. -- DataOfp["CrzTempDev"] = TOC:find("oat_isa_dev")[1]
  119. -- DataOfp["AvgTempDev"] = xfile:find("avg_temp_dev")[1]
  120.  
  121. -- return true
  122. -- end
  123.  
  124. function parseXML()
  125. local xfile = xml2lua.loadFile(SCRIPT_DIRECTORY..SimbriefXMLFile)
  126.  
  127. local parser = xml2lua.parser(handler)
  128. parser:parse(xfile)
  129.  
  130. -- find TOC
  131. while handler.root.OFP.navlog.fix[iTOC].ident ~= "TOC" do
  132. iTOC = iTOC + 1
  133. end
  134.  
  135. originICAO = handler.root.OFP.origin.icao_code
  136. origElevation = handler.root.OFP.origin.elevation
  137. origRwy = handler.root.OFP.origin.plan_rwy
  138. origName = handler.root.OFP.origin.name
  139. origMetar = handler.root.OFP.weather.orig_metar
  140.  
  141. destICAO = handler.root.OFP.destination.icao_code
  142. destElevation = handler.root.OFP.destination.elevation
  143. destRwy = handler.root.OFP.destination.plan_rwy
  144. destName = handler.root.OFP.destination.name
  145. destMetar = handler.root.OFP.weather.dest_metar
  146.  
  147. cpt = handler.root.OFP.crew.cpt
  148. callsign = handler.root.OFP.atc.callsign
  149. aircraftName = handler.root.OFP.aircraft.name
  150. units = handler.root.OFP.params.units
  151. routeDist = handler.root.OFP.general.route_distance
  152. ete = handler.root.OFP.times.est_time_enroute
  153. route = handler.root.OFP.general.route
  154. level = handler.root.OFP.general.initial_altitude
  155. rampFuel = handler.root.OFP.fuel.plan_ramp
  156. minTakeOff = handler.root.OFP.fuel.min_takeoff
  157. planFuel = handler.root.OFP.fuel.enroute_burn + handler.root.OFP.fuel.contingency
  158. adjusted_blockfuel = (math.ceil(planFuel/100) * 100) + 100
  159. reserveFuel = handler.root.OFP.fuel.reserve + handler.root.OFP.fuel.alternate_burn
  160. payload = handler.root.OFP.weights.payload
  161. pax = handler.root.OFP.weights.pax_count
  162. zfw = handler.root.OFP.weights.est_zfw
  163. costIndex = handler.root.OFP.general.costindex
  164. avg_isa_dev = handler.root.OFP.general.avg_temp_dev
  165.  
  166. TOC_wind_spd = handler.root.OFP.navlog.fix[iTOC].wind_spd
  167. TOC_wind_dir = handler.root.OFP.navlog.fix[iTOC].wind_dir
  168. TOC_oat = handler.root.OFP.navlog.fix[iTOC].oat
  169. TOC_oat_isa_dev = handler.root.OFP.navlog.fix[iTOC].oat_isa_dev
  170. end
  171.  
  172. function timeConvert(seconds)
  173. local seconds = tonumber(seconds)
  174.  
  175. if seconds <= 0 then
  176. return "no data";
  177. else
  178. hours = string.format("%02.f", math.floor(seconds/3600));
  179. mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
  180. return hours..":"..mins
  181. end
  182. end
  183.  
  184. function sb_on_build(sb_wnd, x, y)
  185. if fontBig == true then
  186. imgui.SetWindowFontScale(1.2)
  187. else
  188. imgui.SetWindowFontScale(1)
  189. end
  190.  
  191. if cpt == nil then
  192. imgui.TextUnformatted(string.format("Enter your simbrief username below and then click the button."))
  193. else
  194. imgui.TextUnformatted(string.format("Welcome, %s (%s)", cpt, callsign))
  195. end
  196.  
  197. if imgui.TreeNode("Settings") then
  198. -- INPUT
  199. local changed, userNew = imgui.InputText("Simbrief Username", sbUser, 255)
  200.  
  201. if changed then
  202. sbUser = userNew
  203. local newSettings =
  204. {
  205. simbrief =
  206. {
  207. username = userNew,
  208. },
  209. };
  210.  
  211. saveSettings(newSettings)
  212. end
  213.  
  214. local fontChanged, fontNewVal = imgui.Checkbox("Use bigger font size", fontBig)
  215. if fontChanged then
  216. fontBig = fontNewVal
  217. end
  218. imgui.TreePop()
  219. end
  220.  
  221.  
  222. -- BUTTON
  223. if imgui.Button("Fetch data") then
  224. if fetchData() then
  225. --readXML()
  226. parseXML()
  227.  
  228. clickFetch = true
  229. end
  230. end
  231.  
  232. if clickFetch then
  233. imgui.SameLine()
  234. imgui.TextUnformatted(handler.root.OFP.fetch.status)
  235.  
  236. imgui.TextUnformatted(" ")
  237.  
  238. imgui.TextUnformatted(string.format("Aircraft: %s", aircraftName))
  239. imgui.TextUnformatted(string.format("Airports: %s - %s", origName, destName))
  240. imgui.TextUnformatted(string.format("Route: %s/%s %s %s/%s", originICAO, origRwy, route, destICAO, destRwy))
  241. imgui.TextUnformatted(string.format("Distance: %d nm", routeDist))
  242. imgui.SameLine()
  243. imgui.TextUnformatted(string.format("ETE: %s", timeConvert(ete)))
  244. imgui.TextUnformatted(string.format("Cruise Altitude: %d ft", level))
  245. imgui.TextUnformatted(string.format("Elevations: %s (%d ft) - %s (%d ft)", origName, origElevation, destName, destElevation))
  246.  
  247. imgui.TextUnformatted(" ")
  248. imgui.PushStyleColor(imgui.constant.Col.Text, 0xFFFFFF00)
  249. imgui.TextUnformatted(string.format("Block Fuel: %d %s", rampFuel, units))
  250. imgui.TextUnformatted(string.format("Planned Fuel: %d %s (Adjusted: %d %s)", planFuel, units, adjusted_blockfuel, units))
  251. imgui.TextUnformatted(string.format("Reserve fuel: %d %s", reserveFuel, units))
  252. imgui.TextUnformatted(string.format("Takeoff fuel: %d %s", minTakeOff, units))
  253. imgui.PopStyleColor()
  254.  
  255. imgui.TextUnformatted(string.format("Payload: %d %s", payload, units))
  256. imgui.TextUnformatted(string.format("ZFW: %d %s", zfw, units))
  257. imgui.TextUnformatted(string.format("Pax: %d", pax))
  258. imgui.TextUnformatted(string.format("Cost Index: %d", costIndex))
  259. imgui.TextUnformatted(string.format("AVG ISA Dev: %d", avg_isa_dev))
  260.  
  261. imgui.TextUnformatted(" ")
  262. imgui.PushStyleColor(imgui.constant.Col.Text, 0xFF00FF00)
  263. imgui.TextUnformatted(string.format("TOC Wind: %03d/%03d", TOC_wind_dir, TOC_wind_spd))
  264. imgui.TextUnformatted(string.format("TOC Temp: %03d C", TOC_oat))
  265. imgui.TextUnformatted(string.format("TOC ISA Dev: %03d C", TOC_oat_isa_dev))
  266. imgui.PopStyleColor()
  267.  
  268. imgui.TextUnformatted(" ")
  269. imgui.PushStyleColor(imgui.constant.Col.Text, 0xFF00BFFF)
  270. imgui.TextUnformatted(string.format("%s", origMetar))
  271. imgui.TextUnformatted(string.format("%s", destMetar))
  272. imgui.PopStyleColor()
  273. end
  274.  
  275. end
  276.  
  277. -- Open and close window from Lua menu
  278.  
  279. sb_wnd = nil
  280.  
  281. function sb_show_wnd()
  282. readSettings() -- It should read only once
  283. sb_wnd = float_wnd_create(650, 550, 1, true)
  284. --float_wnd_set_imgui_font(sb_wnd, 2)
  285. float_wnd_set_title(sb_wnd, "Simbrief Helper")
  286. float_wnd_set_imgui_builder(sb_wnd, "sb_on_build")
  287. float_wnd_set_onclose(sb_wnd, "sb_hide_wnd")
  288. end
  289.  
  290. function sb_hide_wnd()
  291. if sb_wnd then
  292. float_wnd_destroy(sb_wnd)
  293. end
  294. end
  295.  
  296. sb_show_only_once = 0
  297. sb_hide_only_once = 0
  298.  
  299. function toggle_simbrief_helper_interface()
  300. sb_show_window = not sb_show_window
  301. if sb_show_window then
  302. if sb_show_only_once == 0 then
  303. sb_show_wnd()
  304. sb_show_only_once = 1
  305. sb_hide_only_once = 0
  306. end
  307. else
  308. if sb_hide_only_once == 0 then
  309. sb_hide_wnd()
  310. sb_hide_only_once = 1
  311. sb_show_only_once = 0
  312. end
  313. end
  314. end
  315.  
  316. add_macro("Simbrief Helper", "sb_show_wnd()", "sb_hide_wnd()", "deactivate")
  317. create_command("FlyWithLua/SimbriefHelper/show_toggle", "open/close Simbrief Helper", "toggle_simbrief_helper_interface()", "", "")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement