diff --git a/Source/v2/Meadow.CLI/Meadow.CLI.csproj b/Source/v2/Meadow.CLI/Meadow.CLI.csproj index a4cff560..ad6efaa5 100644 --- a/Source/v2/Meadow.CLI/Meadow.CLI.csproj +++ b/Source/v2/Meadow.CLI/Meadow.CLI.csproj @@ -10,7 +10,7 @@ Wilderness Labs, Inc Wilderness Labs, Inc true - 2.0.3 + 2.0.4 AnyCPU http://developer.wildernesslabs.co/Meadow/Meadow.CLI/ https://github.com/WildernessLabs/Meadow.CLI diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index ba575665..30b14e8d 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -121,7 +121,6 @@ protected override async ValueTask ExecuteCommand() if (package == null) { - Logger?.LogError($"Firmware write failed - No package selected"); return; } @@ -241,6 +240,8 @@ protected override async ValueTask ExecuteCommand() || Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocApplicationFile || Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocBootloaderFile) { + await connection.RuntimeDisable(); + await WriteEspFiles(connection, deviceInfo, package); } @@ -291,11 +292,9 @@ protected override async ValueTask ExecuteCommand() private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? deviceInfo, FirmwarePackage package) { - if (connection == null) - { - connection = await GetConnectionAndDisableRuntime(); - if (connection == null) return; // couldn't find a connected device - } + connection ??= await GetConnectionAndDisableRuntime(); + + if (connection == null) { return; } // couldn't find a connected device Logger?.LogInformation($"{Environment.NewLine}Writing Coprocessor files..."); @@ -372,7 +371,7 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi if (existing == null) { - Logger?.LogError($"Requested version '{Version}' not found"); + Logger?.LogError($"Requested firmware version '{Version}' not found"); return null; } package = existing; diff --git a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs index 2a5d4923..7c357814 100644 --- a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs +++ b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs @@ -6,6 +6,6 @@ namespace Meadow.CLI { public static class Constants { - public const string CLI_VERSION = "2.0.3.0"; + public const string CLI_VERSION = "2.0.4.0"; } } \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/Firmware/DownloadManager.cs b/Source/v2/Meadow.Hcom/Firmware/DownloadManager.cs deleted file mode 100644 index 0be1cb43..00000000 --- a/Source/v2/Meadow.Hcom/Firmware/DownloadManager.cs +++ /dev/null @@ -1,200 +0,0 @@ -using Microsoft.Extensions.Logging; -using System.IO.Compression; -using System.Text.Json; - -namespace Meadow.Hcom; - -public class DownloadManager -{ - static readonly string RootFolder = "WildernessLabs"; - static readonly string FirmwareFolder = "Firmware"; - static readonly string LatestFilename = "latest.txt"; - - public static readonly string OsFilename = "Meadow.OS.bin"; - public static readonly string RuntimeFilename = "Meadow.OS.Runtime.bin"; - public static readonly string NetworkBootloaderFilename = "bootloader.bin"; - public static readonly string NetworkMeadowCommsFilename = "MeadowComms.bin"; - public static readonly string NetworkPartitionTableFilename = "partition-table.bin"; - internal static readonly string VersionCheckUrlRoot = "https://s3-us-west-2.amazonaws.com/downloads.wildernesslabs.co/Meadow_Beta/"; - - public static readonly string UpdateCommand = "dotnet tool update WildernessLabs.Meadow.CLI --global"; - - public static readonly string FirmwareDownloadsFolder = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - RootFolder, FirmwareFolder); - - public static string FirmwareLatestVersion - { - get - { - string latestPath = Path.Combine(FirmwareDownloadsFolder, LatestFilename); - if (File.Exists(latestPath)) - { - return File.ReadAllText(latestPath); - } - throw new FileNotFoundException("Latest firmware not found"); - } - } - - private static readonly HttpClient Client = new() { Timeout = TimeSpan.FromMinutes(1) }; - - private readonly ILogger _logger; - - public DownloadManager(ILogger logger) - { - _logger = logger; - } - - internal async Task DownloadMeadowOSVersionFile(string? version) - { - string versionCheckUrl, versionCheckFile; - - if (string.IsNullOrWhiteSpace(version)) - { - _logger.LogInformation("Downloading latest version file" + Environment.NewLine); - versionCheckUrl = VersionCheckUrlRoot + "latest.json"; - } - else - { - _logger.LogInformation("Downloading version file for Meadow OS " + version + Environment.NewLine); - versionCheckUrl = VersionCheckUrlRoot + version + ".json"; - } - - try - { - versionCheckFile = await DownloadFile(new Uri(versionCheckUrl)); - } - catch - { - return null; - } - - return versionCheckFile; - } - - public async Task DownloadOsBinaries(string? version = null, bool force = false) - { - var versionFilePath = await DownloadMeadowOSVersionFile(version); - - if (versionFilePath == null) - { - _logger.LogError($"Meadow OS {version} cannot be downloaded or is not available"); - return; - } - - var payload = File.ReadAllText(versionFilePath); - var release = JsonSerializer.Deserialize(payload); - - if (release == null) - { - _logger.LogError($"Unable to read release details for Meadow OS"); - return; - } - - if (Directory.Exists(FirmwareDownloadsFolder) == false) - { - Directory.CreateDirectory(FirmwareDownloadsFolder); - //we'll write latest.txt regardless of version if it doesn't exist - File.WriteAllText(Path.Combine(FirmwareDownloadsFolder, LatestFilename), release.Version); - } - else if (version == null) - { //otherwise update if we're pulling the latest release OS - File.WriteAllText(Path.Combine(FirmwareDownloadsFolder, LatestFilename), release.Version); - } - - var firmwareVersionPath = Path.Combine(FirmwareDownloadsFolder, release.Version); - - if (Directory.Exists(firmwareVersionPath)) - { - if (force) - { - DeleteDirectory(firmwareVersionPath); - } - else - { - _logger.LogInformation($"Meadow OS version {release.Version} is already downloaded"); - return; - } - } - - Directory.CreateDirectory(firmwareVersionPath); - - try - { - _logger.LogInformation($"Downloading Meadow OS"); - await DownloadAndUnpack(new Uri(release.DownloadURL), firmwareVersionPath); - } - catch - { - _logger.LogError($"Unable to download Meadow OS {version}"); - return; - } - - try - { - _logger.LogInformation("Downloading coprocessor firmware"); - await DownloadAndUnpack(new Uri(release.NetworkDownloadURL), firmwareVersionPath); - } - catch - { - _logger.LogError($"Unable to download coprocessor firmware {version}"); - return; - } - - _logger.LogInformation($"Downloaded and extracted OS version {release.Version} to: {firmwareVersionPath}"); - } - - private async Task DownloadFile(Uri uri, CancellationToken cancellationToken = default) - { - using var request = new HttpRequestMessage(HttpMethod.Get, uri); - using var response = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); - - response.EnsureSuccessStatusCode(); - - var downloadFileName = Path.GetTempFileName(); - _logger.LogDebug($"Copying downloaded file to temp file {downloadFileName}"); - - using var stream = await response.Content.ReadAsStreamAsync(); - using var downloadFileStream = new DownloadFileStream(stream, _logger); - using var firmwareFile = File.OpenWrite(downloadFileName); - - await downloadFileStream.CopyToAsync(firmwareFile); - - return downloadFileName; - } - - private async Task DownloadAndUnpack(Uri uri, string targetPath, CancellationToken cancellationToken = default) - { - var file = await DownloadFile(uri, cancellationToken); - - _logger.LogDebug($"Extracting {file} to {targetPath}"); - - ZipFile.ExtractToDirectory(file, targetPath); - - try - { - File.Delete(file); - } - catch (Exception ex) - { - _logger.LogDebug(ex, "Unable to delete temporary file"); - } - } - - /// - /// Delete all files and sub directorines in a directory - /// - /// The directory path - /// Optional ILogger for exception reporting - public static void DeleteDirectory(string path, ILogger? logger = null) - { - try - { - Directory.Delete(path, true); - } - catch (IOException e) - { - logger?.LogWarning($"Failed to delete {path} - {e.Message}"); - } - } -} \ No newline at end of file