MainWindowViewModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 
  2. using ServiceBase;
  3. using SqlSugar;
  4. using System;
  5. using System.IO;
  6. using Universal;
  7. namespace EEMSCenterUI.ViewModels;
  8. internal partial class MainWindowViewModel : ObservableObject, IEEMSBaseServerProvider
  9. {
  10. public MainWindowViewModel()
  11. {
  12. this.Server = new()
  13. {
  14. IP = "127.0.0.1",
  15. Port = 50054
  16. };
  17. }
  18. private IClientProvider? _clientProvider;
  19. [ObservableProperty]
  20. private ServiceEndpoint _Server;
  21. [ObservableProperty]
  22. private string? _GUID;
  23. [RelayCommand]
  24. private void StartService()
  25. {
  26. EEMSBaseServer baseServer = new();
  27. if (!baseServer.Initialize(this, out _clientProvider) || _clientProvider is null)
  28. {
  29. MessageBox.Show("Service Start Failed");
  30. return;
  31. }
  32. Task.Factory.StartNew(async () =>
  33. {
  34. if (!await baseServer.StartServiceAsync(this.Server.IP!, this.Server.Port))
  35. MessageBox.Show("Service Start Failed");
  36. }, TaskCreationOptions.LongRunning);
  37. }
  38. [RelayCommand]
  39. private void RequestFile()
  40. {
  41. Guid.TryParse(this.GUID, out Guid guid);
  42. bool? b = this._clientProvider?.RequestFile(guid, FileType.Config).Result;
  43. }
  44. [RelayCommand]
  45. private async Task SendFile()
  46. {
  47. if (_clientProvider is null || this.GUID is null)
  48. {
  49. MessageBox.Show("Send File Failed");
  50. return;
  51. }
  52. if (!Guid.TryParse(this.GUID, out Guid guid))
  53. {
  54. MessageBox.Show("Send File Failed");
  55. return;
  56. }
  57. using MemoryStream stream = new();
  58. Compressor.CompressZipFileDirectory(new(@"E:\RTConfig"), stream);
  59. if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
  60. {
  61. MessageBox.Show("Send File Failed");
  62. return;
  63. }
  64. for (int i = 0; i < buffer.Count; i++)
  65. {
  66. try
  67. {
  68. if (!await _clientProvider.PushFile(guid, FileType.Config, buffer[i], i + 1, buffer.Count))
  69. {
  70. MessageBox.Show("Send File Failed");
  71. return;
  72. }
  73. }
  74. catch
  75. {
  76. MessageBox.Show("Send File Failed");
  77. return;
  78. }
  79. }
  80. }
  81. void IEEMSBaseServerProvider.Started()
  82. {
  83. Task.Factory.StartNew(() =>
  84. {
  85. MessageBox.Show($"Service Started {this.Server.IP}:{this.Server.Port}");
  86. });
  87. }
  88. void IEEMSBaseServerProvider.OnConnected(string ip, ushort port, ServiceHub serviceHub)
  89. {
  90. ServiceEndpoint serviceEndpoint = new()
  91. {
  92. IP = ip,
  93. Port = port,
  94. Hub = serviceHub
  95. };
  96. App.Current.Dispatcher?.Invoke(() =>
  97. {
  98. Clients[$"{ip}{port}{serviceHub}"] = serviceEndpoint;
  99. });
  100. }
  101. public void OnDisConnected(string ip, ushort port, ServiceHub serviceHub)
  102. {
  103. App.Current.Dispatcher?.Invoke(() =>
  104. {
  105. Clients.TryRemove($"{ip}{port}{serviceHub}", out _);
  106. });
  107. }
  108. [ObservableProperty]
  109. ObservableDictionary<string, ServiceEndpoint> _Clients = [];
  110. }
  111. public partial class ServiceEndpoint : ObservableObject
  112. {
  113. [ObservableProperty]
  114. private string? _IP;
  115. [ObservableProperty]
  116. private ushort _Port;
  117. [ObservableProperty]
  118. private ServiceHub _Hub;
  119. }