StatusBarViewModel.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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
  7. {
  8. private readonly ILogService _logService;
  9. private readonly ISharedConfig _sharedConfig;
  10. private readonly IInfoSendingService _infoSendingService;
  11. [ObservableProperty]
  12. private List<IPAddress> _iPAddresses;
  13. [ObservableProperty]
  14. private bool _isSelectingEnable;
  15. [ObservableProperty]
  16. private IPAddress _selectedIP;
  17. [ObservableProperty]
  18. private int _selectedPort;
  19. [ObservableProperty]
  20. private string _buttonContent;
  21. [ObservableProperty]
  22. private IPAddress _remoteIp;
  23. [ObservableProperty]
  24. private int _remotePort;
  25. [ObservableProperty]
  26. private string _status;
  27. public StatusBarViewModel(ILogService logService, ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
  28. {
  29. _logService = logService;
  30. _sharedConfig = sharedConfig;
  31. _infoSendingService = sendInfoService;
  32. _infoSendingService.TcpServerStatusChanged += OnTcpServerStatusChanged;
  33. _infoSendingService.ConnectionChanged += OnConnectionChanged;
  34. _iPAddresses = _sharedConfig.IPAddresses;
  35. _isSelectingEnable=true;
  36. _selectedIP = IPAddress.None;
  37. _selectedPort = 50052;
  38. _buttonContent = "Open";
  39. _remoteIp =IPAddress.None;
  40. _remotePort = -1;
  41. _status = "Disconnected";
  42. }
  43. [RelayCommand]
  44. private void OpenOrClose()
  45. {
  46. if (ButtonContent == "Open")
  47. {
  48. if (SelectedIP == IPAddress.None || SelectedPort < 0)
  49. {
  50. _logService.Log($"IP or Port is not correct");
  51. return;
  52. }
  53. _sharedConfig.SelectedIPAddress = SelectedIP;
  54. _sharedConfig.SelectedPort = SelectedPort;
  55. _infoSendingService.Open();
  56. return;
  57. }
  58. if (ButtonContent == "Close")
  59. {
  60. _infoSendingService.Close();
  61. return;
  62. }
  63. }
  64. private void OnTcpServerStatusChanged(object? sender, bool e)
  65. {
  66. App.Current.Dispatcher.Invoke(() =>
  67. {
  68. ButtonContent = e ? "Close" : "Open";
  69. IsSelectingEnable = !e;
  70. });
  71. }
  72. private void OnConnectionChanged(object? sender, (bool, UniversalNetFrame451.IO.TcpConnection) e)
  73. {
  74. App.Current.Dispatcher.Invoke(() =>
  75. {
  76. if (e.Item1)
  77. {
  78. Status = "Connected";
  79. RemoteIp = e.Item2.RemoteEndPoint.Address;
  80. RemotePort = e.Item2.RemoteEndPoint.Port;
  81. _logService.Log($"Get a connection from {RemoteIp}:{RemotePort}");
  82. }
  83. else
  84. {
  85. Status = "Disconnected";
  86. RemoteIp = IPAddress.None;
  87. RemotePort = -1;
  88. _logService.Log($"The connection lost");
  89. }
  90. });
  91. }
  92. }