123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
-
- using ServiceBase;
- using SqlSugar;
- using System;
- using System.IO;
- using Universal;
- namespace EEMSCenterUI.ViewModels;
- internal partial class MainWindowViewModel : ObservableObject, IEEMSBaseServerProvider
- {
- public MainWindowViewModel()
- {
- this.Server = new()
- {
- IP = "127.0.0.1",
- Port = 50054
- };
- }
- private IClientProvider? _clientProvider;
- [ObservableProperty]
- private ServiceEndpoint _Server;
- [ObservableProperty]
- private string? _GUID;
- [RelayCommand]
- private void StartService()
- {
- EEMSBaseServer baseServer = new();
- if (!baseServer.Initialize(this, out _clientProvider) || _clientProvider is null)
- {
- MessageBox.Show("Service Start Failed");
- return;
- }
- Task.Factory.StartNew(async () =>
- {
- if (!await baseServer.StartServiceAsync(this.Server.IP!, this.Server.Port))
- MessageBox.Show("Service Start Failed");
- }, TaskCreationOptions.LongRunning);
- }
- [RelayCommand]
- private void RequestFile()
- {
- Guid.TryParse(this.GUID, out Guid guid);
- bool? b = this._clientProvider?.RequestFile(guid, FileType.Config).Result;
- }
- [RelayCommand]
- private async Task SendFile()
- {
- if (_clientProvider is null || this.GUID is null)
- {
- MessageBox.Show("Send File Failed");
- return;
- }
- if (!Guid.TryParse(this.GUID, out Guid guid))
- {
- MessageBox.Show("Send File Failed");
- return;
- }
- using MemoryStream stream = new();
- Compressor.CompressZipFileDirectory(new(@"E:\RTConfig"), stream);
- if (!stream.Split(out List<byte[]>? buffer, 8192) || buffer is null)
- {
- MessageBox.Show("Send File Failed");
- return;
- }
- for (int i = 0; i < buffer.Count; i++)
- {
- try
- {
- if (!await _clientProvider.PushFile(guid, FileType.Config, buffer[i], i + 1, buffer.Count))
- {
- MessageBox.Show("Send File Failed");
- return;
- }
- }
- catch
- {
- MessageBox.Show("Send File Failed");
- return;
- }
- }
- }
- void IEEMSBaseServerProvider.Started()
- {
- Task.Factory.StartNew(() =>
- {
- MessageBox.Show($"Service Started {this.Server.IP}:{this.Server.Port}");
- });
- }
- void IEEMSBaseServerProvider.OnConnected(string ip, ushort port, ServiceHub serviceHub)
- {
- ServiceEndpoint serviceEndpoint = new()
- {
- IP = ip,
- Port = port,
- Hub = serviceHub
- };
- App.Current.Dispatcher?.Invoke(() =>
- {
- Clients[$"{ip}{port}{serviceHub}"] = serviceEndpoint;
- });
- }
- public void OnDisConnected(string ip, ushort port, ServiceHub serviceHub)
- {
- App.Current.Dispatcher?.Invoke(() =>
- {
- Clients.TryRemove($"{ip}{port}{serviceHub}", out _);
- });
- }
- [ObservableProperty]
- ObservableDictionary<string, ServiceEndpoint> _Clients = [];
- }
- public partial class ServiceEndpoint : ObservableObject
- {
- [ObservableProperty]
- private string? _IP;
- [ObservableProperty]
- private ushort _Port;
- [ObservableProperty]
- private ServiceHub _Hub;
- }
|