Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Simbrief helper
- -- Alexander Garzon Jan - 2020
- -- Description: pulls OFP data from simbrief (after you generate your flight plan) using your username (API call)
- -- This data wil be used: flight level, block fuel, payload, zfw, destination altitude, etc..
- -- TODO:
- -- * Get RWY magnetic heading
- -- Modules
- --require("LuaXml") // No longer required
- local xml2lua = require("xml2lua")
- local handler = require("xmlhandler.tree")
- -- replace mbox.print() with print()
- -- require("mbox")
- -- print = mbox.print
- -- Useful debug tool
- -- local inspect = require 'inspect'
- -- Variables
- local socket = require "socket"
- local http = require "socket.http"
- local LIP = require("LIP");
- local SettingsFile = "simbrief_helper.ini"
- local SimbriefXMLFile = "simbrief.xml"
- local sbUser = ""
- local fontBig = false
- local DataOfp = {}
- local Settings = {}
- local iTOC = 1
- 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
- local clickFetch = false
- if not SUPPORTS_FLOATING_WINDOWS then
- -- to make sure the script doesn't stop old FlyWithLua versions
- logMsg("imgui not supported by your FlyWithLua version")
- return
- end
- function readSettings()
- Settings = LIP.load(SCRIPT_DIRECTORY..SettingsFile);
- if Settings.simbrief.username ~= nil then
- sbUser = Settings.simbrief.username
- end
- end
- function saveSettings(newSettings)
- LIP.save(SCRIPT_DIRECTORY..SettingsFile, newSettings);
- end
- function fetchData()
- if sbUser == nil then
- logMsg("No simbrief username has been configured")
- return false
- end
- local webRespose, err = http.request("http://www.simbrief.com/api/xml.fetcher.php?username=" .. sbUser)
- -- would be nice to have a try-cath here
- assert(webRespose, err)
- local f = io.open(SCRIPT_DIRECTORY..SimbriefXMLFile, "w")
- f:write(webRespose)
- f:close()
- logMsg("Simbrief XML data downloaded")
- return true
- end
- -- Older LuaXML method
- -- function readXML()
- -- local xfile = xml.load(SCRIPT_DIRECTORY..SimbriefXMLFile)
- -- DataOfp["Status"] = xfile:find("status")[1]
- -- -- validate the file is there
- -- if DataOfp["Status"] ~= "Success" then
- -- logMsg("XML status is not success")
- -- return false
- -- end
- -- DataOfp["Origin"] = xfile:find("origin"):find("icao_code")[1]
- -- DataOfp["Origlevation"] = xfile:find("origin"):find("elevation")[1]
- -- DataOfp["OrigName"] = xfile:find("origin"):find("name")[1]
- -- DataOfp["OrigRwy"] = xfile:find("origin"):find("plan_rwy")[1]
- -- DataOfp["OrigMetar"] = xfile:find("orig_metar")[1]
- -- DataOfp["Destination"] = xfile:find("destination"):find("icao_code")[1]
- -- DataOfp["DestElevation"] = xfile:find("destination"):find("elevation")[1]
- -- DataOfp["DestName"] = xfile:find("destination"):find("name")[1]
- -- DataOfp["DestRwy"] = xfile:find("destination"):find("plan_rwy")[1]
- -- DataOfp["DestMetar"] = xfile:find("dest_metar")[1]
- -- DataOfp["Cpt"] = xfile:find("cpt")[1]
- -- DataOfp["Callsign"] = xfile:find("callsign")[1]
- -- DataOfp["Aircraft"] = xfile:find("aircraft"):find("name")[1]
- -- DataOfp["Units"] = xfile:find("units")[1]
- -- DataOfp["Distance"] = xfile:find("route_distance")[1]
- -- DataOfp["Ete"] = xfile:find("est_time_enroute")[1]
- -- DataOfp["Route"] = xfile:find("route")[1]
- -- DataOfp["Level"] = xfile:find("initial_altitude")[1]
- -- DataOfp["RampFuel"] = xfile:find("plan_ramp")[1]
- -- DataOfp["MinTakeoff"] = xfile:find("min_takeoff")[1]
- -- DataOfp["PlanFuel"] = xfile:find("enroute_burn")[1] + xfile:find("contingency")[1]
- -- DataOfp["ReserveFuel"] = xfile:find("reserve")[1] + xfile:find("alternate_burn")[1]
- -- DataOfp["Payload"] = xfile:find("payload")[1]
- -- DataOfp["Pax"] = xfile:find("passengers")[1]
- -- DataOfp["Zfw"] = xfile:find("est_zfw")[1]
- -- DataOfp["CostIndex"] = xfile:find("costindex")[1]
- -- local TOC = xfile:find("navlog"):find(nil,nil,nil,"TOC");
- -- DataOfp["CrzWindDir"] = TOC:find("wind_dir")[1]
- -- DataOfp["CrzWindSpd"] = TOC:find("wind_spd")[1]
- -- DataOfp["CrzTemp"] = TOC:find("oat")[1]
- -- DataOfp["CrzTempDev"] = TOC:find("oat_isa_dev")[1]
- -- DataOfp["AvgTempDev"] = xfile:find("avg_temp_dev")[1]
- -- return true
- -- end
- function parseXML()
- local xfile = xml2lua.loadFile(SCRIPT_DIRECTORY..SimbriefXMLFile)
- local parser = xml2lua.parser(handler)
- parser:parse(xfile)
- -- find TOC
- while handler.root.OFP.navlog.fix[iTOC].ident ~= "TOC" do
- iTOC = iTOC + 1
- end
- originICAO = handler.root.OFP.origin.icao_code
- origElevation = handler.root.OFP.origin.elevation
- origRwy = handler.root.OFP.origin.plan_rwy
- origName = handler.root.OFP.origin.name
- origMetar = handler.root.OFP.weather.orig_metar
- destICAO = handler.root.OFP.destination.icao_code
- destElevation = handler.root.OFP.destination.elevation
- destRwy = handler.root.OFP.destination.plan_rwy
- destName = handler.root.OFP.destination.name
- destMetar = handler.root.OFP.weather.dest_metar
- cpt = handler.root.OFP.crew.cpt
- callsign = handler.root.OFP.atc.callsign
- aircraftName = handler.root.OFP.aircraft.name
- units = handler.root.OFP.params.units
- routeDist = handler.root.OFP.general.route_distance
- ete = handler.root.OFP.times.est_time_enroute
- route = handler.root.OFP.general.route
- level = handler.root.OFP.general.initial_altitude
- rampFuel = handler.root.OFP.fuel.plan_ramp
- minTakeOff = handler.root.OFP.fuel.min_takeoff
- planFuel = handler.root.OFP.fuel.enroute_burn + handler.root.OFP.fuel.contingency
- adjusted_blockfuel = (math.ceil(planFuel/100) * 100) + 100
- reserveFuel = handler.root.OFP.fuel.reserve + handler.root.OFP.fuel.alternate_burn
- payload = handler.root.OFP.weights.payload
- pax = handler.root.OFP.weights.pax_count
- zfw = handler.root.OFP.weights.est_zfw
- costIndex = handler.root.OFP.general.costindex
- avg_isa_dev = handler.root.OFP.general.avg_temp_dev
- TOC_wind_spd = handler.root.OFP.navlog.fix[iTOC].wind_spd
- TOC_wind_dir = handler.root.OFP.navlog.fix[iTOC].wind_dir
- TOC_oat = handler.root.OFP.navlog.fix[iTOC].oat
- TOC_oat_isa_dev = handler.root.OFP.navlog.fix[iTOC].oat_isa_dev
- end
- function timeConvert(seconds)
- local seconds = tonumber(seconds)
- if seconds <= 0 then
- return "no data";
- else
- hours = string.format("%02.f", math.floor(seconds/3600));
- mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
- return hours..":"..mins
- end
- end
- function sb_on_build(sb_wnd, x, y)
- if fontBig == true then
- imgui.SetWindowFontScale(1.2)
- else
- imgui.SetWindowFontScale(1)
- end
- if cpt == nil then
- imgui.TextUnformatted(string.format("Enter your simbrief username below and then click the button."))
- else
- imgui.TextUnformatted(string.format("Welcome, %s (%s)", cpt, callsign))
- end
- if imgui.TreeNode("Settings") then
- -- INPUT
- local changed, userNew = imgui.InputText("Simbrief Username", sbUser, 255)
- if changed then
- sbUser = userNew
- local newSettings =
- {
- simbrief =
- {
- username = userNew,
- },
- };
- saveSettings(newSettings)
- end
- local fontChanged, fontNewVal = imgui.Checkbox("Use bigger font size", fontBig)
- if fontChanged then
- fontBig = fontNewVal
- end
- imgui.TreePop()
- end
- -- BUTTON
- if imgui.Button("Fetch data") then
- if fetchData() then
- --readXML()
- parseXML()
- clickFetch = true
- end
- end
- if clickFetch then
- imgui.SameLine()
- imgui.TextUnformatted(handler.root.OFP.fetch.status)
- imgui.TextUnformatted(" ")
- imgui.TextUnformatted(string.format("Aircraft: %s", aircraftName))
- imgui.TextUnformatted(string.format("Airports: %s - %s", origName, destName))
- imgui.TextUnformatted(string.format("Route: %s/%s %s %s/%s", originICAO, origRwy, route, destICAO, destRwy))
- imgui.TextUnformatted(string.format("Distance: %d nm", routeDist))
- imgui.SameLine()
- imgui.TextUnformatted(string.format("ETE: %s", timeConvert(ete)))
- imgui.TextUnformatted(string.format("Cruise Altitude: %d ft", level))
- imgui.TextUnformatted(string.format("Elevations: %s (%d ft) - %s (%d ft)", origName, origElevation, destName, destElevation))
- imgui.TextUnformatted(" ")
- imgui.PushStyleColor(imgui.constant.Col.Text, 0xFFFFFF00)
- imgui.TextUnformatted(string.format("Block Fuel: %d %s", rampFuel, units))
- imgui.TextUnformatted(string.format("Planned Fuel: %d %s (Adjusted: %d %s)", planFuel, units, adjusted_blockfuel, units))
- imgui.TextUnformatted(string.format("Reserve fuel: %d %s", reserveFuel, units))
- imgui.TextUnformatted(string.format("Takeoff fuel: %d %s", minTakeOff, units))
- imgui.PopStyleColor()
- imgui.TextUnformatted(string.format("Payload: %d %s", payload, units))
- imgui.TextUnformatted(string.format("ZFW: %d %s", zfw, units))
- imgui.TextUnformatted(string.format("Pax: %d", pax))
- imgui.TextUnformatted(string.format("Cost Index: %d", costIndex))
- imgui.TextUnformatted(string.format("AVG ISA Dev: %d", avg_isa_dev))
- imgui.TextUnformatted(" ")
- imgui.PushStyleColor(imgui.constant.Col.Text, 0xFF00FF00)
- imgui.TextUnformatted(string.format("TOC Wind: %03d/%03d", TOC_wind_dir, TOC_wind_spd))
- imgui.TextUnformatted(string.format("TOC Temp: %03d C", TOC_oat))
- imgui.TextUnformatted(string.format("TOC ISA Dev: %03d C", TOC_oat_isa_dev))
- imgui.PopStyleColor()
- imgui.TextUnformatted(" ")
- imgui.PushStyleColor(imgui.constant.Col.Text, 0xFF00BFFF)
- imgui.TextUnformatted(string.format("%s", origMetar))
- imgui.TextUnformatted(string.format("%s", destMetar))
- imgui.PopStyleColor()
- end
- end
- -- Open and close window from Lua menu
- sb_wnd = nil
- function sb_show_wnd()
- readSettings() -- It should read only once
- sb_wnd = float_wnd_create(650, 550, 1, true)
- --float_wnd_set_imgui_font(sb_wnd, 2)
- float_wnd_set_title(sb_wnd, "Simbrief Helper")
- float_wnd_set_imgui_builder(sb_wnd, "sb_on_build")
- float_wnd_set_onclose(sb_wnd, "sb_hide_wnd")
- end
- function sb_hide_wnd()
- if sb_wnd then
- float_wnd_destroy(sb_wnd)
- end
- end
- sb_show_only_once = 0
- sb_hide_only_once = 0
- function toggle_simbrief_helper_interface()
- sb_show_window = not sb_show_window
- if sb_show_window then
- if sb_show_only_once == 0 then
- sb_show_wnd()
- sb_show_only_once = 1
- sb_hide_only_once = 0
- end
- else
- if sb_hide_only_once == 0 then
- sb_hide_wnd()
- sb_hide_only_once = 1
- sb_show_only_once = 0
- end
- end
- end
- add_macro("Simbrief Helper", "sb_show_wnd()", "sb_hide_wnd()", "deactivate")
- create_command("FlyWithLua/SimbriefHelper/show_toggle", "open/close Simbrief Helper", "toggle_simbrief_helper_interface()", "", "")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement