public void BaseStationMessageHelper_ConvertToBaseStationTransmissionType_Converts_Text_Correctly() { foreach (BaseStationTransmissionType transmissionType in Enum.GetValues(typeof(BaseStationTransmissionType))) { string text = null; switch (transmissionType) { case BaseStationTransmissionType.AirbornePosition: text = "3"; break; case BaseStationTransmissionType.AirborneVelocity: text = "4"; break; case BaseStationTransmissionType.AirToAir: text = "7"; break; case BaseStationTransmissionType.AllCallReply: text = "8"; break; case BaseStationTransmissionType.IdentificationAndCategory: text = "1"; break; case BaseStationTransmissionType.None: continue; case BaseStationTransmissionType.SurfacePosition: text = "2"; break; case BaseStationTransmissionType.SurveillanceAlt: text = "5"; break; case BaseStationTransmissionType.SurveillanceId: text = "6"; break; default: throw new NotImplementedException(); } Assert.AreEqual(transmissionType, BaseStationMessageHelper.ConvertToBaseStationTransmissionType(text), transmissionType.ToString()); } }
public void BaseStationMessageHelper_ConvertToBaseStationTransmissionType_Converts_Unknown_Text_Correctly() { foreach (string text in new string[] { null, "", "0", "9", "10", "AA" }) { Assert.AreEqual(BaseStationTransmissionType.None, BaseStationMessageHelper.ConvertToBaseStationTransmissionType(text)); } }
/// <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); }