From cfafb5dd5475df0092f3d8ec0b3834165fc0f001 Mon Sep 17 00:00:00 2001 From: Joshua Carroll Date: Thu, 7 Jul 2022 18:30:14 -0500 Subject: [PATCH] Bug fixes and updated updater --- gisreporter_/Extension.cs | 21 +++++++ gisreporter_/Program.cs | 111 ++++++++++++--------------------- gisreporter_/Utilities.cs | 1 + gisreporter_updater/Program.cs | 21 ++----- 4 files changed, 69 insertions(+), 85 deletions(-) create mode 100644 gisreporter_/Extension.cs diff --git a/gisreporter_/Extension.cs b/gisreporter_/Extension.cs new file mode 100644 index 0000000..30c7c19 --- /dev/null +++ b/gisreporter_/Extension.cs @@ -0,0 +1,21 @@ +using System; +using System.Linq; + +public static class Extension +{ + public static T[] Concatenate(this T[] first, T[] second) + { + if (first == null) + { + return second; + } + if (second == null) + { + return first; + } + + return first.Concat(second).ToArray(); + } +} + + diff --git a/gisreporter_/Program.cs b/gisreporter_/Program.cs index 34d3885..a9cefab 100644 --- a/gisreporter_/Program.cs +++ b/gisreporter_/Program.cs @@ -17,15 +17,19 @@ class Program private static string gisReceiverUrl = gisserver.PrivateVariables.FunctionAppUrl; private static List mapItems = new List(); private static bool continueRunning = true; - private static DateTime lastMessageReceived; - private static int numberOfTimerCycles = 0; + private static DateTime lastMessageCheck; + private static int numberOfTimerCycles; private static bool updateIsAvailable = false; public static double Version; static void Main(string[] args) { - Version = 1.21; - lastMessageReceived = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0)); + Version = 1.24; + + + // Initialize global variables + lastMessageCheck = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0)); + numberOfTimerCycles = 1; Console.Write(@" /-------------------------------------------------------------------------\ @@ -52,13 +56,12 @@ a tactical address controlled from Paclink. CheckForUpdates(true, args); CheckForNewData(args); + Task t = Task.Run(async () => { do { - Console.Write(string.Format("\r{0} ", DateTime.Now.ToUniversalTime())); - - int i = 60; + int i = 3; // Seconds of delay between message checks. do { await Task.Delay(1000); @@ -69,8 +72,8 @@ a tactical address controlled from Paclink. CheckForNewData(args); - numberOfTimerCycles = numberOfTimerCycles++; - if (numberOfTimerCycles % 10 == 0) + numberOfTimerCycles = numberOfTimerCycles + 1; + if (numberOfTimerCycles % 60 == 0) { CheckForUpdates(false, args); } @@ -88,46 +91,6 @@ private static void CheckForNewData(string[] paths) } } - // This is not currently used. - private static void CheckWinlinkKml(string winlinkPath) - { - if (winlinkPath != string.Empty) - { - // Validate file path - if (Directory.Exists(winlinkPath)) - { - if (File.Exists(String.Format("{0}/Winlink_Messages.kml", winlinkPath))) - { - - } - - if (mapItems.Count > 0) - { - // Send to web service - string json = JsonSerializer.Serialize(mapItems); - HttpWebRequest req = (HttpWebRequest)WebRequest.Create(gisReceiverUrl); - req.Method = "POST"; - req.ContentType = "application/json"; - Stream stream = req.GetRequestStream(); - byte[] buffer = Encoding.UTF8.GetBytes(json); - stream.Write(buffer, 0, buffer.Length); - HttpWebResponse res = (HttpWebResponse)req.GetResponse(); - Console.WriteLine("Server response: " + res.StatusDescription); - - if (res.StatusCode == HttpStatusCode.OK) - { - mapItems.Clear(); - } - } - } - else - { - Console.WriteLine("** The Winlink folder you specified does not exist."); - continueRunning = false; - } - } - } - private static void CheckWinlinkMessages(string path) { if (path != string.Empty) @@ -135,19 +98,19 @@ private static void CheckWinlinkMessages(string path) // Validate file path if (Directory.Exists(path)) { - DateTime newestNewMessageReceived = lastMessageReceived; - string[] filesMime = Directory.GetFiles(path, "*.mime"); - newestNewMessageReceived = ProcessFiles(filesMime, newestNewMessageReceived); - string[] filesBsf = Directory.GetFiles(path, "*.b2f"); - newestNewMessageReceived = ProcessFiles(filesBsf, newestNewMessageReceived); + string[] filesToProcess = filesMime.Concatenate(filesBsf); - lastMessageReceived = newestNewMessageReceived; + ProcessFiles(filesToProcess); if (mapItems.Count > 0) { // Send to web service + string suffix = "s"; + if (mapItems.Count < 2) { suffix = ""; } + + Console.WriteLine(String.Format("Sending {0} new message{1} to the server...", mapItems.Count, suffix)); string json = JsonSerializer.Serialize(mapItems); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(gisReceiverUrl); @@ -161,7 +124,7 @@ private static void CheckWinlinkMessages(string path) try { res = (HttpWebResponse)req.GetResponse(); - Console.WriteLine("Server response: " + res.StatusDescription); + Console.WriteLine(" Server response: " + res.StatusDescription); if (res.StatusCode == HttpStatusCode.OK) { @@ -170,8 +133,9 @@ private static void CheckWinlinkMessages(string path) } catch (Exception ex) { - Console.WriteLine("SERVER ERROR: " + ex.Message); + Console.WriteLine(" ** SERVER ERROR: " + ex.Message); } + Console.WriteLine(""); } } else @@ -189,29 +153,28 @@ private static void CheckWinlinkMessages(string path) /// String array containing full path filenames to the emails to process /// Datetime representing the date and time of the most recently processed email /// Datetime representing the date and time of the most recently processed email - private static DateTime ProcessFiles(string[] files, DateTime newestNewMessageReceived) + private static void ProcessFiles(string[] files) { - DateTime outputNewestNewMessageReceived = newestNewMessageReceived; for (int i = 0; i < files.Length; i++) { - if (File.GetLastWriteTime(files[i]) > lastMessageReceived) + if (File.GetLastWriteTime(files[i]) > lastMessageCheck) { - if (newestNewMessageReceived < File.GetLastWriteTime(files[i])) - { - newestNewMessageReceived = File.GetLastWriteTime(files[i]); - } - WinlinkMessage msg = new WinlinkMessage(File.ReadAllText(files[i]), files[i]); if (msg.MessageXML != null && msg.MessageXML != string.Empty) { - Console.WriteLine(String.Format("- {0} received from {1} at {2}", msg.MessageSubject, msg.SendersCallsign, msg.DateTimeSent)); + if (mapItems.Count == 0) + { + Console.Write(Environment.NewLine); + } + Console.WriteLine(String.Format(" - {0} received from {1} at {2}", msg.MessageSubject, msg.SendersCallsign, msg.DateTimeSent)); MapItem mapItem = msg.ToMapItem(); mapItems.Add(mapItem); } } } - return outputNewestNewMessageReceived; + + lastMessageCheck = DateTime.Now; } private static void CheckForUpdates(bool promptToInstall, string[] programArgs) @@ -222,6 +185,7 @@ private static void CheckForUpdates(bool promptToInstall, string[] programArgs) } else { + Console.WriteLine("Checking for updates..."); // Get the json releases from Github Github.Release latestRelease; using (WebClient wc = new WebClient()) @@ -252,7 +216,9 @@ private static void CheckForUpdates(bool promptToInstall, string[] programArgs) { // Download the updated files Console.Write(@" -***************************** + + +************************************************************* An updated version is available. Is is strongly recommended to update now. @@ -261,7 +227,8 @@ An updated version is available. Is is strongly recommended to update now. Proceed with the update? (Y/N) -***************************** +************************************************************* + ?: "); string input = Console.ReadLine(); @@ -294,7 +261,7 @@ An updated version is available. Is is strongly recommended to update now. } // unzip file +++++ - ZipFile.ExtractToDirectory(tgtDir + Path.DirectorySeparatorChar + latestRelease.assets[zipfileAssetIndex].name, tgtDir); + ZipFile.ExtractToDirectory(tgtDir + Path.DirectorySeparatorChar + latestRelease.assets[zipfileAssetIndex].name, tgtDir, true); string allProgramArgs = ""; for (int i = 0; i < programArgs.Length; i++) @@ -314,6 +281,10 @@ An updated version is available. Is is strongly recommended to update now. } } } + else + { + Console.WriteLine(" No updates available."); + } } } diff --git a/gisreporter_/Utilities.cs b/gisreporter_/Utilities.cs index bb5e0ef..f7b196e 100644 --- a/gisreporter_/Utilities.cs +++ b/gisreporter_/Utilities.cs @@ -58,5 +58,6 @@ public static void ChangeLine(string fileName, string newText, int line_to_edit) arrLine[line_to_edit - 1] = newText; File.WriteAllLines(fileName, arrLine); } + } } \ No newline at end of file diff --git a/gisreporter_updater/Program.cs b/gisreporter_updater/Program.cs index f80e2eb..0894728 100644 --- a/gisreporter_updater/Program.cs +++ b/gisreporter_updater/Program.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.IO; +using System.Threading.Tasks; namespace gisreporter_updater { @@ -8,14 +9,13 @@ class Program { static string pathToUpdateFiles; static string pathToCurrentFiles; - static int processIdOfProgram; static string commandToExecuteWhenDone; static void Main(string[] args) { if (args.Length < 4) { - Console.WriteLine("USAGE: gisreporter_updater [pathToUpdateFiles] [pathToCurrentFiles] [processNameOfProgramToUpdate] [commandToExecuteWhenDone]" + Environment.NewLine); + Console.WriteLine("USAGE: gisreporter_updater [pathToUpdateFiles] [pathToCurrentFiles] [commandToExecuteWhenDone]" + Environment.NewLine); Console.Write("Path to the new files: "); pathToUpdateFiles = Console.ReadLine(); @@ -23,9 +23,6 @@ static void Main(string[] args) Console.Write("Path to the current files: "); pathToCurrentFiles = Console.ReadLine(); - Console.Write("ID of current program process: "); - processIdOfProgram = int.Parse(Console.ReadLine()); - Console.Write("Command to execute when done: "); commandToExecuteWhenDone = Console.ReadLine(); @@ -36,20 +33,14 @@ static void Main(string[] args) { pathToUpdateFiles = args[0].ToString(); pathToCurrentFiles = args[1].ToString(); - processIdOfProgram = int.Parse(args[2].ToString()); commandToExecuteWhenDone = args[3]; } - Console.WriteLine(String.Format("Waiting for process {0} to exit...", processIdOfProgram)); + Console.WriteLine(Environment.NewLine + Environment.NewLine); + Console.WriteLine("Waiting for previous program to stop..."); - if (Process.GetProcessById(processIdOfProgram).WaitForExit(5000)) - { - Console.WriteLine(" Process exited."); - } - { - Process.GetProcessById(processIdOfProgram).Kill(); - Console.WriteLine(" Process killed after timeout."); - } + // We need to wait for the calling program to close before we can overwrite the files + Task.Delay(3000); string[] files = Directory.GetFiles(pathToUpdateFiles, "*.*"); for (int i = 0; i < files.Length; i++)