Skip to content

Commit

Permalink
Modernized code
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Jul 11, 2024
1 parent 6c3db24 commit 4d155e1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 71 deletions.
38 changes: 18 additions & 20 deletions ControlApp/Models/DshmDevMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(DevicePropertyKey.Device_EnumeratorName);
var IsWireless = !enumerator.Equals("USB", StringComparison.InvariantCultureIgnoreCase);
Log.Logger.Debug($"Is Device connected wirelessly: {IsWireless}");
string? enumerator = device.GetProperty<string>(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<string>(DsHidMiniDriver.DeviceAddressProperty).ToUpper();
string deviceAddress = device.GetProperty<string>(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;
}
}

Expand Down
106 changes: 57 additions & 49 deletions ControlApp/Models/JsonApplicationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>Provides methods to load and save the application configuration. </summary>
public static class JsonApplicationConfiguration
{
/// <summary>Provides methods to load and save the application configuration. </summary>
public static class JsonApplicationConfiguration
private const string ConfigExtension = ".json";

/// <summary>Loads the application configuration. </summary>
/// <typeparam name="T">The type of the application configuration. </typeparam>
/// <param name="fileNameWithoutExtension">The configuration file name without extension. </param>
/// <param name="alwaysCreateNewSchemaFile">Defines if the schema file should always be generated and overwritten. </param>
/// <param name="storeInAppData">Defines if the configuration file should be loaded from the user's AppData directory. </param>
/// <returns>The configuration object. </returns>
/// <exception cref="IOException">An I/O error occurred while opening the file. </exception>
public static T Load<T>(string fileNameWithoutExtension, bool alwaysCreateNewSchemaFile, bool storeInAppData)
where T : new()
{
private const string ConfigExtension = ".json";
string configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData);

/// <summary>Loads the application configuration. </summary>
/// <typeparam name="T">The type of the application configuration. </typeparam>
/// <param name="fileNameWithoutExtension">The configuration file name without extension. </param>
/// <param name="alwaysCreateNewSchemaFile">Defines if the schema file should always be generated and overwritten. </param>
/// <param name="storeInAppData">Defines if the configuration file should be loaded from the user's AppData directory. </param>
/// <returns>The configuration object. </returns>
/// <exception cref="IOException">An I/O error occurred while opening the file. </exception>
public static T Load<T>(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<T>(fileNameWithoutExtension, storeInAppData);

return JsonConvert.DeserializeObject<T>(File.ReadAllText(configPath, Encoding.UTF8));
return CreateDefaultConfigurationFile<T>(fileNameWithoutExtension, storeInAppData);
}

/// <summary>Saves the configuration. </summary>
/// <param name="fileNameWithoutExtension">The configuration file name without extension. </param>
/// <param name="configuration">The configuration object to store. </param>
/// <param name="storeInAppData">Defines if the configuration file should be stored in the user's AppData directory. </param>
/// <exception cref="IOException">An I/O error occurred while opening the file. </exception>
public static void Save<T>(string fileNameWithoutExtension, T configuration, bool storeInAppData) where T : new()
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter());
return JsonConvert.DeserializeObject<T>(File.ReadAllText(configPath, Encoding.UTF8));
}

var configPath = CreateFilePath(fileNameWithoutExtension, ConfigExtension, storeInAppData);
File.WriteAllText(configPath, JsonConvert.SerializeObject(configuration, Formatting.Indented, settings), Encoding.UTF8);
}
/// <summary>Saves the configuration. </summary>
/// <param name="fileNameWithoutExtension">The configuration file name without extension. </param>
/// <param name="configuration">The configuration object to store. </param>
/// <param name="storeInAppData">Defines if the configuration file should be stored in the user's AppData directory. </param>
/// <exception cref="IOException">An I/O error occurred while opening the file. </exception>
public static void Save<T>(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<T>(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<T>(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;
}
}
4 changes: 2 additions & 2 deletions ControlApp/ViewModels/Windows/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public partial class MainWindowViewModel : ObservableObject
};

[ObservableProperty]
private bool _IsAppElevated = SecurityUtil.IsElevated;
private bool _isAppElevated = SecurityUtil.IsElevated;

[ObservableProperty]
private ObservableCollection<object> _menuItems = new()
Expand Down Expand Up @@ -62,7 +62,7 @@ public MainWindowViewModel(AppSnackbarMessagesService appSnackbarMessagesService
_appSnackbarMessagesService = appSnackbarMessagesService;
if (IsAppElevated)
{
ApplicationTitle += " [Administrador]";
ApplicationTitle += " [Administrator]";
}
}

Expand Down

0 comments on commit 4d155e1

Please sign in to comment.