public void BaseStationMessageHelper_ConvertToBaseStationMessageType_Converts_Text_Correctly() { foreach (BaseStationMessageType messageType in Enum.GetValues(typeof(BaseStationMessageType))) { string text = null; switch (messageType) { case BaseStationMessageType.NewAircraft: text = "AIR"; break; case BaseStationMessageType.NewIdentifier: text = "ID"; break; case BaseStationMessageType.StatusChange: text = "STA"; break; case BaseStationMessageType.Transmission: text = "MSG"; break; case BaseStationMessageType.Unknown: continue; case BaseStationMessageType.UserClicked: text = "SEL"; break; case BaseStationMessageType.UserDoubleClicked: text = "CLK"; break; default: throw new NotImplementedException(); } Assert.AreEqual(messageType, BaseStationMessageHelper.ConvertToBaseStationMessageType(text), messageType.ToString()); } }
public void BaseStationMessageHelper_ConvertToBaseStationMessageType_Converts_Unknown_Text_Correctly() { foreach (string text in new string[] { null, "", "HHH", " MSG", "ID " }) { Assert.AreEqual(BaseStationMessageType.Unknown, BaseStationMessageHelper.ConvertToBaseStationMessageType(text)); } }
public void BaseStationMessageHelper_ConvertToBaseStationMessageType_Converts_Mlat_Message_Types_Correctly() { var isMlat = false; var messageType = BaseStationMessageHelper.ConvertToBaseStationMessageType("MLAT", ref isMlat); Assert.AreEqual(BaseStationMessageType.Transmission, messageType); Assert.IsTrue(isMlat); isMlat = false; messageType = BaseStationMessageHelper.ConvertToBaseStationMessageType("MSG", ref isMlat); Assert.AreEqual(BaseStationMessageType.Transmission, messageType); Assert.IsFalse(isMlat); // Actually this is untouched, but it's definitely not true }
/// <summary> /// See interface docs. /// </summary> /// <param name="text"></param> /// <param name="signalLevel"></param> /// <returns></returns> public BaseStationMessage Translate(string text, int?signalLevel) { var result = new BaseStationMessage() { SignalLevel = signalLevel }; if (!String.IsNullOrEmpty(text)) { var isMlat = false; string[] parts = text.Split(','); for (int c = 0; c < parts.Length; c++) { string chunk = parts[c]; try { if (!String.IsNullOrEmpty(chunk)) { switch (c) { case 0: result.MessageType = BaseStationMessageHelper.ConvertToBaseStationMessageType(chunk, ref isMlat); break; case 1: result.TransmissionType = result.MessageType == BaseStationMessageType.Transmission ? BaseStationMessageHelper.ConvertToBaseStationTransmissionType(chunk) : BaseStationTransmissionType.None; break; case 2: result.SessionId = ParseInt(chunk); break; case 3: result.AircraftId = ParseInt(chunk); break; case 4: result.Icao24 = chunk == null ? null : chunk.ToUpperInvariant(); break; case 5: result.FlightId = ParseInt(chunk); break; case 6: result.MessageGenerated = ParseDate(chunk); break; case 7: result.MessageGenerated = ParseTime(result.MessageGenerated, chunk); break; case 8: result.MessageLogged = ParseDate(chunk); break; case 9: result.MessageLogged = ParseTime(result.MessageLogged, chunk); break; case 10: if (result.MessageType == BaseStationMessageType.StatusChange) { result.StatusCode = BaseStationMessageHelper.ConvertToBaseStationStatusCode(chunk); } else { result.Callsign = chunk.Replace("@", "").Trim(); } break; case 11: result.Altitude = ParseInt(chunk); break; case 12: result.GroundSpeed = ParseFloat(chunk); break; case 13: result.Track = ParseFloat(chunk); break; case 14: result.Latitude = ParseDouble(chunk); break; case 15: result.Longitude = ParseDouble(chunk); break; case 16: result.VerticalRate = ParseInt(chunk); break; case 17: result.Squawk = ParseInt(chunk); if (result.Squawk == 0) { result.Squawk = null; } break; case 18: result.SquawkHasChanged = ParseBool(chunk); break; case 19: result.Emergency = ParseBool(chunk); break; case 20: result.IdentActive = ParseBool(chunk); break; case 21: result.OnGround = ParseBool(chunk); break; } } } catch (Exception ex) { Debug.WriteLine(String.Format("BaseStationMessageTranslator.Translate caught exception: {0}", ex.ToString())); // I would prefer to pass ex as the inner exception to this. However Microsoft's Application.ThreadException unhandled exception handler // strips off all outer exceptions and only shows the bottom-most exception - i.e., in our case, the exception from a Parse method. This // is not useful in isolation, we need to know what was being translated, the context in which the exception was thrown. So I have ended // up with this, which is not very nice but shows enough information in the unhandled exception handler to allow diagnosis of the problem. throw new BaseStationTranslatorException($"{ex.Message} while translating \"{chunk}\" (chunk {c}) in \"{text}\""); } } result.IsMlat = isMlat; } return(result); }