using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Timers; using Tango.BL.Entities; using Tango.Core.Commands; using Tango.Core.DI; using Tango.Integration.Operation; using Tango.PMR.Printing; using Tango.PPC.Common; using Tango.PPC.Common.Connection; using Tango.PPC.Common.Notifications; using Tango.Settings; using Tango.SharedUI; namespace Tango.PPC.Maintenance.Dialogs { public class HeadCleaningViewVM : DialogViewVM { private HeadCleaningHandler _handler; [TangoInject] private IMachineProvider MachineProvider { get; set; } [TangoInject] private INotificationProvider NotificationProvider { get; set; } private bool _isStarted; public bool IsStarted { get { return _isStarted; } set { _isStarted = value; RaisePropertyChangedAuto(); } } private bool _isCompleted; public bool IsCompleted { get { return _isCompleted; } set { _isCompleted = value; RaisePropertyChangedAuto(); } } private bool _isAborting; public bool IsAborting { get { return _isAborting; } set { _isAborting = value; RaisePropertyChangedAuto(); } } private bool _isFailed; public bool IsFailed { get { return _isFailed; } set { _isFailed = value; RaisePropertyChangedAuto(); } } private StartHeadCleaningResponse _status; public StartHeadCleaningResponse Status { get { return _status; } set { _status = value; RaisePropertyChangedAuto(); } } public bool IsLongCleaning { get; set; } public RelayCommand StartCommand { get; set; } public RelayCommand AbortCommand { get; set; } public HeadCleaningViewVM() { CanClose = true; TangoIOC.Default.Inject(this); StartCommand = new RelayCommand(Start); AbortCommand = new RelayCommand(Abort); } private async void Start() { try { CanClose = false; IsStarted = true; _handler = await MachineProvider.MachineOperator.PerformHeadCleaning(IsLongCleaning); _handler.Completed += _handler_Completed; _handler.Failed += _handler_Failed; _handler.StatusChanged += _handler_StatusChanged; } catch (Exception ex) { _handler_Failed(this, ex); } } private void _handler_StatusChanged(object sender, HeadCleaningStatusChangedEventArgs e) { Status = e.Status; } private void _handler_Failed(object sender, Exception e) { IsStarted = false; IsFailed = true; InvokeUI(() => { CanClose = true; Cancel(); NotificationProvider.ShowError($"Error occurred while trying to perform the head cleaning.\n{e.FlattenMessage()}"); }); } private void _handler_Completed(object sender, EventArgs e) { IsStarted = false; IsCompleted = true; InvokeUI(() => { Accept(); NotificationProvider.ShowSuccess("Head cleaning completed successfully."); }); } protected override void Cancel() { if (CanClose) { base.Cancel(); } } private async void Abort() { IsAborting = true; try { await _handler.Abort(); CanClose = true; Cancel(); await NotificationProvider.ShowInfo("Head cleaning aborted."); } catch (Exception ex) { if (!IsCompleted) { CanClose = true; IsAborting = false; await NotificationProvider.ShowError($"Error occurred while trying to abort the head cleaning.\n{ex.FlattenMessage()}"); } } } } }