Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MultiTerm/MultiTerm.Protocols/UsbHid/UsbHidProtocolSettingsViewM...

100 lines
3.4 KiB

using Common.Messaging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using MultiTerm.Protocols.Types;
using System.Collections.ObjectModel;
namespace MultiTerm.Protocols.UsbHid;
public partial class UsbHidProtocolSettingsViewModel : ProtocolSettingsViewModel, IUsbHidProtocolSettings
{
public override ProtocolType ProtocolType => ProtocolType.UsbHid;
public ushort VendorId { get; set; } = ushort.MinValue;
public ushort ProductId { get; set; } = ushort.MinValue;
[ObservableProperty]
private string serialNumber = string.Empty;
[ObservableProperty]
private string vendorIdHex = string.Empty;
[ObservableProperty]
private string productIdHex = string.Empty;
[ObservableProperty]
private ObservableCollection<UsbHidDeviceInfo> devices = new();
[ObservableProperty]
private UsbHidDeviceInfo? selectedDevice;
[RelayCommand]
private void ReloadDevices()
{
this.Devices = new ObservableCollection<UsbHidDeviceInfo>(UsbHidProtocol.GetDevices());
// select first deivce, if none is selected yet and there are some available
if (this.SelectedDevice == null && this.Devices.Any())
{
this.SelectedDevice = this.Devices.First();
}
}
public UsbHidProtocolSettingsViewModel(IMessenger messenger) : base(messenger)
{
// load devices initially
this.ReloadDevices();
this.SelectedDevice = this.Devices.FirstOrDefault();
}
/// <summary>
/// Updates other fields when <see cref="SelectedDevice"/> was changed.
/// </summary>
/// <param name="value"></param>
partial void OnSelectedDeviceChanged(UsbHidDeviceInfo? value)
{
if(value != null)
{
this.VendorIdHex = value.VendorId;
this.ProductIdHex = value.ProductId;
this.SerialNumber = value.SerialNumber;
}
}
public bool AreValid()
{
// pad left (fill up zeroes)
this.VendorIdHex = this.VendorIdHex.PadLeft(4, '0');
if (this.VendorIdHex.Length > 4)
{
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidMessage(nameof(this.VendorId), "Must consist of a maximum of 4 hexadecimal characters"));
return false;
}
if (ushort.TryParse(this.VendorIdHex, System.Globalization.NumberStyles.HexNumber, null, out ushort vendorId) == false)
{
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidMessage(nameof(this.VendorId), "Invalid. Not a 16bit number."));
return false;
}
// set internal
this.VendorId = vendorId;
// pad left (fill up zeroes)
this.ProductIdHex = this.ProductIdHex.PadLeft(4, '0');
if (this.ProductIdHex.Length > 4)
{
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidMessage(nameof(this.ProductId), "Must consist of a maximum of 4 hexadecimal characters"));
return false;
}
if (ushort.TryParse(this.ProductIdHex, System.Globalization.NumberStyles.HexNumber, null, out ushort productId) == false)
{
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidMessage(nameof(this.ProductId), "Invalid. Not a 16bit number."));
return false;
}
// set internal
this.ProductId = productId;
return true;
}
}