MainWindowViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using PLCControl;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Shaker.Model;
  10. using System.Configuration;
  11. using System.Windows.Input;
  12. using HandyControl.Interactivity.Commands;
  13. using System.Diagnostics;
  14. using ShakerControl.Tools;
  15. using System.Windows;
  16. using EventBus;
  17. using HandyControl.Interactivity;
  18. using System.Windows.Controls;
  19. using Shaker.ViewModel;
  20. using System.Windows.Markup;
  21. namespace ShakerControl.ViewModel
  22. {
  23. internal class MainWindowViewModel:ViewModelBase<ModelBase>
  24. {
  25. private bool ismousedown = false;
  26. private Point lastpoint;
  27. public ICommand MouseDownCommand => new DelegateCommand<ExCommandParameter>(MouseDown);
  28. private void MouseDown(ExCommandParameter parameter)
  29. {
  30. if(parameter.EventArgs is MouseButtonEventArgs args && args.LeftButton == MouseButtonState.Pressed)
  31. {
  32. if (!ismousedown)
  33. {
  34. ismousedown = true;
  35. MoveLenght = new Thickness();
  36. lastpoint = Mouse.GetPosition(args.Device.Target);
  37. }
  38. }
  39. else
  40. {
  41. ismousedown = false;
  42. MoveLenght = new Thickness();
  43. }
  44. }
  45. public double ControlWidth { get => controlWidth; set =>UpdateProperty(ref controlWidth,value); }
  46. public Thickness MoveLenght { get => moveLenght; set =>UpdateProperty(ref moveLenght,value); }
  47. public ICommand MouseMoveCommand=> new DelegateCommand<ExCommandParameter>(MouseMove);
  48. private void MouseMove(ExCommandParameter parameter)
  49. {
  50. if (!ismousedown) return;
  51. if(parameter.EventArgs is MouseEventArgs args && args.LeftButton == MouseButtonState.Pressed)
  52. {
  53. var point = Mouse.GetPosition(args.Device.Target);
  54. MoveLenght = new Thickness(point.X-lastpoint.X,0,lastpoint.X-point.X,0);
  55. }
  56. }
  57. public ICommand MouseUpCommand => new DelegateCommand<ExCommandParameter>(MouseUp);
  58. private void MouseUp(ExCommandParameter parameter)
  59. {
  60. if(parameter.Sender is ItemsControl items)
  61. {
  62. int count = items.Items.Count;
  63. if (MoveLenght.Left <= -30)
  64. {
  65. if (SelectedIndex == count - 1)
  66. {
  67. SelectedIndex = 0;
  68. }
  69. else
  70. {
  71. SelectedIndex++;
  72. }
  73. }
  74. else if(MoveLenght.Left>=30)
  75. {
  76. if(SelectedIndex ==0)
  77. {
  78. SelectedIndex = count - 1;
  79. }
  80. else
  81. {
  82. SelectedIndex--;
  83. }
  84. }
  85. }
  86. ismousedown = false;
  87. MoveLenght = new Thickness();
  88. }
  89. public ICommand MouseLeaveCommand=> new DelegateCommand<ExCommandParameter>(MouseLeave);
  90. private void MouseLeave(ExCommandParameter parameter)
  91. {
  92. if (parameter.Sender is ItemsControl items)
  93. {
  94. int count = items.Items.Count;
  95. if (MoveLenght.Left <= -30)
  96. {
  97. if (SelectedIndex == count - 1)
  98. {
  99. SelectedIndex = 0;
  100. }
  101. else
  102. {
  103. SelectedIndex++;
  104. }
  105. }
  106. else if (MoveLenght.Left >= 30)
  107. {
  108. if (SelectedIndex == 0)
  109. {
  110. SelectedIndex = count - 1;
  111. }
  112. else
  113. {
  114. SelectedIndex--;
  115. }
  116. }
  117. }
  118. ismousedown = false;
  119. MoveLenght = new Thickness();
  120. }
  121. private string clientID=string.Empty;
  122. [AllowNull]
  123. private EasyMQ.IBus bus;
  124. private bool isLoggedIn = false;
  125. private CancellationTokenSource tokenSource = new CancellationTokenSource();
  126. private double controlWidth;
  127. private Thickness moveLenght;
  128. private int selectedIndex=0;
  129. private string serverIP = "127.0.0.1";
  130. private int serverPort = 61616;
  131. private bool mangerIsOnLine = false;
  132. public const string GET_DATA_EVENT = "GetDataEvent";
  133. public bool IsOffine { get;private set; } = false;
  134. private MainWindowViewModel()
  135. {
  136. if(ConfigurationManager.AppSettings.AllKeys.Contains(nameof(IsOffine)))
  137. {
  138. IsOffine = bool.Parse(ConfigurationManager.AppSettings[nameof(IsOffine)]!.ToString());
  139. }
  140. if(ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerIP)))
  141. {
  142. ServerIP = ConfigurationManager.AppSettings[nameof(ServerIP)]!.ToString();
  143. }
  144. else
  145. {
  146. ConfigurationManager.AppSettings.Set(nameof(ServerIP), serverIP);
  147. }
  148. if(ConfigurationManager.AppSettings.AllKeys.Contains(nameof(ServerPort)))
  149. {
  150. try
  151. {
  152. ServerPort = int.Parse(ConfigurationManager.AppSettings[nameof(ServerPort)]!.ToString());
  153. }
  154. catch
  155. {
  156. ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
  157. }
  158. }
  159. else
  160. {
  161. ConfigurationManager.AppSettings.Set(nameof(ServerPort), serverPort.ToString());
  162. }
  163. if(ConfigurationManager.AppSettings.AllKeys.Contains("ClientID"))
  164. {
  165. clientID = ConfigurationManager.AppSettings["ClientID"]!.ToString();
  166. if(string.IsNullOrEmpty(clientID))
  167. {
  168. Tools.MessageBoxHelper.Error($"未配置ClientID");
  169. App.Current.Shutdown();
  170. return;
  171. }
  172. }
  173. else
  174. {
  175. ConfigurationManager.AppSettings.Set("ClientID", "");
  176. Tools.MessageBoxHelper.Error($"未配置ClientID");
  177. App.Current.Shutdown();
  178. return;
  179. }
  180. }
  181. static MainWindowViewModel()
  182. {
  183. }
  184. private ManualResetEvent resetEvent = new ManualResetEvent(true);
  185. public void Continue()
  186. {
  187. if (MainTaskIsStart) return;
  188. resetEvent.Set();
  189. MainTaskIsStart = true;
  190. }
  191. public void Paused()
  192. {
  193. if (!MainTaskIsStart) return;
  194. resetEvent.Reset();
  195. MainTaskIsStart = false;
  196. }
  197. public bool MainTaskIsStart { get; private set; } = true;
  198. public bool MangerIsOnLine { get => mangerIsOnLine; set =>UpdateProperty(ref mangerIsOnLine,value); }
  199. [AllowNull]
  200. public Action InitedActive { get; set; }
  201. public void StartMainThread()
  202. {
  203. InitedActive?.Invoke();
  204. tokenSource?.Cancel();
  205. tokenSource = new CancellationTokenSource();
  206. var dataevent = GetEvent(GET_DATA_EVENT);
  207. Task.Run(() =>
  208. {
  209. while(!tokenSource.IsCancellationRequested)
  210. {
  211. dataevent.Publish(this);
  212. resetEvent.WaitOne();
  213. Thread.Sleep(SystemConfig.LoopTime);
  214. }
  215. });
  216. Paused();
  217. }
  218. public string ServerIP { get => serverIP; set =>UpdateProperty(ref serverIP, value); }
  219. public int ServerPort { get => serverPort; set =>UpdateProperty(ref serverPort, value); }
  220. public void Stop()=>tokenSource?.Cancel();
  221. public ViewModel.Setting.SettingViewModel Setting { get; } = new Setting.SettingViewModel();
  222. public SystemConfigViewModel SystemConfig { get; } = new SystemConfigViewModel();
  223. public ICommand ExitCommand => new DelegateCommand(() =>
  224. {
  225. PromptViewModel.Default.Init();
  226. PromptViewModel.Default.Message = "是否退出程序?";
  227. PromptViewModel.Default.YesAction = () => App.Current.Shutdown(); ;
  228. PromptViewModel.Default.IconType = IconType.Ask;
  229. PromptViewModel.Default.IsOpen = true;
  230. });
  231. public ICommand ShutDownCommand => new DelegateCommand(() =>App.Current.Shutdown());
  232. public UserViewModel User { get; } = new UserViewModel();
  233. public bool IsLoggedIn { get => isLoggedIn; set =>UpdateProperty(ref isLoggedIn,value); }
  234. public MainPage.MainPageViewModel MainPage { get; } = new MainPage.MainPageViewModel();
  235. public string ClientID { get => clientID;private set =>UpdateProperty(ref clientID,value); }
  236. public static MainWindowViewModel Default { get; } = new MainWindowViewModel();
  237. public EasyMQ.IBus Bus => bus;
  238. public int SelectedIndex { get => selectedIndex; set =>UpdateProperty(ref selectedIndex, value); }
  239. public ICommand ClosingCommand => new DelegateCommand(Closing);
  240. private void Closing()
  241. {
  242. SystemLog.Default.WriteLog("客户端退出", LogType.Message);
  243. if(MainPage?.Shakers!=null && MainPage.Shakers.Count>0)
  244. {
  245. foreach (var item in MainPage.Shakers)
  246. {
  247. item.Exit();
  248. }
  249. }
  250. tokenSource?.Cancel();
  251. bus?.Dispose();
  252. }
  253. public void Init(bool isOffine)
  254. {
  255. IsOffine = isOffine;
  256. Init();
  257. }
  258. public override void Init()
  259. {
  260. try
  261. {
  262. if (!IsOffine)
  263. {
  264. bus = EasyMQ.ActiveHutch.Default.CreateBus($"tcp://{ServerIP}:{ServerPort}?alwaysSessionAsync=true", new EasyMQ.MessagePackSerializer());
  265. bus.PubSub.Subscribe<Shaker.Model.MangerExitModel>((data, _) =>
  266. {
  267. DispatherInovke.Inovke(() =>
  268. {
  269. MangerIsOnLine = data.Online;
  270. if (!data.Online)
  271. {
  272. PromptViewModel.Default.Init();
  273. PromptViewModel.Default.Message = "管理器已离线";
  274. PromptViewModel.Default.IconType = IconType.Error;
  275. PromptViewModel.Default.NoVisibility = Visibility.Collapsed;
  276. PromptViewModel.Default.IsOpen = true;
  277. }
  278. });
  279. });
  280. bus.PubSub.Subscribe<SystemConfigModel>((data, _) =>
  281. {
  282. SystemConfig.LoopTime = data.LoopTime;
  283. SystemConfig.RequestPermissionsTimeout = data.RequestPermissionsTimeout;
  284. SystemConfig.CommunicationTimeout = data.CommunicationTimeout;
  285. });
  286. }
  287. }
  288. catch
  289. {
  290. MessageBoxHelper.Error("服务端未运行,程序退出");
  291. Tools.DispatherInovke.Inovke(() => App.Current.Shutdown());
  292. return;
  293. }
  294. MainPage.Init();
  295. }
  296. }
  297. }