From 4d155e1be1798d724597587b93ad056ef5c966df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20H=C3=B6glinger-Stelzer?= Date: Thu, 11 Jul 2024 16:45:06 +0200 Subject: [PATCH] Modernized code --- ControlApp/Models/DshmDevMan.cs | 38 +++---- .../Models/JsonApplicationConfiguration.cs | 106 ++++++++++-------- .../ViewModels/Windows/MainWindowViewModel.cs | 4 +- 3 files changed, 77 insertions(+), 71 deletions(-) diff --git a/ControlApp/Models/DshmDevMan.cs b/ControlApp/Models/DshmDevMan.cs index db74600a..ea39c777 100644 --- a/ControlApp/Models/DshmDevMan.cs +++ b/ControlApp/Models/DshmDevMan.cs @@ -57,45 +57,43 @@ public void UpdateConnectedDshmDevicesList() Log.Logger.Debug($"DsHidMini devices list rebuilt. {Devices.Count} connected devices"); ConnectedDeviceListUpdated?.Invoke(this, new()); } - + public bool TryReconnectDevice(PnPDevice device) { Log.Logger.Information($"Attempting on reconnecting device of instance {device.InstanceId}"); - var enumerator = device.GetProperty(DevicePropertyKey.Device_EnumeratorName); - var IsWireless = !enumerator.Equals("USB", StringComparison.InvariantCultureIgnoreCase); - Log.Logger.Debug($"Is Device connected wirelessly: {IsWireless}"); + string? enumerator = device.GetProperty(DevicePropertyKey.Device_EnumeratorName); + bool isWireless = !enumerator!.Equals("USB", StringComparison.InvariantCultureIgnoreCase); + Log.Logger.Debug($"Is Device connected wireless: {isWireless}"); - if (IsWireless) + if (isWireless) { try { HostRadio hostRadio = new(); - var deviceAddress = device.GetProperty(DsHidMiniDriver.DeviceAddressProperty).ToUpper(); + string deviceAddress = device.GetProperty(DsHidMiniDriver.DeviceAddressProperty)!.ToUpper(); Log.Logger.Debug($"Instructing BT host on disconnecting device of MAC {deviceAddress}"); hostRadio.DisconnectRemoteDevice(deviceAddress); return true; } - catch(Exception ex) + catch (Exception ex) { Log.Error(ex, "Failed to disconnect wireless device."); return false; } } - else + + try { - try - { - var ohmy = device.ToUsbPnPDevice(); - Log.Logger.Debug($"Power cycling device's USB port"); - ohmy.CyclePort(); - return true; - } - catch (Exception e) - { - Log.Logger.Error(e,$"Failed to power cycle device's USB port"); - return false; - } + UsbPnPDevice? usbPnPDevice = device.ToUsbPnPDevice(); + Log.Logger.Debug("Power cycling device's USB port"); + usbPnPDevice.CyclePort(); + return true; + } + catch (Exception e) + { + Log.Logger.Error(e, "Failed to power cycle device's USB port"); + return false; } } diff --git a/ControlApp/Models/JsonApplicationConfiguration.cs b/ControlApp/Models/JsonApplicationConfiguration.cs index 933a9e38..eb66e471 100644 --- a/ControlApp/Models/JsonApplicationConfiguration.cs +++ b/ControlApp/Models/JsonApplicationConfiguration.cs @@ -8,71 +8,79 @@ using System.IO; using System.Text; + using Newtonsoft.Json; using Newtonsoft.Json.Converters; -namespace Nefarius.DsHidMini.ControlApp.Models +namespace Nefarius.DsHidMini.ControlApp.Models; + +/// Provides methods to load and save the application configuration. +public static class JsonApplicationConfiguration { - /// Provides methods to load and save the application configuration. - public static class JsonApplicationConfiguration + private const string ConfigExtension = ".json"; + + /// Loads the application configuration. + /// The type of the application configuration. + /// The configuration file name without extension. + /// Defines if the schema file should always be generated and overwritten. + /// Defines if the configuration file should be loaded from the user's AppData directory. + /// The configuration object. + /// An I/O error occurred while opening the file. + public static T Load(string fileNameWithoutExtension, bool alwaysCreateNewSchemaFile, bool storeInAppData) + where T : new() { - private const string ConfigExtension = ".json"; + string configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData); - /// Loads the application configuration. - /// The type of the application configuration. - /// The configuration file name without extension. - /// Defines if the schema file should always be generated and overwritten. - /// Defines if the configuration file should be loaded from the user's AppData directory. - /// The configuration object. - /// An I/O error occurred while opening the file. - public static T Load(string fileNameWithoutExtension, bool alwaysCreateNewSchemaFile, bool storeInAppData) where T : new() + if (!File.Exists(configPath)) { - var configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData); - - if (!File.Exists(configPath)) - return CreateDefaultConfigurationFile(fileNameWithoutExtension, storeInAppData); - - return JsonConvert.DeserializeObject(File.ReadAllText(configPath, Encoding.UTF8)); + return CreateDefaultConfigurationFile(fileNameWithoutExtension, storeInAppData); } - /// Saves the configuration. - /// The configuration file name without extension. - /// The configuration object to store. - /// Defines if the configuration file should be stored in the user's AppData directory. - /// An I/O error occurred while opening the file. - public static void Save(string fileNameWithoutExtension, T configuration, bool storeInAppData) where T : new() - { - var settings = new JsonSerializerSettings(); - settings.Converters.Add(new StringEnumConverter()); + return JsonConvert.DeserializeObject(File.ReadAllText(configPath, Encoding.UTF8)); + } - var configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData); - File.WriteAllText(configPath, JsonConvert.SerializeObject(configuration, Formatting.Indented, settings), Encoding.UTF8); - } + /// Saves the configuration. + /// The configuration file name without extension. + /// The configuration object to store. + /// Defines if the configuration file should be stored in the user's AppData directory. + /// An I/O error occurred while opening the file. + public static void Save(string fileNameWithoutExtension, T configuration, bool storeInAppData) where T : new() + { + JsonSerializerSettings settings = new JsonSerializerSettings(); + settings.Converters.Add(new StringEnumConverter()); - private static string CreateFilePath(string fileNameWithoutExtension, string extension, bool storeInAppData) - { - if (storeInAppData) - { - var appDataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); - var filePath = Path.Combine(appDataDirectory, fileNameWithoutExtension) + extension; + string configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData); + File.WriteAllText(configPath, JsonConvert.SerializeObject(configuration, Formatting.Indented, settings), + Encoding.UTF8); + } - var directoryPath = Path.GetDirectoryName(filePath); - if (directoryPath != null && !Directory.Exists(directoryPath)) - Directory.CreateDirectory(directoryPath); + private static string CreateFilePath(string fileNameWithoutExtension, string extension, bool storeInAppData) + { + if (storeInAppData) + { + string appDataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + string filePath = Path.Combine(appDataDirectory, fileNameWithoutExtension) + extension; - return filePath; + string? directoryPath = Path.GetDirectoryName(filePath); + if (directoryPath != null && !Directory.Exists(directoryPath)) + { + Directory.CreateDirectory(directoryPath); } - return fileNameWithoutExtension + extension; + + return filePath; } - private static T CreateDefaultConfigurationFile(string fileNameWithoutExtension, bool storeInAppData) where T : new() - { - var config = new T(); - var configData = JsonConvert.SerializeObject(config, Formatting.Indented); - var configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData); + return fileNameWithoutExtension + extension; + } - File.WriteAllText(configPath, configData, Encoding.UTF8); - return config; - } + private static T CreateDefaultConfigurationFile(string fileNameWithoutExtension, bool storeInAppData) + where T : new() + { + T? config = new T(); + string configData = JsonConvert.SerializeObject(config, Formatting.Indented); + string configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData); + + File.WriteAllText(configPath, configData, Encoding.UTF8); + return config; } } \ No newline at end of file diff --git a/ControlApp/ViewModels/Windows/MainWindowViewModel.cs b/ControlApp/ViewModels/Windows/MainWindowViewModel.cs index 026c08da..12864d5c 100644 --- a/ControlApp/ViewModels/Windows/MainWindowViewModel.cs +++ b/ControlApp/ViewModels/Windows/MainWindowViewModel.cs @@ -32,7 +32,7 @@ public partial class MainWindowViewModel : ObservableObject }; [ObservableProperty] - private bool _IsAppElevated = SecurityUtil.IsElevated; + private bool _isAppElevated = SecurityUtil.IsElevated; [ObservableProperty] private ObservableCollection _menuItems = new() @@ -62,7 +62,7 @@ public MainWindowViewModel(AppSnackbarMessagesService appSnackbarMessagesService _appSnackbarMessagesService = appSnackbarMessagesService; if (IsAppElevated) { - ApplicationTitle += " [Administrador]"; + ApplicationTitle += " [Administrator]"; } }