MainWindowViewModel.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 
  2. namespace EEMSCenterUI.ViewModels;
  3. internal partial class MainWindowViewModel : ObservableObject, IEEMSBaseServerProvider
  4. {
  5. public MainWindowViewModel()
  6. {
  7. this.Server = new()
  8. {
  9. IP = "127.0.0.1",
  10. Port = 50054
  11. };
  12. }
  13. [ObservableProperty]
  14. private ServiceEndpoint _Server;
  15. [RelayCommand]
  16. private void StartService()
  17. {
  18. EEMSBaseServer baseServer = new();
  19. baseServer.Initialize(this);
  20. Task.Factory.StartNew(async () =>
  21. {
  22. if (!await baseServer.StartServiceAsync(this.Server.IP!, this.Server.Port))
  23. MessageBox.Show("Service Start Failed");
  24. }, TaskCreationOptions.LongRunning);
  25. }
  26. void IEEMSBaseServerProvider.Started()
  27. {
  28. Task.Factory.StartNew(() =>
  29. {
  30. MessageBox.Show($"Service Started {this.Server.IP}:{this.Server.Port}");
  31. });
  32. }
  33. void IEEMSBaseServerProvider.OnConnected(string ip, ushort port, ServiceHub serviceHub)
  34. {
  35. ServiceEndpoint serviceEndpoint = new()
  36. {
  37. IP = ip,
  38. Port = port,
  39. Hub = serviceHub
  40. };
  41. App.Current.Dispatcher?.Invoke(() =>
  42. {
  43. Clients[$"{ip}{port}{serviceHub}"] = serviceEndpoint;
  44. });
  45. }
  46. public void OnDisConnected(string ip, ushort port, ServiceHub serviceHub)
  47. {
  48. App.Current.Dispatcher?.Invoke(() =>
  49. {
  50. Clients.TryRemove($"{ip}{port}{serviceHub}", out _);
  51. });
  52. }
  53. [ObservableProperty]
  54. ObservableDictionary<string, ServiceEndpoint> _Clients = [];
  55. }
  56. public partial class ServiceEndpoint : ObservableObject
  57. {
  58. [ObservableProperty]
  59. private string? _IP;
  60. [ObservableProperty]
  61. private ushort _Port;
  62. [ObservableProperty]
  63. private ServiceHub _Hub;
  64. }