Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GewoonJaap committed Jul 14, 2021
1 parent 9d3910e commit 52bcc85
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 28 deletions.
27 changes: 13 additions & 14 deletions Config/ModelMatching.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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),
Expand Down
11 changes: 9 additions & 2 deletions Model/Aircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
43 changes: 37 additions & 6 deletions Util/LiveTrafficHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LiveTrafficHandler
private int _requestCount = 0;
private int MaxPlanes = 40;
private List<Addon> _addons;
private readonly int _teleportFixDelay = 40;

public LiveTrafficHandler(SimConnect simConnect)
{
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -115,7 +116,6 @@ private void ParsePlaneData(JObject planeData)
catch (Exception e)
{
Log.Error($"Failed to parse extra data for {callsign}");

}

aircraft = new Aircraft()
Expand All @@ -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);

Expand All @@ -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
{
Expand All @@ -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;
Expand All @@ -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<string> flightradarIds)
Expand Down
28 changes: 23 additions & 5 deletions Util/ModelMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ public static string MatchModel(Aircraft aircraft, List<Addon> addons = null)
{
List<Addon> 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)
Expand All @@ -32,7 +46,7 @@ public static string MatchModel(Aircraft aircraft, List<Addon> 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;
Expand Down Expand Up @@ -66,6 +80,10 @@ private static string TryFindAircraft(JObject models, List<Addon> 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;
}
Expand Down

0 comments on commit 52bcc85

Please sign in to comment.