From 578b90cde18710e765c621dc930956c70e6f80c8 Mon Sep 17 00:00:00 2001 From: kka Date: Thu, 30 Sep 2021 17:55:22 +0800 Subject: [PATCH 1/3] Remove the 4 hour Delay to download Firmware Alway check for with CloudSmith on the Latest and or Fwversion if provided Add Try Catch to CheckVersion in case there is no internet the program does not crash. This enhancement will allow offline usage. --- nanoFirmwareFlasher/FirmwarePackage.cs | 37 +++++++++++++++++++++----- nanoFirmwareFlasher/Program.cs | 30 +++++++++++++-------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/nanoFirmwareFlasher/FirmwarePackage.cs b/nanoFirmwareFlasher/FirmwarePackage.cs index 35d0a91f..ad58cb73 100644 --- a/nanoFirmwareFlasher/FirmwarePackage.cs +++ b/nanoFirmwareFlasher/FirmwarePackage.cs @@ -146,12 +146,23 @@ protected async System.Threading.Tasks.Task DownloadAndExtractAsync() if (fwFiles.Any()) { - // get file creation date (from the 1st one) - if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(fwFiles.First().FullName)).TotalHours < 4) + //// get file creation date (from the 1st one) + //if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(fwFiles.First().FullName)).TotalHours < 4) + //{ + // // fw package has less than 4 hours + // // skip download + // skipDownload = true; + //} + + if (!string.IsNullOrEmpty(_fwVersion)) { - // fw package has less than 4 hours - // skip download - skipDownload = true; + string targetFileName = $"{_targetName}-{_fwVersion}.zip"; + + if (!fwFiles.Where(w => w.Name == targetFileName).Any()) + { + // set Flag to Download the Firmware as it is not downloaded before + skipDownload = false; + } } } @@ -321,8 +332,20 @@ protected async System.Threading.Tasks.Task DownloadAndExtractAsync() if (fwFiles.Any()) { - // take the 1st one - fwFileName = fwFiles.First().FullName; + if (string.IsNullOrEmpty(_fwVersion)) + {// take the 1st one + fwFileName = fwFiles.First().FullName; + } + else + { + string targetFileName = $"{_targetName}-{_fwVersion}.zip"; + fwFileName = fwFiles.Where(w => w.Name == targetFileName).Select(s => s.FullName).FirstOrDefault(); + } + + if (string.IsNullOrEmpty(fwFileName)) + { + return ExitCodes.E9007; + } // get the version form the file name var pattern = @"(\d+\.\d+\.\d+)(\.\d+|-.+)(?=\.zip)"; diff --git a/nanoFirmwareFlasher/Program.cs b/nanoFirmwareFlasher/Program.cs index 4fbf0fde..802606cb 100644 --- a/nanoFirmwareFlasher/Program.cs +++ b/nanoFirmwareFlasher/Program.cs @@ -90,25 +90,33 @@ private static void CheckVersion() { Version latestVersion; Version currentVersion = Version.Parse(_informationalVersionAttribute.InformationalVersion.Split('+')[0]); - - using (var client = new HttpClient()) + try { - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); + using (var client = new HttpClient()) + { + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); - client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("nanoff", currentVersion.ToString())); + client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("nanoff", currentVersion.ToString())); - HttpResponseMessage response = client.GetAsync("https://api.github.com/repos/nanoframework/nanoFirmwareFlasher/releases/latest").Result; + HttpResponseMessage response = client.GetAsync("https://api.github.com/repos/nanoframework/nanoFirmwareFlasher/releases/latest").Result; - dynamic responseContent = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); - string tagName = responseContent.tag_name.ToString(); + dynamic responseContent = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); + string tagName = responseContent.tag_name.ToString(); - latestVersion = Version.Parse(tagName.Substring(1)); - } + latestVersion = Version.Parse(tagName.Substring(1)); + } - if(latestVersion > currentVersion) + if (latestVersion > currentVersion) + { + Console.ForegroundColor = ConsoleColor.DarkYellow; + Console.WriteLine("** There is a new version available, update is recommended **"); + Console.ForegroundColor = ConsoleColor.White; + } + } + catch { Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine("** There is a new version available, update is recommended **"); + Console.WriteLine("** Error Connecting to GitHub for version checking **"); Console.ForegroundColor = ConsoleColor.White; } } From c6b6908fbb4883801809d43a829e9790a4c10dc6 Mon Sep 17 00:00:00 2001 From: kka Date: Fri, 1 Oct 2021 08:28:53 +0800 Subject: [PATCH 2/3] Remove Local Firmware File Check as this is done on Line 313 Remove CheckVersion Enhancment to next Pull Request. --- nanoFirmwareFlasher/FirmwarePackage.cs | 23 +------------------ nanoFirmwareFlasher/Program.cs | 31 ++++++++++---------------- 2 files changed, 13 insertions(+), 41 deletions(-) diff --git a/nanoFirmwareFlasher/FirmwarePackage.cs b/nanoFirmwareFlasher/FirmwarePackage.cs index ad58cb73..9dced456 100644 --- a/nanoFirmwareFlasher/FirmwarePackage.cs +++ b/nanoFirmwareFlasher/FirmwarePackage.cs @@ -144,28 +144,7 @@ protected async System.Threading.Tasks.Task DownloadAndExtractAsync() .ToList(); } - if (fwFiles.Any()) - { - //// get file creation date (from the 1st one) - //if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(fwFiles.First().FullName)).TotalHours < 4) - //{ - // // fw package has less than 4 hours - // // skip download - // skipDownload = true; - //} - - if (!string.IsNullOrEmpty(_fwVersion)) - { - string targetFileName = $"{_targetName}-{_fwVersion}.zip"; - - if (!fwFiles.Where(w => w.Name == targetFileName).Any()) - { - // set Flag to Download the Firmware as it is not downloaded before - skipDownload = false; - } - } - } - + if (!skipDownload) { // try to perform request diff --git a/nanoFirmwareFlasher/Program.cs b/nanoFirmwareFlasher/Program.cs index 802606cb..65e52880 100644 --- a/nanoFirmwareFlasher/Program.cs +++ b/nanoFirmwareFlasher/Program.cs @@ -90,35 +90,28 @@ private static void CheckVersion() { Version latestVersion; Version currentVersion = Version.Parse(_informationalVersionAttribute.InformationalVersion.Split('+')[0]); - try + + using (var client = new HttpClient()) { - using (var client = new HttpClient()) - { - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); - - client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("nanoff", currentVersion.ToString())); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); - HttpResponseMessage response = client.GetAsync("https://api.github.com/repos/nanoframework/nanoFirmwareFlasher/releases/latest").Result; + client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("nanoff", currentVersion.ToString())); - dynamic responseContent = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); - string tagName = responseContent.tag_name.ToString(); + HttpResponseMessage response = client.GetAsync("https://api.github.com/repos/nanoframework/nanoFirmwareFlasher/releases/latest").Result; - latestVersion = Version.Parse(tagName.Substring(1)); - } + dynamic responseContent = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); + string tagName = responseContent.tag_name.ToString(); - if (latestVersion > currentVersion) - { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine("** There is a new version available, update is recommended **"); - Console.ForegroundColor = ConsoleColor.White; - } + latestVersion = Version.Parse(tagName.Substring(1)); } - catch + + if (latestVersion > currentVersion) { Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine("** Error Connecting to GitHub for version checking **"); + Console.WriteLine("** There is a new version available, update is recommended **"); Console.ForegroundColor = ConsoleColor.White; } + } private static Task HandleErrorsAsync(IEnumerable errors) From f1fe6ece263f24250f73c2d7158b0dc35c753975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Mon, 4 Oct 2021 09:18:00 +0100 Subject: [PATCH 3/3] Update Program.cs - Revert all changes. --- nanoFirmwareFlasher/Program.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nanoFirmwareFlasher/Program.cs b/nanoFirmwareFlasher/Program.cs index 65e52880..4fbf0fde 100644 --- a/nanoFirmwareFlasher/Program.cs +++ b/nanoFirmwareFlasher/Program.cs @@ -90,7 +90,7 @@ private static void CheckVersion() { Version latestVersion; Version currentVersion = Version.Parse(_informationalVersionAttribute.InformationalVersion.Split('+')[0]); - + using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); @@ -105,13 +105,12 @@ private static void CheckVersion() latestVersion = Version.Parse(tagName.Substring(1)); } - if (latestVersion > currentVersion) + if(latestVersion > currentVersion) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("** There is a new version available, update is recommended **"); Console.ForegroundColor = ConsoleColor.White; } - } private static Task HandleErrorsAsync(IEnumerable errors)