StatusBarViewModel.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using AlarmInfoServerSim.Services;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. namespace AlarmInfoServerSim.ViewModels;
  4. public partial class StatusBarViewModel : ObservableObject
  5. {
  6. private readonly ILogService _logService;
  7. private readonly ISharedConfig _sharedConfig;
  8. private readonly IInfoSendingService _infoSendingService;
  9. [ObservableProperty]
  10. private string _localIp;
  11. [ObservableProperty]
  12. private string _localPort;
  13. [ObservableProperty]
  14. private string _remoteIp;
  15. [ObservableProperty]
  16. private string _remotePort;
  17. [ObservableProperty]
  18. private string _status;
  19. public StatusBarViewModel(ILogService logService, ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
  20. {
  21. _logService = logService;
  22. _sharedConfig = sharedConfig;
  23. _infoSendingService = sendInfoService;
  24. _infoSendingService.ConnectionChanged += OnConnectionChanged;
  25. _localIp = _sharedConfig.BasicInfo!.RTServerAddress!;
  26. _localPort = _sharedConfig.BasicInfo!.RTServerPort!.ToString();
  27. _remoteIp = "Unknown";
  28. _remotePort = "Unknown";
  29. _status = "Disconnected";
  30. }
  31. private void OnConnectionChanged(object? sender, (bool, UniversalNetFrame451.IO.TcpConnection) e)
  32. {
  33. App.Current.Dispatcher.Invoke(() =>
  34. {
  35. if (e.Item1)
  36. {
  37. Status = "Connected";
  38. RemoteIp = e.Item2.RemoteEndPoint.Address.ToString();
  39. RemotePort = e.Item2.RemoteEndPoint.Port.ToString();
  40. _logService.Log($"Get a connection from {RemoteIp}:{RemotePort}");
  41. }
  42. else
  43. {
  44. Status = "Disconnected";
  45. RemoteIp = "Unknown";
  46. RemotePort = "Unknown";
  47. _logService.Log($"The connection lost");
  48. }
  49. });
  50. }
  51. }