2021-03-05 10:47:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-02-24 08:05:11 +00:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2021-03-05 10:47:12 +00:00
|
|
|
|
using Dapplo.Windows.Devices;
|
|
|
|
|
using Dapplo.Windows.Devices.Enums;
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
|
|
|
|
namespace MassiveKnob.Plugin.SerialDevice.Settings
|
|
|
|
|
{
|
2021-03-05 10:47:12 +00:00
|
|
|
|
public class SerialDeviceSettingsViewModel : IDisposable, INotifyPropertyChanged, IObserver<DeviceNotificationEvent>
|
2021-02-24 08:05:11 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly SerialDeviceSettings settings;
|
2021-03-05 10:47:12 +00:00
|
|
|
|
private IList<string> serialPorts;
|
|
|
|
|
private readonly IDisposable deviceSubscription;
|
2021-02-24 08:05:11 +00:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ReSharper disable UnusedMember.Global - used by WPF Binding
|
2021-03-05 10:47:12 +00:00
|
|
|
|
public IList<string> SerialPorts
|
2021-02-24 08:05:11 +00:00
|
|
|
|
{
|
|
|
|
|
get => serialPorts;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
serialPorts = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string PortName
|
|
|
|
|
{
|
|
|
|
|
get => settings.PortName;
|
|
|
|
|
set
|
|
|
|
|
{
|
2021-03-05 10:47:12 +00:00
|
|
|
|
if (value == settings.PortName || value == null)
|
2021-02-24 08:05:11 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
settings.PortName = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int BaudRate
|
|
|
|
|
{
|
|
|
|
|
get => settings.BaudRate;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == settings.BaudRate)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
settings.BaudRate = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-28 10:55:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool DtrEnable
|
|
|
|
|
{
|
|
|
|
|
get => settings.DtrEnable;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == settings.DtrEnable)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
settings.DtrEnable = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-24 08:05:11 +00:00
|
|
|
|
// ReSharper restore UnusedMember.Global
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SerialDeviceSettingsViewModel(SerialDeviceSettings settings)
|
|
|
|
|
{
|
|
|
|
|
this.settings = settings;
|
|
|
|
|
|
|
|
|
|
serialPorts = SerialPort.GetPortNames();
|
2021-03-05 10:47:12 +00:00
|
|
|
|
deviceSubscription = DeviceNotification.OnNotification.Subscribe(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
deviceSubscription.Dispose();
|
2021-02-24 08:05:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsSettingsProperty(string propertyName)
|
|
|
|
|
{
|
|
|
|
|
return propertyName != nameof(SerialPorts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
2021-03-05 10:47:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnOtherPropertyChanged(string propertyName)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void OnNext(DeviceNotificationEvent value)
|
|
|
|
|
{
|
|
|
|
|
if ((value.EventType == DeviceChangeEvent.DeviceArrival ||
|
|
|
|
|
value.EventType == DeviceChangeEvent.DeviceRemoveComplete) &&
|
|
|
|
|
value.Is(DeviceBroadcastDeviceType.DeviceInterface))
|
|
|
|
|
{
|
|
|
|
|
SerialPorts = SerialPort.GetPortNames();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnError(Exception error)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted()
|
|
|
|
|
{
|
|
|
|
|
}
|
2021-02-24 08:05:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|