StatusBarViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using AlarmInfoServerSim.Services;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using CommunityToolkit.Mvvm.Input;
  4. using System.Net;
  5. namespace AlarmInfoServerSim.ViewModels;
  6. public partial class StatusBarViewModel : ObservableObject ,IDisposable
  7. {
  8. private readonly ILogService _logService;
  9. private readonly ISharedConfig _sharedConfig;
  10. private readonly IInfoSendingService _infoSendingService;
  11. private bool disposedValue;
  12. [ObservableProperty]
  13. private List<IPAddress> _iPAddresses;
  14. [ObservableProperty]
  15. private bool _isSelectingEnable;
  16. [ObservableProperty]
  17. private IPAddress _selectedIP;
  18. [ObservableProperty]
  19. private int _selectedPort;
  20. [ObservableProperty]
  21. private string _buttonContent;
  22. [ObservableProperty]
  23. private string _remoteIp;
  24. [ObservableProperty]
  25. private string _remotePort;
  26. [ObservableProperty]
  27. private string _status;
  28. public StatusBarViewModel(ILogService logService, ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
  29. {
  30. _logService = logService;
  31. _sharedConfig = sharedConfig;
  32. _infoSendingService = sendInfoService;
  33. _infoSendingService.TcpServerStatusChanged += OnTcpServerStatusChanged;
  34. _infoSendingService.ConnectionChanged += OnConnectionChanged;
  35. _iPAddresses = _sharedConfig.IPAddresses;
  36. _isSelectingEnable = true;
  37. _selectedIP = IPAddress.None;
  38. _selectedPort = 50052;
  39. _buttonContent = "Open";
  40. _remoteIp = "Unknown";
  41. _remotePort = "Unknown";
  42. _status = "Disconnected";
  43. }
  44. [RelayCommand]
  45. private void OpenOrClose()
  46. {
  47. if (ButtonContent == "Open")
  48. {
  49. if (SelectedIP == IPAddress.None || SelectedPort < 0)
  50. {
  51. _logService.Log($"IP or Port is not correct");
  52. return;
  53. }
  54. _sharedConfig.SelectedIPAddress = SelectedIP;
  55. _sharedConfig.SelectedPort = SelectedPort;
  56. _infoSendingService.Open();
  57. return;
  58. }
  59. if (ButtonContent == "Close")
  60. {
  61. _infoSendingService.Close();
  62. return;
  63. }
  64. }
  65. private void OnTcpServerStatusChanged(object? sender, bool e)
  66. {
  67. App.Current.Dispatcher.Invoke(() =>
  68. {
  69. ButtonContent = e ? "Close" : "Open";
  70. IsSelectingEnable = !e;
  71. });
  72. }
  73. private void OnConnectionChanged(object? sender, (bool, UniversalNetFrame451.IO.TcpConnection) e)
  74. {
  75. App.Current.Dispatcher.Invoke(() =>
  76. {
  77. if (e.Item1)
  78. {
  79. Status = "Connected";
  80. RemoteIp = e.Item2.RemoteEndPoint.Address.ToString();
  81. RemotePort = e.Item2.RemoteEndPoint.Port.ToString();
  82. _logService.Log($"Get a connection from {RemoteIp}:{RemotePort}");
  83. }
  84. else
  85. {
  86. Status = "Disconnected";
  87. RemoteIp = "Unknown";
  88. RemotePort = "Unknown";
  89. _logService.Log($"The connection lost");
  90. }
  91. });
  92. }
  93. protected virtual void Dispose(bool disposing)
  94. {
  95. if (!disposedValue)
  96. {
  97. if (disposing)
  98. {
  99. // TODO: dispose managed state (managed objects)
  100. _infoSendingService.TcpServerStatusChanged-=OnTcpServerStatusChanged;
  101. _infoSendingService.ConnectionChanged -= OnConnectionChanged;
  102. }
  103. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  104. // TODO: set large fields to null
  105. disposedValue = true;
  106. }
  107. }
  108. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  109. // ~StatusBarViewModel()
  110. // {
  111. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  112. // Dispose(disposing: false);
  113. // }
  114. public void Dispose()
  115. {
  116. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  117. Dispose(disposing: true);
  118. GC.SuppressFinalize(this);
  119. }
  120. }