aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.DB/Messages/CloseEntityEditViewMessage.cs
blob: 22e65149e8c6809f620bdc77f31c137d8e9685ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.MachineStudio.Common.Messages;

namespace Tango.MachineStudio.DB.Messages
{
    /// <summary>
    /// Invoked when the user has closed the current editable entity.
    /// </summary>
    /// <seealso cref="Tango.MachineStudio.Common.Messages.IStudioMessage" />
    public class CloseEntityEditViewMessage : IStudioMessage
    {
    }
}
; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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();
            }
        }
    }
}