MainWindowViewModel.cs 2.1 KB

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