Skip to content

Commit

Permalink
Updated to use vatsys internal checking of metars.
Browse files Browse the repository at this point in the history
  • Loading branch information
badvectors committed Nov 2, 2024
1 parent 336a02b commit 1ff1312
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 98 deletions.
12 changes: 5 additions & 7 deletions ATISControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ATISPlugin
{
public class ATISControl
{
private int ATISIndex { get; set; }
public int ATISIndex { get; private set; }

private string Callsign { get; set; }
public char ID { get; set; } = 'Z';
Expand Down Expand Up @@ -116,19 +116,19 @@ private void LoopTimer_Elapsed(object sender, ElapsedEventArgs e)
BroadcastStart();
}

public async Task Create(string airport, string frequency, string coordinates)
public async Task Create(string icao, string frequency, string coordinates)
{
if (!Network.IsConnected || !Network.IsValidATC) return;

if (Network.GetATISConnected(ATISIndex)) return;

try
{
ICAO = airport;
ICAO = icao;
FrequencyDisplay = Normalize25KhzFrequency(frequency);
Frequency = Normalize25KhzFrequency(FrequencyToUInt(frequency));
AliasFrequency = Normalize25KhzFrequency(199998000U);
Callsign = $"{airport}_ATIS";
Callsign = $"{icao}_ATIS";
VisPoint = new Coordinate(coordinates);
IsZulu = false;

Expand Down Expand Up @@ -324,12 +324,10 @@ public void SuggestionsCancel()
SuggestedLines.Clear();
}

public async Task<bool> UpdateMetar()
public bool UpdateMetar(string metar)
{
if (ICAO == null || IsZulu) return false;

var metar = await Plugin.GetMetar(ICAO);

if (metar == METARRaw) return false;

METARLastRaw = METARRaw;
Expand Down
14 changes: 6 additions & 8 deletions EditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private ATISControl Control
}
private string ICAO { get; set; }
private char ID { get; set; }
private int Number { get; set; } = 1;
public int Number { get; private set; } = 1;
private Dictionary<string, string> Saves { get; set; } = new Dictionary<string, string>();
private bool Edits => Saves.Any() ||
TimeCheck != Control.TimeCheck ||
Expand Down Expand Up @@ -766,13 +766,11 @@ private void RefreshForm()
}
}

private async Task GetMetar()
private void GetMetar()
{
labelMETAR.Text = "LOADING";

await Control.UpdateMetar();

RefreshForm();
MET.Instance.RequestProduct(new MET.ProductRequest(MET.ProductType.VATSIM_METAR, ICAO, true));
}

private void IncreaseID()
Expand Down Expand Up @@ -802,7 +800,7 @@ private async void ButtonCreate_Click(object sender, EventArgs e)

await Control.Create(ICAO, frequency.Frequency.ToString(), airport.Position);

await GetMetar();
GetMetar();
}

private void ComboBoxAirport_SelectedIndexChanged(object sender, EventArgs e)
Expand All @@ -817,9 +815,9 @@ private async void ButtonDelete_Click(object sender, EventArgs e)
Change(Number);
}

private async void ButtonGetMetar_Click(object sender, EventArgs e)
private void ButtonGetMetar_Click(object sender, EventArgs e)
{
await GetMetar();
GetMetar();
}

private void ButtonCancel_Click(object sender, EventArgs e)
Expand Down
129 changes: 47 additions & 82 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public class Plugin : IPlugin, IDisposable
public string Name => "ATIS Editor";
public static string DisplayName => "ATIS Editor";

private static readonly string MetarUri = "https://metar.vatsim.net/metar.php?id=";

public static readonly Version Version = new Version(2, 4);
public static readonly Version Version = new Version(2, 5);
private static readonly string VersionUrl = "https://raw.githubusercontent.com/badvectors/ATISPlugin/master/Version.json";

private static readonly string ZuluUrl = "https://raw.githubusercontent.com/badvectors/ATISPlugin/master/Zulu.json";

private static readonly HttpClient Client = new HttpClient();
Expand Down Expand Up @@ -114,21 +113,16 @@ public Plugin()
File.Delete(file);
}

ATIS1 = new ATISControl(0);
ATIS2 = new ATISControl(1);
ATIS3 = new ATISControl(2);
ATIS4 = new ATISControl(3);
ATIS1 = new ATISControl(1);
ATIS2 = new ATISControl(2);
ATIS3 = new ATISControl(3);
ATIS4 = new ATISControl(4);

ATIS1.StatusChanged += OnUpdate;
ATIS2.StatusChanged += OnUpdate;
ATIS3.StatusChanged += OnUpdate;
ATIS4.StatusChanged += OnUpdate;

METARTimer.Elapsed += new ElapsedEventHandler(METARTimer_Elapsed);
METARTimer.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds;
METARTimer.AutoReset = false;
METARTimer.Start();

BroadcastTimer.Elapsed += new ElapsedEventHandler(BroadcastTimer_Elasped);
BroadcastTimer.Interval = TimeSpan.FromSeconds(5).TotalMilliseconds;
BroadcastTimer.AutoReset = false;
Expand All @@ -143,6 +137,38 @@ public Plugin()
_ = GetZuluInfo();

_ = CheckVersion();

MET.Instance.ProductsChanged += METARChanged;
}

private void METARChanged(object sender, MET.ProductsChangedEventArgs e)
{
var products = MET.Instance.GetProducts(e.ProductRequest);

if (products == null || products.Count == 0) return;

var metar = products[0];

if (metar.Type != MET.ProductType.VATSIM_METAR || metar.Text == "No product available.") return;

var atis = GetATIS(metar.Icao);

if (atis == null) return;

var updated = atis.UpdateMetar(metar.Text);

if (!updated) return;

OnMETARUpdate(atis.ATISIndex);
}

private ATISControl GetATIS(string icao)
{
if (ATIS1.ICAO == icao) return ATIS1;
if (ATIS2.ICAO == icao) return ATIS3;
if (ATIS3.ICAO == icao) return ATIS3;
if (ATIS4.ICAO == icao) return ATIS4;
return null;
}

private static async Task CheckVersion()
Expand Down Expand Up @@ -206,63 +232,6 @@ private async void BroadcastTimer_Elasped(object sender, ElapsedEventArgs e)
BroadcastTimer.Start();
}

private async void METARTimer_Elapsed(object sender, ElapsedEventArgs e)
{
var changes = false;

try
{
var atis1Updated = await ATIS1.UpdateMetar();

if (atis1Updated)
{
changes = true;
OnMETARUpdate(1);
}
}
catch { }

try
{
var atis2Updated = await ATIS2.UpdateMetar();

if (atis2Updated)
{
changes = true;
OnMETARUpdate(2);
}
}
catch { }

try
{
var atis3Updated = await ATIS3.UpdateMetar();

if (atis3Updated)
{
changes = true;
OnMETARUpdate(3);
}
}
catch { }

try
{
var atis4Updated = await ATIS4.UpdateMetar();

if (atis4Updated)
{
changes = true;
OnMETARUpdate(4);
}
}
catch { }

if (changes) PlayUpdateSound();

METARTimer.Start();
}

private void Audio_VSCSFrequenciesChanged(object sender, EventArgs e)
{
Frequencies.Clear();
Expand Down Expand Up @@ -308,9 +277,16 @@ private void OnMETARUpdate(int number)
{
ShowEditorWindow();

Editor.Change(number);
if (Editor.Number == number)
{
Editor.RefreshEvent.Invoke(null, null);
}
else
{
Editor.Change(number);
}

Editor?.RefreshEvent.Invoke(this, null);
PlayUpdateSound();
}

private void GetData()
Expand All @@ -320,17 +296,6 @@ private void GetData()
Airspace = (Airspace)LoadXML(DatasetPath + "\\Airspace.xml", typeof(Airspace));
}

public static async Task<string> GetMetar(string icao)
{
var response = await Client.GetAsync($"{MetarUri}{icao}");

if (!response.IsSuccessStatusCode) return null;

var content = await response.Content.ReadAsStringAsync();

return content;
}

public object LoadXML(string filePath, Type type)
{
if (!File.Exists(filePath)) return null;
Expand Down
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"Major": 2,
"Minor": 4
"Minor": 5
}

0 comments on commit 1ff1312

Please sign in to comment.