InchingControlViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using MaterialDesignThemes.Wpf;
  2. using NPOI.SS.Formula.Functions;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Ioc;
  6. using Prism.Mvvm;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Windows;
  13. using TeamAAS_VP.Controls;
  14. using TeamAAS_VP.Core;
  15. using TeamAAS_VP.Events;
  16. using TeamAAS_VP.Models;
  17. using TeamAAS_VP.Views.DebugMod;
  18. using TeamAAS_VP;
  19. using static MaterialDesignThemes.Wpf.Theme.ToolBar;
  20. using TeamAAS_VP.Resources.Languages;
  21. namespace TeamAAS_VP.ViewModels.DebugMod
  22. {
  23. public class InchingControlViewModel : BindableBase
  24. {
  25. IContainerProvider _container;
  26. IEventAggregator _eventAggregator;
  27. Timer timer;
  28. private Management management;
  29. #region 属性
  30. private ObservableCollection<UserInching> _UserInchingCollection;
  31. public ObservableCollection<UserInching> UserInchingCollection
  32. {
  33. get { return _UserInchingCollection; }
  34. set { SetProperty(ref _UserInchingCollection,value); }
  35. }
  36. #endregion
  37. #region 命令
  38. private DelegateCommand _LoadedCommand;
  39. public DelegateCommand LoadedCommand =>
  40. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  41. private DelegateCommand _AddCommand;
  42. public DelegateCommand AddCommand =>
  43. _AddCommand ?? (_AddCommand = new DelegateCommand(ExecuteAddCommand));
  44. private DelegateCommand<object> _RemoveCommand;
  45. public DelegateCommand<object> RemoveCommand =>
  46. _RemoveCommand ?? (_RemoveCommand = new DelegateCommand<object>(ExecuteRemoveCommand));
  47. private DelegateCommand<object> _EditCommand;
  48. public DelegateCommand<object> EditCommand =>
  49. _EditCommand ?? (_EditCommand = new DelegateCommand<object>(ExecuteEditCommand));
  50. private DelegateCommand<object> _InchingCommand;
  51. public DelegateCommand<object> InchingCommand =>
  52. _InchingCommand ?? (_InchingCommand = new DelegateCommand<object>(ExecuteInchingCommand));
  53. private DelegateCommand _SaveCommand;
  54. public DelegateCommand SaveCommand =>
  55. _SaveCommand ?? (_SaveCommand = new DelegateCommand(ExecuteSaveCommand));
  56. #endregion
  57. #region 事件
  58. #endregion
  59. public InchingControlViewModel(IContainerProvider container, IEventAggregator ea)
  60. {
  61. _container = container;
  62. _eventAggregator = ea;
  63. management = _container.Resolve<Management>();
  64. UserInchingCollection =new ObservableCollection<UserInching>();
  65. //订阅权限登录事件
  66. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  67. }
  68. #region 方法
  69. private bool isLoad = false;
  70. void ExecuteLoadedCommand()
  71. {
  72. if (!isLoad)
  73. {
  74. LoadInchingConfig();
  75. timer = new Timer(DoTime, null, 1000, 1000);
  76. isLoad = true;
  77. }
  78. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  79. {
  80. IsAllowEdit = false;
  81. }
  82. else
  83. {
  84. IsAllowEdit = true;
  85. }
  86. }
  87. async void ExecuteEditCommand(object obj)
  88. {
  89. var inch = obj as UserInching;
  90. var view = new AddSimpleInching();
  91. ((AddSimpleInchingViewModel)(view.DataContext)).UserInching = inch;
  92. //show the dialog
  93. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  94. if (result != null && result is UserInching)
  95. {
  96. UserInching inching = (UserInching)result;
  97. var c = UserInchingCollection.FirstOrDefault(i => i.ID == inching.ID);
  98. c = inching;
  99. }
  100. }
  101. void ExecuteRemoveCommand(object obj)
  102. {
  103. var inch = obj as UserInching;
  104. if (MessageBox.Show($"{Lang.是否删除点动模块}:{inch.Name}?", Lang.移除点动模块, MessageBoxButton.YesNo,MessageBoxImage.Question)==MessageBoxResult.Yes)
  105. {
  106. UserInchingCollection.Remove(inch);
  107. }
  108. }
  109. async void ExecuteAddCommand()
  110. {
  111. var view = new AddSimpleInching();
  112. ((AddSimpleInchingViewModel)(view.DataContext)).UserInching = new UserInching() { ID=Guid.NewGuid()};
  113. //show the dialog
  114. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  115. if (result != null && result is UserInching)
  116. {
  117. UserInching inching = (UserInching)result;
  118. if (string.IsNullOrEmpty(inching.Name)) return;
  119. UserInchingCollection.Add(inching);
  120. }
  121. }
  122. void ExecuteSaveCommand()
  123. {
  124. SaveInchingConfig();
  125. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 });
  126. }
  127. /// <summary>
  128. /// 执行点动命令
  129. /// </summary>
  130. /// <param name="obj"></param>
  131. void ExecuteInchingCommand(object obj)
  132. {
  133. if (management.BgModbusTcpCommunicate == null || !management.BgModbusTcpCommunicate.IsEnabled) return;
  134. try
  135. {
  136. var inch = obj as UserInching;
  137. switch (inch.DataStore)
  138. {
  139. case Enums.ModbusDataStore.CoilDiscretes:
  140. if (management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress+1])
  141. {
  142. management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress + 1] = false;
  143. }
  144. else
  145. {
  146. management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress + 1] = true;
  147. }
  148. break;
  149. case Enums.ModbusDataStore.InputDiscretes:
  150. if (management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1])
  151. {
  152. management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1] = false;
  153. }
  154. else
  155. {
  156. management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1] = true;
  157. }
  158. break;
  159. case Enums.ModbusDataStore.InputRegisters:
  160. if (management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] == inch.OnValue)
  161. {
  162. management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] = inch.OffValue;
  163. }
  164. else
  165. {
  166. management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] = inch.OnValue;
  167. }
  168. break;
  169. case Enums.ModbusDataStore.HoldingRegisters:
  170. if (management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] == inch.OnValue)
  171. {
  172. management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] = inch.OffValue;
  173. }
  174. else
  175. {
  176. management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] = inch.OnValue;
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  186. }
  187. }
  188. private async void DoTime(object state)
  189. {
  190. try
  191. {
  192. if (management.BgModbusTcpCommunicate==null || !management.BgModbusTcpCommunicate.IsEnabled)
  193. {
  194. return;
  195. }
  196. await App.Current.Dispatcher.InvokeAsync(new
  197. Action(() =>
  198. {
  199. for (int i = 0; i < UserInchingCollection.Count; i++)
  200. {
  201. switch (UserInchingCollection[i].DataStore)
  202. {
  203. case Enums.ModbusDataStore.CoilDiscretes:
  204. UserInchingCollection[i].State = management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[UserInchingCollection[i].DataAddress + 1];
  205. break;
  206. case Enums.ModbusDataStore.InputDiscretes:
  207. UserInchingCollection[i].State = management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[UserInchingCollection[i].DataAddress + 1];
  208. break;
  209. case Enums.ModbusDataStore.InputRegisters:
  210. if (management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[UserInchingCollection[i].DataAddress + 1] == UserInchingCollection[i].OnValue)
  211. {
  212. UserInchingCollection[i].State = true;
  213. }
  214. else
  215. {
  216. UserInchingCollection[i].State = false;
  217. }
  218. break;
  219. case Enums.ModbusDataStore.HoldingRegisters:
  220. if (management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[UserInchingCollection[i].DataAddress + 1] == UserInchingCollection[i].OnValue)
  221. {
  222. UserInchingCollection[i].State = true;
  223. }
  224. else
  225. {
  226. UserInchingCollection[i].State = false;
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. foreach (var item in UserInchingCollection)
  234. {
  235. }
  236. }));
  237. }
  238. catch (Exception)
  239. {
  240. }
  241. }
  242. private void LoadInchingConfig()
  243. {
  244. try
  245. {
  246. UserInchingCollection = FileHelper.ReadJsonFile<ObservableCollection<UserInching>>(FilePath.UserInchingConfigurationPath);
  247. }
  248. catch (Exception ex)
  249. {
  250. UserInchingCollection = new ObservableCollection<UserInching>();
  251. LogHelper.WriteLogError("加载用户点动集合配置文件时出错!", ex);
  252. }
  253. }
  254. public void SaveInchingConfig()
  255. {
  256. try
  257. {
  258. FileHelper.WriteJsonFile(UserInchingCollection, FilePath.UserInchingConfigurationPath);
  259. }
  260. catch (Exception ex)
  261. {
  262. LogHelper.WriteLogError("保存用户点动集合配置至文件时出错!", ex);
  263. }
  264. }
  265. #endregion
  266. private bool _IsAllowEdit = false;
  267. public bool IsAllowEdit
  268. {
  269. get { return _IsAllowEdit; }
  270. set { SetProperty(ref _IsAllowEdit, value); }
  271. }
  272. private void LoginChange(Models.User user)
  273. {
  274. if (user.userPart == Enums.UserPart.Operator)
  275. {
  276. IsAllowEdit = false;
  277. }
  278. else
  279. {
  280. IsAllowEdit = true;
  281. }
  282. }
  283. }
  284. }