StatusBarViewModel.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 _ipAddress;
  13. [ObservableProperty]
  14. private ushort _port;
  15. public StatusBarViewModel(ISharedConfig sharedConfig, IInfoSendingService sendInfoService)
  16. {
  17. _sharedConfig = sharedConfig;
  18. _status = "Disconnected";
  19. _ipAddress = "Unknown";
  20. _port = _sharedConfig.BasicInfo?.RTServerPort ?? 0;
  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. IpAddress=e.RemoteEndPoint.Address.ToString();
  30. }
  31. }
  32. private void OnConnectionStatusChanged(object? sender, bool e)
  33. {
  34. Status = e ? "Connected" : "Disconnected";
  35. }
  36. }
  37. }