123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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<IModel>
- {
- private readonly string PlugnsPath = AppDomain.CurrentDomain.BaseDirectory + "Communication\\";
- private Dictionary<bool, Type> CommunicationViewModelTypes = new Dictionary<bool, Type>();
- private bool enbaledServiceControl = false;
- private bool localIsConnect = false;
- private bool serviceIsStart = false;
- private CommunicationViewModel()
- {
- Tools.PluginsLoader.Defalut.Load<ICommunication.ICommunication>(PlugnsPath)
- .Select(x => (x.GetType().GetCustomAttribute<ICommunication.CommunicationTypeAttribute>()!.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); }
- }
- }
|