aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.UI/Dialogs/MachineConnectionLostViewVM.cs
blob: b21c3788eca2c07a88ced044f46bd9bc855c48d6 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using Tango.FSE.Common;
using Tango.Integration.ExternalBridge;

namespace Tango.FSE.UI.Dialogs
{
    /// <summary>
    /// Represents a machine communication failure view model.
    /// </summary>
    /// <seealso cref="Tango.FSE.Common.FSEDialogViewVM" />
    public class MachineConnectionLostViewVM : FSEDialogViewVM
    {
        private DispatcherTimer _timer;

        /// <summary>
        /// Gets or sets the connected machine.
        /// </summary>
        public IExternalBridgeClient Machine { get; set; }

        /// <summary>
        /// Gets or sets the error message.
        /// </summary>
        public String Error { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether to auto close this dialog with a reconnect response after a period of time expires.
        /// </summary>
        public bool IsAutoReconnect { get; set; }

        private int _remainingSeconds;
        /// <summary>
        /// Gets or sets the remaining seconds.
        /// </summary>
        public int RemainingSeconds
        {
            get { return _remainingSeconds; }
            set { _remainingSeconds = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MachineConnectionLostViewVM"/> class.
        /// </summary>
        /// <param name="machine">The machine.</param>
        /// <param name="error">The error.</param>
        /// <param name="autoReconnectSeconds">The automatic reconnect seconds.</param>
        public MachineConnectionLostViewVM(IExternalBridgeClient machine, String error, int? autoReconnectSeconds)
        {
            AutoMode = true;
            OKText = "CONNECT";
            CancelText = "CANCEL";
            CanClose = true;
            HasDefault = true;

            Machine = machine;
            Error = error;
            IsAutoReconnect = autoReconnectSeconds != null;

            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += _timer_Tick;

            if (IsAutoReconnect)
            {
                RemainingSeconds = Math.Max(autoReconnectSeconds.Value, 2);
                _timer.Start();
            }
        }

        /// <summary>
        /// Invokes the <see cref="E:Tango.SharedUI.DialogViewVM.Accepted" /> event.
        /// </summary>
        protected override void Accept()
        {
            _timer.Stop();
            base.Accept();
        }

        /// <summary>
        /// Invokes the <see cref="E:Tango.SharedUI.DialogViewVM.Canceled" /> event.
        /// </summary>
        protected override void Cancel()
        {
            _timer.Stop();
            base.Cancel();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            if (IsVisible)
            {
                RemainingSeconds--;

                if (RemainingSeconds == 0)
                {
                    Accept();
                }
            }
        }

    }
}