From 51c98c24251a580a333b95fe93f696249008cf50 Mon Sep 17 00:00:00 2001 From: Adrian Stevens Date: Sun, 14 Jan 2024 22:07:54 -0800 Subject: [PATCH] Improved validation and user feedback --- .../Commands/Current/App/AppBuildCommand.cs | 13 +-- .../Commands/Current/App/AppDebugCommand.cs | 24 ++--- .../Commands/Current/App/AppDeployCommand.cs | 3 +- .../Commands/Current/App/AppRunCommand.cs | 93 +++++++++---------- .../Commands/Current/App/AppTrimCommand.cs | 6 +- .../Current/Cloud/CloudLoginCommand.cs | 7 +- .../Current/Cloud/CloudLogoutCommand.cs | 7 +- .../Collection/CloudCollectionListCommand.cs | 7 +- .../JsonDocumentBindingConverter.cs | 6 +- .../Command/CloudCommandPublishCommand.cs | 4 +- .../Package/CloudPackageCreateCommand.cs | 8 +- .../Cloud/Package/CloudPackageListCommand.cs | 10 +- .../Package/CloudPackagePublishCommand.cs | 8 +- .../Package/CloudPackageUploadCommand.cs | 9 +- .../Commands/Current/Config/ConfigCommand.cs | 13 +-- .../Current/Device/DeviceClockCommand.cs | 10 +- .../Current/Device/DeviceInfoCommand.cs | 5 +- .../Current/Device/DeviceProvisionCommand.cs | 2 +- .../Current/Device/DeviceResetCommand.cs | 5 +- .../Commands/Current/Dfu/DfuInstallCommand.cs | 16 +--- .../Current/File/FileDeleteCommand.cs | 4 +- .../Firmware/FirmwareDefaultCommand.cs | 4 +- .../Current/Firmware/FirmwareDeleteCommand.cs | 4 +- .../Current/Firmware/FirmwareListCommand.cs | 7 +- .../Current/Firmware/FirmwareWriteCommand.cs | 3 +- .../Current/Flash/FlashEraseCommand.cs | 3 +- .../Commands/Current/Port/PortListCommand.cs | 4 +- .../Current/Port/PortSelectCommand.cs | 43 ++++----- .../Current/Runtime/RuntimeDisableCommand.cs | 11 ++- .../Current/Runtime/RuntimeEnableCommand.cs | 11 ++- .../Current/Runtime/RuntimeStateCommand.cs | 11 ++- .../Current/Trace/TraceDisableCommand.cs | 9 +- .../Current/Trace/TraceEnableCommand.cs | 9 +- .../Current/Trace/TraceLevelCommand.cs | 8 +- .../Meadow.Hcom/Firmware/FirmwareUpdater.cs | 6 +- 35 files changed, 174 insertions(+), 219 deletions(-) diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs index 9831daf0..04c21517 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppBuildCommand.cs @@ -3,7 +3,7 @@ namespace Meadow.CLI.Commands.DeviceManagement; -[Command("app build", Description = "Compiles a Meadow application")] +[Command("app build", Description = "Compile a Meadow application")] public class AppBuildCommand : BaseCommand { private readonly IPackageManager _packageManager; @@ -22,9 +22,7 @@ public AppBuildCommand(IPackageManager packageManager, ILoggerFactory loggerFact protected override ValueTask ExecuteCommand() { - string path = Path == null - ? AppDomain.CurrentDomain.BaseDirectory - : Path; + string path = Path ?? AppDomain.CurrentDomain.BaseDirectory; // is the path a file? if (!File.Exists(path)) @@ -37,10 +35,7 @@ protected override ValueTask ExecuteCommand() } } - if (Configuration == null) - { - Configuration = "Release"; - } + Configuration ??= "Release"; Logger?.LogInformation($"Building {Configuration} configuration of {path}..."); @@ -53,7 +48,7 @@ protected override ValueTask ExecuteCommand() } else { - Logger?.LogError($"Build successful"); + Logger?.LogInformation($"Build successful"); } return ValueTask.CompletedTask; } diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppDebugCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppDebugCommand.cs index a20fcb83..4655a1c6 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppDebugCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppDebugCommand.cs @@ -3,7 +3,7 @@ namespace Meadow.CLI.Commands.DeviceManagement; -[Command("app debug", Description = "Debugs a running application")] +[Command("app debug", Description = "Debug a running application")] public class AppDebugCommand : BaseDeviceCommand { // VS 2019 - 4024 @@ -14,8 +14,7 @@ public class AppDebugCommand : BaseDeviceCommand public AppDebugCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { @@ -27,20 +26,17 @@ protected override async ValueTask ExecuteCommand() return; } - if (connection != null) + connection.DeviceMessageReceived += (s, e) => { - connection.DeviceMessageReceived += (s, e) => - { - Logger?.LogInformation(e.message); - }; + Logger?.LogInformation(e.message); + }; - using (var server = await connection.StartDebuggingSession(Port, Logger, CancellationToken)) + using (var server = await connection.StartDebuggingSession(Port, Logger, CancellationToken)) + { + if (Console != null) { - if (Console != null) - { - Logger?.LogInformation("Debugging server started. Press Enter to exit"); - await Console.Input.ReadLineAsync(); - } + Logger?.LogInformation("Debugging server started - press Enter to exit"); + await Console.Input.ReadLineAsync(); } } } diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppDeployCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppDeployCommand.cs index a8125b30..8ff2b81a 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppDeployCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppDeployCommand.cs @@ -1,11 +1,10 @@ using CliFx.Attributes; -using Meadow.CLI; using Microsoft.Extensions.Logging; namespace Meadow.CLI.Commands.DeviceManagement; -[Command("app deploy", Description = "Deploys a built Meadow application to a target device")] +[Command("app deploy", Description = "Deploy a built Meadow application to a target device")] public class AppDeployCommand : BaseDeviceCommand { private readonly IPackageManager _packageManager; diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs index f11cc484..04f997ca 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppRunCommand.cs @@ -35,60 +35,57 @@ protected override async ValueTask ExecuteCommand() return; } - if (connection != null) + string path = Path == null + ? AppDomain.CurrentDomain.BaseDirectory + : Path; + + if (!Directory.Exists(path)) { - string path = Path == null - ? AppDomain.CurrentDomain.BaseDirectory - : Path; - - if (!Directory.Exists(path)) - { - Logger?.LogError($"Target directory '{path}' not found."); - return; - } - - var lastFile = string.Empty; - - // in order to deploy, the runtime must be disabled - var wasRuntimeEnabled = await connection.IsRuntimeEnabled(); - if (wasRuntimeEnabled) - { - Logger?.LogInformation("Disabling runtime..."); - - await connection.RuntimeDisable(CancellationToken); - } - - if (!await BuildApplication(path, CancellationToken)) - { - return; - } - - if (!await TrimApplication(path, CancellationToken)) - { - return; - } - - // illink returns before all files are written - attempt a delay of 1s - await Task.Delay(1000); + Logger?.LogError($"Target directory '{path}' not found."); + return; + } - if (!await DeployApplication(connection, path, CancellationToken)) - { - return; - } + var lastFile = string.Empty; - Logger?.LogInformation("Enabling the runtime..."); - await connection.RuntimeEnable(CancellationToken); + // in order to deploy, the runtime must be disabled + var wasRuntimeEnabled = await connection.IsRuntimeEnabled(); + if (wasRuntimeEnabled) + { + Logger?.LogInformation("Disabling runtime..."); - Logger?.LogInformation("Listening for messages from Meadow...\n"); - connection.DeviceMessageReceived += OnDeviceMessageReceived; + await connection.RuntimeDisable(CancellationToken); + } - while (!CancellationToken.IsCancellationRequested) - { - await Task.Delay(1000); - } + if (!await BuildApplication(path, CancellationToken)) + { + return; + } - Logger?.LogInformation("Listen cancelled..."); + if (!await TrimApplication(path, CancellationToken)) + { + return; } + + // illink returns before all files are written - attempt a delay of 1s + await Task.Delay(1000); + + if (!await DeployApplication(connection, path, CancellationToken)) + { + return; + } + + Logger?.LogInformation("Enabling the runtime..."); + await connection.RuntimeEnable(CancellationToken); + + Logger?.LogInformation("Listening for messages from Meadow...\n"); + connection.DeviceMessageReceived += OnDeviceMessageReceived; + + while (!CancellationToken.IsCancellationRequested) + { + await Task.Delay(1000); + } + + Logger?.LogInformation("Listen cancelled..."); } private Task BuildApplication(string path, CancellationToken cancellationToken) @@ -170,4 +167,4 @@ private void OnDeviceMessageReceived(object? sender, (string message, string? so Logger?.LogInformation($"{e.source}> {e.message.TrimEnd('\n', '\r')}"); } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs index 5d35d922..6fb547bf 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/App/AppTrimCommand.cs @@ -1,6 +1,4 @@ using CliFx.Attributes; -using CliFx.Infrastructure; -using Meadow.CLI; using Microsoft.Extensions.Logging; namespace Meadow.CLI.Commands.DeviceManagement; @@ -8,7 +6,7 @@ namespace Meadow.CLI.Commands.DeviceManagement; [Command("app trim", Description = "Runs an already-compiled Meadow application through reference trimming")] public class AppTrimCommand : BaseCommand { - private IPackageManager _packageManager; + private readonly IPackageManager _packageManager; [CommandOption('c', Description = "The build configuration to trim", IsRequired = false)] public string? Configuration { get; set; } @@ -61,4 +59,4 @@ protected override async ValueTask ExecuteCommand() await _packageManager.TrimApplication(file, false, null, CancellationToken); } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLoginCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLoginCommand.cs index 0d617e8d..f7238df3 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLoginCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLoginCommand.cs @@ -18,12 +18,11 @@ public CloudLoginCommand( CollectionService collectionService, ILoggerFactory? loggerFactory) : base(identityManager, userService, deviceService, collectionService, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { - if (Host == null) Host = DefaultHost; + Host ??= DefaultHost; Logger?.LogInformation($"Logging into {Host}..."); @@ -37,4 +36,4 @@ protected override async ValueTask ExecuteCommand() : "There was a problem retrieving your account information."); } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLogoutCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLogoutCommand.cs index 334cf5d6..85d99b48 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLogoutCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/CloudLogoutCommand.cs @@ -15,13 +15,14 @@ public CloudLogoutCommand( CollectionService collectionService, ILoggerFactory? loggerFactory) : base(identityManager, userService, deviceService, collectionService, loggerFactory) - { - } + { } - protected override async ValueTask ExecuteCommand() + protected override ValueTask ExecuteCommand() { Logger?.LogInformation($"Logging out of Meadow.Cloud..."); IdentityManager.Logout(); + + return ValueTask.CompletedTask; } } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/CloudCollectionListCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/CloudCollectionListCommand.cs index 25127cdd..90867fdb 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/CloudCollectionListCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/CloudCollectionListCommand.cs @@ -20,12 +20,11 @@ public CloudCollectionListCommand( CollectionService collectionService, ILoggerFactory? loggerFactory) : base(identityManager, userService, deviceService, collectionService, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { - if (Host == null) Host = DefaultHost; + Host ??= DefaultHost; var org = await ValidateOrg(Host, OrgId, CancellationToken); if (org == null) return; @@ -45,4 +44,4 @@ protected override async ValueTask ExecuteCommand() } } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs index aa8f4bf2..c9120958 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Collection/JsonDocumentBindingConverter.cs @@ -6,15 +6,15 @@ namespace Meadow.CLI.Commands.DeviceManagement; public class JsonDocumentBindingConverter : BindingConverter { - public override JsonDocument Convert(string rawValue) + public override JsonDocument Convert(string? rawValue) { try { - return JsonDocument.Parse(rawValue); + return JsonDocument.Parse(rawValue!); } catch (JsonException ex) { throw new CommandException($"Provided argument is not valid JSON: {ex.Message}", showHelp: false, innerException: ex); } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs index ba8c9694..9ca34e17 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Command/CloudCommandPublishCommand.cs @@ -11,7 +11,7 @@ namespace Meadow.CLI.Commands.DeviceManagement; public class CloudCommandPublishCommand : BaseCloudCommand { [CommandParameter(0, Description = "The name of the command", IsRequired = true, Name = "COMMAND_NAME")] - public string CommandName { get; set; } + public string CommandName { get; set; } = default!; [CommandOption("collectionId", 'c', Description = "The target collection for publishing the command")] public string? CollectionId { get; set; } @@ -54,7 +54,7 @@ protected override async ValueTask ExecuteCommand() throw new CommandException("Cannot specify both a collection ID (-c|--collectionId) and list of device IDs (-d|--deviceIds). Only one is allowed.", showHelp: true); } - if (Host == null) Host = DefaultHost; + Host ??= DefaultHost; var token = await IdentityManager.GetAccessToken(CancellationToken); if (string.IsNullOrWhiteSpace(token)) diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs index f9a01c73..718d8fa3 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageCreateCommand.cs @@ -41,10 +41,7 @@ public CloudPackageCreateCommand( protected override async ValueTask ExecuteCommand() { - if (ProjectPath == null) - { - ProjectPath = AppDomain.CurrentDomain.BaseDirectory; - } + ProjectPath ??= AppDomain.CurrentDomain.BaseDirectory; // build Logger?.LogInformation($"Building {Configuration} version of application..."); @@ -85,6 +82,5 @@ protected override async ValueTask ExecuteCommand() { Logger?.LogError($"Package assembly failed."); } - } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageListCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageListCommand.cs index 2a4d531d..29a0584b 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageListCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageListCommand.cs @@ -8,7 +8,7 @@ namespace Meadow.CLI.Commands.DeviceManagement; [Command("cloud package list", Description = "Lists all Meadow Packages (MPAK)")] public class CloudPackageListCommand : BaseCloudCommand { - private PackageService _packageService; + private readonly PackageService _packageService; [CommandOption("orgId", 'o', Description = "Optional organization ID", IsRequired = false)] public string? OrgId { get; set; } @@ -30,16 +30,16 @@ public CloudPackageListCommand( protected override async ValueTask ExecuteCommand() { - if (Host == null) Host = DefaultHost; + Host ??= DefaultHost; var org = await ValidateOrg(Host, OrgId, CancellationToken); - if (org == null) return; + if (org == null) { return; } var packages = await _packageService.GetOrgPackages(org.Id, Host, CancellationToken); if (packages == null || packages.Count == 0) { - Logger?.LogInformation("No packages found."); + Logger?.LogInformation("No packages found"); } else { @@ -50,4 +50,4 @@ protected override async ValueTask ExecuteCommand() } } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackagePublishCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackagePublishCommand.cs index 9f0e440e..7aa75daf 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackagePublishCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackagePublishCommand.cs @@ -8,7 +8,7 @@ namespace Meadow.CLI.Commands.DeviceManagement; [Command("cloud package publish", Description = "Publishes a Meadow Package (MPAK)")] public class CloudPackagePublishCommand : BaseCloudCommand { - private PackageService _packageService; + private readonly PackageService _packageService; [CommandParameter(0, Name = "PackageID", Description = "ID of the package to publish", IsRequired = true)] public string PackageId { get; init; } @@ -36,18 +36,18 @@ public CloudPackagePublishCommand( protected override async ValueTask ExecuteCommand() { - if (Host == null) Host = DefaultHost; + Host ??= DefaultHost; try { Logger?.LogInformation($"Publishing package {PackageId} to collection {CollectionId}..."); await _packageService.PublishPackage(PackageId, CollectionId, Metadata, Host, CancellationToken); - Logger?.LogInformation("Publish successful."); + Logger?.LogInformation("Publish successful"); } catch (MeadowCloudException mex) { Logger?.LogError($"Publish failed: {mex.Message}"); } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageUploadCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageUploadCommand.cs index 6d9db340..d91df4a6 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageUploadCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Cloud/Package/CloudPackageUploadCommand.cs @@ -20,7 +20,7 @@ public class CloudPackageUploadCommand : BaseCloudCommand public ConfigCommand(ISettingsManager settingsManager, ILoggerFactory? loggerFactory) : base(settingsManager, loggerFactory) - { - - } + { } - protected override async ValueTask ExecuteCommand() + protected override ValueTask ExecuteCommand() { if (List) { @@ -64,5 +59,7 @@ protected override async ValueTask ExecuteCommand() throw new CommandException($"Too many parameters provided"); } } + + return ValueTask.CompletedTask; } } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceClockCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceClockCommand.cs index dbf5fc88..9b217762 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceClockCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceClockCommand.cs @@ -11,16 +11,15 @@ public class DeviceClockCommand : BaseDeviceCommand public DeviceClockCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { - + Logger?.LogInformation($"Device clock failed - device or connection not found"); return; } @@ -47,6 +46,5 @@ protected override async ValueTask ExecuteCommand() Logger?.LogInformation($"Unable to parse '{Time}' to a valid time."); } } - } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceInfoCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceInfoCommand.cs index 2c4c6265..5b92bc89 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceInfoCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceInfoCommand.cs @@ -16,8 +16,9 @@ protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogInformation($"Device info failed - device or connection not found"); return; } @@ -27,4 +28,4 @@ protected override async ValueTask ExecuteCommand() Logger?.LogInformation(deviceInfo.ToString()); } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceProvisionCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceProvisionCommand.cs index 85d51793..9f155d95 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceProvisionCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceProvisionCommand.cs @@ -107,4 +107,4 @@ protected override async ValueTask ExecuteCommand() return; } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceResetCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceResetCommand.cs index 833809a2..ed3c83db 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceResetCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Device/DeviceResetCommand.cs @@ -16,11 +16,12 @@ protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogInformation($"Device reset failed - device or connection not found"); return; } await connection.Device.Reset(); } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Dfu/DfuInstallCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Dfu/DfuInstallCommand.cs index 5c1b173e..f9708726 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Dfu/DfuInstallCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Dfu/DfuInstallCommand.cs @@ -1,8 +1,5 @@ using CliFx.Attributes; -using CliFx.Infrastructure; -using Meadow.CLI; using Meadow.CLI.Core.Internals.Dfu; -using Meadow.Hcom; using Meadow.Software; using Microsoft.Extensions.Logging; using System.Runtime.InteropServices; @@ -26,15 +23,11 @@ protected DfuInstallCommand(ISettingsManager settingsManager, ILoggerFactory log public DfuInstallCommand(ISettingsManager settingsManager, ILoggerFactory loggerFactory) : base(settingsManager, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { - if (Version == null) - { - Version = DefaultVersion; - } + Version ??= DefaultVersion; switch (Version) { @@ -43,7 +36,7 @@ protected override async ValueTask ExecuteCommand() // valid break; default: - Logger?.LogError("Only versions 0.10 and 0.11 are supported."); + Logger?.LogError("Only DFU versions 0.10 and 0.11 are supported"); return; } @@ -64,8 +57,7 @@ protected override async ValueTask ExecuteCommand() } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - Logger?.LogWarning( - "To install DFU on Linux, use the package manager to install the dfu-util package"); + Logger?.LogWarning("To install DFU on Linux, use the package manager to install the dfu-util package"); } } diff --git a/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs index 18b81387..010038ef 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs @@ -20,7 +20,7 @@ protected override async ValueTask ExecuteCommand() if (connection == null || connection.Device == null) { - Logger?.LogInformation($"File delete failed - device or connection not found"); + Logger?.LogError($"File delete failed - device or connection not found"); return; } @@ -73,4 +73,4 @@ protected override async ValueTask ExecuteCommand() } } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDefaultCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDefaultCommand.cs index 7fbfeac1..978ff994 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDefaultCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDefaultCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using Meadow.CLI; using Meadow.Software; using Microsoft.Extensions.Logging; @@ -10,8 +9,7 @@ public class FirmwareDefaultCommand : BaseFileCommand { public FirmwareDefaultCommand(FileManager fileManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory) : base(fileManager, settingsManager, loggerFactory) - { - } + { } [CommandParameter(0, Name = "Version number to use as default", IsRequired = false)] public string? Version { get; set; } = null; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDeleteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDeleteCommand.cs index 30dbc2f1..f6625ab9 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDeleteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareDeleteCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using Meadow.CLI; using Meadow.Software; using Microsoft.Extensions.Logging; @@ -10,8 +9,7 @@ public class FirmwareDeleteCommand : BaseFileCommand { public FirmwareDeleteCommand(FileManager fileManager, ISettingsManager settingsManager, ILoggerFactory loggerFactory) : base(fileManager, settingsManager, loggerFactory) - { - } + { } [CommandParameter(0, Name = "Version number to delete", IsRequired = true)] public string Version { get; set; } = default!; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareListCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareListCommand.cs index ce9d6c6b..ffb763c7 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareListCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareListCommand.cs @@ -1,7 +1,4 @@ -using CliFx; -using CliFx.Attributes; -using CliFx.Infrastructure; -using Meadow.CLI; +using CliFx.Attributes; using Meadow.Software; using Microsoft.Extensions.Logging; @@ -107,4 +104,4 @@ private async Task DisplayTerseResults(FileManager manager) } } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index ae9b0fc1..96f5c52d 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -344,5 +344,4 @@ await DfuUtils.FlashFile( logger: Logger, format: DfuUtils.DfuFlashFormat.ConsoleOut); } -} - +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Flash/FlashEraseCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Flash/FlashEraseCommand.cs index 8dcdd610..8a458a9c 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Flash/FlashEraseCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Flash/FlashEraseCommand.cs @@ -8,8 +8,7 @@ public class FlashEraseCommand : BaseDeviceCommand { public FlashEraseCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { diff --git a/Source/v2/Meadow.Cli/Commands/Current/Port/PortListCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Port/PortListCommand.cs index 4b959b55..995d480a 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Port/PortListCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Port/PortListCommand.cs @@ -10,12 +10,12 @@ public class PortListCommand : BaseCommand public PortListCommand(ILoggerFactory loggerFactory) : base(loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { Portlist = await MeadowConnectionManager.GetSerialPorts(); + if (Portlist.Count > 0) { var plural = Portlist.Count > 1 ? "s" : string.Empty; diff --git a/Source/v2/Meadow.Cli/Commands/Current/Port/PortSelectCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Port/PortSelectCommand.cs index a5b43034..6e2e26af 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Port/PortSelectCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Port/PortSelectCommand.cs @@ -1,5 +1,4 @@ using CliFx.Attributes; -using Meadow.CLI; using Microsoft.Extensions.Logging; namespace Meadow.CLI.Commands.DeviceManagement; @@ -9,40 +8,36 @@ public class PortSelectCommand : BaseCommand { public PortSelectCommand(ILoggerFactory loggerFactory) : base(loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { - if (LoggerFactory != null) + if (LoggerFactory != null && Console != null) { - if (Console != null) - { - var portListCommand = new PortListCommand(LoggerFactory); + var portListCommand = new PortListCommand(LoggerFactory); - await portListCommand.ExecuteAsync(Console); + await portListCommand.ExecuteAsync(Console); - if (portListCommand.Portlist?.Count > 0) + if (portListCommand.Portlist?.Count > 0) + { + if (portListCommand.Portlist?.Count > 1) { - if (portListCommand.Portlist?.Count > 1) - { - Logger?.LogInformation($"{Environment.NewLine}Type the number of the port you would like to use.{Environment.NewLine}or just press Enter to keep your current port."); + Logger?.LogInformation($"{Environment.NewLine}Type the number of the port you would like to use.{Environment.NewLine}or just press Enter to keep your current port."); - byte deviceSelected; - if (byte.TryParse(await Console.Input.ReadLineAsync(), out deviceSelected)) + byte deviceSelected; + if (byte.TryParse(await Console.Input.ReadLineAsync(), out deviceSelected)) + { + if (deviceSelected > 0 && deviceSelected <= portListCommand.Portlist?.Count) { - if (deviceSelected > 0 && deviceSelected <= portListCommand.Portlist?.Count) - { - await CallConfigCommand(portListCommand.Portlist[deviceSelected - 1]); - } + await CallConfigCommand(portListCommand.Portlist[deviceSelected - 1]); } } - else - { - // Only 1 device attached, let's auto select it - if (portListCommand.Portlist != null) - await CallConfigCommand(portListCommand.Portlist[0]); - } + } + else + { + // Only 1 device attached, let's auto select it + if (portListCommand.Portlist != null) + await CallConfigCommand(portListCommand.Portlist[0]); } } } diff --git a/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeDisableCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeDisableCommand.cs index e1fc867e..908c2d01 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeDisableCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeDisableCommand.cs @@ -8,23 +8,24 @@ public class RuntimeDisableCommand : BaseDeviceCommand { public RuntimeDisableCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - Logger?.LogInformation($"Disabling runtime..."); - } + { } protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogError($"Runtime disable failed - device or connection not found"); return; } + Logger?.LogInformation($"Disabling runtime..."); + await connection.Device.RuntimeDisable(CancellationToken); var state = await connection.Device.IsRuntimeEnabled(CancellationToken); Logger?.LogInformation($"Runtime is {(state ? "ENABLED" : "DISABLED")}"); } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeEnableCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeEnableCommand.cs index 8061a64b..066dba13 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeEnableCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeEnableCommand.cs @@ -8,23 +8,24 @@ public class RuntimeEnableCommand : BaseDeviceCommand { public RuntimeEnableCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - Logger?.LogInformation($"Enabling runtime..."); - } + { } protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogError($"Runtime disable failed - device or connection not found"); return; } + Logger?.LogInformation($"Enabling runtime..."); + await connection.Device.RuntimeEnable(CancellationToken); var state = await connection.Device.IsRuntimeEnabled(CancellationToken); Logger?.LogInformation($"Runtime is {(state ? "ENABLED" : "DISABLED")}"); } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeStateCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeStateCommand.cs index 19569c1c..0c772ee4 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeStateCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Runtime/RuntimeStateCommand.cs @@ -8,21 +8,22 @@ public class RuntimeStateCommand : BaseDeviceCommand { public RuntimeStateCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - Logger?.LogInformation($"Querying runtime state..."); - } + { } protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogError($"Runtime state failed - device or connection not found"); return; } + Logger?.LogInformation($"Querying runtime state..."); + var state = await connection.Device.IsRuntimeEnabled(CancellationToken); Logger?.LogInformation($"Runtime is {(state ? "ENABLED" : "DISABLED")}"); } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceDisableCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceDisableCommand.cs index c3aa27f5..94b7a602 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceDisableCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceDisableCommand.cs @@ -8,15 +8,15 @@ public class TraceDisableCommand : BaseDeviceCommand { public TraceDisableCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogError($"Trace disable failed - device or connection not found"); return; } @@ -29,5 +29,4 @@ protected override async ValueTask ExecuteCommand() await connection.Device.TraceDisable(CancellationToken); } -} - +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceEnableCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceEnableCommand.cs index faafb992..31e393be 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceEnableCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceEnableCommand.cs @@ -11,15 +11,15 @@ public class TraceEnableCommand : BaseDeviceCommand public TraceEnableCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogError($"Trace enable failed - device or connection not found"); return; } @@ -38,5 +38,4 @@ protected override async ValueTask ExecuteCommand() await connection.Device.TraceEnable(CancellationToken); } -} - +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceLevelCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceLevelCommand.cs index 73309878..1b66ff11 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceLevelCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Trace/TraceLevelCommand.cs @@ -11,15 +11,15 @@ public class TraceLevelCommand : BaseDeviceCommand public TraceLevelCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory) : base(connectionManager, loggerFactory) - { - } + { } protected override async ValueTask ExecuteCommand() { var connection = await GetCurrentConnection(); - if (connection == null) + if (connection == null || connection.Device == null) { + Logger?.LogError($"Trace level failed - device or connection not found"); return; } @@ -43,4 +43,4 @@ protected override async ValueTask ExecuteCommand() await connection.Device.TraceEnable(CancellationToken); } } -} +} \ No newline at end of file diff --git a/Source/v2/Meadow.Hcom/Firmware/FirmwareUpdater.cs b/Source/v2/Meadow.Hcom/Firmware/FirmwareUpdater.cs index 03c70cd9..5bf5aea1 100644 --- a/Source/v2/Meadow.Hcom/Firmware/FirmwareUpdater.cs +++ b/Source/v2/Meadow.Hcom/Firmware/FirmwareUpdater.cs @@ -5,8 +5,8 @@ namespace Meadow.Hcom; public class FirmwareUpdater { - private ILogger? _logger; - private Task? _updateTask; + private readonly ILogger? _logger; + private readonly Task? _updateTask; private IMeadowConnection _connection; private UpdateState _state; @@ -130,7 +130,7 @@ private async void StateMachine() } break; case UpdateState.DFUCompleted: - // if we started in DFU mode, we'll have no connection. We'll have to just assume the first one to appear is what we're after + // if we started in DFU mode, we'll have no connection. We'll have to just assume the first one to appear is what we're after try { // wait for device to reconnect