using Prism.Commands; using Prism.Ioc; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; using Team.FFFeederService.Interfaces; using Team.FFFeederService; using TeamAAS_VP.Core; using System.Web.Hosting; using TeamAAS_VP.Views.Product; using TeamAAS_VP.Models; using Prism.Events; using TeamAAS_VP.Events; using System.Collections.ObjectModel; using static Org.BouncyCastle.Math.Primes; using TeamAAS_VP.Enums; using MaterialDesignThemes.Wpf; using TeamAAS_VP.Controls; using TeamAAS_VP; using System.Windows; using System.Net.Sockets; using System.Threading.Tasks; using System.Threading; using TeamAAS_VP.Resources.Languages; namespace TeamAAS_VP.ViewModels.DebugMod { public class FeederClearViewModel : BindableBase { IContainerProvider _container; IEventAggregator _eventAggregator; CancellationTokenSource TokenSource; #region 属性 public Management management { get; set; } public FeederService feederService { get; set; } private IVoiceCoilMotorFeeder _Feeder; public IVoiceCoilMotorFeeder Feeder { get { return _Feeder; } set { SetProperty(ref _Feeder, value); FeederChangedEvent?.Invoke(value); } } private ObservableCollection _Feeders = new ObservableCollection(); /// /// Feeder参数 /// public ObservableCollection Feeders { get { return _Feeders; } set { SetProperty(ref _Feeders, value); } } private FeederInfo _SelectFeeder; public FeederInfo SelectFeeder { get { return _SelectFeeder; } set { SetProperty(ref _SelectFeeder, value); if (value != null) { if (FeedersClearWork == null) { FeedersClearWork = new Dictionary>(); } if (FeedersClearWork.ContainsKey(value.Id)) { FeederClearWorks = FeedersClearWork[value.Id]; if (FeederClearWorks == null) { FeederClearWorks = new ObservableCollection(); } } else { FeederClearWorks = new ObservableCollection(); FeedersClearWork.Add(value.Id, FeederClearWorks); } SelelctFeederClearWork = null; } } } private ObservableCollection _FeederClearWorks; /// /// Feeder清料任务集合 /// public ObservableCollection FeederClearWorks { get { return _FeederClearWorks; } set { SetProperty(ref _FeederClearWorks, value); } } private int _SelectFeederFunctionIndex; public int SelectFeederFunctionIndex { get { return _SelectFeederFunctionIndex; } set { SetProperty(ref _SelectFeederFunctionIndex, value); if (value >= 0 && value <= 25) { SelectFeederFunction = (VibrationFunction)value; } else { SelectFeederFunction = VibrationFunction.Delay; } } } private VibrationFunction _SelectFeederFunction; /// /// 选中的feeder振动功能码 /// public VibrationFunction SelectFeederFunction { get { return _SelectFeederFunction; } set { SetProperty(ref _SelectFeederFunction, value); } } private FeederClearWork _SelelctFeederClearWork; public FeederClearWork SelelctFeederClearWork { get { return _SelelctFeederClearWork; } set { SetProperty(ref _SelelctFeederClearWork, value); if (value != null) { IsCanExecute = true; } else { IsCanExecute = false; if (SelectFeeder != null) { IsCanSelect = true; } else { IsCanSelect = false; } } } } public Dictionary> FeedersClearWork; private bool _IsCanExecute = false; public bool IsCanExecute { get { return _IsCanExecute; } set { SetProperty(ref _IsCanExecute, value); if (value) { IsCanEdit = true; IsCanSelect = true; } else { IsCanEdit = false; IsCanSelect = false; } } } private bool CanCreate { get { if (SelectFeeder != null && IsCanCreate) { return true; } else { return false; } } } private bool CanSave { get { if (SelectFeeder != null && SelelctFeederClearWork != null && IsCanExecute) { IsCanEdit = true; return true; } else { IsCanEdit = false; return false; } } } private bool _IsCanEdit = false; public bool IsCanEdit { get { return _IsCanEdit; } set { SetProperty(ref _IsCanEdit, value); } } private bool _IsCanSelect = true; public bool IsCanSelect { get { return _IsCanSelect; } set { SetProperty(ref _IsCanSelect, value); } } private bool _IsCanCreate = true; public bool IsCanCreate { get { return _IsCanCreate; } set { SetProperty(ref _IsCanCreate, value); } } #endregion #region 命令 private DelegateCommand _LoadedCommand; public DelegateCommand LoadedCommand => _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand)); private DelegateCommand _StartClearFeederCommand; public DelegateCommand StartClearFeederCommand => _StartClearFeederCommand ?? (_StartClearFeederCommand = new DelegateCommand(ExecuteStartClearFeederCommand).ObservesCanExecute(() => IsCanExecute)); private DelegateCommand _SaveClearTaskCommand; public DelegateCommand SaveClearTaskCommand => _SaveClearTaskCommand ?? (_SaveClearTaskCommand = new DelegateCommand(ExecuteSaveClearTaskCommandName).ObservesCanExecute(() => CanSave).ObservesProperty(() => SelectFeeder).ObservesProperty(() => SelelctFeederClearWork).ObservesProperty(() => IsCanExecute)); private DelegateCommand _AddFunctionCommand; public DelegateCommand AddFunctionCommand => _AddFunctionCommand ?? (_AddFunctionCommand = new DelegateCommand(ExecuteAddFunctionCommand)); private DelegateCommand _RemoveFunctionCommand; public DelegateCommand RemoveFunctionCommand => _RemoveFunctionCommand ?? (_RemoveFunctionCommand = new DelegateCommand(ExecuteRemoveFunctionCommand)); private DelegateCommand _AddClearTaskCommand; public DelegateCommand AddClearTaskCommand => _AddClearTaskCommand ?? (_AddClearTaskCommand = new DelegateCommand(ExecuteAddClearTaskCommand).ObservesCanExecute(() => CanCreate).ObservesProperty(() => SelectFeeder).ObservesProperty(() => IsCanCreate)); private DelegateCommand _RemoveClearTaskCommand; public DelegateCommand RemoveClearTaskCommand => _RemoveClearTaskCommand ?? (_RemoveClearTaskCommand = new DelegateCommand(ExecuteRemoveClearTaskCommand).ObservesCanExecute(() => CanSave).ObservesProperty(() => SelectFeeder).ObservesProperty(() => SelelctFeederClearWork).ObservesProperty(() => IsCanExecute)); #endregion #region 事件 public event Action FeederChangedEvent; #endregion public FeederClearViewModel(IContainerProvider container, IEventAggregator ea) { _container = container; _eventAggregator = ea; //订阅权限登录事件 _eventAggregator.GetEvent().Subscribe(LoginChange); } #region 方法 void ExecuteLoadedCommand() { try { management = _container.Resolve(); feederService = management.FeederService; if (management.CurrentUser.userPart == Enums.UserPart.Operator) { IsAllowEdit = false; } else { IsAllowEdit = true; } if (FeedersClearWork == null) { FeedersClearWork = management.LoadFeedersClearTask(); } Feeders = management.DeviceConfig.Feeders; } catch (Exception) { } } /// /// 保存清料任务 /// /// async void ExecuteSaveClearTaskCommandName(object feeder) { try { if (SelectFeeder == null) { return; } if (SelelctFeederClearWork == null) { return; } if (Feeder == null) { Feeder = management.FeederService.GetFeeder(SelectFeeder.Id); } //获取并绑定当前Feeder的运行参数 if (Feeder != null && Feeder.IsConnected) { foreach (var item in SelelctFeederClearWork.FeederWorks.FeederWorkList) { if (item.Function == VibrationFunction.Delay) continue; var paramCollection = await Feeder.GetShakeGroupValueAsync((int)item.Function); item.SingleAction = paramCollection; } } management.SaveFeedersClearTask(FeedersClearWork); MessageBox.Show(Lang.完成); } catch (Exception ex) { LogHelper.WriteLogError("Feeder清料界面保存振动任务时出错!", ex); MessageBox.Show(ex.Message); } } /// /// 开始清料 /// /// async void ExecuteStartClearFeederCommand(object feeder) { try { if (MessageBox.Show(Lang.是否清料, Lang.Feeder清料, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) { return; } IsCanExecute = false; IsCanCreate = false; TokenSource = new CancellationTokenSource(); var view = new WaitingPage(); //show the dialog DialogHost.Show(view, "RootDialog", null, ExtendedClosingEventHandler, ExtendedClosedEventHandler); await management.ExecuteFeederClear(SelectFeeder.Id, SelelctFeederClearWork, TokenSource.Token); if (DialogHost.IsDialogOpen("RootDialog")) { TokenSource = null; DialogHost.Close("RootDialog"); MessageBox.Show(Lang.完成); } else { TokenSource = null; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { IsCanExecute = true; IsCanCreate = true; } } /// /// 增加振动盘振动任务 /// async void ExecuteAddFunctionCommand() { try { if (SelectFeeder == null) { return; } if (SelelctFeederClearWork == null) { return; } if (SelectFeederFunction == VibrationFunction.Delay) { var view = new AddProduct() { Title = $"{Lang.添加动作}:", Item = $"{Lang.持续时长}(ms)" }; //show the dialog var result = await DialogHost.Show(view, "RootDialog", null, null, null); if (result != null && !string.IsNullOrEmpty(result.ToString()) && int.TryParse(result.ToString(), out int time)) { SelelctFeederClearWork.FeederWorks.Add(new FeederWork() { Step = SelelctFeederClearWork.FeederWorks.Count, Function = SelectFeederFunction, Duration = time }); } } else { var view = new AddProduct() { Title = $"{Lang.添加动作}:", Item = $"{Lang.持续时长}(ms)" }; //show the dialog var result = await DialogHost.Show(view, "RootDialog", null, null, null); if (result != null && !string.IsNullOrEmpty(result.ToString()) && int.TryParse(result.ToString(), out int time)) { if (Feeder != null && Feeder.IsConnected) { var paramCollection = await Feeder.GetShakeGroupValueAsync((int)SelectFeederFunction); SelelctFeederClearWork.FeederWorks.Add(new FeederWork() { Step = SelelctFeederClearWork.FeederWorks.Count, Function = SelectFeederFunction, Duration = time, SingleAction = paramCollection }); } else { SelelctFeederClearWork.FeederWorks.Add(new FeederWork() { Step = SelelctFeederClearWork.FeederWorks.Count, Function = SelectFeederFunction, Duration = time }); } } } } catch (Exception ex) { LogHelper.WriteLogError("Feeder清料界面增加振动盘任务时出错!", ex); MessageBox.Show(ex.Message); } } /// /// 移除振动盘振动任务 /// /// void ExecuteRemoveFunctionCommand(object index) { try { if (SelectFeeder == null) { return; } if (SelelctFeederClearWork == null) { return; } if (SelelctFeederClearWork.FeederWorks == null || SelelctFeederClearWork.FeederWorks.FeederWorkList == null) { return; } if ((int)index < 0) return; SelelctFeederClearWork.FeederWorks.FeederWorkList.RemoveAt((int)index); } catch (Exception ex) { LogHelper.WriteLogError("Feeder清料界面移除振动盘任务时出错!", ex); MessageBox.Show(ex.Message); } } /// /// 增加清料任务 /// /// async void ExecuteAddClearTaskCommand(object feeder) { try { var view = new AddProduct() { Title = $"{Lang.创建清料任务}:", Item = "Name" }; //show the dialog var result = await DialogHost.Show(view, "RootDialog", null, null, null); if (result != null && !string.IsNullOrEmpty(result.ToString())) { if (!FileHelper.CheckFileName(result.ToString())) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.输入的名称不符合文件名命名规则, Duration = 1 }); return; } var tk = FeederClearWorks.FirstOrDefault(b => b.Name == result.ToString()); if (tk != null) { _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = $"{Lang.已存在同名任务}", Duration = 1 }); return; } FeederClearWorks.Add(new FeederClearWork() { Name = result.ToString(), CreateTime = DateTime.Now, BackOutput = VibrationFunction.A, BeforeScript = false, BeforeOutput = VibrationFunction.A, BackScript = false, ClearCount = 1, FeederWorks = new FeederWorks(), VibrationCount = 1 }); FeederClearWorks[FeederClearWorks.Count - 1].FeederWorks.FeederWorkList = new ObservableCollection(); SelelctFeederClearWork = FeederClearWorks[FeederClearWorks.Count - 1]; management.SaveFeedersClearTask(FeedersClearWork); } } catch (Exception ex) { LogHelper.WriteLogError("Feeder清料-新建任务时出错", ex); MessageBox.Show(ex.Message); } } /// /// 移除清料任务 /// /// void ExecuteRemoveClearTaskCommand(object feeder) { try { if (feeder == null) return; if (MessageBox.Show(Lang.是否移除清料任务, Lang.移除清料任务, MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK) { return; } FeederClearWorks.Remove(SelelctFeederClearWork); SelelctFeederClearWork = null; management.SaveFeedersClearTask(FeedersClearWork); _eventAggregator.GetEvent().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 }); } catch (Exception ex) { LogHelper.WriteLogError("Feeder清料-移除任务时出错", ex); MessageBox.Show(ex.Message); } } private void ExtendedClosingEventHandler(object sender, DialogClosingEventArgs eventArgs) { if (TokenSource!=null) { TokenSource.Cancel(); } } private void ExtendedClosedEventHandler(object sender, DialogClosedEventArgs eventArgs) { } #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; } } } }