StatusBarViewModel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using PLCIOPointTool.Services;
  4. using TwinCAT;
  5. namespace PLCIOPointTool.ViewModels;
  6. public partial class StatusBarViewModel : ObservableObject, IDisposable
  7. {
  8. private readonly IClientService _clientService;
  9. private bool disposedValue;
  10. #region ObservableProperty
  11. [ObservableProperty]
  12. private string _netID = "192.168.250.10.1.1";
  13. [ObservableProperty]
  14. private int _port = 851;
  15. [ObservableProperty]
  16. [NotifyPropertyChangedFor(nameof(CanConnect), nameof(CanDisconnect))]
  17. private bool _isConnected = false;
  18. [ObservableProperty]
  19. private string _status = ConnectionState.Disconnected.ToString();
  20. [ObservableProperty]
  21. [NotifyCanExecuteChangedFor(nameof(ConnectCommand), nameof(DisconnectCommand))]
  22. private bool _isNotOperating = true;
  23. #endregion
  24. public StatusBarViewModel(IClientService clientService)
  25. {
  26. _clientService = clientService;
  27. _clientService.ConnectionStateChanged += OnConnectionStateChanged;
  28. }
  29. public bool CanConnect => !IsConnected;
  30. public bool CanDisconnect => IsConnected;
  31. [RelayCommand]
  32. private void Connect()
  33. {
  34. IsNotOperating = false;
  35. Task.Run(async () =>
  36. {
  37. await _clientService.ConnectAsync(NetID, Port);
  38. App.Current.Dispatcher.Invoke(() =>
  39. {
  40. IsNotOperating = true;
  41. });
  42. });
  43. }
  44. [RelayCommand]
  45. private void Disconnect()
  46. {
  47. IsNotOperating = false;
  48. Task.Run(async () =>
  49. {
  50. await _clientService.DisconnectAsync();
  51. App.Current.Dispatcher.Invoke(() =>
  52. {
  53. IsNotOperating = true;
  54. });
  55. });
  56. }
  57. private void OnConnectionStateChanged(object? sender, ConnectionState e)
  58. {
  59. IsConnected = e switch
  60. {
  61. ConnectionState.Disconnected => false,
  62. ConnectionState.Connected => true,
  63. _ => throw new NotImplementedException()
  64. };
  65. Status = e.ToString();
  66. }
  67. #region Dispose
  68. protected virtual void Dispose(bool disposing)
  69. {
  70. if (!disposedValue)
  71. {
  72. if (disposing)
  73. {
  74. // TODO: dispose managed state (managed objects)
  75. _clientService.ConnectionStateChanged -= OnConnectionStateChanged;
  76. }
  77. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  78. // TODO: set large fields to null
  79. disposedValue = true;
  80. }
  81. }
  82. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  83. // ~StatusBarViewModel()
  84. // {
  85. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  86. // Dispose(disposing: false);
  87. // }
  88. public void Dispose()
  89. {
  90. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  91. Dispose(disposing: true);
  92. GC.SuppressFinalize(this);
  93. }
  94. #endregion
  95. }