MainWindowViewModel.cs 2.2 KB

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