aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Communication/PMR/Stubs/StubGPIOReadByteRequest.pb-c.c
blob: 7bc27d2440c76bb99d670ceab6d19879c0f334b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 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();
                _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()}");
                }
            }
        }
    }
}