using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using EEMSService; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using UICommon.DataType; namespace EEMSCenterUI.ViewModels; internal partial class MainWindowViewModel : ObservableObject, IEEMSBaseServerProvider { public MainWindowViewModel() { this.Server = new() { IP = "127.0.0.1", Port = 50054 }; } [ObservableProperty] private ServiceEndpoint _Server; [RelayCommand] private void StartService() { EEMSBaseServer baseServer = new(); baseServer.Initialize(this); Task.Factory.StartNew(async () => { if (!await baseServer.StartServiceAsync(this.Server.IP!, this.Server.Port)) MessageBox.Show("Service Start Failed"); }, TaskCreationOptions.LongRunning); } 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 _Clients = []; } public partial class ServiceEndpoint : ObservableObject { [ObservableProperty] private string? _IP; [ObservableProperty] private ushort _Port; [ObservableProperty] private ServiceHub _Hub; }