using Prism.Commands; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Team.FFFeederService.Interfaces; using TeamAAS_VP; using static NPOI.HSSF.Util.HSSFColor; namespace TeamAAS_VP.Controls { /// /// FeederSystemParam.xaml 的交互逻辑 /// public partial class FeederSystemParam : UserControl, INotifyPropertyChanged { private bool IsLoading = false; public FeederSystemParam() { InitializeComponent(); this.DataContext = this; this.Loaded += FeederSystemParam_Loaded; } #region 属性 public IVoiceCoilMotorFeeder Feeder { get; set; } private ushort _LightTimeOutValue; /// /// 背光超时时长 /// public ushort LightTimeOutValue { get { return _LightTimeOutValue; } set { SetProperty(ref _LightTimeOutValue,value); } } private ushort _LightValue; /// /// 背光亮度 /// public ushort LightValue { get { return _LightValue; } set { SetProperty(ref _LightValue, value); } } private int _BackLightFlashTime; /// /// 背光闪烁时间 /// public int BackLightFlashTime { get { return _BackLightFlashTime; } set { SetProperty(ref _BackLightFlashTime, value); } } private int _PlatShakeTimeout; /// /// 平台振动超时 /// public int PlatShakeTimeout { get { return _BackLightFlashTime; } set { SetProperty(ref _BackLightFlashTime, value); } } private int _UpperFeederShakeTimeout; /// /// 料斗振动超时 /// public int UpperFeederShakeTimeout { get { return _UpperFeederShakeTimeout; } set { SetProperty(ref _UpperFeederShakeTimeout, value); } } #endregion #region 命令 private DelegateCommand _LightTimeOutValueChangedCommand; public DelegateCommand LightTimeOutValueChangedCommand => _LightTimeOutValueChangedCommand ?? (_LightTimeOutValueChangedCommand = new DelegateCommand(ExecuteLightTimeOutValueChangedCommand)); private DelegateCommand _LightValueChangedCommand; public DelegateCommand LightValueChangedCommand => _LightValueChangedCommand ?? (_LightValueChangedCommand = new DelegateCommand(ExecuteLightValueChangedCommand)); private DelegateCommand _LightFlashTimeChangedCommand; public DelegateCommand LightFlashTimeChangedCommand => _LightFlashTimeChangedCommand ?? (_LightFlashTimeChangedCommand = new DelegateCommand(ExecuteLightFlashTimeChangedCommand)); private DelegateCommand _PlatShakeTimeoutChangedCommand; public DelegateCommand PlatShakeTimeoutChangedCommand => _PlatShakeTimeoutChangedCommand ?? (_PlatShakeTimeoutChangedCommand = new DelegateCommand(ExecutePlatShakeTimeoutChangedCommand)); private DelegateCommand _UpperFeederShakeTimeoutChangedCommand; public DelegateCommand UpperFeederShakeTimeoutChangedCommand => _UpperFeederShakeTimeoutChangedCommand ?? (_UpperFeederShakeTimeoutChangedCommand = new DelegateCommand(ExecuteUpperFeederShakeTimeoutChangedCommand)); private DelegateCommand _WriteCommand; public DelegateCommand WriteCommand => _WriteCommand ?? (_WriteCommand = new DelegateCommand(ExecuteWriteCommand)); #endregion #region 方法 private async void FeederSystemParam_Loaded(object sender, RoutedEventArgs e) { if (Feeder == null || !Feeder.IsConnected) return; IsLoading = true; try { LightTimeOutValue = await Feeder.GetLightTimeoutAsync(); LightValue = await Feeder.GetLightBrightnessAsync(); BackLightFlashTime = await Feeder.GetBackLightFlashTimeAsync(); UpperFeederShakeTimeout = await Feeder.GetUpperFeederShakeTimeoutAsync(); PlatShakeTimeout = await Feeder.GetPlatShakeTimeoutAsync(); } catch (Exception ex) { LogHelper.WriteLogError("Feeder系统参数加载时出错!", ex); } IsLoading = false; } /// /// 背光超时时长修改时 /// async void ExecuteLightTimeOutValueChangedCommand(double? lightvalue) { if (Feeder == null || !Feeder.IsConnected || IsLoading) return; if (lightvalue == null) return; try { await Feeder.SetLightTimeOutAsync((ushort)lightvalue); } catch (Exception ex) { LogHelper.WriteLogError("Feeder背光超时时长修改时出错!", ex); } } /// /// 背光亮度改变时 /// async void ExecuteLightValueChangedCommand(double? lightvalue) { if (Feeder == null || !Feeder.IsConnected || IsLoading) return; if (lightvalue==null) return; try { await Feeder.SetLightValueToBufferAsync((ushort)lightvalue); } catch (Exception ex) { LogHelper.WriteLogError("Feeder背光亮度改变时出错!", ex); } } /// /// 背光闪烁时间改变时 /// /// async void ExecuteLightFlashTimeChangedCommand(double? lightvalue) { if (Feeder == null || !Feeder.IsConnected || IsLoading) return; if (lightvalue == null) return; try { await Feeder.SetBackLightFlashTimeAsync((ushort)lightvalue); } catch (Exception ex) { LogHelper.WriteLogError("Feeder背光闪烁时间改变时出错!", ex); } } /// /// 平台振动超时改变时 /// async void ExecutePlatShakeTimeoutChangedCommand(double? time) { if (Feeder == null || !Feeder.IsConnected || IsLoading) return; if (time == null) return; try { await Feeder.SetPlatShakeTimeoutAsync((ushort)time); } catch (Exception ex) { LogHelper.WriteLogError("Feeder平台振动超时改变时出错!", ex); } } /// /// 料斗振动超时改变时 /// /// async void ExecuteUpperFeederShakeTimeoutChangedCommand(double? time) { if (Feeder == null || !Feeder.IsConnected || IsLoading) return; if (time == null) return; try { await Feeder.SetUpperFeederShakeTimeoutAsync((ushort)time); } catch (Exception ex) { LogHelper.WriteLogError("Feeder料斗振动超时时出错!", ex); } } /// /// 保存 /// async void ExecuteWriteCommand() { if (Feeder == null || !Feeder.IsConnected || IsLoading) return; try { await Feeder.SaveGlobalSetAsync(); } catch (Exception ex) { LogHelper.WriteLogError("Feeder保存系统参数时出错!", ex); } } #endregion #region 属性更改通知 /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; storage = value; RaisePropertyChanged(propertyName); return true; } /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// Action that is called after the property value has been changed. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; storage = value; onChanged?.Invoke(); RaisePropertyChanged(propertyName); return true; } /// /// Raises this object's PropertyChanged event. /// /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers /// that support . protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } /// /// Raises this object's PropertyChanged event. /// /// The PropertyChangedEventArgs protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChanged?.Invoke(this, args); } #endregion } }