using MaterialDesignThemes.Wpf; using NPOI.SS.Formula.Functions; using Prism.Commands; using Prism.Events; using Prism.Ioc; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading; using System.Windows; using TeamAAS_VP; using TeamAAS_VP.Controls; using TeamAAS_VP.Core; using TeamAAS_VP.Events; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP.Resources.Languages; using TeamAAS_VP.Views.DebugMod; using static MaterialDesignThemes.Wpf.Theme.ToolBar; namespace TeamAAS_VP.ViewModels.DebugMod { public class InchingControlViewModel : BindableBase { IContainerProvider _container; IEventAggregator _eventAggregator; ISystemDatabaseService _systemDatabaseService; Timer timer; private Management management; #region 属性 private ObservableCollection _UserInchingCollection; public ObservableCollection UserInchingCollection { get { return _UserInchingCollection; } set { SetProperty(ref _UserInchingCollection,value); } } #endregion #region 命令 private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); private DelegateCommand _AddCommand; public DelegateCommand AddCommand => _AddCommand ?? (_AddCommand = new DelegateCommand(ExecuteAddCommand)); private DelegateCommand _RemoveCommand; public DelegateCommand RemoveCommand => _RemoveCommand ?? (_RemoveCommand = new DelegateCommand(ExecuteRemoveCommand)); private DelegateCommand _EditCommand; public DelegateCommand EditCommand => _EditCommand ?? (_EditCommand = new DelegateCommand(ExecuteEditCommand)); private DelegateCommand _InchingCommand; public DelegateCommand InchingCommand => _InchingCommand ?? (_InchingCommand = new DelegateCommand(ExecuteInchingCommand)); private DelegateCommand _SaveCommand; public DelegateCommand SaveCommand => _SaveCommand ?? (_SaveCommand = new DelegateCommand(ExecuteSaveCommand)); #endregion #region 事件 #endregion public InchingControlViewModel(IContainerProvider container, IEventAggregator ea, ISystemDatabaseService systemDatabaseService) { _container = container; _eventAggregator = ea; _systemDatabaseService = systemDatabaseService; management = _container.Resolve(); UserInchingCollection =new ObservableCollection(); //订阅权限登录事件 _eventAggregator.GetEvent().Subscribe(LoginChange); } #region 方法 private bool isLoad = false; void ExecuteLoadedCommand() { if (!isLoad) { LoadInchingConfig(); timer = new Timer(DoTime, null, 1000, 1000); isLoad = true; } if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } } async void ExecuteEditCommand(object obj) { var inch = obj as UserInching; var view = new AddSimpleInching(); ((AddSimpleInchingViewModel)(view.DataContext)).UserInching = inch; //show the dialog var result = await DialogHost.Show(view, "RootDialog", null, null, null); if (result != null && result is UserInching) { UserInching inching = (UserInching)result; var c = UserInchingCollection.FirstOrDefault(i => i.ID == inching.ID); c = inching; } } void ExecuteRemoveCommand(object obj) { var inch = obj as UserInching; if (MessageBox.Show($"{Lang.是否删除点动模块}:{inch.Name}?", Lang.移除点动模块, MessageBoxButton.YesNo,MessageBoxImage.Question)==MessageBoxResult.Yes) { UserInchingCollection.Remove(inch); } } async void ExecuteAddCommand() { var view = new AddSimpleInching(); ((AddSimpleInchingViewModel)(view.DataContext)).UserInching = new UserInching() { ID=Guid.NewGuid()}; //show the dialog var result = await DialogHost.Show(view, "RootDialog", null, null, null); if (result != null && result is UserInching) { UserInching inching = (UserInching)result; if (string.IsNullOrEmpty(inching.Name)) return; UserInchingCollection.Add(inching); } } void ExecuteSaveCommand() { SaveInchingConfig(); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 1 }); } /// /// 执行点动命令 /// /// void ExecuteInchingCommand(object obj) { if (management.BgModbusTcpCommunicate == null || !management.BgModbusTcpCommunicate.IsEnabled) return; try { var inch = obj as UserInching; switch (inch.DataStore) { case Enums.ModbusDataStore.CoilDiscretes: if (management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress+1]) { management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress + 1] = false; } else { management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[inch.DataAddress + 1] = true; } break; case Enums.ModbusDataStore.InputDiscretes: if (management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1]) { management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1] = false; } else { management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[inch.DataAddress + 1] = true; } break; case Enums.ModbusDataStore.InputRegisters: if (management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] == inch.OnValue) { management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] = inch.OffValue; } else { management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[inch.DataAddress + 1] = inch.OnValue; } break; case Enums.ModbusDataStore.HoldingRegisters: if (management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] == inch.OnValue) { management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] = inch.OffValue; } else { management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[inch.DataAddress + 1] = inch.OnValue; } break; default: break; } } catch (Exception ex) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 }); } } private async void DoTime(object state) { try { if (management.BgModbusTcpCommunicate==null || !management.BgModbusTcpCommunicate.IsEnabled) { return; } await App.Current.Dispatcher.InvokeAsync(new Action(() => { for (int i = 0; i < UserInchingCollection.Count; i++) { switch (UserInchingCollection[i].DataStore) { case Enums.ModbusDataStore.CoilDiscretes: UserInchingCollection[i].State = management.BgModbusTcpCommunicate.Slave.DataStore.CoilDiscretes[UserInchingCollection[i].DataAddress + 1]; break; case Enums.ModbusDataStore.InputDiscretes: UserInchingCollection[i].State = management.BgModbusTcpCommunicate.Slave.DataStore.InputDiscretes[UserInchingCollection[i].DataAddress + 1]; break; case Enums.ModbusDataStore.InputRegisters: if (management.BgModbusTcpCommunicate.Slave.DataStore.InputRegisters[UserInchingCollection[i].DataAddress + 1] == UserInchingCollection[i].OnValue) { UserInchingCollection[i].State = true; } else { UserInchingCollection[i].State = false; } break; case Enums.ModbusDataStore.HoldingRegisters: if (management.BgModbusTcpCommunicate.Slave.DataStore.HoldingRegisters[UserInchingCollection[i].DataAddress + 1] == UserInchingCollection[i].OnValue) { UserInchingCollection[i].State = true; } else { UserInchingCollection[i].State = false; } break; default: break; } } foreach (var item in UserInchingCollection) { } })); } catch (Exception) { } } private void LoadInchingConfig() { try { UserInchingCollection = FileHelper.ReadJsonFile>(FilePath.UserInchingConfigurationPath); } catch (Exception ex) { UserInchingCollection = new ObservableCollection(); LogHelper.WriteLogError(Lang.加载用户点动集合配置文件时出错, ex); } } public void SaveInchingConfig() { try { FileHelper.WriteJsonFile(UserInchingCollection, FilePath.UserInchingConfigurationPath); } catch (Exception ex) { LogHelper.WriteLogError(Lang.保存用户点动集合配置至文件时出错, ex); } } #endregion private bool _IsAllowEdit = false; public bool IsAllowEdit { get { return _IsAllowEdit; } set { SetProperty(ref _IsAllowEdit, value); } } private void LoginChange(Models.User user) { if (user.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } } } }