Skip to content

Commit

Permalink
-Fix interval for download
Browse files Browse the repository at this point in the history
  • Loading branch information
e4rthdog committed May 3, 2021
1 parent 7b2768b commit 27b8704
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
10 changes: 7 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<VatLine>();
Expand Down Expand Up @@ -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 =>
{
Expand Down
25 changes: 19 additions & 6 deletions Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ public static void WriteLn(string _str, ConsoleColor _background, ConsoleColor _
Console.ForegroundColor = currentForeground;
}

public static List<VatLine> DownloadVatsimData(string _uri, string _filename, string _interval)
public static List<VatLine> DownloadVatsimData(string _uri, string dataFile, string _interval)
{
var Airports = new List<Airport>();
List<VatLine> dataList = new List<VatLine>();
DateTime lastDownload = File.GetLastWriteTime(_filename);
DateTime lastDownload = File.GetLastWriteTime(dataFile);
TimeSpan span = DateTime.Now.Subtract(lastDownload);
Airports = LoadAirports();
if (span.TotalMinutes > Convert.ToDouble(_interval))
Expand All @@ -191,11 +191,12 @@ public static List<VatLine> 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);
}
Expand Down Expand Up @@ -233,12 +234,12 @@ public static List<VatLine> 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(".", ",")),
Expand All @@ -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);
}
}

}
}

0 comments on commit 27b8704

Please sign in to comment.