CommunicationViewModel.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Shaker.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ShakerApp.ViewModels
  10. {
  11. public sealed class CommunicationViewModel:ViewModelBase<IModel>
  12. {
  13. private readonly string PlugnsPath = AppDomain.CurrentDomain.BaseDirectory + "Communication\\";
  14. private Dictionary<bool, Type> CommunicationViewModelTypes = new Dictionary<bool, Type>();
  15. private bool enbaledServiceControl = false;
  16. private bool localIsConnect = false;
  17. private bool serviceIsStart = false;
  18. private CommunicationViewModel()
  19. {
  20. Tools.PluginsLoader.Defalut.Load<ICommunication.ICommunication>(PlugnsPath)
  21. .Select(x => (x.GetType().GetCustomAttribute<ICommunication.CommunicationTypeAttribute>()!.IsLan, x.GetType()))
  22. .ToList()
  23. .ForEach(x => CommunicationViewModelTypes[x.IsLan] =x.Item2);
  24. }
  25. public bool EnbaledServiceControl { get => enbaledServiceControl; set =>SetProperty(ref enbaledServiceControl , value); }
  26. static CommunicationViewModel()
  27. {
  28. }
  29. public void Connect(string ip,int port)
  30. {
  31. try
  32. {
  33. if (LocalCommunication == null)
  34. {
  35. LocalCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!;
  36. }
  37. if (LocalCommunication!.IsLan == (ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan))
  38. {
  39. if (LocalCommunication.IsConnected) LocalCommunication.Close();
  40. LocalCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!;
  41. }
  42. LocalCommunication.Connect(ip, port);
  43. MainViewModel.Default.SyncConfig();
  44. }
  45. catch
  46. {
  47. }
  48. if (LocalCommunication == null) LocalIsConnect = false;
  49. else LocalIsConnect = LocalCommunication.IsConnected;
  50. }
  51. public void DisConnect()
  52. {
  53. LocalCommunication?.Close();
  54. LocalIsConnect = false;
  55. }
  56. public void StartService(string ip,int port)
  57. {
  58. ServiceIsStart = false;
  59. if (ShakerSettingViewModel.Instance.WorkingMode == Models.WorkingMode.Remote) return;
  60. if (ServiceCommunication == null) return;
  61. try
  62. {
  63. if (ServiceCommunication.IsLan == (ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan))
  64. {
  65. if (ServiceCommunication.IP != ip || ServiceCommunication.Port != port)
  66. {
  67. if (ServiceCommunication.IsConnected) ServiceCommunication.Close();
  68. ServiceCommunication.StartService(ip, port);
  69. }
  70. else
  71. {
  72. if (ServiceCommunication.IsConnected) return;
  73. }
  74. }
  75. else
  76. {
  77. ServiceCommunication?.Close();
  78. ServiceCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!;
  79. ServiceCommunication.StartService(ip, port);
  80. }
  81. }
  82. catch
  83. {
  84. }
  85. if (ServiceCommunication == null) ServiceIsStart = false;
  86. else ServiceIsStart = ServiceCommunication.IsConnected;
  87. }
  88. public void StopService()
  89. {
  90. ServiceIsStart = false;
  91. if (ShakerSettingViewModel.Instance.WorkingMode == Models.WorkingMode.Remote || ServiceCommunication ==null || !ServiceCommunication.IsConnected) return;
  92. ServiceCommunication?.Close();
  93. }
  94. public static CommunicationViewModel Intance { get; } = new CommunicationViewModel();
  95. [AllowNull]
  96. public ICommunication.ICommunication LocalCommunication { get; private set; }
  97. [AllowNull]
  98. public ICommunication.ICommunication ServiceCommunication { get; private set; }
  99. public bool LocalIsConnect { get => localIsConnect; set =>SetProperty(ref localIsConnect , value); }
  100. public bool ServiceIsStart { get => serviceIsStart; set =>SetProperty(ref serviceIsStart , value); }
  101. }
  102. }