public void BaseStationMessageHelper_ConvertToBaseStationStatusCode_Converts_Unknown_Text_Correctly() { foreach (string text in new string[] { null, "", "ad", " OK", "SL " }) { Assert.AreEqual(BaseStationStatusCode.None, BaseStationMessageHelper.ConvertToBaseStationStatusCode(text)); } }
public void BaseStationMessageHelper_ConvertToBaseStationStatusCode_Converts_Text_Correctly() { foreach (BaseStationStatusCode statusCode in Enum.GetValues(typeof(BaseStationStatusCode))) { string text = null; switch (statusCode) { case BaseStationStatusCode.Delete: text = "AD"; break; case BaseStationStatusCode.None: continue; case BaseStationStatusCode.OK: text = "OK"; break; case BaseStationStatusCode.PositionLost: text = "PL"; break; case BaseStationStatusCode.Remove: text = "RM"; break; case BaseStationStatusCode.SignalLost: text = "SL"; break; default: throw new NotImplementedException(); } Assert.AreEqual(statusCode, BaseStationMessageHelper.ConvertToBaseStationStatusCode(text), statusCode.ToString()); } }
/// <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); }