From 27b87043f545f51fdfb76bf07a2e601c76eea7ee Mon Sep 17 00:00:00 2001 From: earthdog Date: Mon, 3 May 2021 11:17:12 +0300 Subject: [PATCH] -Fix interval for download --- Program.cs | 10 +++++++--- Util.cs | 25 +++++++++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Program.cs b/Program.cs index f1a7f7d..2834257 100644 --- a/Program.cs +++ b/Program.cs @@ -19,8 +19,12 @@ static void Main(string[] args) .Build(); string remoteUri = config["VATSIMURL"]; - string fileName = config["VATSIMDATAFILE"]; - string myStringWebResource = remoteUri + fileName; + + string dataFile = config["VATSIMDATAFILE"]; + if (!File.Exists(dataFile)) + File.Create(dataFile).Close(); + Util.SetLastDownload(true, dataFile); + string lookFor; int refreshInterval; var VATSIMList = new List(); @@ -52,7 +56,7 @@ static void Main(string[] args) { tableArrivals.ClearRows(); tableDepartures.ClearRows(); - VATSIMList = Util.DownloadVatsimData("https://data.vatsim.net/v3/vatsim-data.json", fileName, config["VATSIMINTERVAL"]); + VATSIMList = Util.DownloadVatsimData(remoteUri, dataFile, config["VATSIMINTERVAL"]); tableArrivals.SetHeaders("Callsign", "Aircraft", "Departure", "Arrival", "TAS", "Altitude", "Distance To"); VATSIMList.Where(d => d.planned_destairport == lookFor).OrderBy(o => o.DistanceTo).ToList().ForEach(d => { diff --git a/Util.cs b/Util.cs index 35ac26d..8627644 100644 --- a/Util.cs +++ b/Util.cs @@ -177,11 +177,11 @@ public static void WriteLn(string _str, ConsoleColor _background, ConsoleColor _ Console.ForegroundColor = currentForeground; } - public static List DownloadVatsimData(string _uri, string _filename, string _interval) + public static List DownloadVatsimData(string _uri, string dataFile, string _interval) { var Airports = new List(); List dataList = new List(); - DateTime lastDownload = File.GetLastWriteTime(_filename); + DateTime lastDownload = File.GetLastWriteTime(dataFile); TimeSpan span = DateTime.Now.Subtract(lastDownload); Airports = LoadAirports(); if (span.TotalMinutes > Convert.ToDouble(_interval)) @@ -191,11 +191,12 @@ public static List DownloadVatsimData(string _uri, string _filename, st { VatsimData.VatsimData.GetData(_uri); WriteLn("DONE!", ConsoleColor.Black, ConsoleColor.Yellow); + SetLastDownload(false, dataFile); } catch (WebException wex) { - WriteLn(String.Format("\nERROR!! => [{0}]]", wex.Message), ConsoleColor.Red, ConsoleColor.White); - WriteLn(String.Format("Press any key to exit.", wex.Message), ConsoleColor.Red, ConsoleColor.White); + WriteLn(string.Format("\nERROR!! => [{0}]]", wex.Message), ConsoleColor.Red, ConsoleColor.White); + WriteLn(string.Format("Press any key to exit.", wex.Message), ConsoleColor.Red, ConsoleColor.White); Console.ReadKey(); System.Environment.Exit(-1); } @@ -233,12 +234,12 @@ public static List DownloadVatsimData(string _uri, string _filename, st altitude = fp.Altitude, lat = pilot.Latitude.ToString(), lon = pilot.Longitude.ToString(), - DistanceTo = Util.distance( + DistanceTo = distance( Convert.ToDouble(user_lat_dest.Replace(".", ",")), Convert.ToDouble(user_lon_dest.Replace(".", ",")), Convert.ToDouble(pilot.Latitude.ToString().Replace(".", ",")), Convert.ToDouble(pilot.Longitude.ToString().Replace(".", ",")), 'N'), - DistanceFrom = Util.distance( + DistanceFrom = distance( Convert.ToDouble(user_lat_dep.Replace(".", ",")), Convert.ToDouble(user_lon_dep.Replace(".", ",")), Convert.ToDouble(pilot.Latitude.ToString().Replace(".", ",")), @@ -260,5 +261,17 @@ public static string GetVersion() return gitVersion; } + public static void SetLastDownload(Boolean isFirstTime, string dataFile) + { + if (isFirstTime) + { + File.SetLastWriteTimeUtc(dataFile, new DateTime(1974, 3, 25)); + } + else + { + File.SetLastWriteTimeUtc(dataFile, DateTime.UtcNow); + } + } + } }