using System; using System.Collections.Generic; using System.Linq; 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 Tango.BL.Entities; using Tango.Core.EventArguments; using Tango.Integration.Operation; using Tango.PMR.Diagnostics; using System.Reactive.Concurrency; using System.Reactive; namespace Tango.PPC.Technician.Controls { /// /// Interaction logic for DispenserController.xaml /// public partial class DispenserController : UserControl { public IdsPack IdsPack { get { return (IdsPack)GetValue(IdsPackProperty); } set { SetValue(IdsPackProperty, value); } } public static readonly DependencyProperty IdsPackProperty = DependencyProperty.Register("IdsPack", typeof(IdsPack), typeof(DispenserController), new PropertyMetadata(null)); public IMachineOperator MachineOperator { get { return (IMachineOperator)GetValue(MachineOperatorProperty); } set { SetValue(MachineOperatorProperty, value); } } public static readonly DependencyProperty MachineOperatorProperty = DependencyProperty.Register("MachineOperator", typeof(IMachineOperator), typeof(DispenserController), new PropertyMetadata(null)); public bool IsHoming { get { return (bool)GetValue(IsHomingProperty); } set { SetValue(IsHomingProperty, value); } } public static readonly DependencyProperty IsHomingProperty = DependencyProperty.Register("IsHoming", typeof(bool), typeof(DispenserController), new PropertyMetadata(false, (d, e) => (d as DispenserController).OnChange())); public bool IsPriming { get { return (bool)GetValue(IsPrimingProperty); } set { SetValue(IsPrimingProperty, value); } } public static readonly DependencyProperty IsPrimingProperty = DependencyProperty.Register("IsPriming", typeof(bool), typeof(DispenserController), new PropertyMetadata(false, (d, e) => (d as DispenserController).OnChange())); public bool IsBusy { get { return (bool)GetValue(IsBusyProperty); } set { SetValue(IsBusyProperty, value); } } public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(DispenserController), new PropertyMetadata(false)); private void OnChange() { IsBusy = IsPriming || IsHoming; } public DispenserController() { InitializeComponent(); } #region Home private async void BtnHome_Click(object sender, RoutedEventArgs e) { if (!IsHoming) { try { IsHoming = true; MachineOperator.StartDispenserHoming(new DispenserHomingRequest() { Index = IdsPack.PackIndex, Speed = numSpeed.Value, Direction = MotorDirection.Forward }) .Subscribe((response) => { }, (ex) => { InvokeUI(() => IsHoming = false); }, () => { InvokeUI(() => IsHoming = false); }); } catch { IsHoming = false; } } else { IsHoming = false; try { await MachineOperator.StopDispenserHoming(new DispenserAbortHomingRequest() { Index = IdsPack.PackIndex, }); } catch { } } } #endregion private void InvokeUI(Action action) { Dispatcher.BeginInvoke(action); } private void btnPriming_Click(object sender, RoutedEventArgs e) { try { IsPriming = true; MachineOperator.StartDispenserHoming(new DispenserHomingRequest() { Index = IdsPack.PackIndex, Speed = numSpeed.Value, Direction = MotorDirection.Forward }) .Subscribe((response) => { }, (ex) => { InvokeUI(() => IsPriming = false); }, () => { InvokeUI(() => IsPriming = false); }); } catch { IsPriming = false; } } private void btnHoming_Click(object sender, RoutedEventArgs e) { try { IsHoming = true; MachineOperator.StartDispenserHoming(new DispenserHomingRequest() { Index = IdsPack.PackIndex, Speed = numSpeed.Value, Direction = MotorDirection.Backward }) .Subscribe((response) => { }, (ex) => { InvokeUI(() => IsHoming = false); }, () => { InvokeUI(() => IsHoming = false); }); } catch { IsHoming = false; } } private async void btnStop_Click(object sender, RoutedEventArgs e) { IsHoming = false; IsPriming = false; try { await MachineOperator.StopDispenserHoming(new DispenserAbortHomingRequest() { Index = IdsPack.PackIndex, }); } catch { } } } }