Skip to content

Commit

Permalink
Bug fixes and updated updater
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaCarroll committed Jul 7, 2022
1 parent b7727f8 commit cfafb5d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 85 deletions.
21 changes: 21 additions & 0 deletions gisreporter_/Extension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Linq;

public static class Extension
{
public static T[] Concatenate<T>(this T[] first, T[] second)
{
if (first == null)
{
return second;
}
if (second == null)
{
return first;
}

return first.Concat(second).ToArray();
}
}


111 changes: 41 additions & 70 deletions gisreporter_/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ class Program
private static string gisReceiverUrl = gisserver.PrivateVariables.FunctionAppUrl;
private static List<MapItem> mapItems = new List<MapItem>();
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(@"
/-------------------------------------------------------------------------\
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -88,66 +91,26 @@ 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)
{
// 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<string>(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);
Expand All @@ -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)
{
Expand All @@ -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
Expand All @@ -189,29 +153,28 @@ private static void CheckWinlinkMessages(string path)
/// <param name="files">String array containing full path filenames to the emails to process</param>
/// <param name="newestNewMessageReceived">Datetime representing the date and time of the most recently processed email</param>
/// <returns>Datetime representing the date and time of the most recently processed email</returns>
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)
Expand All @@ -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())
Expand Down Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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++)
Expand All @@ -314,6 +281,10 @@ An updated version is available. Is is strongly recommended to update now.
}
}
}
else
{
Console.WriteLine(" No updates available.");
}
}
}

Expand Down
1 change: 1 addition & 0 deletions gisreporter_/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}
21 changes: 6 additions & 15 deletions gisreporter_updater/Program.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace gisreporter_updater
{
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();

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();

Expand All @@ -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++)
Expand Down

0 comments on commit cfafb5d

Please sign in to comment.