aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/Dialogs/ThreadLoadingViewVM.cs
blob: bb503e7184d439255aab77494c5a4d09ebee39e3 (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
59pre { 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: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; 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.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RealTimeGraphX
{
    /// <summary>
    /// Represents a blocking concurrent queue for graph data consumption.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <seealso cref="System.Collections.Concurrent.BlockingCollection{T}" />
    public class GraphDataQueue<T> : BlockingCollection<T>
    {
        /// <summary>
        /// Initializes a new instance of the GraphDataQueue, Use Add and TryAdd for Enqueue and TryEnqueue and Take and TryTake for Dequeue and TryDequeue functionality
        /// </summa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Builders;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.Integration.Operation;
using Tango.PMR.ThreadLoading;
using Tango.PPC.Common;
using Tango.PPC.Common.Connection;
using Tango.PPC.Common.Notifications;
using Tango.Settings;
using Tango.SharedUI;

namespace Tango.PPC.UI.Dialogs
{
    public class ThreadLoadingViewVM : DialogViewVM
    {
        public enum ThreadLoadingStage
        {
            Welcome,
            Preparing,
            ReadyForLoading,
            Finalizing,
            Completed,
            PreparationError,
            FinalizationError,
        }

        [TangoInject]
        private IMachineProvider MachineProvider { get; set; }

        [TangoInject]
        private INotificationProvider NotificationProvider { get; set; }

        private PPCSettings _settings;

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

        private List<Rml> _rmls;
        public List<Rml> Rmls
        {
            get { return _rmls; }
            set { _rmls = value; RaisePropertyChangedAuto(); }
        }

        private Rml _selectedRml;
        public Rml SelectedRml
        {
            get { return _selectedRml; }
            set { _selectedRml = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private ThreadLoadingStage _stage;
        public ThreadLoadingStage Stage
        {
            get { return _stage; }
            set { _stage = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private bool _isArcHead;
        public bool IsArcHead
        {
            get { return _isArcHead; }
            set { _isArcHead = value; RaisePropertyChangedAuto(); }
        }

        private String _error;
        public String Error
        {
            get { return _error; }
            set { _error = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand ContinueCommand { get; set; }
        public RelayCommand AbortCommand { get; set; }

        public ThreadLoadingViewVM(bool userInvoked = false)
        {
            CanClose = false;

            TangoIOC.Default.Inject(this);
            _settings = SettingsManager.Default.GetOrCreate<PPCSettings>();

            MachineProvider.MachineOperator.ThreadLoadingStatusChanged += MachineOperator_ThreadLoadingStatusChanged;
            MachineProvider.MachineDisconnected += MachineProvider_MachineDisconnected;

            IsArcHead = MachineProvider.Machine.MachineHeadType == BL.Enumerations.HeadTypes.Arc;

            ContinueCommand = new RelayCommand(Continue, CanContinue);
            AbortCommand = new RelayCommand(Abort);

            AdaptToState(userInvoked);
        }

        private void MachineProvider_MachineDisconnected(object sender, EventArgs e)
        {
            InvokeUI(() =>
            {
                Cancel();
            });
        }

        private void AdaptToState(bool userInvoked = false)
        {
            var status = MachineProvider.MachineOperator.ThreadLoadingStatus;

            if (status != null)
            {
                if (status.State == ThreadLoadingState.Preparing)
                {
                    Stage = ThreadLoadingStage.Preparing;
                }
                else if (status.State == ThreadLoadingState.ReadyForLoading)
                {
                    Stage = ThreadLoadingStage.ReadyForLoading;
                }
                else if (status.State == ThreadLoadingState.Finalizing)
                {
                    Stage = ThreadLoadingStage.Finalizing;
                }
                else if (status.State == ThreadLoadingState.PreparationError)
                {
                    OnPreparationError(status.ErrorReason);
                }
                else if (status.State == ThreadLoadingState.FinalizationError)
                {
                    OnFinalizationError(status.ErrorReason);
                }
                else if (status.State == ThreadLoadingState.Completed)
                {
                    if (userInvoked)
                    {
                        Stage = ThreadLoadingStage.Welcome;
                    }
                    else
                    {
                        Stage = ThreadLoadingStage.Completed;
                    }
                }
            }
        }

        private void MachineOperator_ThreadLoadingStatusChanged(object sender, StartThreadLoadingResponse e)
        {
            Status = e;
            AdaptToState();
        }

        private void Continue()
        {
            if (Stage == ThreadLoadingStage.Welcome)
            {
                Stage = ThreadLoadingStage.Preparing;
                StartPreparing();
            }
            else if (Stage == ThreadLoadingStage.ReadyForLoading)
            {
                ContinueThreadLoading();
            }
            else if (Stage == ThreadLoadingStage.Completed)
            {
                Accept();
            }
            else if (Stage == ThreadLoadingStage.PreparationError)
            {
                Stage = ThreadLoadingStage.Preparing;
                StartPreparing();
            }
            else if (Stage == ThreadLoadingStage.FinalizationError)
            {
                ContinueThreadLoading();
            }
        }

        private async void StartPreparing()
        {
            try
            {
                await MachineProvider.MachineOperator.StartThreadLoading();
            }
            catch (Exception ex)
            {
                OnPreparationError(ex.Message);
            }
        }

        private async void ContinueThreadLoading()
        {
            try
            {
                Stage = ThreadLoadingStage.Finalizing;
                await MachineProvider.MachineOperator.ContinueThreadLoading(SelectedRml.GetActiveProcessGroup().ProcessParametersTables.FirstOrDefault());
            }
            catch (Exception ex)
            {
                OnFinalizationError(ex.Message);
            }
        }

        private bool CanContinue()
        {
            bool canContinue = false;

            if (Stage != ThreadLoadingStage.Preparing && Stage != ThreadLoadingStage.Finalizing)
            {
                canContinue = true;
            }

            if (Stage == ThreadLoadingStage.ReadyForLoading && SelectedRml == null)
            {
                canContinue = false;
            }

            return canContinue;
        }

        private void OnPreparationError(String error)
        {
            Error = error;
            Stage = ThreadLoadingStage.PreparationError;
        }

        private void OnFinalizationError(String error)
        {
            Error = error;
            Stage = ThreadLoadingStage.FinalizationError;
        }

        private void Abort()
        {
            Cancel();
        }

        public async override void OnShow()
        {
            base.OnShow();

            LogManager.Log("Loading site RMLS...");

            List<Rml> rmls = new List<Rml>();

            using (ObservablesContext db = ObservablesContext.CreateDefault())
            {
                rmls = await new RmlsCollectionBuilder(db).SetAll().ForHeadType(MachineProvider.Machine.MachineHeadType).ForSite(MachineProvider.Machine.SiteGuid).WithActiveParametersGroup().BuildListAsync();
            }

            var selectedRml = rmls.SingleOrDefault(x => x.Guid == _settings.LoadedRmlGuid);

            Rmls = rmls;
            SelectedRml = selectedRml != null ? selectedRml : rmls.FirstOrDefault();
        }

        protected override void Cancel()
        {
            CleanUp();
            base.Cancel();
        }

        protected override void Accept()
        {
            CleanUp();
            base.Accept();
        }

        private void CleanUp()
        {
            MachineProvider.MachineOperator.ThreadLoadingStatusChanged -= MachineOperator_ThreadLoadingStatusChanged;
            MachineProvider.MachineDisconnected -= MachineProvider_MachineDisconnected;

            if (SelectedRml != null)
            {
                _settings.LoadedRmlGuid = SelectedRml.Guid;
                _settings.Save();
            }
        }
    }
}