StatusBarViewModel.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using AlarmInfoServerSim.Services;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. namespace AlarmInfoServerSim.ViewModels
  4. {
  5. public partial class StatusBarViewModel : ObservableObject
  6. {
  7. private readonly ISharedConfig _sharedConfig;
  8. private readonly IInfoSendingService _sendInfoService;
  9. [ObservableProperty]
  10. private string _status;
  11. [ObservableProperty]
  12. private string _remoteIp;
  13. [ObservableProperty]
  14. private string _remotePort;
  15. public StatusBarViewModel(ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
  16. {
  17. _sharedConfig = sharedConfig;
  18. _status = "Disconnected";
  19. _remoteIp = "Unknown";
  20. _remotePort = "Unknown";
  21. _sendInfoService = sendInfoService;
  22. _sendInfoService.ConnectionStatusChanged += OnConnectionStatusChanged;
  23. _sendInfoService.TcpConnectionChanged += OnTcpConnectionChanged;
  24. }
  25. private void OnTcpConnectionChanged(object? sender, UniversalNetFrame451.IO.TcpConnection e)
  26. {
  27. if (Status == "Connected")
  28. {
  29. RemoteIp = e.RemoteEndPoint.Address.ToString();
  30. RemotePort = e.RemoteEndPoint.Port.ToString();
  31. }
  32. }
  33. private void OnConnectionStatusChanged(object? sender, bool e)
  34. {
  35. if(e)
  36. {
  37. Status = "Connected";
  38. }
  39. else
  40. {
  41. Status = "Disconnected";
  42. RemoteIp = "Unknown";
  43. RemotePort = "Unknown";
  44. }
  45. }
  46. }
  47. }