using MaterialDesignThemes.Wpf;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
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 Team.FFFeederService.Models;
using TouchSocket.Core;
namespace TeamAAS_VP.Controls
{
///
/// FeederOutput.xaml 的交互逻辑
///
public partial class FeederOutput : UserControl, INotifyPropertyChanged
{
public FeederOutput()
{
InitializeComponent();
this.DataContext = this;
SequenceCollection = new ObservableCollection();
for (var i = 'A'; i < 'Z'; i++)
{
SequenceCollection.Add(i.ToString());
}
SequenceItems = new ObservableCollection();
for (var i = 1; i <= 26; i++)
{
SequenceItems.Add(i);
}
}
#region 属性
public IVoiceCoilMotorFeeder Feeder { get; set; }
private ObservableCollection _SequenceCollection;
///
/// 输出振动集
///
public ObservableCollection SequenceCollection
{
get { return _SequenceCollection; }
set { SetProperty(ref _SequenceCollection,value); }
}
private int _SelectedSequence=-1;
///
/// 当前选择输出
///
public int SelectedSequence
{
get { return _SelectedSequence; }
set { SetProperty(ref _SelectedSequence, value); }
}
private HopperFeederParam _CurrentUpperFeederParamDto;
///
/// 当前选中的料仓参数
///
public HopperFeederParam CurrentUpperFeederParamDto
{
get { return _CurrentUpperFeederParamDto; }
set { SetProperty(ref _CurrentUpperFeederParamDto, value); }
}
private ObservableCollection _SequenceItems;
///
/// 输入触发序列集
///
public ObservableCollection SequenceItems
{
get { return _SequenceItems; }
set { SetProperty(ref _SequenceItems,value); }
}
private int _InputTriggerId1=-1;
///
/// 当前1选中的序列
///
public int InputTriggerId1
{
get { return _InputTriggerId1; }
set { SetProperty(ref _InputTriggerId1, value); }
}
private int _InputTriggerId2 = -1;
///
/// 当前2选中的序列
///
public int InputTriggerId2
{
get { return _InputTriggerId2; }
set { SetProperty(ref _InputTriggerId2, value); }
}
#endregion
#region 命令
private DelegateCommand _SelectSequenceCommand;
public DelegateCommand SelectSequenceCommand =>
_SelectSequenceCommand ?? (_SelectSequenceCommand = new DelegateCommand(ExecuteSelectSequenceCommand));
private DelegateCommand _StopActionCommand;
public DelegateCommand StopActionCommand =>
_StopActionCommand ?? (_StopActionCommand = new DelegateCommand(ExecuteStopActionCommand));
private DelegateCommand _WriteParamCommand;
public DelegateCommand WriteParamCommand =>
_WriteParamCommand ?? (_WriteParamCommand = new DelegateCommand(ExecuteWriteParamCommand));
private DelegateCommand _ExecuteActionCommand;
public DelegateCommand ExecuteActionCommand =>
_ExecuteActionCommand ?? (_ExecuteActionCommand = new DelegateCommand(ExecuteExecuteActionCommand));
private DelegateCommand _SelectInputSequenceCommand1;
public DelegateCommand SelectInputSequenceCommand1 =>
_SelectInputSequenceCommand1 ?? (_SelectInputSequenceCommand1 = new DelegateCommand(ExecuteSelectInputSequenceCommand1));
private DelegateCommand _SelectInputSequenceCommand2;
public DelegateCommand SelectInputSequenceCommand2 =>
_SelectInputSequenceCommand2 ?? (_SelectInputSequenceCommand2 = new DelegateCommand(ExecuteSelectInputSequenceCommand2));
private DelegateCommand _WriteInputCommand;
public DelegateCommand WriteInputCommand =>
_WriteInputCommand ?? (_WriteInputCommand = new DelegateCommand(ExecuteWriteInputCommand));
#endregion
#region 方法
///
/// 选中输出时
///
async void ExecuteSelectSequenceCommand()
{
if (Feeder == null) return;
if (!Feeder.IsConnected) return;
if (SelectedSequence == -1) return;
var param = await Feeder.GetUpperParamAsync(SelectedSequence);
CurrentUpperFeederParamDto = param;
var index = await Feeder.GetInputTrigger(1);
if (index <= 0 || index > 26)
{
InputTriggerId2 = 1;
}
InputTriggerId1 = index - 1;
index = await Feeder.GetInputTrigger(2);
InputTriggerId2 = index - 1;
}
///
/// 选中输入1触发序列时
///
async void ExecuteSelectInputSequenceCommand1()
{
if (Feeder == null) return;
if (!Feeder.IsConnected) return;
if (InputTriggerId2 == -1) return;
try
{
var index = await Feeder.GetInputTrigger(1);
if (index <= 0 || index > 26)
{
InputTriggerId1 = 1;
}
else
{
InputTriggerId1 = index - 1;
}
}
catch (Exception)
{
}
}
///
/// 选中输入2触发序列时
///
async void ExecuteSelectInputSequenceCommand2()
{
if (Feeder == null) return;
if (!Feeder.IsConnected) return;
if (InputTriggerId2 == -1) return;
try
{
var index = await Feeder.GetInputTrigger(2);
if (index <= 0 || index > 26)
{
InputTriggerId2 = 1;
}
else
{
InputTriggerId2 = index - 1;
}
}
catch (Exception)
{
}
}
///
/// 执行
///
async void ExecuteExecuteActionCommand()
{
if (Feeder == null) return;
if (!Feeder.IsConnected) return;
if (SelectedSequence < 0) return;
try
{
await Feeder.OutputAsync(SelectedSequence + 1, CurrentUpperFeederParamDto.HopperDuration);
}
catch (Exception)
{
}
}
///
/// 停止
///
async void ExecuteStopActionCommand()
{
if (Feeder == null) return;
if (!Feeder.IsConnected) return;
if (SelectedSequence < 0) return;
try
{
await Feeder.StopOutputAsync();
}
catch (Exception)
{
}
}
///
/// 下载参数
///
async void ExecuteWriteParamCommand()
{
if (Feeder == null) return;
if (!Feeder.IsConnected) return;
if (SelectedSequence < 0) return;
if (CurrentUpperFeederParamDto == null)
{
return;
}
try
{
await Feeder.WriteOutputParamAsync(SelectedSequence, CurrentUpperFeederParamDto);
}
catch (Exception)
{
}
}
///
/// 下载参数
///
async void ExecuteWriteInputCommand()
{
if (Feeder == null) return;
if (!Feeder.IsConnected) return;
if (InputTriggerId1 < 0) return;
if (InputTriggerId1 < 2) return;
try
{
await Feeder.SetInputTrigger(1, InputTriggerId1 + 1);
await Feeder.SetInputTrigger(2, InputTriggerId2 + 1);
}
catch (Exception)
{
}
}
#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
}
}