/// <summary> /// Update from METAR Callback data /// </summary> /// <param name="metarDatas"></param> public void Update(MetarTafDataList metarDatas) { var closest = metarDatas.GetClosest(SC.SimConnectClient.Instance.HudBarModule.Lat, SC.SimConnectClient.Instance.HudBarModule.Lon); if (closest.Valid) { MText = closest.Pretty; StationText = $"{closest.Data.Station.StationID}"; if (!float.IsNaN(closest.Distance_nm)) { StationText += $" {closest.Distance_nm:##0.0} nm @{closest.Bearing_deg:000}°"; } HasNewData = true; ConditionColor = (closest.Data.FlightCategory.FlightCategoryColor == "green") ? GUI.GUI_Colors.ColorType.cMetG : (closest.Data.FlightCategory.FlightCategoryColor == "blue") ? GUI.GUI_Colors.ColorType.cMetB : (closest.Data.FlightCategory.FlightCategoryColor == "red") ? GUI.GUI_Colors.ColorType.cMetR : (closest.Data.FlightCategory.FlightCategoryColor == "magenta") ? GUI.GUI_Colors.ColorType.cMetM : (closest.Data.FlightCategory.FlightCategoryColor == "black") ? GUI.GUI_Colors.ColorType.cMetK : // SUB ILS (closest.Data.FlightCategory.FlightCategoryColor == "white") ? GUI.GUI_Colors.ColorType.cMetR : GUI.GUI_Colors.ColorType.cActBG; // unknown } else { MText = ""; StationText = "n.a."; HasNewData = true; ConditionColor = GUI.GUI_Colors.ColorType.cActBG; } }
/// <summary> /// Decodes a METAR / TAF message received as CSV /// Returns one or more MetarTafData Records as List /// </summary> /// <param name="csvData">The Metar/Taf Message</param> /// <returns>A list of MetarTafData</returns> internal static MetarTafDataList DecodeCSV(string csvData) { // arrives as /* * No errors * No warnings * 3 ms * data source=tafs * N results * header line * value line */ var ret = new MetarTafDataList(); if (string.IsNullOrEmpty(csvData)) { var rec = new MetarTafData(); ret.Add(rec); rec.Error = $"Empty TAF record received"; rec.Valid = false; return(ret); } using (var sr = new StringReader(csvData)) { try { var line = sr.ReadLine(); // errors if (!line.StartsWith("No errors")) { var rec = new MetarTafData(); ret.Add(rec); rec.Error = line; rec.Valid = false; return(ret); } line = sr.ReadLine( ); // warnings line = sr.ReadLine( ); // response time line = sr.ReadLine( ); // data sources line = sr.ReadLine( ); // N results string[] e = line.Split(new char[] { ' ' }); // get the N if (!int.TryParse(e[0], out int nRec)) { var rec = new MetarTafData(); ret.Add(rec); rec.Error = $"TAF cannot derive number of records\n{line}"; rec.Valid = false; return(ret); } if (nRec <= 0) { var rec = new MetarTafData(); ret.Add(rec); rec.Error = $"TAF contains 0 records (not a known station?)\n{line}"; rec.Valid = false; return(ret); } line = sr.ReadLine( ); // headers if (!line.StartsWith($"{Fields.raw_text},")) { var rec = new MetarTafData(); ret.Add(rec); rec.Error = $"Unrecognizable TAF record header format\n{line}"; rec.Valid = false; return(ret); } // Create an item Lookup table var hList = line.Split(new char[] { ',' }).ToList(); // the list of items from the header var lookup = CreateLookup(line); // a lookup where the value is the item index in the list (and CSV line) if (lookup.Count == 0) { var rec = new MetarTafData(); ret.Add(rec); rec.Error = $"Field(s) not found in TAF record header format\n{line}"; rec.Valid = false; return(ret); } // get all station reports for (int i = 0; i < nRec; i++) { line = sr.ReadLine( ); // result line ret.Add(DecodeCSVLine(line, lookup)); } } #pragma warning disable CS0168 // Variable is declared but never used catch (Exception e) { #pragma warning restore CS0168 // Variable is declared but never used var rec = new MetarTafData(); ret.Add(rec); rec.Error = $"Unrecognizable TAF record\n{csvData}"; rec.Valid = false; } return(ret); } }