From 52bcc85ce3da4382b22e59098843860d09ec8956 Mon Sep 17 00:00:00 2001 From: Jaap Date: Wed, 14 Jul 2021 17:48:21 +0200 Subject: [PATCH] Fixes --- Config/ModelMatching.json | 27 ++++++++++++------------ Model/Aircraft.cs | 11 ++++++++-- Properties/AssemblyInfo.cs | 2 +- Util/LiveTrafficHandler.cs | 43 ++++++++++++++++++++++++++++++++------ Util/ModelMatching.cs | 28 ++++++++++++++++++++----- 5 files changed, 83 insertions(+), 28 deletions(-) diff --git a/Config/ModelMatching.json b/Config/ModelMatching.json index 777b3f7..3941307 100644 --- a/Config/ModelMatching.json +++ b/Config/ModelMatching.json @@ -9,7 +9,9 @@ "Cessna F152": "Cessna 172P Skyhawk G1000", "C172": "Cessna 172P Skyhawk G1000", "C152": "Cessna 172P Skyhawk G1000", + "AutoGyro MTOsport": "Cessna 172P Skyhawk G1000", "Cessna 182S Skylane": "Cessna 172P Skyhawk G1000", + "Cessna 182T Skylane": "Cessna 172P Skyhawk G1000", "Cessna 182N Skylane": "Cessna 172P Skyhawk G1000", "Cessna 172M Skyhawk": "Cessna 172P Skyhawk G1000", "Cessna 172L Skyhawk": "Cessna 172P Skyhawk G1000", @@ -23,7 +25,7 @@ "Cessna F172P Skylane": "Cessna 172P Skyhawk G1000", "Cessna F172P Skyhawk": "Cessna 172P Skyhawk G1000", "Cessna 182P Skylane": "Cessna 172P Skyhawk G1000", - "Cessna 172P Skyhawk G1000": "Cessna 172P Skyhawk G1000", + "Cessna 172P Skyhawk G1000": "Cessna 172Sp Skyhawk G1000", "Cessna 172P Skyhawk G1000 Default": "Cessna Skyhawk G1000 Asobo", "Tecnam P2008 JC": "Cessna 172P Skyhawk G1000", "Tecnam P-92 Echo J": "Cessna 172P Skyhawk G1000", @@ -35,18 +37,6 @@ "Boeing 767-304(ER)": "Boeing 787-10", "Boeing 767-322(ER)": "Boeing 787-10", "Boeing 767-316(ER)(BCF)": "Boeing 787-10", - "Boeing 777-306(ER)": "Boeing 787-10", - "Boeing 777-300(ER)": "Boeing 787-10", - "Boeing 777-31H(ER)": "Boeing 787-10", - "Boeing 777-3DZ(ER)": "Boeing 787-10", - "Boeing 777-322(ER)": "Boeing 787-10", - "Boeing 777-36N(ER)": "Boeing 787-10", - "Boeing 777-328(ER)": "Boeing 787-10", - "Boeing 777-228(ER)": "Boeing 787-10", - "Boeing 777-21H(LR)": "Boeing 787-10", - "Boeing 777-F6N": "Boeing 787-10", - "Boeing 777-F1H": "Boeing 787-10", - "Boeing 777-F": "Boeing 787-10", "Boeing 757-256": "Boeing 787-10", "B789": "Boeing 787-10", "Boeing 787-8 Dreamliner": "Boeing 787-10", @@ -83,6 +73,7 @@ "Learjet 60XR": "Cessna CJ4 Citation", "Cessna 680A Citation Latitude": "Cessna CJ4 Citation", "Embraer Phenom 300": "Cessna CJ4 Citation", + "Embraer Phenom 100": "Cessna CJ4 Citation", "Hawker 750": "Cessna CJ4 Citation", "Bombardier Global 6000": "Cessna CJ4 Citation", "Bombardier Global 5000": "Cessna CJ4 Citation", @@ -92,6 +83,8 @@ "Dassault Falcon 2000LX": "Cessna CJ4 Citation", "Dassault Falcon 2000": "Cessna CJ4 Citation", "Learjet 45": "Cessna CJ4 Citation", + "Learjet 36A": "Cessna CJ4 Citation", + "LJ35": "Cessna CJ4 Citation", "Dassault Falcon 7X": "Cessna CJ4 Citation", "Learjet 40": "Cessna CJ4 Citation", "Learjet 35A": "Cessna CJ4 Citation", @@ -103,6 +96,7 @@ "Hawker Beechcraft 1000": "Cessna CJ4 Citation", "Cessba 650 Citation VI": "Cessna CJ4 Citation", "Cessna 525B CitationJet CJ3+": "Cessna CJ4 Citation", + "Cessna 525 CitationJet CJ1": "Cessna CJ4 Citation", "Cessna 525B CitationJet CJ3": "Cessna CJ4 Citation", "Cessna 650 Citation III": "Cessna CJ4 Citation", "Cessna 510 Citation Mustang": "Cessna CJ4 Citation", @@ -131,6 +125,7 @@ "Bonanza G36": "Bonanza G36", "Bonanza G36 Default": "Bonanza G36 Asobo", "EC35": "Airbus H135", + "EC30": "Airbus H135", "EC75": "Airbus H135", "EC45": "Airbus H135", "BK17": "Airbus H135", @@ -145,6 +140,7 @@ "A109": "Airbus H135", "A139": "Airbus H135", "AS32": "Airbus H135", + "AS65": "Airbus H135", "Airbus H145": "Airbus H135", "Airbus H135": "Airbus H135", "Airbus Helicopter H135": "Airbus H135", @@ -165,7 +161,10 @@ "Diamond DA40": "DA40-NG", "DA42": "DA40-NG", "DA40-NG": "DA40-NG", - "DA40-NG Default": "DA40-NG Asobo" + "DA40-NG Default": "DA40-NG Asobo", + "GLID": "Discus-2b", + "Discus-2b": "Discus-2b", + "Discus-2b Default": "Discus-2b" } //How does model matching work? Lets say "Boeing 787-9 Dreamliner" is detected, the modelmatcher will now search for an aircraft with the key: "Boeing 787-9 Dreamliner", this will return "Boeing 787-10" //The modelmatcher will now append the livery name, lets say "KLM", this results in: "Boeing 787-10 KLM", if this livery is found, it will use this aircraft, if it isn't, it will fallback to: "Boeing 787-10 Default" (It appends default to the value gotten from the key), diff --git a/Model/Aircraft.cs b/Model/Aircraft.cs index 07471ba..3ec62db 100644 --- a/Model/Aircraft.cs +++ b/Model/Aircraft.cs @@ -13,6 +13,9 @@ public class Aircraft public uint objectId { get; set; } = 0; public string matchedModel { get; set; } + public bool isTeleportFixed { get; set; } = false; + public DateTime spawnTime { get; set; } + #endregion SimData #region Aircraft @@ -25,6 +28,10 @@ public class Aircraft public string icaoAirline { get; set; } public string modelCode { get; set; } + public string shorterModelCode { get => modelCode.Remove(modelCode.Length - 1, 1); } + + public string shortModel { get => model.Substring(0, model.IndexOf('-') > -1 ? model.IndexOf('-') : model.Length); } + #endregion Aircraft #region FlightPath @@ -80,9 +87,9 @@ public object[] GetWayPointObjectArray() } else { - wp[0].Flags = (uint)(SIMCONNECT_WAYPOINT_FLAGS.SPEED_REQUESTED | SIMCONNECT_WAYPOINT_FLAGS.ALTITUDE_IS_AGL | SIMCONNECT_WAYPOINT_FLAGS.COMPUTE_VERTICAL_SPEED); + wp[0].Flags = (uint)(SIMCONNECT_WAYPOINT_FLAGS.SPEED_REQUESTED | SIMCONNECT_WAYPOINT_FLAGS.ALTITUDE_IS_AGL); } - wp[0].Altitude = altimeter; + wp[0].Altitude = isTeleportFixed ? altimeter : 100; wp[0].Latitude = latitude; wp[0].Longitude = longitude; wp[0].ktsSpeed = speed; diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index ccde7bf..b7c1ea8 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -42,4 +42,4 @@ // You can specify all the values or you can default the Build and Revision Numbers by using the '*' // as shown below: [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyFileVersion("2.0.0.0")] +[assembly: AssemblyFileVersion("2.1.0.0")] diff --git a/Util/LiveTrafficHandler.cs b/Util/LiveTrafficHandler.cs index 4fb5cb2..8f5cd4a 100644 --- a/Util/LiveTrafficHandler.cs +++ b/Util/LiveTrafficHandler.cs @@ -17,6 +17,7 @@ public class LiveTrafficHandler private int _requestCount = 0; private int MaxPlanes = 40; private List _addons; + private readonly int _teleportFixDelay = 40; public LiveTrafficHandler(SimConnect simConnect) { @@ -72,7 +73,7 @@ private void ParsePlaneData(JObject planeData) double longitude = (double)property.Value[2]; double latitude = (double)property.Value[1]; int heading = (int)property.Value[3]; - double altimeter = (int)property.Value[4];// * 0.3048; + double altimeter = Math.Round((int)property.Value[4] * 0.3048); int speed = (int)property.Value[5]; string callsign = (string)property.Value[16]; bool isGrounded = (bool)property.Value[14]; @@ -115,7 +116,6 @@ private void ParsePlaneData(JObject planeData) catch (Exception e) { Log.Error($"Failed to parse extra data for {callsign}"); - } aircraft = new Aircraft() @@ -134,7 +134,9 @@ private void ParsePlaneData(JObject planeData) airportOrigin = airportOrigin, airportDestination = airportDestination, modelCode = modelCode, - icaoAirline = icaoAirline + icaoAirline = icaoAirline, + isTeleportFixed = isGrounded, + spawnTime = DateTime.Now }; aircraft.matchedModel = ModelMatching.MatchModel(aircraft, _addons); @@ -157,6 +159,24 @@ private void ParsePlaneData(JObject planeData) Longitude = longitude, Speed = speed }); + + if (!aircraft.isTeleportFixed && (DateTime.Now - aircraft.spawnTime).Seconds > _teleportFixDelay) + { + aircraft.isTeleportFixed = true; + PositionData position = new PositionData + { + Latitude = latitude, + Longitude = longitude, + Altitude = altimeter, + Heading = heading, + Pitch = 0, + Bank = 0, + Airspeed = (uint)speed, + OnGround = 0 + }; + Log.Debug($"Teleport fixing: {aircraft.tailNumber}"); + _simConnect.SetDataOnSimObject(SimConnectDataDefinition.PlaneLocation, aircraft.objectId, SIMCONNECT_DATA_SET_FLAG.DEFAULT, position); + } } else { @@ -175,8 +195,12 @@ private void ParsePlaneData(JObject planeData) _simConnect.SetDataOnSimObject(SimConnectDataDefinition.PlaneLocation, aircraft.objectId, SIMCONNECT_DATA_SET_FLAG.DEFAULT, position); } - aircraft.latitude = latitude; - aircraft.longitude = longitude; + + if (!aircraft.isTeleportFixed) + { + aircraft.latitude = latitude; + aircraft.longitude = longitude; + } aircraft.altimeter = altimeter; aircraft.heading = heading; aircraft.speed = speed; @@ -186,7 +210,14 @@ private void ParsePlaneData(JObject planeData) aircraft.objectId, SIMCONNECT_DATA_SET_FLAG.DEFAULT, aircraft.GetWayPointObjectArray()); } - DespawnOldPlanes(flightRadarIds); + try + { + DespawnOldPlanes(flightRadarIds); + } + catch (Exception ex) + { + Log.Error($"Error when trying to despawn aircraft, {ex.Message}"); + } } private void DespawnOldPlanes(List flightradarIds) diff --git a/Util/ModelMatching.cs b/Util/ModelMatching.cs index 534d29a..446c1f6 100644 --- a/Util/ModelMatching.cs +++ b/Util/ModelMatching.cs @@ -14,15 +14,29 @@ public static string MatchModel(Aircraft aircraft, List addons = null) { List installedAddons = addons ?? AddonScanner.ScanAddons(); - Log.Information($"Model matching: {aircraft.model} with airline: {aircraft.airline} and modelCode {aircraft.modelCode}"); + Log.Information($"Model matching: {aircraft.model} with airline: {aircraft.airline}, airline ICAO Code: {aircraft.icaoAirline} and modelCode {aircraft.modelCode}"); JObject models = JObject.Parse(File.ReadAllText(@".\Config\ModelMatching.json")); - if (models.GetValue(aircraft.model) == null) Log.Information($"Failed to model match: {aircraft.model}"); - string matchedModel = (string)models.GetValue(aircraft.model) ?? (string)models.GetValue(aircraft.modelCode) ?? installedAddons.FirstOrDefault(addon => addon.ModelCode == aircraft.modelCode)?.Title?.Replace("Asobo", "")?.Trim() ?? (string)models.GetValue("Default Aircraft") ?? "Airbus A320 Neo"; + string matchedModel = (string)models.GetValue(aircraft.model) ?? (string)models.GetValue(aircraft.modelCode) ?? installedAddons.FirstOrDefault(addon => (addon.ModelCode == aircraft.modelCode || addon.Title.Contains(aircraft.shortModel) || addon.Title.Contains(aircraft.shorterModelCode)) && addon.Icao_Airline == "")?.Title ?? installedAddons.FirstOrDefault(addon => addon.ModelCode.Contains(aircraft.shorterModelCode) || addon.Title.Contains(aircraft.shorterModelCode))?.Title ?? (string)models.GetValue("Default Aircraft") ?? "Airbus A320 Neo"; + + matchedModel = matchedModel.Replace("Asobo", "")?.Trim(); + + var test = installedAddons.FirstOrDefault(addon => addon.Title.Contains(aircraft.shortModel)); if (installedAddons.FirstOrDefault(addon => addon.Title.StartsWith(matchedModel)) == null && installedAddons.FirstOrDefault(addon => addon.Title == matchedModel + " Asobo") == null) { Log.Information($"Failed to model match: {matchedModel} not installed!"); - matchedModel = installedAddons.FirstOrDefault(addon => addon.ModelCode == aircraft.modelCode)?.Title ?? "Airbus A320 Neo"; + if (installedAddons.FirstOrDefault(addon => + addon.Title == (string)models.GetValue($"{matchedModel} Default")) != null) + { + matchedModel = (string)models.GetValue($"{matchedModel} Default"); + } + else + { + matchedModel = installedAddons.FirstOrDefault(addon => + addon.ModelCode == aircraft.modelCode || addon.Title.Contains(aircraft.shortModel)) + ?.Title ?? + "Airbus A320 Neo"; + } } if (TryFindAircraft(models, installedAddons, aircraft, matchedModel) != null) @@ -32,7 +46,7 @@ public static string MatchModel(Aircraft aircraft, List addons = null) else { if (models.GetValue(matchedModel + " Default") == null) Log.Information($"Failed to model match: {matchedModel} Default"); - matchedModel = (string)models.GetValue($"{matchedModel} Default") ?? installedAddons.FirstOrDefault(addon => addon.ModelCode == aircraft.modelCode)?.Title ?? "Airbus A320 Neo Asobo"; + matchedModel = (string)models.GetValue($"{matchedModel} Default") ?? installedAddons.FirstOrDefault(addon => addon.ModelCode == aircraft.modelCode || addon.Title == matchedModel)?.Title ?? "Airbus A320 Neo Asobo"; } Log.Information($"Model matched model: {aircraft.modelCode}, airline: {aircraft.airline} with: {matchedModel}"); return matchedModel; @@ -66,6 +80,10 @@ private static string TryFindAircraft(JObject models, List installedAddon { foundAircraft = installedAddons.First(addon => addon.ModelCode == aircraft.modelCode && (addon.Title.Contains(aircraft.icaoAirline) || addon.Title.Contains(aircraft.airline))).Title; } + else if (installedAddons.FirstOrDefault(addon => (addon.ModelCode.Contains(aircraft.shorterModelCode) || addon.Title.Contains(aircraft.shorterModelCode) || addon.Title.Contains(aircraft.shortModel)) && (addon.Title.Contains(aircraft.icaoAirline) || addon.Title.Contains(aircraft.airline))) != null) + { + foundAircraft = installedAddons.First(addon => (addon.ModelCode.Contains(aircraft.shorterModelCode) || addon.Title.Contains(aircraft.shorterModelCode) || addon.Title.Contains(aircraft.shortModel)) && (addon.Title.Contains(aircraft.icaoAirline) || addon.Title.Contains(aircraft.airline))).Title; + } return foundAircraft; }