using Shaker.Model; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ShakerApp.ViewModels { public sealed class CommunicationViewModel:ViewModelBase { private readonly string PlugnsPath = AppDomain.CurrentDomain.BaseDirectory + "Communication\\"; private Dictionary CommunicationViewModelTypes = new Dictionary(); private bool enbaledServiceControl = false; private bool localIsConnect = false; private bool serviceIsStart = false; private CommunicationViewModel() { Tools.PluginsLoader.Defalut.Load(PlugnsPath) .Select(x => (x.GetType().GetCustomAttribute()!.IsLan, x.GetType())) .ToList() .ForEach(x => CommunicationViewModelTypes[x.IsLan] =x.Item2); } public bool EnbaledServiceControl { get => enbaledServiceControl; set =>SetProperty(ref enbaledServiceControl , value); } static CommunicationViewModel() { } public void Connect(string ip,int port) { try { if (LocalCommunication == null) { LocalCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!; } if (LocalCommunication!.IsLan == (ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan)) { if (LocalCommunication.IsConnected) LocalCommunication.Close(); LocalCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!; } LocalCommunication.Connect(ip, port); MainViewModel.Default.SyncConfig(); } catch { } if (LocalCommunication == null) LocalIsConnect = false; else LocalIsConnect = LocalCommunication.IsConnected; } public void DisConnect() { LocalCommunication?.Close(); LocalIsConnect = false; } public void StartService(string ip,int port) { ServiceIsStart = false; if (ShakerSettingViewModel.Instance.WorkingMode == Models.WorkingMode.Remote) return; if (ServiceCommunication == null) return; try { if (ServiceCommunication.IsLan == (ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan)) { if (ServiceCommunication.IP != ip || ServiceCommunication.Port != port) { if (ServiceCommunication.IsConnected) ServiceCommunication.Close(); ServiceCommunication.StartService(ip, port); } else { if (ServiceCommunication.IsConnected) return; } } else { ServiceCommunication?.Close(); ServiceCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!; ServiceCommunication.StartService(ip, port); } } catch { } if (ServiceCommunication == null) ServiceIsStart = false; else ServiceIsStart = ServiceCommunication.IsConnected; } public void StopService() { ServiceIsStart = false; if (ShakerSettingViewModel.Instance.WorkingMode == Models.WorkingMode.Remote || ServiceCommunication ==null || !ServiceCommunication.IsConnected) return; ServiceCommunication?.Close(); } public static CommunicationViewModel Intance { get; } = new CommunicationViewModel(); [AllowNull] public ICommunication.ICommunication LocalCommunication { get; private set; } [AllowNull] public ICommunication.ICommunication ServiceCommunication { get; private set; } public bool LocalIsConnect { get => localIsConnect; set =>SetProperty(ref localIsConnect , value); } public bool ServiceIsStart { get => serviceIsStart; set =>SetProperty(ref serviceIsStart , value); } } }