CommunicationViewModel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Avalonia.Controls;
  2. using Shaker.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace ShakerApp.ViewModels
  13. {
  14. public sealed class CommunicationViewModel:ViewModelBase<IModel>
  15. {
  16. private readonly string PlugnsPath =Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "Communication");
  17. private Dictionary<bool, Type> CommunicationViewModelTypes = new Dictionary<bool, Type>();
  18. private bool enbaledServiceControl = false;
  19. private bool localIsConnect = false;
  20. private bool serviceIsStart = false;
  21. private CommunicationViewModel()
  22. {
  23. Tools.PluginsLoader.Defalut.Load<ICommunication.ICommunication>(PlugnsPath)
  24. .Select(x => (x.GetType().GetCustomAttribute<ICommunication.CommunicationTypeAttribute>()!.IsLan, x.GetType()))
  25. .ToList()
  26. .ForEach(x => CommunicationViewModelTypes[x.IsLan] =x.Item2);
  27. }
  28. public bool EnbaledServiceControl { get => enbaledServiceControl; set =>SetProperty(ref enbaledServiceControl , value); }
  29. static CommunicationViewModel()
  30. {
  31. }
  32. public void Connect(string ip,int port)
  33. {
  34. LogViewModel.Instance.AddLog($"连接{ip}:{port}");
  35. try
  36. {
  37. if (LocalCommunication == null)
  38. {
  39. LocalCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!;
  40. LocalCommunication.Disconnected += (_, _) =>
  41. {
  42. LogViewModel.Instance.AddLog($"{LocalCommunication.IP}:{LocalCommunication.Port}连接断开", LogType.Error);
  43. LocalIsConnect = false;
  44. MainViewModel.Default.ShowToast(App.Current?.FindResource("DisConnect") + "", Avalonia.Controls.Notifications.NotificationType.Error);
  45. GetEvent(Topic.DisConnect).Publish(this, null);
  46. };
  47. }
  48. if (LocalCommunication!.IsLan == (ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan))
  49. {
  50. if (LocalCommunication.IsConnected) LocalCommunication.Close();
  51. }
  52. else
  53. {
  54. LocalCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!;
  55. LocalCommunication.Disconnected += (_, _) =>
  56. {
  57. LogViewModel.Instance.AddLog($"{LocalCommunication.IP}:{LocalCommunication.Port}连接断开", LogType.Error);
  58. LocalIsConnect = false;
  59. MainViewModel.Default.ShowToast( App.Current?.FindResource("DisConnect") + "", Avalonia.Controls.Notifications.NotificationType.Error);
  60. GetEvent(Topic.DisConnect).Publish(this, null);
  61. };
  62. }
  63. LocalCommunication.Connect(ip, port);
  64. Thread.Sleep(50);
  65. if (LocalCommunication.IsConnected)
  66. {
  67. LogViewModel.Instance.AddLog($"获取控制器响应" );
  68. bool success = false;
  69. for (int i = 0; i < 3; i++)
  70. {
  71. var result = LocalCommunication.GetEvent<string>(Topic.SERVICERESULT).Publish(this, null);
  72. if (!string.IsNullOrEmpty(result))
  73. {
  74. success = true;
  75. break ;
  76. }
  77. }
  78. if(!success)
  79. {
  80. LogViewModel.Instance.AddLog($"获取控制器响应失败", LogType.Error);
  81. LocalCommunication.Close();
  82. LocalIsConnect = false;
  83. }
  84. else
  85. {
  86. MainViewModel.Default.SyncConfig();
  87. }
  88. }
  89. else
  90. {
  91. LogViewModel.Instance.AddLog($"{ip}:{port}连接失败", LogType.Error);
  92. }
  93. }
  94. catch(Exception ex)
  95. {
  96. LogViewModel.Instance.AddLog($"{ip}:{port}连接失败,"+ex.Message, LogType.Error);
  97. }
  98. finally
  99. {
  100. if (LocalCommunication == null) LocalIsConnect = false;
  101. else LocalIsConnect = LocalCommunication.IsConnected;
  102. }
  103. }
  104. public void DisConnect()
  105. {
  106. LocalCommunication?.Close();
  107. LocalIsConnect = false;
  108. }
  109. public void StartService(string ip,int port)
  110. {
  111. ServiceIsStart = false;
  112. if (ShakerSettingViewModel.Instance.WorkingMode == Models.WorkingMode.Remote) return;
  113. if (ServiceCommunication == null)
  114. {
  115. ServiceCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!;
  116. ServiceCommunication.Disconnected += (_, _) =>
  117. {
  118. LogViewModel.Instance.AddLog($"{ServiceCommunication.IP}:{ServiceCommunication.Port}连接断开", LogType.Error);
  119. ServiceIsStart = false;
  120. MainViewModel.Default.ShowToast(App.Current?.FindResource("DisConnect") + "", Avalonia.Controls.Notifications.NotificationType.Error);
  121. GetEvent(Topic.DisConnect).Publish(this, null);
  122. };
  123. }
  124. try
  125. {
  126. if (ServiceCommunication.IsLan == (ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan))
  127. {
  128. if (ServiceCommunication.IP != ip || ServiceCommunication.Port != port)
  129. {
  130. if (ServiceCommunication.IsConnected) ServiceCommunication.Close();
  131. ServiceCommunication.StartService(ip, port);
  132. }
  133. else
  134. {
  135. if (!ServiceCommunication.IsConnected)
  136. {
  137. ServiceCommunication.StartService(ip, port);
  138. }
  139. }
  140. }
  141. else
  142. {
  143. ServiceCommunication?.Close();
  144. ServiceCommunication = (ICommunication.ICommunication)Activator.CreateInstance(CommunicationViewModelTypes[ShakerSettingViewModel.Instance.RemoteControlModel == Models.RemoteControlModel.Lan])!;
  145. ServiceCommunication.StartService(ip, port);
  146. }
  147. }
  148. catch
  149. {
  150. }
  151. if (ServiceCommunication == null) ServiceIsStart = false;
  152. else ServiceIsStart = true;
  153. if(ServiceIsStart)
  154. {
  155. MainViewModel.Default.InitServiceMsg();
  156. }
  157. }
  158. public void StopService()
  159. {
  160. ServiceIsStart = false;
  161. if (ShakerSettingViewModel.Instance.WorkingMode == Models.WorkingMode.Remote || ServiceCommunication ==null || !ServiceCommunication.IsConnected) return;
  162. ServiceCommunication?.Close();
  163. }
  164. public static CommunicationViewModel Instance { get; } = new CommunicationViewModel();
  165. [AllowNull]
  166. public ICommunication.ICommunication LocalCommunication { get; private set; }
  167. [AllowNull]
  168. public ICommunication.ICommunication ServiceCommunication { get; private set; }
  169. public bool LocalIsConnect { get => localIsConnect; set =>SetProperty(ref localIsConnect , value); }
  170. public bool ServiceIsStart
  171. {
  172. get => serviceIsStart;
  173. set
  174. {
  175. SetProperty(ref serviceIsStart, value);
  176. ShakerSettingViewModel.Instance.ServieIsStart = value;
  177. }
  178. }
  179. }
  180. }