aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/PowerOffViewVM.cs
blob: e3ab7d111519c28141565de07afa8bd11a0ad9c4 (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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bo
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Integration.Operation;
using Tango.PMR.Power;
using Tango.PPC.Common;
using Tango.PPC.UI.AppBarItems;
using Tango.PPC.UI.Views;

namespace Tango.PPC.UI.ViewModels
{
    public class PowerOffViewVM : PPCViewModel
    {
        private PowerDownHandler _handler;
        private PowerOffAppBarItem _appBarItem;
        private int _abortTries;

        private StartPowerDownResponse _status;
        public StartPowerDownResponse Status
        {
            get { return _status; }
            set { _status = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand AbortCommand { get; set; }

        public PowerOffViewVM()
        {
            _appBarItem = new PowerOffAppBarItem();
            _appBarItem.Pressed += (x, e) =>
            {
                NavigationManager.NavigateTo<InternalModule>(nameof(PowerOffView));
            };
            AbortCommand = new RelayCommand(AbortPowerOff);
        }

        public override void OnApplicationStarted()
        {

        }

        public override void OnApplicationReady()
        {
            base.OnApplicationReady();
            MachineProvider.MachineOperator.PowerDownStarted += MachineOperator_PowerDownStarted;
        }

        private void MachineOperator_PowerDownStarted(object sender, PowerDownStartedEventArgs e)
        {
            _abortTries = 0;
            _handler = e.Handler;

            _handler.StatusChanged += OnStatusChanged;
            _handler.Failed += OnFailed;
            _handler.Completed += OnCompleted;

            InvokeUI(async () =>
            {
                await NavigationManager.NavigateTo<InternalModule>(nameof(PowerOffView));
                NotificationProvider.PushAppBarItem(_appBarItem);
            });
        }

        private void OnStatusChanged(object sender, PowerDownStatusChangedEventArgs e)
        {
            Status = e.Status;
            _appBarItem.Status = Status;
        }

        private void OnCompleted(object sender, EventArgs e)
        {
            InvokeUI(async () =>
            {
                if (IsVisible)
                {
                    await NavigationManager.NavigateBack();
                }

                NotificationProvider.PopAppBarItem(_appBarItem);
            });
        }

        private void OnFailed(object sender, Exception ex)
        {
            InvokeUI(async () =>
            {
                await NotificationProvider.ShowError($"An error occurred while powering off the machine.\n{ex.FlattenMessage()}");

                if (IsVisible)
                {
                    await NavigationManager.NavigateBack();
                }

                NotificationProvider.PopAppBarItem(_appBarItem);
            });
        }

        private async void AbortPowerOff()
        {
            try
            {
                NotificationProvider.SetGlobalBusyMessage("Aborting machine power off...");
                await _handler.Abort();
                await NavigationManager.NavigateBack();
                NotificationProvider.PopAppBarItem(_appBarItem);
            }
            catch (Exception ex)
            {
                _abortTries++;
                LogManager.Log(ex, "Power down abort error.");
                NotificationProvider.ReleaseGlobalBusyMessage();
                await NotificationProvider.ShowError($"An error occurred while trying to abort the power off sequence.\n{ex.FlattenMessage()}");

                if (_abortTries > 2)
                {
                    if (IsVisible)
                    {
                        await NavigationManager.NavigateBack();
                    }

                    NotificationProvider.PopAppBarItem(_appBarItem);
                }
            }
            finally
            {
                NotificationProvider.ReleaseGlobalBusyMessage();
            }
        }
    }
}